Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/attrs-24.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Aug 6, 2024
2 parents 6d62856 + f856d13 commit 76e0067
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.5.0'
rev: 'v0.5.6'
hooks:
- id: ruff
files: "^datamodel_code_generator|^tests"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ Options:
--http-headers HTTP_HEADER [HTTP_HEADER ...]
Set headers in HTTP requests to the remote host.
(example: "Authorization: Basic dXNlcjpwYXNz")
--http-ignore-tls Disable verification of the remote host's TLS
--http-ignore-tls Disable verification of the remote host''s TLS
certificate
--http-query-parameters QUERY_PARAMETER [QUERY_PARAMETER ...]
Set query parameters in HTTP requests to the remote host.
Expand Down Expand Up @@ -419,7 +419,7 @@ Field customization:
e.g. underscores
--snake-case-field Change camel-case field name to snake-case
--special-field-name-prefix SPECIAL_FIELD_NAME_PREFIX
Set field name prefix when first character can't be
Set field name prefix when first character can''t be
used as Python field name (default: `field`)
--strip-default-none Strip default None on fields
--use-default Use default value even if a field is required
Expand All @@ -440,7 +440,7 @@ Model customization:
Set class name of root model
--collapse-root-models
Models generated with a root-type field will be
mergedinto the models using that root-type model
merged into the models using that root-type model
--disable-appending-item-suffix
Disable appending `Item` suffix to model name in an
array
Expand All @@ -449,7 +449,7 @@ Model customization:
Enable faux immutability
--enable-version-header
Enable package version on file headers
--keep-model-order Keep generated models' order
--keep-model-order Keep generated models'' order
--reuse-model Reuse models on the field when a module has the model
with the same content
--target-python-version {3.6,3.7,3.8,3.9,3.10,3.11}
Expand Down
4 changes: 0 additions & 4 deletions datamodel_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,12 @@ def validate_each_item(each_item: Any) -> Tuple[str, str]:
def validate_additional_imports(cls, values: Dict[str, Any]) -> Dict[str, Any]:
if values.get('additional_imports') is not None:
values['additional_imports'] = values.get('additional_imports').split(',')
else:
values['additional_imports'] = []
return values

@model_validator(mode='before')
def validate_custom_formatters(cls, values: Dict[str, Any]) -> Dict[str, Any]:
if values.get('custom_formatters') is not None:
values['custom_formatters'] = values.get('custom_formatters').split(',')
else:
values['custom_formatters'] = []
return values

