Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Regression] Invalid imports in generated models when referencing files with both minus (-) and dots (.) #2336

Open
ilovelinux opened this issue Mar 4, 2025 · 0 comments · May be fixed by #2343
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@ilovelinux
Copy link
Contributor

ilovelinux commented Mar 4, 2025

Describe the bug
datamodel-code-generator doesn't support references to schemas with both minus (-) and dots (.) because file names are not sanitized. This lead to a crash because invalid python code is passed to black, which raises an exception.

This issue was (partially) fixed in:

May be related to:

It works for references without any dot (.)

For more details & reproducible example: https://github.com/ilovelinux/datamodel-code-generator-issue-2336

To Reproduce

Example schema:

array-commons.schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/array-commons.schema.json",
  "title": "Commons",
  "description": "Commons objects",
  "$defs": {
    "defaultArray": {
      "type": "array",
      "minLength": 1,
      "maxLength": 100
    }
  }
}

products.schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/products.schema.json",
  "title": "Products",
  "description": "The products in the catalog",
  "$ref": "array-commons.schema.json#/$defs/defaultArray",
}

Used commandline:

$ datamodel-codegen --input inputs/ --input-file-type jsonschema --output outputs/

Expected behavior

It should generate working code.

Version:

  • OS: Linux
  • Python version: 3.13
  • datamodel-code-generator version: 0.28.2

Additional context

For more details & reproducible example: https://github.com/ilovelinux/datamodel-code-generator-issue-2336

@gaborbernat gaborbernat added bug Something isn't working help wanted Extra attention is needed labels Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants