Skip to content

Commit

Permalink
Support typed default value (#154)
Browse files Browse the repository at this point in the history
* support typed default value

* add unittest for Field

* fix format
  • Loading branch information
koxudaxi authored Jun 17, 2020
1 parent 462420a commit 6630985
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
31 changes: 27 additions & 4 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ def validate_items(cls, values: Any) -> Any:
return None
return values

@property
def typed_default(self) -> Union[str, bool, Dict[Any, Any], None]:
if self.default and self.type:
type_ = self.type[0] if isinstance(self.type, list) else self.type
return self._get_typed_default(type_, self.default)
return None

@classmethod
def _get_typed_default(
cls, type_: str, default_value: Any
) -> Union[str, bool, Dict[Any, Any], None]:
if type_ == 'integer' or type_ == 'number':
return default_value
elif type_ == 'boolean':
if default_value == 'true':
return True
return False
elif type_ == 'object' and isinstance(default_value, dict):
return default_value
elif type_ == 'null':
return None
return f"'{default_value}'"


JsonSchemaObject.update_forward_refs()

Expand Down Expand Up @@ -348,7 +371,7 @@ def parse_object_fields(self, obj: JsonSchemaObject) -> List[DataModelFieldBase]
name=field_name,
example=field.examples,
description=field.description,
default=field.default,
default=field.typed_default,
title=field.title,
data_types=field_types,
required=required,
Expand Down Expand Up @@ -418,8 +441,8 @@ def parse_array_fields(
field = self.data_model_field_type(
data_types=item_obj_data_types,
example=obj.examples,
default=obj.default,
description=obj.default,
default=obj.typed_default,
description=obj.description,
title=obj.title,
required=True,
is_list=True,
Expand Down Expand Up @@ -463,7 +486,7 @@ def parse_root_type(self, name: str, obj: JsonSchemaObject) -> None:
data_types=types,
description=obj.description,
example=obj.examples,
default=obj.default,
default=obj.typed_default,
required=not obj.nullable,
)
],
Expand Down
2 changes: 2 additions & 0 deletions tests/model/pydantic/test_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def test_base_model_reserved_name():
{'examples': {'name': 'dog', 'age': 1}},
'Field(None, examples={"\'name\'": "\'dog\'", "\'age\'": 1})',
),
({'default': 'abc', 'title': 'title'}, 'Field(\'abc\', title=\'title\')'),
({'default': 123, 'title': 'title'}, 'Field(123, title=\'title\')'),
],
)
def test_data_model_field(kwargs, expected):
Expand Down
15 changes: 15 additions & 0 deletions tests/parser/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,18 @@ def test_parse_one_of_object(source_obj, generated_classes):
)
parser.parse_raw_obj('onOfObject', source_obj)
assert dump_templates(list(parser.results)) == generated_classes


@pytest.mark.parametrize(
'type_,default,expected',
[
('string', 'abc', "'abc'"),
('number', 123, 123),
('boolean', 'true', True),
('boolean', 'false', False),
('null', 'null', None),
('object', {'abc': 123, 'efg': 'hij'}, {'abc': 123, 'efg': 'hij'}),
],
)
def test_typed_default(type_, default, expected):
assert JsonSchemaObject(type=type_, default=default).typed_default == expected

0 comments on commit 6630985

Please sign in to comment.