Skip to content

Commit

Permalink
Fix missing constr import issue
Browse files Browse the repository at this point in the history
Add duplicate field constraints test case
  • Loading branch information
koxudaxi committed Oct 6, 2023
1 parent e22cb87 commit 28e9a17
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ def __collapse_root_models(
]
else: # pragma: no cover
continue
imports.append(copied_data_type.imports)

data_type.remove_reference()

Expand Down
13 changes: 13 additions & 0 deletions tests/data/expected/main/duplicate_field_constraints/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# generated by datamodel-codegen:
# filename: common.yml
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Any

from pydantic import RootModel


class Model(RootModel[Any]):
root: Any
15 changes: 15 additions & 0 deletions tests/data/expected/main/duplicate_field_constraints/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# generated by datamodel-codegen:
# filename: test.yml
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel, Field, constr

from . import common


class Test(BaseModel):
uid: constr(pattern=r'[0-9ABCDEFGHJKMNPQRSTVWXYZ]{26,26}') = Field(
..., description='ulid of this object'
)
7 changes: 7 additions & 0 deletions tests/data/jsonschema/duplicate_field_constraints/common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
$schema: https://json-schema.org/draft/2020-12/schema
$id: common.yml
definitions:
ulid:
type: string
pattern: '[0-9ABCDEFGHJKMNPQRSTVWXYZ]{26,26}'
10 changes: 10 additions & 0 deletions tests/data/jsonschema/duplicate_field_constraints/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
$schema: https://json-schema.org/draft/2020-12/schema
$id: test.yml
title: test
required:
- uid
properties:
uid:
description: ulid of this object
$ref: ./common.yml#/definitions/ulid
26 changes: 26 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5804,3 +5804,29 @@ def test_main_msgspec_use_annotated_with_field_constraints():
/ 'output.py'
).read_text()
)


@freeze_time('2019-07-26')
def test_main_duplicate_field_constraints():
with TemporaryDirectory() as output_dir:
output_path: Path = Path(output_dir)
return_code: Exit = main(
[
'--input',
str(JSON_SCHEMA_DATA_PATH / 'duplicate_field_constraints'),
'--output',
str(output_path),
'--input-file-type',
'jsonschema',
'--collapse-root-models',
'--output-model-type',
'pydantic_v2.BaseModel',
]
)
assert return_code == Exit.OK
main_modular_dir = EXPECTED_MAIN_PATH / 'duplicate_field_constraints'
for path in main_modular_dir.rglob('*.py'):
result = output_path.joinpath(
path.relative_to(main_modular_dir)
).read_text()
assert result == path.read_text()

0 comments on commit 28e9a17

Please sign in to comment.