diff --git a/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache b/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache index 6715306d13ba..71661abb123a 100644 --- a/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache @@ -162,7 +162,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{#allVars}} {{#isNullable}} # set to None if {{{name}}} (nullable) is None - if self.{{name}} is None: + # and __fields_set__ contains the field + if self.{{name}} is None and "{{{name}}}" in self.__fields_set__: _dict['{{{baseName}}}'] = None {{/isNullable}} diff --git a/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py b/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py index b4a7d1160c89..0d54cfc46c2c 100644 --- a/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py +++ b/samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py @@ -71,15 +71,18 @@ class DefaultValue(BaseModel): }, exclude_none=True) # set to None if array_string_nullable (nullable) is None - if self.array_string_nullable is None: + # and __fields_set__ contains the field + if self.array_string_nullable is None and "array_string_nullable" in self.__fields_set__: _dict['array_string_nullable'] = None # set to None if array_string_extension_nullable (nullable) is None - if self.array_string_extension_nullable is None: + # and __fields_set__ contains the field + if self.array_string_extension_nullable is None and "array_string_extension_nullable" in self.__fields_set__: _dict['array_string_extension_nullable'] = None # set to None if string_nullable (nullable) is None - if self.string_nullable is None: + # and __fields_set__ contains the field + if self.string_nullable is None and "string_nullable" in self.__fields_set__: _dict['string_nullable'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py index 5f097942ba58..fc634228f93c 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py @@ -103,7 +103,8 @@ class EnumTest(BaseModel): }, exclude_none=True) # set to None if outer_enum (nullable) is None - if self.outer_enum is None: + # and __fields_set__ contains the field + if self.outer_enum is None and "outer_enum" in self.__fields_set__: _dict['outerEnum'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/health_check_result.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/health_check_result.py index 78ca4ed28523..8f674067ff83 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/health_check_result.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/health_check_result.py @@ -53,7 +53,8 @@ class HealthCheckResult(BaseModel): }, exclude_none=True) # set to None if nullable_message (nullable) is None - if self.nullable_message is None: + # and __fields_set__ contains the field + if self.nullable_message is None and "nullable_message" in self.__fields_set__: _dict['NullableMessage'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/nullable_class.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/nullable_class.py index dcc98c945450..32d313365d62 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/nullable_class.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/nullable_class.py @@ -65,47 +65,58 @@ class NullableClass(BaseModel): }, exclude_none=True) # set to None if required_integer_prop (nullable) is None - if self.required_integer_prop is None: + # and __fields_set__ contains the field + if self.required_integer_prop is None and "required_integer_prop" in self.__fields_set__: _dict['required_integer_prop'] = None # set to None if integer_prop (nullable) is None - if self.integer_prop is None: + # and __fields_set__ contains the field + if self.integer_prop is None and "integer_prop" in self.__fields_set__: _dict['integer_prop'] = None # set to None if number_prop (nullable) is None - if self.number_prop is None: + # and __fields_set__ contains the field + if self.number_prop is None and "number_prop" in self.__fields_set__: _dict['number_prop'] = None # set to None if boolean_prop (nullable) is None - if self.boolean_prop is None: + # and __fields_set__ contains the field + if self.boolean_prop is None and "boolean_prop" in self.__fields_set__: _dict['boolean_prop'] = None # set to None if string_prop (nullable) is None - if self.string_prop is None: + # and __fields_set__ contains the field + if self.string_prop is None and "string_prop" in self.__fields_set__: _dict['string_prop'] = None # set to None if date_prop (nullable) is None - if self.date_prop is None: + # and __fields_set__ contains the field + if self.date_prop is None and "date_prop" in self.__fields_set__: _dict['date_prop'] = None # set to None if datetime_prop (nullable) is None - if self.datetime_prop is None: + # and __fields_set__ contains the field + if self.datetime_prop is None and "datetime_prop" in self.__fields_set__: _dict['datetime_prop'] = None # set to None if array_nullable_prop (nullable) is None - if self.array_nullable_prop is None: + # and __fields_set__ contains the field + if self.array_nullable_prop is None and "array_nullable_prop" in self.__fields_set__: _dict['array_nullable_prop'] = None # set to None if array_and_items_nullable_prop (nullable) is None - if self.array_and_items_nullable_prop is None: + # and __fields_set__ contains the field + if self.array_and_items_nullable_prop is None and "array_and_items_nullable_prop" in self.__fields_set__: _dict['array_and_items_nullable_prop'] = None # set to None if object_nullable_prop (nullable) is None - if self.object_nullable_prop is None: + # and __fields_set__ contains the field + if self.object_nullable_prop is None and "object_nullable_prop" in self.__fields_set__: _dict['object_nullable_prop'] = None # set to None if object_and_items_nullable_prop (nullable) is None - if self.object_and_items_nullable_prop is None: + # and __fields_set__ contains the field + if self.object_and_items_nullable_prop is None and "object_and_items_nullable_prop" in self.__fields_set__: _dict['object_and_items_nullable_prop'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/outer_object_with_enum_property.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/outer_object_with_enum_property.py index f41c127a7804..db3e40fb4122 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/outer_object_with_enum_property.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/outer_object_with_enum_property.py @@ -56,7 +56,8 @@ class OuterObjectWithEnumProperty(BaseModel): }, exclude_none=True) # set to None if str_value (nullable) is None - if self.str_value is None: + # and __fields_set__ contains the field + if self.str_value is None and "str_value" in self.__fields_set__: _dict['str_value'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py index e752abbe62eb..c9dc0681baf1 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py @@ -195,7 +195,7 @@ class ModelTests(unittest.TestCase): # test enum ref property # test to_json d = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1) - self.assertEqual(d.to_json(), '{"value": 1, "str_value": null}') + self.assertEqual(d.to_json(), '{"value": 1}') d2 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1, str_value=petstore_api.OuterEnum.DELIVERED) self.assertEqual(d2.to_json(), '{"str_value": "delivered", "value": 1}') # test from_json (round trip) diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py index 560b95f7bb4d..20db0d562a7e 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py @@ -110,7 +110,8 @@ class EnumTest(BaseModel): _dict[_key] = _value # set to None if outer_enum (nullable) is None - if self.outer_enum is None: + # and __fields_set__ contains the field + if self.outer_enum is None and "outer_enum" in self.__fields_set__: _dict['outerEnum'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/health_check_result.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/health_check_result.py index df9fe934a8d3..51dcd4cdccfe 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/health_check_result.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/health_check_result.py @@ -60,7 +60,8 @@ class HealthCheckResult(BaseModel): _dict[_key] = _value # set to None if nullable_message (nullable) is None - if self.nullable_message is None: + # and __fields_set__ contains the field + if self.nullable_message is None and "nullable_message" in self.__fields_set__: _dict['NullableMessage'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/nullable_class.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/nullable_class.py index 5ead496e689c..6c77bc263fa2 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/nullable_class.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/nullable_class.py @@ -65,47 +65,58 @@ class NullableClass(BaseModel): }, exclude_none=True) # set to None if required_integer_prop (nullable) is None - if self.required_integer_prop is None: + # and __fields_set__ contains the field + if self.required_integer_prop is None and "required_integer_prop" in self.__fields_set__: _dict['required_integer_prop'] = None # set to None if integer_prop (nullable) is None - if self.integer_prop is None: + # and __fields_set__ contains the field + if self.integer_prop is None and "integer_prop" in self.__fields_set__: _dict['integer_prop'] = None # set to None if number_prop (nullable) is None - if self.number_prop is None: + # and __fields_set__ contains the field + if self.number_prop is None and "number_prop" in self.__fields_set__: _dict['number_prop'] = None # set to None if boolean_prop (nullable) is None - if self.boolean_prop is None: + # and __fields_set__ contains the field + if self.boolean_prop is None and "boolean_prop" in self.__fields_set__: _dict['boolean_prop'] = None # set to None if string_prop (nullable) is None - if self.string_prop is None: + # and __fields_set__ contains the field + if self.string_prop is None and "string_prop" in self.__fields_set__: _dict['string_prop'] = None # set to None if date_prop (nullable) is None - if self.date_prop is None: + # and __fields_set__ contains the field + if self.date_prop is None and "date_prop" in self.__fields_set__: _dict['date_prop'] = None # set to None if datetime_prop (nullable) is None - if self.datetime_prop is None: + # and __fields_set__ contains the field + if self.datetime_prop is None and "datetime_prop" in self.__fields_set__: _dict['datetime_prop'] = None # set to None if array_nullable_prop (nullable) is None - if self.array_nullable_prop is None: + # and __fields_set__ contains the field + if self.array_nullable_prop is None and "array_nullable_prop" in self.__fields_set__: _dict['array_nullable_prop'] = None # set to None if array_and_items_nullable_prop (nullable) is None - if self.array_and_items_nullable_prop is None: + # and __fields_set__ contains the field + if self.array_and_items_nullable_prop is None and "array_and_items_nullable_prop" in self.__fields_set__: _dict['array_and_items_nullable_prop'] = None # set to None if object_nullable_prop (nullable) is None - if self.object_nullable_prop is None: + # and __fields_set__ contains the field + if self.object_nullable_prop is None and "object_nullable_prop" in self.__fields_set__: _dict['object_nullable_prop'] = None # set to None if object_and_items_nullable_prop (nullable) is None - if self.object_and_items_nullable_prop is None: + # and __fields_set__ contains the field + if self.object_and_items_nullable_prop is None and "object_and_items_nullable_prop" in self.__fields_set__: _dict['object_and_items_nullable_prop'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/outer_object_with_enum_property.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/outer_object_with_enum_property.py index 28a7bd69b9dc..93e47d8b48b9 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/outer_object_with_enum_property.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/outer_object_with_enum_property.py @@ -63,7 +63,8 @@ class OuterObjectWithEnumProperty(BaseModel): _dict[_key] = _value # set to None if str_value (nullable) is None - if self.str_value is None: + # and __fields_set__ contains the field + if self.str_value is None and "str_value" in self.__fields_set__: _dict['str_value'] = None return _dict diff --git a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py index 935c489dadaf..c32a36c74608 100644 --- a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py +++ b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py @@ -289,7 +289,7 @@ class ModelTests(unittest.TestCase): # test enum ref property # test to_json d = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1) - self.assertEqual(d.to_json(), '{"value": 1, "str_value": null}') + self.assertEqual(d.to_json(), '{"value": 1}') d2 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1, str_value=petstore_api.OuterEnum.DELIVERED) self.assertEqual(d2.to_json(), '{"str_value": "delivered", "value": 1}') # test from_json (round trip) @@ -297,6 +297,13 @@ class ModelTests(unittest.TestCase): self.assertEqual(d3.str_value, petstore_api.OuterEnum.DELIVERED) self.assertEqual(d3.value, petstore_api.OuterEnumInteger.NUMBER_1) self.assertEqual(d3.to_json(), '{"str_value": "delivered", "value": 1}') + d4 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1, str_value=None) + self.assertEqual(d4.to_json(), '{"value": 1, "str_value": null}') + d5 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1) + self.assertEqual(d5.__fields_set__, {'value'}) + d5.str_value = None # set None explicitly + self.assertEqual(d5.__fields_set__, {'value', 'str_value'}) + self.assertEqual(d5.to_json(), '{"value": 1, "str_value": null}') def test_valdiator(self): # test regular expression