Skip to content

Commit

Permalink
Warn with default format for type when not found (#273)
Browse files Browse the repository at this point in the history
* Warn with default format for type when not found

* - fix code style
- add unittest

Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
  • Loading branch information
Liam-Deacon and koxudaxi authored Dec 25, 2020
1 parent 7701735 commit a23fe1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Type,
Union,
)
from warnings import warn

import yaml
from pydantic import BaseModel, Field, root_validator, validator
Expand Down Expand Up @@ -253,9 +254,17 @@ def get_data_type(self, obj: JsonSchemaObject) -> DataType:
return self.data_type_manager.get_data_type(Types.any)

def _get_data_type(type_: str, format__: str) -> DataType:
data_formats: Optional[Types] = json_schema_data_formats[type_].get(
format__
)
if data_formats is None:
warn(
"format of {!r} not understood for {!r} - using default"
"".format(format__, type_)
)
data_formats = json_schema_data_formats[type_]['default']
return self.data_type_manager.get_data_type(
json_schema_data_formats[type_][format__],
**obj.dict() if not self.field_constraints else {},
data_formats, **obj.dict() if not self.field_constraints else {},
)

if isinstance(obj.type, list):
Expand Down
1 change: 1 addition & 0 deletions tests/parser/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ def test_parse_nested_array():
('string', 'uuid5', 'UUID5', 'pydantic', 'UUID5'),
('string', 'ipv4', 'IPv4Address', 'pydantic', 'IPv4Address'),
('string', 'ipv6', 'IPv6Address', 'pydantic', 'IPv6Address'),
('string', 'unknown-type', 'str', None, None),
],
)
def test_get_data_type(schema_type, schema_format, result_type, from_, import_):
Expand Down

0 comments on commit a23fe1d

Please sign in to comment.