forked from loafle/openapi-generator-original
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
This commit is contained in:
parent
cd349dc5ea
commit
740b971074
@ -176,7 +176,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
|
|||||||
if callable(to_json):
|
if callable(to_json):
|
||||||
return self.actual_instance.to_dict()
|
return self.actual_instance.to_dict()
|
||||||
else:
|
else:
|
||||||
return json.dumps(self.actual_instance)
|
# primitive type
|
||||||
|
return self.actual_instance
|
||||||
|
|
||||||
def to_str(self) -> str:
|
def to_str(self) -> str:
|
||||||
"""Returns the string representation of the actual instance"""
|
"""Returns the string representation of the actual instance"""
|
||||||
|
@ -166,7 +166,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
|
|||||||
if callable(to_json):
|
if callable(to_json):
|
||||||
return self.actual_instance.to_dict()
|
return self.actual_instance.to_dict()
|
||||||
else:
|
else:
|
||||||
return json.dumps(self.actual_instance)
|
# primitive type
|
||||||
|
return self.actual_instance
|
||||||
|
|
||||||
def to_str(self) -> str:
|
def to_str(self) -> str:
|
||||||
"""Returns the string representation of the actual instance"""
|
"""Returns the string representation of the actual instance"""
|
||||||
|
@ -146,7 +146,8 @@ class AnyOfColor(BaseModel):
|
|||||||
if callable(to_json):
|
if callable(to_json):
|
||||||
return self.actual_instance.to_dict()
|
return self.actual_instance.to_dict()
|
||||||
else:
|
else:
|
||||||
return json.dumps(self.actual_instance)
|
# primitive type
|
||||||
|
return self.actual_instance
|
||||||
|
|
||||||
def to_str(self) -> str:
|
def to_str(self) -> str:
|
||||||
"""Returns the string representation of the actual instance"""
|
"""Returns the string representation of the actual instance"""
|
||||||
|
@ -125,7 +125,8 @@ class AnyOfPig(BaseModel):
|
|||||||
if callable(to_json):
|
if callable(to_json):
|
||||||
return self.actual_instance.to_dict()
|
return self.actual_instance.to_dict()
|
||||||
else:
|
else:
|
||||||
return json.dumps(self.actual_instance)
|
# primitive type
|
||||||
|
return self.actual_instance
|
||||||
|
|
||||||
def to_str(self) -> str:
|
def to_str(self) -> str:
|
||||||
"""Returns the string representation of the actual instance"""
|
"""Returns the string representation of the actual instance"""
|
||||||
|
@ -146,7 +146,8 @@ class AnyOfColor(BaseModel):
|
|||||||
if callable(to_json):
|
if callable(to_json):
|
||||||
return self.actual_instance.to_dict()
|
return self.actual_instance.to_dict()
|
||||||
else:
|
else:
|
||||||
return json.dumps(self.actual_instance)
|
# primitive type
|
||||||
|
return self.actual_instance
|
||||||
|
|
||||||
def to_str(self) -> str:
|
def to_str(self) -> str:
|
||||||
"""Returns the string representation of the actual instance"""
|
"""Returns the string representation of the actual instance"""
|
||||||
|
@ -125,7 +125,8 @@ class AnyOfPig(BaseModel):
|
|||||||
if callable(to_json):
|
if callable(to_json):
|
||||||
return self.actual_instance.to_dict()
|
return self.actual_instance.to_dict()
|
||||||
else:
|
else:
|
||||||
return json.dumps(self.actual_instance)
|
# primitive type
|
||||||
|
return self.actual_instance
|
||||||
|
|
||||||
def to_str(self) -> str:
|
def to_str(self) -> str:
|
||||||
"""Returns the string representation of the actual instance"""
|
"""Returns the string representation of the actual instance"""
|
||||||
|
@ -177,7 +177,7 @@ class ModelTests(unittest.TestCase):
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
|
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
|
||||||
|
|
||||||
# test from_josn
|
# test from_json
|
||||||
json_str = '[12,34,56]'
|
json_str = '[12,34,56]'
|
||||||
p = petstore_api.AnyOfColor.from_json(json_str)
|
p = petstore_api.AnyOfColor.from_json(json_str)
|
||||||
self.assertEqual(p.actual_instance, [12, 34,56])
|
self.assertEqual(p.actual_instance, [12, 34,56])
|
||||||
@ -187,6 +187,28 @@ class ModelTests(unittest.TestCase):
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
|
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
|
||||||
|
|
||||||
|
# test from_json, schema 3
|
||||||
|
json_str = '"#123456"'
|
||||||
|
p = petstore_api.AnyOfColor.from_json(json_str)
|
||||||
|
self.assertIsInstance(p.actual_instance, str)
|
||||||
|
self.assertEqual(p.actual_instance, '#123456')
|
||||||
|
|
||||||
|
# test to_json, schema 3
|
||||||
|
p = petstore_api.AnyOfColor(actual_instance='#123456')
|
||||||
|
self.assertEqual(p.to_json(), '"#123456"')
|
||||||
|
|
||||||
|
# test from_dict, schema 3
|
||||||
|
obj = '#123456'
|
||||||
|
p = petstore_api.AnyOfColor.from_dict(obj)
|
||||||
|
self.assertIsInstance(p.actual_instance, str)
|
||||||
|
self.assertEqual(p.actual_instance, '#123456')
|
||||||
|
|
||||||
|
# test to_dict, schema 3
|
||||||
|
p = petstore_api.AnyOfColor(actual_instance='#123456')
|
||||||
|
self.assertEqual(p.to_dict(), '#123456')
|
||||||
|
p = petstore_api.AnyOfColor.from_dict(p.to_dict())
|
||||||
|
self.assertEqual(p.actual_instance, '#123456')
|
||||||
|
|
||||||
def test_oneOf(self):
|
def test_oneOf(self):
|
||||||
# test new Pig
|
# test new Pig
|
||||||
bp = petstore_api.BasquePig.from_dict({"className": "BasquePig", "color": "red"})
|
bp = petstore_api.BasquePig.from_dict({"className": "BasquePig", "color": "red"})
|
||||||
@ -300,6 +322,9 @@ class ModelTests(unittest.TestCase):
|
|||||||
" DanishPig expected dict not int (type=type_error)")
|
" DanishPig expected dict not int (type=type_error)")
|
||||||
self.assertEqual(str(e), error_message)
|
self.assertEqual(str(e), error_message)
|
||||||
|
|
||||||
|
# test to_dict
|
||||||
|
self.assertEqual(p.to_dict(), {'className': 'BasquePig', 'color': 'red'})
|
||||||
|
|
||||||
# test to_json
|
# test to_json
|
||||||
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')
|
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user