fix to_json, to_dict in python nextgen (#15337)

This commit is contained in:
William Cheng 2023-04-28 14:04:30 +08:00 committed by GitHub
parent f4e0eb9bc9
commit f4c041e496
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 132 additions and 41 deletions

View File

@ -135,17 +135,25 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() return json.dumps(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"""

View File

@ -161,17 +161,26 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return None
to_dict = getattr(self.actual_instance, "to_dict", None)
if callable(to_dict):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() # 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"""

View File

@ -115,17 +115,25 @@ class AnyOfColor(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() return json.dumps(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"""

View File

@ -94,17 +94,25 @@ class AnyOfPig(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() return json.dumps(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"""

View File

@ -129,17 +129,26 @@ class Color(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return None
to_dict = getattr(self.actual_instance, "to_dict", None)
if callable(to_dict):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() # 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"""

View File

@ -105,17 +105,26 @@ class Pig(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return None
to_dict = getattr(self.actual_instance, "to_dict", None)
if callable(to_dict):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() # 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"""

View File

@ -115,17 +115,25 @@ class AnyOfColor(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() return json.dumps(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"""

View File

@ -94,17 +94,25 @@ class AnyOfPig(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() return json.dumps(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"""

View File

@ -129,17 +129,26 @@ class Color(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return None
to_dict = getattr(self.actual_instance, "to_dict", None)
if callable(to_dict):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() # 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"""

View File

@ -120,17 +120,26 @@ class Pig(BaseModel):
def to_json(self) -> str: def to_json(self) -> str:
"""Returns the JSON representation of the actual instance""" """Returns the JSON representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return "null"
to_json = getattr(self.actual_instance, "to_json", None)
if callable(to_json):
return self.actual_instance.to_json() return self.actual_instance.to_json()
else: else:
return "null" return json.dumps(self.actual_instance)
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""Returns the dict representation of the actual instance""" """Returns the dict representation of the actual instance"""
if self.actual_instance is not None: if self.actual_instance is None:
return None
to_dict = getattr(self.actual_instance, "to_dict", None)
if callable(to_dict):
return self.actual_instance.to_dict() return self.actual_instance.to_dict()
else: else:
return dict() # 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"""

View File

@ -103,13 +103,19 @@ class ModelTests(unittest.TestCase):
# test from_josn # test from_josn
json_str = '[12,34,56]' json_str = '[12,34,56]'
p = petstore_api.Color.from_json(json_str) p = petstore_api.Color.from_json(json_str)
self.assertEqual(p.actual_instance, [12, 34,56]) self.assertEqual(p.actual_instance, [12, 34, 56])
try: try:
p = petstore_api.Color.from_json('[2342112,0,0,0]') p = petstore_api.Color.from_json('[2342112,0,0,0]')
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 to_json, to_dict method
json_str = '[12,34,56]'
p = petstore_api.Color.from_json(json_str)
self.assertEqual(p.to_json(), "[12, 34, 56]")
self.assertEqual(p.to_dict(), [12, 34, 56])
# test nullable # test nullable
p = petstore_api.Color.from_json(None) p = petstore_api.Color.from_json(None)
self.assertEqual(p.actual_instance, None) self.assertEqual(p.actual_instance, None)