Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
@classmethod
def get_discriminator_value(cls, obj: Dict[str, Any]) -> Optional[str]:
"""Returns the discriminator value (object type) of the data"""
discriminator_value = obj[cls.__discriminator_property_name]
discriminator_value = obj.get(cls.__discriminator_property_name)
if discriminator_value:
return cls.__discriminator_value_class_map.get(discriminator_value)
else:
Expand Down Expand Up @@ -336,10 +336,9 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
_items = []
if self.{{{name}}}:
for _item_{{{name}}} in self.{{{name}}}:
if _item_{{{name}}}:
_items.append(
[{{#compatibleWithPythonLegacy}}_to_openapi_value(_inner_item){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_inner_item.to_dict(){{/compatibleWithPythonLegacy}} for _inner_item in _item_{{{name}}} if _inner_item is not None]
)
_items.append(
[{{#compatibleWithPythonLegacy}}_to_openapi_value(_inner_item){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_inner_item.to_dict(){{/compatibleWithPythonLegacy}} if _inner_item is not None else None for _inner_item in _item_{{{name}}}] if _item_{{{name}}} is not None else None
)
_dict[{{#lambda.pythonSingleQuotedStringLiteral}}{{{baseName}}}{{/lambda.pythonSingleQuotedStringLiteral}}] = _items
{{/items.items.isPrimitiveType}}
{{/items.isArray}}
Expand All @@ -349,10 +348,9 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
_items = []
if self.{{{name}}}:
for _item_{{{name}}} in self.{{{name}}}:
if _item_{{{name}}}:
_items.append(
{_inner_key: {{#compatibleWithPythonLegacy}}_to_openapi_value(_inner_value){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_inner_value.to_dict(){{/compatibleWithPythonLegacy}} for _inner_key, _inner_value in _item_{{{name}}}.items()}
)
_items.append(
{_inner_key: {{#compatibleWithPythonLegacy}}_to_openapi_value(_inner_value){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_inner_value.to_dict(){{/compatibleWithPythonLegacy}} if _inner_value is not None else None for _inner_key, _inner_value in _item_{{{name}}}.items()} if _item_{{{name}}} is not None else None
)
_dict[{{#lambda.pythonSingleQuotedStringLiteral}}{{{baseName}}}{{/lambda.pythonSingleQuotedStringLiteral}}] = _items
{{/items.items.isPrimitiveType}}
{{/items.isMap}}
Expand All @@ -364,8 +362,7 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
_items = []
if self.{{{name}}}:
for _item_{{{name}}} in self.{{{name}}}:
if _item_{{{name}}}:
_items.append({{#compatibleWithPythonLegacy}}_to_openapi_value(_item_{{{name}}}){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_item_{{{name}}}.to_dict(){{/compatibleWithPythonLegacy}})
_items.append({{#compatibleWithPythonLegacy}}_to_openapi_value(_item_{{{name}}}){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_item_{{{name}}}.to_dict(){{/compatibleWithPythonLegacy}} if _item_{{{name}}} is not None else None)
_dict[{{#lambda.pythonSingleQuotedStringLiteral}}{{{baseName}}}{{/lambda.pythonSingleQuotedStringLiteral}}] = _items
{{/items.isEnumOrRef}}
{{/items.isPrimitiveType}}
Expand All @@ -379,10 +376,9 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
_field_dict_of_array = {}
if self.{{{name}}}:
for _key_{{{name}}} in self.{{{name}}}:
if self.{{{name}}}[_key_{{{name}}}] is not None:
_field_dict_of_array[_key_{{{name}}}] = [
{{#compatibleWithPythonLegacy}}_to_openapi_value(_item){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_item.to_dict(){{/compatibleWithPythonLegacy}} for _item in self.{{{name}}}[_key_{{{name}}}]
]
_field_dict_of_array[_key_{{{name}}}] = [
{{#compatibleWithPythonLegacy}}_to_openapi_value(_item){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_item.to_dict(){{/compatibleWithPythonLegacy}} if _item is not None else None for _item in self.{{{name}}}[_key_{{{name}}}]
] if self.{{{name}}}[_key_{{{name}}}] is not None else None
_dict[{{#lambda.pythonSingleQuotedStringLiteral}}{{{baseName}}}{{/lambda.pythonSingleQuotedStringLiteral}}] = _field_dict_of_array
{{/items.items.isPrimitiveType}}
{{/items.isArray}}
Expand All @@ -392,10 +388,9 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
_field_dict_of_dict = {}
if self.{{{name}}}:
for _key_{{{name}}}, _value_{{{name}}} in self.{{{name}}}.items():
if _value_{{{name}}} is not None:
_field_dict_of_dict[_key_{{{name}}}] = {
_key: {{#compatibleWithPythonLegacy}}_to_openapi_value(_value){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_value.to_dict(){{/compatibleWithPythonLegacy}} for _key, _value in _value_{{{name}}}.items()
}
_field_dict_of_dict[_key_{{{name}}}] = {
_key: {{#compatibleWithPythonLegacy}}_to_openapi_value(_value){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}_value.to_dict(){{/compatibleWithPythonLegacy}} if _value is not None else None for _key, _value in _value_{{{name}}}.items()
} if _value_{{{name}}} is not None else None
_dict[{{#lambda.pythonSingleQuotedStringLiteral}}{{{baseName}}}{{/lambda.pythonSingleQuotedStringLiteral}}] = _field_dict_of_dict
{{/items.items.isPrimitiveType}}
{{/items.isMap}}
Expand All @@ -407,8 +402,7 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
_field_dict = {}
if self.{{{name}}}:
for _key_{{{name}}} in self.{{{name}}}:
if self.{{{name}}}[_key_{{{name}}}]:
_field_dict[_key_{{{name}}}] = {{#compatibleWithPythonLegacy}}_to_openapi_value(self.{{{name}}}[_key_{{{name}}}]){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}self.{{{name}}}[_key_{{{name}}}].to_dict(){{/compatibleWithPythonLegacy}}
_field_dict[_key_{{{name}}}] = {{#compatibleWithPythonLegacy}}_to_openapi_value(self.{{{name}}}[_key_{{{name}}}]){{/compatibleWithPythonLegacy}}{{^compatibleWithPythonLegacy}}self.{{{name}}}[_key_{{{name}}}].to_dict(){{/compatibleWithPythonLegacy}} if self.{{{name}}}[_key_{{{name}}}] is not None else None
_dict[{{#lambda.pythonSingleQuotedStringLiteral}}{{{baseName}}}{{/lambda.pythonSingleQuotedStringLiteral}}] = _field_dict
{{/items.isEnumOrRef}}
{{/items.isPrimitiveType}}
Expand Down Expand Up @@ -512,7 +506,7 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
{{/items.items.isPrimitiveType}}
{{^items.items.isPrimitiveType}}
{{{vendorExtensions.x-py-wire-name-literal}}}: [
[{{{items.items.dataType}}}.from_dict(_inner_item) for _inner_item in _item]
[{{{items.items.dataType}}}.from_dict(_inner_item) for _inner_item in _item] if _item is not None else None
for _item in obj[{{{vendorExtensions.x-py-wire-name-literal}}}]
] if obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) is not None else None{{^-last}},{{/-last}}
{{/items.items.isPrimitiveType}}
Expand All @@ -523,7 +517,7 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
{{/items.items.isPrimitiveType}}
{{^items.items.isPrimitiveType}}
{{{vendorExtensions.x-py-wire-name-literal}}}: [
{_inner_key: {{{items.items.dataType}}}.from_dict(_inner_value) for _inner_key, _inner_value in _item.items()}
{_inner_key: {{{items.items.dataType}}}.from_dict(_inner_value) for _inner_key, _inner_value in _item.items()} if _item is not None else None
for _item in obj[{{{vendorExtensions.x-py-wire-name-literal}}}]
] if obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) is not None else None{{^-last}},{{/-last}}
{{/items.items.isPrimitiveType}}
Expand Down Expand Up @@ -594,12 +588,12 @@ https://github.com/OpenAPITools/openapi-generator/blob/c84b949df1a9ec04ba75989cb
{{{vendorExtensions.x-py-wire-name-literal}}}: {{{dataType}}}.from_dict(obj[{{{vendorExtensions.x-py-wire-name-literal}}}]) if obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) is not None else None{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{#isEnumOrRef}}
{{{vendorExtensions.x-py-wire-name-literal}}}: obj.get({{{vendorExtensions.x-py-wire-name-literal}}}){{#defaultValue}} if obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) is not None else {{{defaultValue}}}{{/defaultValue}}{{^-last}},{{/-last}}
{{{vendorExtensions.x-py-wire-name-literal}}}: obj.get({{{vendorExtensions.x-py-wire-name-literal}}}){{#defaultValue}} if {{#isNullable}}{{{vendorExtensions.x-py-wire-name-literal}}} in obj{{/isNullable}}{{^isNullable}}obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) is not None{{/isNullable}} else {{{defaultValue}}}{{/defaultValue}}{{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#defaultValue}}
{{{vendorExtensions.x-py-wire-name-literal}}}: obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) if obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) is not None else {{{defaultValue}}}{{^-last}},{{/-last}}
{{{vendorExtensions.x-py-wire-name-literal}}}: obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) if {{#isNullable}}{{{vendorExtensions.x-py-wire-name-literal}}} in obj{{/isNullable}}{{^isNullable}}obj.get({{{vendorExtensions.x-py-wire-name-literal}}}) is not None{{/isNullable}} else {{{defaultValue}}}{{^-last}},{{/-last}}
{{/defaultValue}}
{{^defaultValue}}
{{{vendorExtensions.x-py-wire-name-literal}}}: obj.get({{{vendorExtensions.x-py-wire-name-literal}}}){{^-last}},{{/-last}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ def to_dict(self) -> Dict[str, Any]:
_items = []
if self.tags:
for _item_tags in self.tags:
if _item_tags:
_items.append(_item_tags.to_dict())
_items.append(_item_tags.to_dict() if _item_tags is not None else None)
_dict['tags'] = _items
return _dict

Expand Down
3 changes: 1 addition & 2 deletions samples/client/echo_api/python/openapi_client/models/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ def to_dict(self) -> Dict[str, Any]:
_items = []
if self.tags:
for _item_tags in self.tags:
if _item_tags:
_items.append(_item_tags.to_dict())
_items.append(_item_tags.to_dict() if _item_tags is not None else None)
_dict['tags'] = _items
return _dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,51 +328,45 @@ def __openapi_generator_modern_projection(self) -> Dict[str, Any]:
_items = []
if self.nested_list:
for _item_nested_list in self.nested_list:
if _item_nested_list:
_items.append(_to_openapi_value(_item_nested_list))
_items.append(_to_openapi_value(_item_nested_list) if _item_nested_list is not None else None)
_dict['nestedList'] = _items
# override the default output from pydantic by calling `to_dict()` of each value in nested_map (dict)
_field_dict = {}
if self.nested_map:
for _key_nested_map in self.nested_map:
if self.nested_map[_key_nested_map]:
_field_dict[_key_nested_map] = _to_openapi_value(self.nested_map[_key_nested_map])
_field_dict[_key_nested_map] = _to_openapi_value(self.nested_map[_key_nested_map]) if self.nested_map[_key_nested_map] is not None else None
_dict['nestedMap'] = _field_dict
# override the default output from pydantic by calling `to_dict()` of each item in nested_lists (list of list)
_items = []
if self.nested_lists:
for _item_nested_lists in self.nested_lists:
if _item_nested_lists:
_items.append(
[_to_openapi_value(_inner_item) for _inner_item in _item_nested_lists if _inner_item is not None]
)
_items.append(
[_to_openapi_value(_inner_item) if _inner_item is not None else None for _inner_item in _item_nested_lists] if _item_nested_lists is not None else None
)
_dict['nestedLists'] = _items
# override the default output from pydantic by calling `to_dict()` of each item in nested_maps (list of dict)
_items = []
if self.nested_maps:
for _item_nested_maps in self.nested_maps:
if _item_nested_maps:
_items.append(
{_inner_key: _to_openapi_value(_inner_value) for _inner_key, _inner_value in _item_nested_maps.items()}
)
_items.append(
{_inner_key: _to_openapi_value(_inner_value) if _inner_value is not None else None for _inner_key, _inner_value in _item_nested_maps.items()} if _item_nested_maps is not None else None
)
_dict['nestedMaps'] = _items
# override the default output from pydantic by calling `to_dict()` of each value in map_of_lists (dict of array)
_field_dict_of_array = {}
if self.map_of_lists:
for _key_map_of_lists in self.map_of_lists:
if self.map_of_lists[_key_map_of_lists] is not None:
_field_dict_of_array[_key_map_of_lists] = [
_to_openapi_value(_item) for _item in self.map_of_lists[_key_map_of_lists]
]
_field_dict_of_array[_key_map_of_lists] = [
_to_openapi_value(_item) if _item is not None else None for _item in self.map_of_lists[_key_map_of_lists]
] if self.map_of_lists[_key_map_of_lists] is not None else None
_dict['mapOfLists'] = _field_dict_of_array
# override the default output from pydantic by calling `to_dict()` of each value in map_of_maps (dict of dict)
_field_dict_of_dict = {}
if self.map_of_maps:
for _key_map_of_maps, _value_map_of_maps in self.map_of_maps.items():
if _value_map_of_maps is not None:
_field_dict_of_dict[_key_map_of_maps] = {
_key: _to_openapi_value(_value) for _key, _value in _value_map_of_maps.items()
}
_field_dict_of_dict[_key_map_of_maps] = {
_key: _to_openapi_value(_value) if _value is not None else None for _key, _value in _value_map_of_maps.items()
} if _value_map_of_maps is not None else None
_dict['mapOfMaps'] = _field_dict_of_dict
# set to None if nullable_value (nullable) is None
# and model_fields_set contains the field
Expand Down Expand Up @@ -424,11 +418,11 @@ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
if obj.get("nestedMap") is not None
else None,
"nestedLists": [
[NestedModel.from_dict(_inner_item) for _inner_item in _item]
[NestedModel.from_dict(_inner_item) for _inner_item in _item] if _item is not None else None
for _item in obj["nestedLists"]
] if obj.get("nestedLists") is not None else None,
"nestedMaps": [
{_inner_key: NestedModel.from_dict(_inner_value) for _inner_key, _inner_value in _item.items()}
{_inner_key: NestedModel.from_dict(_inner_value) for _inner_key, _inner_value in _item.items()} if _item is not None else None
for _item in obj["nestedMaps"]
] if obj.get("nestedMaps") is not None else None,
"mapOfLists": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ def to_dict(self) -> Dict[str, Any]:
_field_dict_of_dict = {}
if self.map_of_map_non_primitive_property:
for _key_map_of_map_non_primitive_property, _value_map_of_map_non_primitive_property in self.map_of_map_non_primitive_property.items():
if _value_map_of_map_non_primitive_property is not None:
_field_dict_of_dict[_key_map_of_map_non_primitive_property] = {
_key: _value.to_dict() for _key, _value in _value_map_of_map_non_primitive_property.items()
}
_field_dict_of_dict[_key_map_of_map_non_primitive_property] = {
_key: _value.to_dict() if _value is not None else None for _key, _value in _value_map_of_map_non_primitive_property.items()
} if _value_map_of_map_non_primitive_property is not None else None
_dict['map_of_map_non_primitive_property'] = _field_dict_of_dict
return _dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Animal(BaseModel):
@classmethod
def get_discriminator_value(cls, obj: Dict[str, Any]) -> Optional[str]:
"""Returns the discriminator value (object type) of the data"""
discriminator_value = obj[cls.__discriminator_property_name]
discriminator_value = obj.get(cls.__discriminator_property_name)
if discriminator_value:
return cls.__discriminator_value_class_map.get(discriminator_value)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ def to_dict(self) -> Dict[str, Any]:
_items = []
if self.another_property:
for _item_another_property in self.another_property:
if _item_another_property:
_items.append(
[_inner_item.to_dict() for _inner_item in _item_another_property if _inner_item is not None]
)
_items.append(
[_inner_item.to_dict() if _inner_item is not None else None for _inner_item in _item_another_property] if _item_another_property is not None else None
)
_dict['another_property'] = _items
return _dict

Expand All @@ -92,7 +91,7 @@ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:

_obj = cls.model_validate({
"another_property": [
[Tag.from_dict(_inner_item) for _inner_item in _item]
[Tag.from_dict(_inner_item) for _inner_item in _item] if _item is not None else None
for _item in obj["another_property"]
] if obj.get("another_property") is not None else None
})
Expand Down
Loading
Loading