From cf432522aa699a7f0780b0b8e52564f8d2fef6b1 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 21 Mar 2023 23:59:26 +0800 Subject: [PATCH] [python-nextgen] fix optional dict in property (#15009) * fix optional dict in property * update samples --- .../python-nextgen/model_generic.mustache | 2 +- ...ith-fake-endpoints-models-for-testing.yaml | 14 +++ .../.openapi-generator/FILES | 4 + .../petstore/python-nextgen-aiohttp/README.md | 2 + .../docs/InnerDictWithProperty.md | 28 ++++++ .../docs/ParentWithOptionalDict.md | 28 ++++++ .../petstore_api/__init__.py | 2 + .../petstore_api/models/__init__.py | 2 + .../models/inner_dict_with_property.py | 70 +++++++++++++++ ...perties_and_additional_properties_class.py | 2 +- .../models/parent_with_optional_dict.py | 78 ++++++++++++++++ .../test/test_inner_dict_with_property.py | 56 ++++++++++++ .../test/test_parent_with_optional_dict.py | 59 ++++++++++++ .../python-nextgen/.openapi-generator/FILES | 4 + .../client/petstore/python-nextgen/README.md | 2 + .../docs/InnerDictWithProperty.md | 28 ++++++ .../docs/ParentWithOptionalDict.md | 28 ++++++ .../python-nextgen/petstore_api/__init__.py | 2 + .../petstore_api/models/__init__.py | 2 + .../models/inner_dict_with_property.py | 82 +++++++++++++++++ ...perties_and_additional_properties_class.py | 2 +- .../models/parent_with_optional_dict.py | 90 +++++++++++++++++++ .../test/test_inner_dict_with_property.py | 56 ++++++++++++ .../test/test_parent_with_optional_dict.py | 59 ++++++++++++ .../python-nextgen/tests/test_model.py | 6 ++ 25 files changed, 705 insertions(+), 3 deletions(-) create mode 100644 samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/InnerDictWithProperty.md create mode 100644 samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/ParentWithOptionalDict.md create mode 100644 samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/inner_dict_with_property.py create mode 100644 samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/parent_with_optional_dict.py create mode 100644 samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_inner_dict_with_property.py create mode 100644 samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_parent_with_optional_dict.py create mode 100644 samples/openapi3/client/petstore/python-nextgen/docs/InnerDictWithProperty.md create mode 100644 samples/openapi3/client/petstore/python-nextgen/docs/ParentWithOptionalDict.md create mode 100644 samples/openapi3/client/petstore/python-nextgen/petstore_api/models/inner_dict_with_property.py create mode 100644 samples/openapi3/client/petstore/python-nextgen/petstore_api/models/parent_with_optional_dict.py create mode 100644 samples/openapi3/client/petstore/python-nextgen/test/test_inner_dict_with_property.py create mode 100644 samples/openapi3/client/petstore/python-nextgen/test/test_parent_with_optional_dict.py diff --git a/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache b/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache index 575e5940f5c..6e27b44bb2f 100644 --- a/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache @@ -220,7 +220,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} {{#isMap}} {{^items.isPrimitiveType}} {{^items.isEnumOrRef}} - "{{{name}}}": dict((_k, {{{dataType}}}.from_dict(_v)) for _k, _v in obj.get("{{{baseName}}}").items()){{^-last}},{{/-last}} + "{{{name}}}": dict((_k, {{{dataType}}}.from_dict(_v)) for _k, _v in obj.get("{{{baseName}}}").items()) if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}} {{/items.isEnumOrRef}} {{#items.isEnumOrRef}} "{{{name}}}": dict((_k, _v) for _k, _v in obj.get("{{{baseName}}}").items()){{^-last}},{{/-last}} diff --git a/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml index ac9df8980a4..128fc52b403 100644 --- a/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml @@ -2109,3 +2109,17 @@ components: - $ref: '#/components/schemas/RgbColor' - $ref: '#/components/schemas/RgbaColor' - $ref: '#/components/schemas/HexColor' + InnerDictWithProperty: + type: object + properties: + aProperty: + type: object + DictWithAdditionalProperties: + type: object + additionalProperties: + $ref: "#/components/schemas/InnerDictWithProperty" + ParentWithOptionalDict: + type: object + properties: + optionalDict: + $ref: "#/components/schemas/DictWithAdditionalProperties" diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/.openapi-generator/FILES b/samples/openapi3/client/petstore/python-nextgen-aiohttp/.openapi-generator/FILES index 8e90f16b24f..5d9cf72b28b 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/.openapi-generator/FILES @@ -39,6 +39,7 @@ docs/FooGetDefaultResponse.md docs/FormatTest.md docs/HasOnlyReadOnly.md docs/HealthCheckResult.md +docs/InnerDictWithProperty.md docs/List.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md @@ -55,6 +56,7 @@ docs/OuterEnumDefaultValue.md docs/OuterEnumInteger.md docs/OuterEnumIntegerDefaultValue.md docs/OuterObjectWithEnumProperty.md +docs/ParentWithOptionalDict.md docs/Pet.md docs/PetApi.md docs/Pig.md @@ -115,6 +117,7 @@ petstore_api/models/foo_get_default_response.py petstore_api/models/format_test.py petstore_api/models/has_only_read_only.py petstore_api/models/health_check_result.py +petstore_api/models/inner_dict_with_property.py petstore_api/models/list.py petstore_api/models/map_test.py petstore_api/models/mixed_properties_and_additional_properties_class.py @@ -131,6 +134,7 @@ petstore_api/models/outer_enum_default_value.py petstore_api/models/outer_enum_integer.py petstore_api/models/outer_enum_integer_default_value.py petstore_api/models/outer_object_with_enum_property.py +petstore_api/models/parent_with_optional_dict.py petstore_api/models/pet.py petstore_api/models/pig.py petstore_api/models/read_only_first.py diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/README.md b/samples/openapi3/client/petstore/python-nextgen-aiohttp/README.md index be281eb9946..9a72457e234 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/README.md +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/README.md @@ -164,6 +164,7 @@ Class | Method | HTTP request | Description - [FormatTest](docs/FormatTest.md) - [HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [HealthCheckResult](docs/HealthCheckResult.md) + - [InnerDictWithProperty](docs/InnerDictWithProperty.md) - [List](docs/List.md) - [MapTest](docs/MapTest.md) - [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) @@ -180,6 +181,7 @@ Class | Method | HTTP request | Description - [OuterEnumInteger](docs/OuterEnumInteger.md) - [OuterEnumIntegerDefaultValue](docs/OuterEnumIntegerDefaultValue.md) - [OuterObjectWithEnumProperty](docs/OuterObjectWithEnumProperty.md) + - [ParentWithOptionalDict](docs/ParentWithOptionalDict.md) - [Pet](docs/Pet.md) - [Pig](docs/Pig.md) - [ReadOnlyFirst](docs/ReadOnlyFirst.md) diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/InnerDictWithProperty.md b/samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/InnerDictWithProperty.md new file mode 100644 index 00000000000..45d76ad458c --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/InnerDictWithProperty.md @@ -0,0 +1,28 @@ +# InnerDictWithProperty + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**a_property** | **object** | | [optional] + +## Example + +```python +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty + +# TODO update the JSON string below +json = "{}" +# create an instance of InnerDictWithProperty from a JSON string +inner_dict_with_property_instance = InnerDictWithProperty.from_json(json) +# print the JSON string representation of the object +print InnerDictWithProperty.to_json() + +# convert the object into a dict +inner_dict_with_property_dict = inner_dict_with_property_instance.to_dict() +# create an instance of InnerDictWithProperty from a dict +inner_dict_with_property_form_dict = inner_dict_with_property.from_dict(inner_dict_with_property_dict) +``` +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/ParentWithOptionalDict.md b/samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/ParentWithOptionalDict.md new file mode 100644 index 00000000000..04bf9494201 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/ParentWithOptionalDict.md @@ -0,0 +1,28 @@ +# ParentWithOptionalDict + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**optional_dict** | [**Dict[str, InnerDictWithProperty]**](InnerDictWithProperty.md) | | [optional] + +## Example + +```python +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict + +# TODO update the JSON string below +json = "{}" +# create an instance of ParentWithOptionalDict from a JSON string +parent_with_optional_dict_instance = ParentWithOptionalDict.from_json(json) +# print the JSON string representation of the object +print ParentWithOptionalDict.to_json() + +# convert the object into a dict +parent_with_optional_dict_dict = parent_with_optional_dict_instance.to_dict() +# create an instance of ParentWithOptionalDict from a dict +parent_with_optional_dict_form_dict = parent_with_optional_dict.from_dict(parent_with_optional_dict_dict) +``` +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/__init__.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/__init__.py index 4b74838606e..bbf50585f34 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/__init__.py @@ -69,6 +69,7 @@ from petstore_api.models.foo_get_default_response import FooGetDefaultResponse from petstore_api.models.format_test import FormatTest from petstore_api.models.has_only_read_only import HasOnlyReadOnly from petstore_api.models.health_check_result import HealthCheckResult +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.list import List from petstore_api.models.map_test import MapTest from petstore_api.models.mixed_properties_and_additional_properties_class import MixedPropertiesAndAdditionalPropertiesClass @@ -85,6 +86,7 @@ from petstore_api.models.outer_enum_default_value import OuterEnumDefaultValue from petstore_api.models.outer_enum_integer import OuterEnumInteger from petstore_api.models.outer_enum_integer_default_value import OuterEnumIntegerDefaultValue from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict from petstore_api.models.pet import Pet from petstore_api.models.pig import Pig from petstore_api.models.read_only_first import ReadOnlyFirst diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/__init__.py index c1bd7823cfb..5f313ba72ae 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/__init__.py @@ -48,6 +48,7 @@ from petstore_api.models.foo_get_default_response import FooGetDefaultResponse from petstore_api.models.format_test import FormatTest from petstore_api.models.has_only_read_only import HasOnlyReadOnly from petstore_api.models.health_check_result import HealthCheckResult +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.list import List from petstore_api.models.map_test import MapTest from petstore_api.models.mixed_properties_and_additional_properties_class import MixedPropertiesAndAdditionalPropertiesClass @@ -64,6 +65,7 @@ from petstore_api.models.outer_enum_default_value import OuterEnumDefaultValue from petstore_api.models.outer_enum_integer import OuterEnumInteger from petstore_api.models.outer_enum_integer_default_value import OuterEnumIntegerDefaultValue from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict from petstore_api.models.pet import Pet from petstore_api.models.pig import Pig from petstore_api.models.read_only_first import ReadOnlyFirst diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/inner_dict_with_property.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/inner_dict_with_property.py new file mode 100644 index 00000000000..8bce01a25b4 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/inner_dict_with_property.py @@ -0,0 +1,70 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import annotations +from inspect import getfullargspec +import pprint +import re # noqa: F401 +import json + + +from typing import Any, Dict, Optional +from pydantic import BaseModel, Field + +class InnerDictWithProperty(BaseModel): + """ + InnerDictWithProperty + """ + a_property: Optional[Dict[str, Any]] = Field(None, alias="aProperty") + __properties = ["aProperty"] + + class Config: + allow_population_by_field_name = True + validate_assignment = True + + def to_str(self) -> str: + """Returns the string representation of the model using alias""" + return pprint.pformat(self.dict(by_alias=True)) + + def to_json(self) -> str: + """Returns the JSON representation of the model using alias""" + return json.dumps(self.to_dict()) + + @classmethod + def from_json(cls, json_str: str) -> InnerDictWithProperty: + """Create an instance of InnerDictWithProperty from a JSON string""" + return cls.from_dict(json.loads(json_str)) + + def to_dict(self): + """Returns the dictionary representation of the model using alias""" + _dict = self.dict(by_alias=True, + exclude={ + }, + exclude_none=True) + return _dict + + @classmethod + def from_dict(cls, obj: dict) -> InnerDictWithProperty: + """Create an instance of InnerDictWithProperty from a dict""" + if obj is None: + return None + + if type(obj) is not dict: + return InnerDictWithProperty.parse_obj(obj) + + _obj = InnerDictWithProperty.parse_obj({ + "a_property": obj.get("aProperty") + }) + return _obj + diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/mixed_properties_and_additional_properties_class.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/mixed_properties_and_additional_properties_class.py index 01f82a8d827..28122bd3b75 100644 --- a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/mixed_properties_and_additional_properties_class.py +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/mixed_properties_and_additional_properties_class.py @@ -76,7 +76,7 @@ class MixedPropertiesAndAdditionalPropertiesClass(BaseModel): _obj = MixedPropertiesAndAdditionalPropertiesClass.parse_obj({ "uuid": obj.get("uuid"), "date_time": obj.get("dateTime"), - "map": dict((_k, Dict[str, Animal].from_dict(_v)) for _k, _v in obj.get("map").items()) + "map": dict((_k, Dict[str, Animal].from_dict(_v)) for _k, _v in obj.get("map").items()) if obj.get("map") is not None else None }) return _obj diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/parent_with_optional_dict.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/parent_with_optional_dict.py new file mode 100644 index 00000000000..84843058284 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/parent_with_optional_dict.py @@ -0,0 +1,78 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import annotations +from inspect import getfullargspec +import pprint +import re # noqa: F401 +import json + + +from typing import Dict, Optional +from pydantic import BaseModel, Field +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty + +class ParentWithOptionalDict(BaseModel): + """ + ParentWithOptionalDict + """ + optional_dict: Optional[Dict[str, InnerDictWithProperty]] = Field(None, alias="optionalDict") + __properties = ["optionalDict"] + + class Config: + allow_population_by_field_name = True + validate_assignment = True + + def to_str(self) -> str: + """Returns the string representation of the model using alias""" + return pprint.pformat(self.dict(by_alias=True)) + + def to_json(self) -> str: + """Returns the JSON representation of the model using alias""" + return json.dumps(self.to_dict()) + + @classmethod + def from_json(cls, json_str: str) -> ParentWithOptionalDict: + """Create an instance of ParentWithOptionalDict from a JSON string""" + return cls.from_dict(json.loads(json_str)) + + def to_dict(self): + """Returns the dictionary representation of the model using alias""" + _dict = self.dict(by_alias=True, + exclude={ + }, + exclude_none=True) + # override the default output from pydantic by calling `to_dict()` of each value in optional_dict (dict) + _field_dict = {} + if self.optional_dict: + for _key in self.optional_dict: + if self.optional_dict[_key]: + _field_dict[_key] = self.optional_dict[_key].to_dict() + _dict['optionalDict'] = _field_dict + return _dict + + @classmethod + def from_dict(cls, obj: dict) -> ParentWithOptionalDict: + """Create an instance of ParentWithOptionalDict from a dict""" + if obj is None: + return None + + if type(obj) is not dict: + return ParentWithOptionalDict.parse_obj(obj) + + _obj = ParentWithOptionalDict.parse_obj({ + "optional_dict": dict((_k, Dict[str, InnerDictWithProperty].from_dict(_v)) for _k, _v in obj.get("optionalDict").items()) if obj.get("optionalDict") is not None else None + }) + return _obj + diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_inner_dict_with_property.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_inner_dict_with_property.py new file mode 100644 index 00000000000..8a63b70d552 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_inner_dict_with_property.py @@ -0,0 +1,56 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import absolute_import + +import unittest +import datetime + +import petstore_api +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty # noqa: E501 +from petstore_api.rest import ApiException + +class TestInnerDictWithProperty(unittest.TestCase): + """InnerDictWithProperty unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional): + """Test InnerDictWithProperty + include_option is a boolean, when False only required + params are included, when True both required and + optional params are included """ + # uncomment below to create an instance of `InnerDictWithProperty` + """ + model = petstore_api.models.inner_dict_with_property.InnerDictWithProperty() # noqa: E501 + if include_optional : + return InnerDictWithProperty( + a_property = None + ) + else : + return InnerDictWithProperty( + ) + """ + + def testInnerDictWithProperty(self): + """Test InnerDictWithProperty""" + # inst_req_only = self.make_instance(include_optional=False) + # inst_req_and_optional = self.make_instance(include_optional=True) + +if __name__ == '__main__': + unittest.main() diff --git a/samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_parent_with_optional_dict.py b/samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_parent_with_optional_dict.py new file mode 100644 index 00000000000..25b769e3d39 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen-aiohttp/test/test_parent_with_optional_dict.py @@ -0,0 +1,59 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import absolute_import + +import unittest +import datetime + +import petstore_api +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict # noqa: E501 +from petstore_api.rest import ApiException + +class TestParentWithOptionalDict(unittest.TestCase): + """ParentWithOptionalDict unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional): + """Test ParentWithOptionalDict + include_option is a boolean, when False only required + params are included, when True both required and + optional params are included """ + # uncomment below to create an instance of `ParentWithOptionalDict` + """ + model = petstore_api.models.parent_with_optional_dict.ParentWithOptionalDict() # noqa: E501 + if include_optional : + return ParentWithOptionalDict( + optional_dict = { + 'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty( + a_property = petstore_api.models.a_property.aProperty(), ) + } + ) + else : + return ParentWithOptionalDict( + ) + """ + + def testParentWithOptionalDict(self): + """Test ParentWithOptionalDict""" + # inst_req_only = self.make_instance(include_optional=False) + # inst_req_and_optional = self.make_instance(include_optional=True) + +if __name__ == '__main__': + unittest.main() diff --git a/samples/openapi3/client/petstore/python-nextgen/.openapi-generator/FILES b/samples/openapi3/client/petstore/python-nextgen/.openapi-generator/FILES index 0bd7787dea2..c84577aa151 100755 --- a/samples/openapi3/client/petstore/python-nextgen/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python-nextgen/.openapi-generator/FILES @@ -39,6 +39,7 @@ docs/FooGetDefaultResponse.md docs/FormatTest.md docs/HasOnlyReadOnly.md docs/HealthCheckResult.md +docs/InnerDictWithProperty.md docs/List.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md @@ -55,6 +56,7 @@ docs/OuterEnumDefaultValue.md docs/OuterEnumInteger.md docs/OuterEnumIntegerDefaultValue.md docs/OuterObjectWithEnumProperty.md +docs/ParentWithOptionalDict.md docs/Pet.md docs/PetApi.md docs/Pig.md @@ -115,6 +117,7 @@ petstore_api/models/foo_get_default_response.py petstore_api/models/format_test.py petstore_api/models/has_only_read_only.py petstore_api/models/health_check_result.py +petstore_api/models/inner_dict_with_property.py petstore_api/models/list.py petstore_api/models/map_test.py petstore_api/models/mixed_properties_and_additional_properties_class.py @@ -131,6 +134,7 @@ petstore_api/models/outer_enum_default_value.py petstore_api/models/outer_enum_integer.py petstore_api/models/outer_enum_integer_default_value.py petstore_api/models/outer_object_with_enum_property.py +petstore_api/models/parent_with_optional_dict.py petstore_api/models/pet.py petstore_api/models/pig.py petstore_api/models/read_only_first.py diff --git a/samples/openapi3/client/petstore/python-nextgen/README.md b/samples/openapi3/client/petstore/python-nextgen/README.md index c5e730e2862..e64a6743b26 100755 --- a/samples/openapi3/client/petstore/python-nextgen/README.md +++ b/samples/openapi3/client/petstore/python-nextgen/README.md @@ -164,6 +164,7 @@ Class | Method | HTTP request | Description - [FormatTest](docs/FormatTest.md) - [HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [HealthCheckResult](docs/HealthCheckResult.md) + - [InnerDictWithProperty](docs/InnerDictWithProperty.md) - [List](docs/List.md) - [MapTest](docs/MapTest.md) - [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) @@ -180,6 +181,7 @@ Class | Method | HTTP request | Description - [OuterEnumInteger](docs/OuterEnumInteger.md) - [OuterEnumIntegerDefaultValue](docs/OuterEnumIntegerDefaultValue.md) - [OuterObjectWithEnumProperty](docs/OuterObjectWithEnumProperty.md) + - [ParentWithOptionalDict](docs/ParentWithOptionalDict.md) - [Pet](docs/Pet.md) - [Pig](docs/Pig.md) - [ReadOnlyFirst](docs/ReadOnlyFirst.md) diff --git a/samples/openapi3/client/petstore/python-nextgen/docs/InnerDictWithProperty.md b/samples/openapi3/client/petstore/python-nextgen/docs/InnerDictWithProperty.md new file mode 100644 index 00000000000..45d76ad458c --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen/docs/InnerDictWithProperty.md @@ -0,0 +1,28 @@ +# InnerDictWithProperty + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**a_property** | **object** | | [optional] + +## Example + +```python +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty + +# TODO update the JSON string below +json = "{}" +# create an instance of InnerDictWithProperty from a JSON string +inner_dict_with_property_instance = InnerDictWithProperty.from_json(json) +# print the JSON string representation of the object +print InnerDictWithProperty.to_json() + +# convert the object into a dict +inner_dict_with_property_dict = inner_dict_with_property_instance.to_dict() +# create an instance of InnerDictWithProperty from a dict +inner_dict_with_property_form_dict = inner_dict_with_property.from_dict(inner_dict_with_property_dict) +``` +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/python-nextgen/docs/ParentWithOptionalDict.md b/samples/openapi3/client/petstore/python-nextgen/docs/ParentWithOptionalDict.md new file mode 100644 index 00000000000..04bf9494201 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen/docs/ParentWithOptionalDict.md @@ -0,0 +1,28 @@ +# ParentWithOptionalDict + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**optional_dict** | [**Dict[str, InnerDictWithProperty]**](InnerDictWithProperty.md) | | [optional] + +## Example + +```python +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict + +# TODO update the JSON string below +json = "{}" +# create an instance of ParentWithOptionalDict from a JSON string +parent_with_optional_dict_instance = ParentWithOptionalDict.from_json(json) +# print the JSON string representation of the object +print ParentWithOptionalDict.to_json() + +# convert the object into a dict +parent_with_optional_dict_dict = parent_with_optional_dict_instance.to_dict() +# create an instance of ParentWithOptionalDict from a dict +parent_with_optional_dict_form_dict = parent_with_optional_dict.from_dict(parent_with_optional_dict_dict) +``` +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/__init__.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/__init__.py index 4b74838606e..bbf50585f34 100755 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/__init__.py @@ -69,6 +69,7 @@ from petstore_api.models.foo_get_default_response import FooGetDefaultResponse from petstore_api.models.format_test import FormatTest from petstore_api.models.has_only_read_only import HasOnlyReadOnly from petstore_api.models.health_check_result import HealthCheckResult +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.list import List from petstore_api.models.map_test import MapTest from petstore_api.models.mixed_properties_and_additional_properties_class import MixedPropertiesAndAdditionalPropertiesClass @@ -85,6 +86,7 @@ from petstore_api.models.outer_enum_default_value import OuterEnumDefaultValue from petstore_api.models.outer_enum_integer import OuterEnumInteger from petstore_api.models.outer_enum_integer_default_value import OuterEnumIntegerDefaultValue from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict from petstore_api.models.pet import Pet from petstore_api.models.pig import Pig from petstore_api.models.read_only_first import ReadOnlyFirst diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/__init__.py index c1bd7823cfb..5f313ba72ae 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/__init__.py @@ -48,6 +48,7 @@ from petstore_api.models.foo_get_default_response import FooGetDefaultResponse from petstore_api.models.format_test import FormatTest from petstore_api.models.has_only_read_only import HasOnlyReadOnly from petstore_api.models.health_check_result import HealthCheckResult +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.list import List from petstore_api.models.map_test import MapTest from petstore_api.models.mixed_properties_and_additional_properties_class import MixedPropertiesAndAdditionalPropertiesClass @@ -64,6 +65,7 @@ from petstore_api.models.outer_enum_default_value import OuterEnumDefaultValue from petstore_api.models.outer_enum_integer import OuterEnumInteger from petstore_api.models.outer_enum_integer_default_value import OuterEnumIntegerDefaultValue from petstore_api.models.outer_object_with_enum_property import OuterObjectWithEnumProperty +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict from petstore_api.models.pet import Pet from petstore_api.models.pig import Pig from petstore_api.models.read_only_first import ReadOnlyFirst diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/inner_dict_with_property.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/inner_dict_with_property.py new file mode 100644 index 00000000000..23cc8e6a3a1 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/inner_dict_with_property.py @@ -0,0 +1,82 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import annotations +from inspect import getfullargspec +import pprint +import re # noqa: F401 +import json + + +from typing import Any, Dict, Optional +from pydantic import BaseModel, Field + +class InnerDictWithProperty(BaseModel): + """ + InnerDictWithProperty + """ + a_property: Optional[Dict[str, Any]] = Field(None, alias="aProperty") + additional_properties: Dict[str, Any] = {} + __properties = ["aProperty"] + + class Config: + allow_population_by_field_name = True + validate_assignment = True + + def to_str(self) -> str: + """Returns the string representation of the model using alias""" + return pprint.pformat(self.dict(by_alias=True)) + + def to_json(self) -> str: + """Returns the JSON representation of the model using alias""" + return json.dumps(self.to_dict()) + + @classmethod + def from_json(cls, json_str: str) -> InnerDictWithProperty: + """Create an instance of InnerDictWithProperty from a JSON string""" + return cls.from_dict(json.loads(json_str)) + + def to_dict(self): + """Returns the dictionary representation of the model using alias""" + _dict = self.dict(by_alias=True, + exclude={ + "additional_properties" + }, + exclude_none=True) + # puts key-value pairs in additional_properties in the top level + if self.additional_properties is not None: + for _key, _value in self.additional_properties.items(): + _dict[_key] = _value + + return _dict + + @classmethod + def from_dict(cls, obj: dict) -> InnerDictWithProperty: + """Create an instance of InnerDictWithProperty from a dict""" + if obj is None: + return None + + if type(obj) is not dict: + return InnerDictWithProperty.parse_obj(obj) + + _obj = InnerDictWithProperty.parse_obj({ + "a_property": obj.get("aProperty") + }) + # store additional fields in additional_properties + for _key in obj.keys(): + if _key not in cls.__properties: + _obj.additional_properties[_key] = obj.get(_key) + + return _obj + diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/mixed_properties_and_additional_properties_class.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/mixed_properties_and_additional_properties_class.py index b3b259b688f..2ea8cad7e4f 100644 --- a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/mixed_properties_and_additional_properties_class.py +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/mixed_properties_and_additional_properties_class.py @@ -83,7 +83,7 @@ class MixedPropertiesAndAdditionalPropertiesClass(BaseModel): _obj = MixedPropertiesAndAdditionalPropertiesClass.parse_obj({ "uuid": obj.get("uuid"), "date_time": obj.get("dateTime"), - "map": dict((_k, Dict[str, Animal].from_dict(_v)) for _k, _v in obj.get("map").items()) + "map": dict((_k, Dict[str, Animal].from_dict(_v)) for _k, _v in obj.get("map").items()) if obj.get("map") is not None else None }) # store additional fields in additional_properties for _key in obj.keys(): diff --git a/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/parent_with_optional_dict.py b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/parent_with_optional_dict.py new file mode 100644 index 00000000000..3e9e08375b8 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen/petstore_api/models/parent_with_optional_dict.py @@ -0,0 +1,90 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import annotations +from inspect import getfullargspec +import pprint +import re # noqa: F401 +import json + + +from typing import Dict, Optional +from pydantic import BaseModel, Field +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty + +class ParentWithOptionalDict(BaseModel): + """ + ParentWithOptionalDict + """ + optional_dict: Optional[Dict[str, InnerDictWithProperty]] = Field(None, alias="optionalDict") + additional_properties: Dict[str, Any] = {} + __properties = ["optionalDict"] + + class Config: + allow_population_by_field_name = True + validate_assignment = True + + def to_str(self) -> str: + """Returns the string representation of the model using alias""" + return pprint.pformat(self.dict(by_alias=True)) + + def to_json(self) -> str: + """Returns the JSON representation of the model using alias""" + return json.dumps(self.to_dict()) + + @classmethod + def from_json(cls, json_str: str) -> ParentWithOptionalDict: + """Create an instance of ParentWithOptionalDict from a JSON string""" + return cls.from_dict(json.loads(json_str)) + + def to_dict(self): + """Returns the dictionary representation of the model using alias""" + _dict = self.dict(by_alias=True, + exclude={ + "additional_properties" + }, + exclude_none=True) + # override the default output from pydantic by calling `to_dict()` of each value in optional_dict (dict) + _field_dict = {} + if self.optional_dict: + for _key in self.optional_dict: + if self.optional_dict[_key]: + _field_dict[_key] = self.optional_dict[_key].to_dict() + _dict['optionalDict'] = _field_dict + # puts key-value pairs in additional_properties in the top level + if self.additional_properties is not None: + for _key, _value in self.additional_properties.items(): + _dict[_key] = _value + + return _dict + + @classmethod + def from_dict(cls, obj: dict) -> ParentWithOptionalDict: + """Create an instance of ParentWithOptionalDict from a dict""" + if obj is None: + return None + + if type(obj) is not dict: + return ParentWithOptionalDict.parse_obj(obj) + + _obj = ParentWithOptionalDict.parse_obj({ + "optional_dict": dict((_k, Dict[str, InnerDictWithProperty].from_dict(_v)) for _k, _v in obj.get("optionalDict").items()) if obj.get("optionalDict") is not None else None + }) + # store additional fields in additional_properties + for _key in obj.keys(): + if _key not in cls.__properties: + _obj.additional_properties[_key] = obj.get(_key) + + return _obj + diff --git a/samples/openapi3/client/petstore/python-nextgen/test/test_inner_dict_with_property.py b/samples/openapi3/client/petstore/python-nextgen/test/test_inner_dict_with_property.py new file mode 100644 index 00000000000..8a63b70d552 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen/test/test_inner_dict_with_property.py @@ -0,0 +1,56 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import absolute_import + +import unittest +import datetime + +import petstore_api +from petstore_api.models.inner_dict_with_property import InnerDictWithProperty # noqa: E501 +from petstore_api.rest import ApiException + +class TestInnerDictWithProperty(unittest.TestCase): + """InnerDictWithProperty unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional): + """Test InnerDictWithProperty + include_option is a boolean, when False only required + params are included, when True both required and + optional params are included """ + # uncomment below to create an instance of `InnerDictWithProperty` + """ + model = petstore_api.models.inner_dict_with_property.InnerDictWithProperty() # noqa: E501 + if include_optional : + return InnerDictWithProperty( + a_property = None + ) + else : + return InnerDictWithProperty( + ) + """ + + def testInnerDictWithProperty(self): + """Test InnerDictWithProperty""" + # inst_req_only = self.make_instance(include_optional=False) + # inst_req_and_optional = self.make_instance(include_optional=True) + +if __name__ == '__main__': + unittest.main() diff --git a/samples/openapi3/client/petstore/python-nextgen/test/test_parent_with_optional_dict.py b/samples/openapi3/client/petstore/python-nextgen/test/test_parent_with_optional_dict.py new file mode 100644 index 00000000000..25b769e3d39 --- /dev/null +++ b/samples/openapi3/client/petstore/python-nextgen/test/test_parent_with_optional_dict.py @@ -0,0 +1,59 @@ +# coding: utf-8 + +""" + OpenAPI Petstore + + This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" + + +from __future__ import absolute_import + +import unittest +import datetime + +import petstore_api +from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict # noqa: E501 +from petstore_api.rest import ApiException + +class TestParentWithOptionalDict(unittest.TestCase): + """ParentWithOptionalDict unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional): + """Test ParentWithOptionalDict + include_option is a boolean, when False only required + params are included, when True both required and + optional params are included """ + # uncomment below to create an instance of `ParentWithOptionalDict` + """ + model = petstore_api.models.parent_with_optional_dict.ParentWithOptionalDict() # noqa: E501 + if include_optional : + return ParentWithOptionalDict( + optional_dict = { + 'key' : petstore_api.models.inner_dict_with_property.InnerDictWithProperty( + a_property = petstore_api.models.a_property.aProperty(), ) + } + ) + else : + return ParentWithOptionalDict( + ) + """ + + def testParentWithOptionalDict(self): + """Test ParentWithOptionalDict""" + # inst_req_only = self.make_instance(include_optional=False) + # inst_req_and_optional = self.make_instance(include_optional=True) + +if __name__ == '__main__': + unittest.main() 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 3fbcbc493ac..29f332642b2 100644 --- a/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py +++ b/samples/openapi3/client/petstore/python-nextgen/tests/test_model.py @@ -387,3 +387,9 @@ class ModelTests(unittest.TestCase): enum_test = petstore_api.EnumTest(enum_string_required="lower") self.assertEqual(enum_test.enum_integer_default, 5) + def test_object_with_optional_dict(self): + # for https://github.com/OpenAPITools/openapi-generator/issues/14913 + # shouldn't throw exception by the optional dict property + a = petstore_api.ParentWithOptionalDict.from_dict({}) + self.assertFalse(a is None) +