[python-nextgen] use __fields_set__ to determine if the field is needed in to_dict (#15086)

* use __fields_set__ to determine if the field is needed

* fix tests
This commit is contained in:
William Cheng
2023-04-01 10:23:38 +08:00
committed by GitHub
parent 1710615fd8
commit 0dc84520e7
12 changed files with 73 additions and 34 deletions

View File

@@ -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}}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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