Skip to content

Commit 0dc8452

Browse files
authored
[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
1 parent 1710615 commit 0dc8452

12 files changed

Lines changed: 73 additions & 34 deletions

File tree

modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
162162
{{#allVars}}
163163
{{#isNullable}}
164164
# set to None if {{{name}}} (nullable) is None
165-
if self.{{name}} is None:
165+
# and __fields_set__ contains the field
166+
if self.{{name}} is None and "{{{name}}}" in self.__fields_set__:
166167
_dict['{{{baseName}}}'] = None
167168

168169
{{/isNullable}}

samples/client/echo_api/python-nextgen/openapi_client/models/default_value.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,18 @@ def to_dict(self):
7171
},
7272
exclude_none=True)
7373
# set to None if array_string_nullable (nullable) is None
74-
if self.array_string_nullable is None:
74+
# and __fields_set__ contains the field
75+
if self.array_string_nullable is None and "array_string_nullable" in self.__fields_set__:
7576
_dict['array_string_nullable'] = None
7677

7778
# set to None if array_string_extension_nullable (nullable) is None
78-
if self.array_string_extension_nullable is None:
79+
# and __fields_set__ contains the field
80+
if self.array_string_extension_nullable is None and "array_string_extension_nullable" in self.__fields_set__:
7981
_dict['array_string_extension_nullable'] = None
8082

8183
# set to None if string_nullable (nullable) is None
82-
if self.string_nullable is None:
84+
# and __fields_set__ contains the field
85+
if self.string_nullable is None and "string_nullable" in self.__fields_set__:
8386
_dict['string_nullable'] = None
8487

8588
return _dict

samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/enum_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def to_dict(self):
103103
},
104104
exclude_none=True)
105105
# set to None if outer_enum (nullable) is None
106-
if self.outer_enum is None:
106+
# and __fields_set__ contains the field
107+
if self.outer_enum is None and "outer_enum" in self.__fields_set__:
107108
_dict['outerEnum'] = None
108109

109110
return _dict

samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/health_check_result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def to_dict(self):
5353
},
5454
exclude_none=True)
5555
# set to None if nullable_message (nullable) is None
56-
if self.nullable_message is None:
56+
# and __fields_set__ contains the field
57+
if self.nullable_message is None and "nullable_message" in self.__fields_set__:
5758
_dict['NullableMessage'] = None
5859

5960
return _dict

samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/nullable_class.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,58 @@ def to_dict(self):
6565
},
6666
exclude_none=True)
6767
# set to None if required_integer_prop (nullable) is None
68-
if self.required_integer_prop is None:
68+
# and __fields_set__ contains the field
69+
if self.required_integer_prop is None and "required_integer_prop" in self.__fields_set__:
6970
_dict['required_integer_prop'] = None
7071

7172
# set to None if integer_prop (nullable) is None
72-
if self.integer_prop is None:
73+
# and __fields_set__ contains the field
74+
if self.integer_prop is None and "integer_prop" in self.__fields_set__:
7375
_dict['integer_prop'] = None
7476

7577
# set to None if number_prop (nullable) is None
76-
if self.number_prop is None:
78+
# and __fields_set__ contains the field
79+
if self.number_prop is None and "number_prop" in self.__fields_set__:
7780
_dict['number_prop'] = None
7881

7982
# set to None if boolean_prop (nullable) is None
80-
if self.boolean_prop is None:
83+
# and __fields_set__ contains the field
84+
if self.boolean_prop is None and "boolean_prop" in self.__fields_set__:
8185
_dict['boolean_prop'] = None
8286

8387
# set to None if string_prop (nullable) is None
84-
if self.string_prop is None:
88+
# and __fields_set__ contains the field
89+
if self.string_prop is None and "string_prop" in self.__fields_set__:
8590
_dict['string_prop'] = None
8691

8792
# set to None if date_prop (nullable) is None
88-
if self.date_prop is None:
93+
# and __fields_set__ contains the field
94+
if self.date_prop is None and "date_prop" in self.__fields_set__:
8995
_dict['date_prop'] = None
9096

9197
# set to None if datetime_prop (nullable) is None
92-
if self.datetime_prop is None:
98+
# and __fields_set__ contains the field
99+
if self.datetime_prop is None and "datetime_prop" in self.__fields_set__:
93100
_dict['datetime_prop'] = None
94101

95102
# set to None if array_nullable_prop (nullable) is None
96-
if self.array_nullable_prop is None:
103+
# and __fields_set__ contains the field
104+
if self.array_nullable_prop is None and "array_nullable_prop" in self.__fields_set__:
97105
_dict['array_nullable_prop'] = None
98106

99107
# set to None if array_and_items_nullable_prop (nullable) is None
100-
if self.array_and_items_nullable_prop is None:
108+
# and __fields_set__ contains the field
109+
if self.array_and_items_nullable_prop is None and "array_and_items_nullable_prop" in self.__fields_set__:
101110
_dict['array_and_items_nullable_prop'] = None
102111

103112
# set to None if object_nullable_prop (nullable) is None
104-
if self.object_nullable_prop is None:
113+
# and __fields_set__ contains the field
114+
if self.object_nullable_prop is None and "object_nullable_prop" in self.__fields_set__:
105115
_dict['object_nullable_prop'] = None
106116

