Skip to content

Commit

Permalink
support empty array (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Apr 5, 2020
1 parent 3364fe8 commit 834cdd3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions datamodel_code_generator/model/pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
Import(from_='typing', import_='Dict'),
],
),
Types.null: DataType(type='Any', imports_=[Import(from_='typing', import_='Any')]),
}

kwargs_schema_to_model = {
Expand Down
3 changes: 2 additions & 1 deletion datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def get_model_by_path(schema: Dict[str, Any], keys: List[str]) -> Dict:
},
'boolean': {'default': Types.boolean},
'object': {'default': Types.object},
'null': {'default': Types.null},
}


Expand Down Expand Up @@ -341,7 +342,7 @@ def parse_array_fields(
if isinstance(obj.items, JsonSchemaObject):
items: List[JsonSchemaObject] = [obj.items]
else:
items = obj.items # type: ignore
items = obj.items or [] # type: ignore
item_obj_data_types: List[DataType] = []
is_union: bool = False
for item in items:
Expand Down
1 change: 1 addition & 0 deletions datamodel_code_generator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ class Types(Enum):
ipv6 = auto()
boolean = auto()
object = auto()
null = auto()
6 changes: 6 additions & 0 deletions tests/data/jsonschema/person.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
},
"friends": {
"type": "array"
},
"comment": {
"type": "null"
}
}
}
8 changes: 6 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def test_main_autodetect():
from __future__ import annotations
from typing import Optional
from typing import Any, List, Optional
from pydantic import BaseModel, Field, conint
Expand All @@ -330,6 +330,8 @@ class Person(BaseModel):
age: Optional[conint(ge=0.0)] = Field(
None, description='Age in years which must be equal to or greater than zero.'
)
friends: Optional[List] = None
comment: Optional[Any] = None
'''
)

Expand Down Expand Up @@ -384,7 +386,7 @@ def test_main_jsonschema():
from __future__ import annotations
from typing import Optional
from typing import Any, List, Optional
from pydantic import BaseModel, Field, conint
Expand All @@ -395,6 +397,8 @@ class Person(BaseModel):
age: Optional[conint(ge=0.0)] = Field(
None, description='Age in years which must be equal to or greater than zero.'
)
friends: Optional[List] = None
comment: Optional[Any] = None
'''
)
with pytest.raises(SystemExit):
Expand Down

0 comments on commit 834cdd3

Please sign in to comment.