Skip to content

Commit 740b971

Browse files
authored
python-pydantic-v1: Return the primitive type in to_dict for anyOf models (#19488)
* python: Return the primitive type in to_dict for anyOf models * Regenerate samples * Update test
1 parent cd349dc commit 740b971

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

modules/openapi-generator/src/main/resources/python-fastapi/model_anyof.mustache

+2-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
176176
if callable(to_json):
177177
return self.actual_instance.to_dict()
178178
else:
179-
return json.dumps(self.actual_instance)
179+
# primitive type
180+
return self.actual_instance
180181

181182
def to_str(self) -> str:
182183
"""Returns the string representation of the actual instance"""

modules/openapi-generator/src/main/resources/python-pydantic-v1/model_anyof.mustache

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
166166
if callable(to_json):
167167
return self.actual_instance.to_dict()
168168
else:
169-
return json.dumps(self.actual_instance)
169+
# primitive type
170+
return self.actual_instance
170171

171172
def to_str(self) -> str:
172173
"""Returns the string representation of the actual instance"""

samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/any_of_color.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def to_dict(self) -> dict:
146146
if callable(to_json):
147147
return self.actual_instance.to_dict()
148148
else:
149-
return json.dumps(self.actual_instance)
149+
# primitive type
150+
return self.actual_instance
150151

151152
def to_str(self) -> str:
152153
"""Returns the string representation of the actual instance"""

samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/any_of_pig.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ def to_dict(self) -> dict:
125125
if callable(to_json):
126126
return self.actual_instance.to_dict()
127127
else:
128-
return json.dumps(self.actual_instance)
128+
# primitive type
129+
return self.actual_instance
129130

130131
def to_str(self) -> str:
131132
"""Returns the string representation of the actual instance"""

samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/any_of_color.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def to_dict(self) -> dict:
146146
if callable(to_json):
147147
return self.actual_instance.to_dict()
148148
else:
149-
return json.dumps(self.actual_instance)
149+
# primitive type
150+
return self.actual_instance
150151

151152
def to_str(self) -> str:
152153
"""Returns the string representation of the actual instance"""

samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/any_of_pig.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ def to_dict(self) -> dict:
125125
if callable(to_json):
126126
return self.actual_instance.to_dict()
127127
else:
128-
return json.dumps(self.actual_instance)
128+
# primitive type
129+
return self.actual_instance
129130

130131
def to_str(self) -> str:
131132
"""Returns the string representation of the actual instance"""

samples/openapi3/client/petstore/python-pydantic-v1/tests/test_model.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def test_anyOf_array_of_integers(self):
177177
except ValueError as e:
178178
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
179179

180-
# test from_josn
180+
# test from_json
181181
json_str = '[12,34,56]'
182182
p = petstore_api.AnyOfColor.from_json(json_str)
183183
self.assertEqual(p.actual_instance, [12, 34,56])
@@ -187,6 +187,28 @@ def test_anyOf_array_of_integers(self):
187187
except ValueError as e:
188188
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
189189

190+
# test from_json, schema 3
191+
json_str = '"#123456"'
192+
p = petstore_api.AnyOfColor.from_json(json_str)
193+
self.assertIsInstance(p.actual_instance, str)
194+
self.assertEqual(p.actual_instance, '#123456')
195+
196+
# test to_json, schema 3
197+
p = petstore_api.AnyOfColor(actual_instance='#123456')
198+
self.assertEqual(p.to_json(), '"#123456"')
199+
200+
# test from_dict, schema 3
201+
obj = '#123456'
202+
p = petstore_api.AnyOfColor.from_dict(obj)
203+
self.assertIsInstance(p.actual_instance, str)
204+
self.assertEqual(p.actual_instance, '#123456')
205+
206+
# test to_dict, schema 3
207+
p = petstore_api.AnyOfColor(actual_instance='#123456')
208+
self.assertEqual(p.to_dict(), '#123456')
209+
p = petstore_api.AnyOfColor.from_dict(p.to_dict())
210+
self.assertEqual(p.actual_instance, '#123456')
211+
190212
def test_oneOf(self):
191213
# test new Pig
192214
bp = petstore_api.BasquePig.from_dict({"className": "BasquePig", "color": "red"})
@@ -300,6 +322,9 @@ def test_anyOf(self):
300322
" DanishPig expected dict not int (type=type_error)")
301323
self.assertEqual(str(e), error_message)
302324

325+
# test to_dict
326+
self.assertEqual(p.to_dict(), {'className': 'BasquePig', 'color': 'red'})
327+
303328
# test to_json
304329
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')
305330

0 commit comments

Comments
 (0)