Skip to content

Commit 991f0ff

Browse files
authored
Fix incorrect relative imports with --use-exact-imports and --collapse-root-models (#3020)
1 parent 69eda18 commit 991f0ff

9 files changed

Lines changed: 87 additions & 1 deletion

File tree

src/datamodel_code_generator/parser/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ def __change_from_import( # noqa: PLR0913, PLR0914
14131413
else:
14141414
from_, import_ = full_path = relative(current_module_name, target_full_name)
14151415
if imports.use_exact:
1416-
from_, import_ = exact_import(from_, import_, data_type.reference.short_name)
1416+
from_, import_ = full_path = exact_import(from_, import_, data_type.reference.short_name)
14171417
import_ = import_.replace("-", "_")
14181418
current_module_path = tuple(current_module_name.split(".")) if current_module_name else ()
14191419
if ( # pragma: no cover
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by datamodel-codegen:
2+
# filename: exact_imports_collapse_root_models_title
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# generated by datamodel-codegen:
2+
# filename: data/ReflectionSetData.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic import BaseModel, Field
7+
8+
from ..datatypes.FooStats import FooStats
9+
10+
11+
class ReflectionSetData(BaseModel):
12+
foo_stats_overall: FooStats | None = Field(None, alias='fooStatsOverall')
13+
foo_stats_shells: list[FooStats] | None = Field(
14+
None, alias='fooStatsShells', title='Foo Stats Shells'
15+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by datamodel-codegen:
2+
# filename: exact_imports_collapse_root_models_title
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# generated by datamodel-codegen:
2+
# filename: datatypes/FooStats.json
3+
4+
from __future__ import annotations
5+
6+
from pydantic import BaseModel
7+
8+
9+
class FooStats(BaseModel):
10+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by datamodel-codegen:
2+
# filename: exact_imports_collapse_root_models_title
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "ReflectionSetData",
4+
"type": "object",
5+
"properties": {
6+
"fooStatsOverall": {
7+
"allOf": [
8+
{
9+
"$ref": "../datatypes/FooStats.json"
10+
}
11+
]
12+
},
13+
"fooStatsShells": {
14+
"items": {
15+
"$ref": "../datatypes/FooStats.json"
16+
},
17+
"title": "Foo Stats Shells",
18+
"type": "array"
19+
}
20+
}
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "FooStats",
4+
"type": "object"
5+
}

tests/main/jsonschema/test_main_jsonschema.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8729,3 +8729,32 @@ def test_main_exact_imports_collapse_root_models_module_class_collision(output_d
87298729
],
87308730
force_exec_validation=True,
87318731
)
8732+
8733+
8734+
@PYDANTIC_V2_SKIP
8735+
def test_main_exact_imports_collapse_root_models_title_array(output_dir: Path) -> None:
8736+
"""Test --use-exact-imports with --collapse-root-models when array field has title.
8737+
8738+
Regression test for https://github.com/koxudaxi/datamodel-code-generator/issues/3001
8739+
When an allOf $ref and an array items $ref point to the same type, and the array
8740+
field has a title (causing a root model to be created and collapsed), both fields
8741+
should use the same import alias.
8742+
"""
8743+
run_main_and_assert(
8744+
input_path=JSON_SCHEMA_DATA_PATH / "exact_imports_collapse_root_models_title",
8745+
output_path=output_dir,
8746+
input_file_type="jsonschema",
8747+
expected_directory=EXPECTED_JSON_SCHEMA_PATH / "exact_imports_collapse_root_models_title",
8748+
extra_args=[
8749+
"--output-model-type",
8750+
"pydantic_v2.BaseModel",
8751+
"--target-python-version",
8752+
"3.10",
8753+
"--use-exact-imports",
8754+
"--collapse-root-models",
8755+
"--use-title-as-name",
8756+
"--snake-case-field",
8757+
"--disable-timestamp",
8758+
],
8759+
force_exec_validation=True,
8760+
)

0 commit comments

Comments
 (0)