datamodel-code-generator
datamodel-code-generator copied to clipboard
[jsonschema] Invalid import path when referencing files with dots.
Describe the bug
When a local file is referenced and it has more than one dot in the name, it is misinterpreted during import syntax generation. It can leads to wrong models (example 1) or generator error (example 2)
For more details & reproducible example: https://github.com/ilovelinux/datamodel-code-generator-issue-2332
To Reproduce
Example 1
commons.schema.json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/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": "commons.schema.json#/$defs/defaultArray"
}
Example 2
commons.with.more.dots.schema.json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/commons.with.more.dots.schema.json",
"title": "Commons",
"description": "Commons objects",
"$defs": {
"defaultArray": {
"type": "array",
"minLength": 1,
"maxLength": 100
}
}
}
products.with.more.dots.schema.json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/products.with.more.dots.schema.schema.json",
"title": "Products",
"description": "The products in the catalog",
"$ref": "commons.with.more.dots.schema.json#/$defs/defaultArray"
}
Used commandline:
$ datamodel-codegen --input inputs/ --input-file-type jsonschema --output outputs/
Expected behavior
Example 1
in products_schema.py, common_schema should be imported correctly.
Example 2
It should generate code. It crashes instead.
Version:
- OS: Linux
- Python version: 3.13
- datamodel-code-generator version: 0.28.2
Additional context
Probably related to https://github.com/koxudaxi/datamodel-code-generator/pull/2008
For more details & reproducible example: https://github.com/ilovelinux/datamodel-code-generator-issue-2332
Workaround for Example 1
Generate the models using --treat-dot-as-module flag.