107117
# set to None if object_and_items_nullable_prop (nullable) is None
108-
if self.object_and_items_nullable_prop is None:
118+
# and __fields_set__ contains the field
119+
if self.object_and_items_nullable_prop is None and "object_and_items_nullable_prop" in self.__fields_set__:
109120
_dict['object_and_items_nullable_prop'] = None
110121

111122
return _dict

samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/outer_object_with_enum_property.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def to_dict(self):
5656
},
5757
exclude_none=True)
5858
# set to None if str_value (nullable) is None
59-
if self.str_value is None:
59+
# and __fields_set__ contains the field
60+
if self.str_value is None and "str_value" in self.__fields_set__:
6061
_dict['str_value'] = None
6162

6263
return _dict

samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_enum_ref_property(self):
195195
# test enum ref property
196196
# test to_json
197197
d = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1)
198-
self.assertEqual(d.to_json(), '{"value": 1, "str_value": null}')
198+
self.assertEqual(d.to_json(), '{"value": 1}')
199199
d2 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1, str_value=petstore_api.OuterEnum.DELIVERED)
200200
self.assertEqual(d2.to_json(), '{"str_value": "delivered", "value": 1}')
201201
# test from_json (round trip)

samples/openapi3/client/petstore/python-nextgen/petstore_api/models/enum_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def to_dict(self):
110110
_dict[_key] = _value
111111

112112
# set to None if outer_enum (nullable) is None
113-
if self.outer_enum is None:
113+
# and __fields_set__ contains the field
114+
if self.outer_enum is None and "outer_enum" in self.__fields_set__:
114115
_dict['outerEnum'] = None
115116

116117
return _dict

samples/openapi3/client/petstore/python-nextgen/petstore_api/models/health_check_result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def to_dict(self):
6060
_dict[_key] = _value
6161

6262
# set to None if nullable_message (nullable) is None
63-
if self.nullable_message is None:
63+
# and __fields_set__ contains the field
64+
if self.nullable_message is None and "nullable_message" in self.__fields_set__:
6465
_dict['NullableMessage'] = None
6566

6667
return _dict

samples/openapi3/client/petstore/python-nextgen/petstore_api/models/nullable_class.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,58 @@ def to_dict(self):
6565
},
6666
exclude_none=True)
6767
# set to None if required_integer_prop (nullable) is None
68-
if self.required_integer_prop is None:
68+
# and __fields_set__ contains the field
69+
if self.required_integer_prop is None and "required_integer_prop" in self.__fields_set__:
6970
_dict['required_integer_prop'] = None
7071

7172
# set to None if integer_prop (nullable) is None
72-
if self.integer_prop is None:
73+
# and __fields_set__ contains the field
74+
if self.integer_prop is None and "integer_prop" in self.__fields_set__:
7375
_dict['integer_prop'] = None
7476

7577
# set to None if number_prop (nullable) is None
76-
if self.number_prop is None:
78+
# and __fields_set__ contains the field
79+
if self.number_prop is None and "number_prop" in self.__fields_set__:
7780
_dict['number_prop'] = None
7881

7982
# set to None if boolean_prop (nullable) is None
80-
if self.boolean_prop is None:
83+
# and __fields_set__ contains the field
84+
if self.boolean_prop is None and "boolean_prop" in self.__fields_set__:
8185
_dict['boolean_prop'] = None
8286

8387
# set to None if string_prop (nullable) is None
84-
if self.string_prop is None:
88+
# and __fields_set__ contains the field
89+
if self.string_prop is None and "string_prop" in self.__fields_set__:
8590
_dict['string_prop'] = None
8691

8792
# set to None if date_prop (nullable) is None
88-
if self.date_prop is None:
93+
# and __fields_set__ contains the field
94+
if self.date_prop is None and "date_prop" in self.__fields_set__:
8995
_dict['date_prop'] = None
9096

9197
# set to None if datetime_prop (nullable) is None
92-
if self.datetime_prop is None:
98+
# and __fields_set__ contains the field
99+
if self.datetime_prop is None and "datetime_prop" in self.__fields_set__:
93100
_dict['datetime_prop'] = None
94101

95102
# set to None if array_nullable_prop (nullable) is None
96-
if self.array_nullable_prop is None:
103+
# and __fields_set__ contains the field
104+
if self.array_nullable_prop is None and "array_nullable_prop" in self.__fields_set__:
97105
_dict['array_nullable_prop'] = None
98106

99107
# set to None if array_and_items_nullable_prop (nullable) is None
100-
if self.array_and_items_nullable_prop is None:
108+
# and __fields_set__ contains the field
109+
if self.array_and_items_nullable_prop is None and "array_and_items_nullable_prop" in self.__fields_set__:
101110
_dict['array_and_items_nullable_prop'] = None
102111

103112
# set to None if object_nullable_prop (nullable) is None
104-
if self.object_nullable_prop is None:
113+
# and __fields_set__ contains the field
114+
if self.object_nullable_prop is None and "object_nullable_prop" in self.__fields_set__:
105115
_dict['object_nullable_prop'] = None
106116

107117
# set to None if object_and_items_nullable_prop (nullable) is None
108-
if self.object_and_items_nullable_prop is None:
118+
# and __fields_set__ contains the field
119+
if self.object_and_items_nullable_prop is None and "object_and_items_nullable_prop" in self.__fields_set__:
109120
_dict['object_and_items_nullable_prop'] = None
110121

111122
return _dict

0 commit comments

Comments
 (0)