if PYDANTIC_V2:
Expand Down
2 changes: 1 addition & 1 deletion datamodel_code_generator/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def start_section(self, heading: Optional[str]) -> None:
'--collapse-root-models',
action='store_true',
default=None,
help='Models generated with a root-type field will be merged'
help='Models generated with a root-type field will be merged '
'into the models using that root-type model',
)
model_options.add_argument(
Expand Down
80 changes: 59 additions & 21 deletions datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ def _append_additional_imports(
additional_imports = []

for additional_import_string in additional_imports:
if additional_import_string is None:
continue
new_import = Import.from_full_path(additional_import_string)
self.imports.append(new_import)

Expand Down Expand Up @@ -664,7 +666,7 @@ def __replace_duplicate_name_in_module(cls, models: List[DataModel]) -> None:
for model in models:
class_name: str = model.class_name
generated_name: str = scoped_model_resolver.add(
model.path, class_name, unique=True, class_name=True
[model.path], class_name, unique=True, class_name=True
).name
if class_name != generated_name:
model.class_name = generated_name
Expand All @@ -686,7 +688,7 @@ def __change_from_import(
init: bool,
) -> None:
for model in models:
scoped_model_resolver.add(model.path, model.class_name)
scoped_model_resolver.add([model.path], model.class_name)
for model in models:
before_import = model.imports
imports.append(before_import)
Expand Down Expand Up @@ -958,7 +960,11 @@ def __reuse_model(
models.remove(duplicate)

def __collapse_root_models(
self, models: List[DataModel], unused_models: List[DataModel], imports: Imports
self,
models: List[DataModel],
unused_models: List[DataModel],
imports: Imports,
scoped_model_resolver: ModelResolver,
) -> None:
if not self.collapse_root_models:
return None
Expand Down Expand Up @@ -1033,6 +1039,31 @@ def __collapse_root_models(
]
else: # pragma: no cover
continue

for d in root_type_field.data_type.data_types:
if d.reference is None:
continue
from_, import_ = full_path = relative(
model.module_name, d.full_name
)
if from_ and import_:
alias = scoped_model_resolver.add(full_path, import_)
d.alias = (
alias.name
if d.reference.short_name == import_
else f'{alias.name}.{d.reference.short_name}'
)
imports.append(
[
Import(
from_=from_,
import_=import_,
alias=alias.name,
reference_path=d.reference.path,
)
]
)

original_field = get_most_of_parent(data_type, DataModelFieldBase)
if original_field: # pragma: no cover
# TODO: Improve detection of reference type
Expand Down Expand Up @@ -1261,9 +1292,12 @@ def parse(
def module_key(data_model: DataModel) -> Tuple[str, ...]:
return tuple(data_model.module_path)

def sort_key(data_model: DataModel) -> Tuple[int, Tuple[str, ...]]:
return (len(data_model.module_path), tuple(data_model.module_path))

# process in reverse order to correctly establish module levels
grouped_models = groupby(
sorted(sorted_data_models.values(), key=module_key, reverse=True),
sorted(sorted_data_models.values(), key=sort_key, reverse=True),
key=module_key,
)

Expand Down Expand Up @@ -1329,7 +1363,9 @@ class Processed(NamedTuple):
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.__collapse_root_models(
models, unused_models, imports, scoped_model_resolver
)
self.__set_default_enum_member(models)
self.__sort_models(models, imports)
self.__set_one_literal_on_default(models)
Expand Down Expand Up @@ -1368,22 +1404,24 @@ class Processed(NamedTuple):

for module, models, init, imports, scoped_model_resolver in processed_models:
result: List[str] = []
if with_import:
result += [str(self.imports), str(imports), '\n']

code = dump_templates(models)
result += [code]

if self.dump_resolve_reference_action is not None:
result += [
'\n',
self.dump_resolve_reference_action(
m.reference.short_name
for m in models
if m.path in require_update_action_models
),
]

if models:
if with_import:
result += [str(self.imports), str(imports), '\n']

code = dump_templates(models)
result += [code]

if self.dump_resolve_reference_action is not None:
result += [
'\n',
self.dump_resolve_reference_action(
m.reference.short_name
for m in models
if m.path in require_update_action_models
),
]
if not result and not init:
continue
body = '\n'.join(result)
if code_formatter:
body = code_formatter.format_code(body)
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ Model customization:
Set class name of root model
--collapse-root-models
Models generated with a root-type field will be
mergedinto the models using that root-type model
merged into the models using that root-type model
--disable-appending-item-suffix
Disable appending `Item` suffix to model name in an
array
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/data/expected/main/jsonschema/external_other_ref2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# generated by datamodel-codegen:
# filename: other/ref2.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel


class Other(BaseModel):
key: Optional[str] = None
17 changes: 17 additions & 0 deletions tests/data/expected/main/jsonschema/external_ref0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# generated by datamodel-codegen:
# filename: ref0.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel

from . import ref2
from .other import ref2 as ref2_1


class Model(BaseModel):
ref1: Optional[ref2.Model] = None
other_ref1: Optional[ref2_1.Other] = None
2 changes: 0 additions & 2 deletions tests/data/expected/main/jsonschema/nested_skip/a/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# generated by datamodel-codegen:
# filename: nested_skip.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# generated by datamodel-codegen:
# filename: nested_skip.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# generated by datamodel-codegen:
# filename: nested_skip.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations
4 changes: 4 additions & 0 deletions tests/data/jsonschema/external_collapse/child.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"type": "string"
}
6 changes: 6 additions & 0 deletions tests/data/jsonschema/external_collapse/parent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"properties": {
"item": {"$ref": "child.json"}
}
}
9 changes: 9 additions & 0 deletions tests/data/jsonschema/external_reference/other/ref1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
{
"$ref": "ref2.json#/"
},
{"type": "null"}
]
}
7 changes: 7 additions & 0 deletions tests/data/jsonschema/external_reference/other/ref2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Other",
"properties": {
"key": {"type": "string"}
}
}
11 changes: 11 additions & 0 deletions tests/data/jsonschema/external_reference/ref0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"ref1": {
"$ref": "ref1.json#/"
},
"other_ref1": {
"$ref": "other/ref1.json#/"
}
}
}
9 changes: 9 additions & 0 deletions tests/data/jsonschema/external_reference/ref1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
{
"$ref": "ref2.json#/"
},
{"type": "null"}
]
}
6 changes: 6 additions & 0 deletions tests/data/jsonschema/external_reference/ref2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"key": {"type": "string"}
}
}
Loading

0 comments on commit 76e0067

Please sign in to comment.