From f4c041e4966b176e6fb4e7d6fd66f888346b7c62 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Fri, 28 Apr 2023 14:04:30 +0800 Subject: [PATCH] fix to_json, to_dict in python nextgen (#15337) --- .../python-nextgen/model_anyof.mustache | 16 ++++++++++++---- .../python-nextgen/model_oneof.mustache | 17 +++++++++++++---- .../petstore_api/models/any_of_color.py | 16 ++++++++++++---- .../petstore_api/models/any_of_pig.py | 16 ++++++++++++---- .../petstore_api/models/color.py | 17 +++++++++++++---- .../petstore_api/models/pig.py | 17 +++++++++++++---- .../petstore_api/models/any_of_color.py | 16 ++++++++++++---- .../petstore_api/models/any_of_pig.py | 16 ++++++++++++---- .../python-nextgen/petstore_api/models/color.py | 17 +++++++++++++---- .../python-nextgen/petstore_api/models/pig.py | 17 +++++++++++++---- .../petstore/python-nextgen/tests/test_model.py | 8 +++++++- 11 files changed, 132 insertions(+), 41 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-nextgen/model_anyof.mustache b/modules/openapi-generator/src/main/resources/python-nextgen/model_anyof.mustache index d0dbb52f33c..1ad7fd3438a 100644 --- a/modules/openapi-generator/src/main/resources/python-nextgen/model_anyof.mustache +++ b/modules/openapi-generator/src/main/resources/python-nextgen/model_anyof.mustache @@ -135,17 +135,25 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + return json.dumps(self.actual_instance) def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/modules/openapi-generator/src/main/resources/python-nextgen/model_oneof.mustache b/modules/openapi-generator/src/main/resources/python-nextgen/model_oneof.mustache index d0a2fc6b6c4..07df030757e 100644 --- a/modules/openapi-generator/src/main/resources/python-nextgen/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/python-nextgen/model_oneof.mustache @@ -161,17 +161,26 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + # primitive type + return self.actual_instance def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_color.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_color.py index 61dffbb4c50..ab70e3bded3 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_color.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_color.py @@ -115,17 +115,25 @@ class AnyOfColor(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + return json.dumps(self.actual_instance) def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_pig.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_pig.py index d18b58876df..59f6c40c266 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_pig.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/any_of_pig.py @@ -94,17 +94,25 @@ class AnyOfPig(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + return json.dumps(self.actual_instance) def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/color.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/color.py index 2cc4abc9757..32e773fca36 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/color.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/color.py @@ -129,17 +129,26 @@ class Color(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + # primitive type + return self.actual_instance def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pig.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pig.py index 5f27432d247..5b7205270e9 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pig.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/pig.py @@ -105,17 +105,26 @@ class Pig(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + # primitive type + return self.actual_instance def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_color.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_color.py index 61dffbb4c50..ab70e3bded3 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_color.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_color.py @@ -115,17 +115,25 @@ class AnyOfColor(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + return json.dumps(self.actual_instance) def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_pig.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_pig.py index d18b58876df..59f6c40c266 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_pig.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/any_of_pig.py @@ -94,17 +94,25 @@ class AnyOfPig(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + return json.dumps(self.actual_instance) def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/color.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/color.py index 2cc4abc9757..32e773fca36 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/color.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/color.py @@ -129,17 +129,26 @@ class Color(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + # primitive type + return self.actual_instance def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pig.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pig.py index e39ad8ccea9..913dde44590 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pig.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/pig.py @@ -120,17 +120,26 @@ class Pig(BaseModel): def to_json(self) -> str: """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() else: - return "null" + return json.dumps(self.actual_instance) def to_dict(self) -> dict: """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() else: - return dict() + # primitive type + return self.actual_instance def to_str(self) -> str: """Returns the string representation of the actual instance""" diff --git a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py index 021f75c8f47..0bc48418c0f 100644 --- a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py +++ b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py @@ -103,13 +103,19 @@ class ModelTests(unittest.TestCase): # test from_josn json_str = '[12,34,56]' 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: p = petstore_api.Color.from_json('[2342112,0,0,0]') except ValueError as 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 p = petstore_api.Color.from_json(None) self.assertEqual(p.actual_instance, None)