Skip to content

Commit

Permalink
fix array include null (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Dec 25, 2020
1 parent a23fe1d commit 8df7158
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion datamodel_code_generator/model/pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
Types.ipv6: DataType(type='IPv6Address', imports_=[IMPORT_IPV6ADDRESS]),
Types.boolean: DataType(type='bool'),
Types.object: DataType(type='Dict[str, Any]', imports_=[IMPORT_ANY, IMPORT_DICT,],),
Types.null: DataType(type='Any', imports_=[IMPORT_ANY]),
Types.null: DataType(type='Any', imports_=[IMPORT_ANY], is_optional=True),
Types.array: DataType(type='List[Any]', imports_=[IMPORT_LIST, IMPORT_ANY]),
Types.any: DataType(type='Any', imports_=[IMPORT_ANY]),
}
Expand Down
13 changes: 12 additions & 1 deletion datamodel_code_generator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def __init__(self, **values: Any) -> None:
super().__init__(**values)
if self.type and (self.reference or self.ref):
self.unresolved_types.add(self.type)
for type_ in self.data_types:
if type_.type == 'Any' and type_.is_optional:
if any(t for t in self.data_types if t.type != 'Any'):
self.is_optional = True
self.data_types = [
t
for t in self.data_types
if not (t.type == 'Any' and t.is_optional)
]
break

imports: Tuple[Tuple[bool, Import], ...] = (
(self.is_optional, IMPORT_OPTIONAL),
(len(self.data_types) > 1, IMPORT_UNION),
Expand Down Expand Up @@ -100,7 +111,7 @@ def type_hint(self) -> str:
else:
dict_ = 'Dict'
type_ = f'{dict_}[str, {type_}]' if type_ else 'dict_'
if self.is_optional:
if self.is_optional and type_ != 'Any':
type_ = f'Optional[{type_}]'
if self.is_func:
if self.kwargs:
Expand Down
17 changes: 17 additions & 0 deletions tests/data/expected/main/main_json_array_include_null/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# generated by datamodel-codegen:
# filename: array_include_null.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from typing import List, Optional

from pydantic import BaseModel


class Item(BaseModel):
oofield: Optional[List[int]]


class Model(BaseModel):
items: List[Item]
10 changes: 10 additions & 0 deletions tests/data/json/array_include_null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"items": [
{
"oofield": null
},
{
"oofield": [1, 2, 3]
}
]
}
25 changes: 25 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,31 @@ def test_main_json_failed():
main()


@freeze_time('2019-07-26')
def test_main_json_arrary_include_null():
with TemporaryDirectory() as output_dir:
output_file: Path = Path(output_dir) / 'output.py'
return_code: Exit = main(
[
'--input',
str(JSON_DATA_PATH / 'array_include_null.json'),
'--output',
str(output_file),
'--input-file-type',
'json',
]
)
assert return_code == Exit.OK
assert (
output_file.read_text()
== (
EXPECTED_MAIN_PATH / 'main_json_array_include_null' / 'output.py'
).read_text()
)
with pytest.raises(SystemExit):
main()


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

0 comments on commit 8df7158

Please sign in to comment.