Skip to content

Commit

Permalink
Fix overriden required (#1868)
Browse files Browse the repository at this point in the history
* reorder required to resolve it's imports

* add tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: fft001 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 6, 2024
1 parent 365c231 commit e1c795e
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,14 +1235,14 @@ class Processed(NamedTuple):

scoped_model_resolver = ModelResolver()

self.__override_required_field(models)
self.__replace_unique_list_to_set(models)
self.__change_from_import(models, imports, scoped_model_resolver, init)
self.__extract_inherited_enum(models)
self.__set_reference_default_value_to_field(models)
self.__reuse_model(models, require_update_action_models)
self.__collapse_root_models(models, unused_models, imports)
self.__set_default_enum_member(models)
self.__override_required_field(models)
self.__sort_models(models, imports)
self.__set_one_literal_on_default(models)
self.__apply_discriminator_type(models, imports)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# generated by datamodel-codegen:
# filename: referenced.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from datetime import datetime
from typing import Optional

from pydantic import BaseModel


class Model(BaseModel):
some_optional_property: Optional[str] = None
some_optional_typed_property: Optional[datetime] = None
14 changes: 14 additions & 0 deletions tests/data/expected/main/main_require_referenced_field/required.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# generated by datamodel-codegen:
# filename: required.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from datetime import datetime

from .referenced import Model as Model_1


class Model(Model_1.Model):
some_optional_property: str
some_optional_typed_property: datetime
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# generated by datamodel-codegen:
# filename: referenced.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import AwareDatetime, BaseModel


class Model(BaseModel):
some_optional_property: Optional[str] = None
some_optional_typed_property: Optional[AwareDatetime] = None
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# generated by datamodel-codegen:
# filename: required.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import AwareDatetime

from .referenced import Model as Model_1


class Model(Model_1.Model):
some_optional_property: str
some_optional_typed_property: AwareDatetime
15 changes: 15 additions & 0 deletions tests/data/jsonschema/require_referenced_field/referenced.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "test",
"description": "test",
"type": "object",
"properties": {
"some_optional_property": {
"type": "string"
},
"some_optional_typed_property": {
"type": "string",
"format":"date-time"
}
}
}
12 changes: 12 additions & 0 deletions tests/data/jsonschema/require_referenced_field/required.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "test",
"description": "test",
"type": "object",
"allOf": [
{
"$ref": "referenced.json"
}
],
"required" : ["some_optional_property", "some_optional_typed_property"]
}
39 changes: 39 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,45 @@ def test_space_and_special_characters_dict():
)


@pytest.mark.parametrize(
'output_model,expected_output',
[
(
'pydantic.BaseModel',
'main_require_referenced_field',
),
(
'pydantic_v2.BaseModel',
'main_require_referenced_field_pydantic_v2',
),
],
)
@freeze_time('2019-07-26')
def test_main_require_referenced_field(output_model, expected_output):
with TemporaryDirectory() as output_dir:
output_dir: Path = Path(output_dir)
return_code: Exit = main(
[
'--input',
str(JSON_SCHEMA_DATA_PATH / 'require_referenced_field/'),
'--output',
str(output_dir),
'--input-file-type',
'jsonschema',
'--output-model-type',
output_model,
]
)
assert return_code == Exit.OK

assert (output_dir / 'referenced.py').read_text() == (
EXPECTED_MAIN_PATH / expected_output / 'referenced.py'
).read_text()
assert (output_dir / 'required.py').read_text() == (
EXPECTED_MAIN_PATH / expected_output / 'required.py'
).read_text()


@freeze_time('2019-07-26')
def test_csv_file():
with TemporaryDirectory() as output_dir:
Expand Down

0 comments on commit e1c795e

Please sign in to comment.