Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,8 @@ def __reuse_model(self, models: list[DataModel], require_update_action_models: l
model_cache: dict[tuple[HashableComparable, ...], Reference] = {}
duplicates = []
for model in models.copy():
if self.collapse_root_models and isinstance(model, self.data_model_root_type):
continue
model_key = model.get_dedup_key()
cached_model_reference = model_cache.get(model_key)
if cached_model_reference:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# generated by datamodel-codegen:
# filename: reuse_model_collapse_root_models_constraints.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel, Field, RootModel


class Shared(BaseModel):
short_code: str = Field(
..., alias='shortCode', max_length=8, min_length=0, title='ShortCode'
)


class RecordA(Shared):
timestamp: str = Field(..., title='Timestamp')


class RecordB(Shared):
created: str = Field(..., title='Timestamp')
label: str = Field(..., title='LongText')


class ReproBugs(RootModel[RecordA | RecordB]):
root: RecordA | RecordB = Field(
...,
description='Minimal schema to reproduce reuse_model + collapse_root_models bugs in datamodel-code-generator 0.55.0',
title='ReproBugs',
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ReproBugs",
"description": "Minimal schema to reproduce reuse_model + collapse_root_models bugs in datamodel-code-generator 0.55.0",
"$defs": {
"Shared": {
"type": "object",
"title": "Shared",
"properties": {
"shortCode": {
"title": "ShortCode",
"type": "string",
"maxLength": 8,
"minLength": 0
}
},
"required": [
"shortCode"
]
},
"RecordA": {
"type": "object",
"title": "RecordA",
"allOf": [
{
"$ref": "#/$defs/Shared"
},
{
"type": "object",
"properties": {
"timestamp": {
"title": "Timestamp",
"type": "string"
}
},
"required": [
"timestamp"
]
}
]
},
"RecordB": {
"type": "object",
"title": "RecordB",
"allOf": [
{
"$ref": "#/$defs/Shared"
},
{
"type": "object",
"properties": {
"created": {
"title": "Timestamp",
"type": "string"
},
"label": {
"title": "LongText",
"type": "string"
}
},
"required": [
"created",
"label"
]
}
]
}
},
"oneOf": [
{
"$ref": "#/$defs/RecordA"
},
{
"$ref": "#/$defs/RecordB"
}
]
}
31 changes: 31 additions & 0 deletions tests/main/jsonschema/test_main_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,37 @@ def test_main_reuse_model_collapse_nested(output_file: Path) -> None:
)


@pytest.mark.parametrize(
"collapse_reuse_model_args",
[[], ["--collapse-reuse-models"]],
)
def test_main_reuse_model_collapse_root_models_preserves_constraints(
output_file: Path, collapse_reuse_model_args: list[str]
) -> None:
"""Test root-model collapse keeps distinct field constraints when titles collide."""
extra_args = [
"--reuse-model",
"--collapse-root-models",
"--use-title-as-name",
"--field-constraints",
"--snake-case-field",
"--target-python-version",
"3.10",
"--output-model-type",
"pydantic_v2.BaseModel",
*collapse_reuse_model_args,
]

run_main_and_assert(
input_path=JSON_SCHEMA_DATA_PATH / "reuse_model_collapse_root_models_constraints.json",
output_path=output_file,
input_file_type="jsonschema",
assert_func=assert_file_content,
expected_file="reuse_model_collapse_root_models_constraints.py",
extra_args=extra_args,
)


@pytest.mark.cli_doc(
options=["--capitalize-enum-members"],
option_description="""Capitalize enum member names to UPPER_CASE format.
Expand Down
Loading