Skip to content

Commit

Permalink
support empty dict on items (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Jun 5, 2020
1 parent 6982fe9 commit 148460d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Union,
)

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, validator

from datamodel_code_generator import snooper_to_methods
from datamodel_code_generator.format import PythonVersion
Expand Down Expand Up @@ -110,6 +110,12 @@ def is_array(self) -> bool:
def ref_object_name(self) -> str:
return self.ref.rsplit('/', 1)[-1] # type: ignore

@validator('items', pre=True)
def validate_items(cls, values: Any) -> Any:
if not values: # this condition expects empty dict
return None
return values


JsonSchemaObject.update_forward_refs()

Expand Down Expand Up @@ -355,7 +361,7 @@ def parse_array_fields(
self, name: str, obj: JsonSchemaObject
) -> Tuple[List[DataModelFieldBase], List[DataType]]:
if isinstance(obj.items, JsonSchemaObject):
items: List[JsonSchemaObject] = [obj.items]
items = [obj.items]
else:
items = obj.items or []
item_obj_data_types: List[DataType] = []
Expand Down
5 changes: 5 additions & 0 deletions tests/parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ class Pets(BaseModel):
class Pets(BaseModel):
__root__: List[Pet]''',
),
(
{'type': 'array', 'items': {},},
'''class Pets(BaseModel):
__root__: List''',
),
],
)
def test_parse_array(source_obj, generated_classes):
Expand Down

0 comments on commit 148460d

Please sign in to comment.