From 8e6169e788f695810fa15f3bdd782111bbc13f9d Mon Sep 17 00:00:00 2001 From: Yousef Haggy Date: Sun, 8 Dec 2024 14:00:26 -0500 Subject: [PATCH 1/2] fix model_generic python templates to have valid imports for polymorphism --- .../main/resources/python-pydantic-v1/model_generic.mustache | 4 ++-- .../src/main/resources/python/model_generic.mustache | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/model_generic.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/model_generic.mustache index a9ba41b394a9..5422b0790d0f 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/model_generic.mustache @@ -17,7 +17,7 @@ from typing import TYPE_CHECKING from importlib import import_module if TYPE_CHECKING: {{#mappedModels}} - from {{packageName}}.models.{{model.classVarName}} import {{modelName}} + from {{packageName}}.models.{{model.classFilename}} import {{modelName}} {{/mappedModels}} {{/discriminator}} @@ -238,7 +238,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} object_type = cls.get_discriminator_value(obj) {{#mappedModels}} if object_type == '{{{modelName}}}': - return import_module("{{packageName}}.models.{{model.classVarName}}").{{modelName}}.from_dict(obj) + return import_module("{{packageName}}.models.{{model.classFilename}}").{{modelName}}.from_dict(obj) {{/mappedModels}} raise ValueError("{{{classname}}} failed to lookup discriminator value from " + json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name + diff --git a/modules/openapi-generator/src/main/resources/python/model_generic.mustache b/modules/openapi-generator/src/main/resources/python/model_generic.mustache index efd8e3041684..9fa35c84fab4 100644 --- a/modules/openapi-generator/src/main/resources/python/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/python/model_generic.mustache @@ -18,7 +18,7 @@ from typing_extensions import Self from typing import TYPE_CHECKING if TYPE_CHECKING: {{#mappedModels}} - from {{packageName}}.models.{{model.classVarName}} import {{modelName}} + from {{packageName}}.models.{{model.classFilename}} import {{modelName}} {{/mappedModels}} {{/discriminator}} @@ -261,7 +261,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}} object_type = cls.get_discriminator_value(obj) {{#mappedModels}} if object_type == '{{{modelName}}}': - return import_module("{{packageName}}.models.{{model.classVarName}}").{{modelName}}.from_dict(obj) + return import_module("{{packageName}}.models.{{model.classFilename}}").{{modelName}}.from_dict(obj) {{/mappedModels}} raise ValueError("{{{classname}}} failed to lookup discriminator value from " + From ab4f245c7177816b1e9b945822878040ea20fd56 Mon Sep 17 00:00:00 2001 From: yugi Date: Tue, 7 Jan 2025 09:30:43 -0500 Subject: [PATCH 2/2] update samples --- ...ith-fake-endpoints-models-for-testing.yaml | 7 ++ .../python-aiohttp/.openapi-generator/FILES | 3 + .../client/petstore/python-aiohttp/README.md | 1 + .../python-aiohttp/docs/HuntingDog.md | 29 +++++ .../python-aiohttp/petstore_api/__init__.py | 1 + .../petstore_api/models/__init__.py | 1 + .../petstore_api/models/animal.py | 9 +- .../petstore_api/models/hunting_dog.py | 90 +++++++++++++++ .../python-aiohttp/test/test_hunting_dog.py | 51 +++++++++ .../.openapi-generator/FILES | 3 + .../python-pydantic-v1-aiohttp/README.md | 1 + .../docs/HuntingDog.md | 28 +++++ .../petstore_api/__init__.py | 1 + .../petstore_api/models/__init__.py | 1 + .../petstore_api/models/animal.py | 10 +- .../petstore_api/models/hunting_dog.py | 74 +++++++++++++ .../test/test_hunting_dog.py | 52 +++++++++ .../.openapi-generator/FILES | 3 + .../petstore/python-pydantic-v1/README.md | 1 + .../python-pydantic-v1/docs/HuntingDog.md | 28 +++++ .../petstore_api/__init__.py | 1 + .../petstore_api/models/__init__.py | 1 + .../petstore_api/models/animal.py | 10 +- .../petstore_api/models/hunting_dog.py | 86 +++++++++++++++ .../test/test_hunting_dog.py | 52 +++++++++ .../petstore/python/.openapi-generator/FILES | 3 + .../openapi3/client/petstore/python/README.md | 1 + .../client/petstore/python/docs/HuntingDog.md | 29 +++++ .../petstore/python/petstore_api/__init__.py | 1 + .../python/petstore_api/models/__init__.py | 1 + .../python/petstore_api/models/animal.py | 9 +- .../python/petstore_api/models/hunting_dog.py | 103 ++++++++++++++++++ .../petstore/python/test/test_hunting_dog.py | 51 +++++++++ 33 files changed, 730 insertions(+), 12 deletions(-) create mode 100644 samples/openapi3/client/petstore/python-aiohttp/docs/HuntingDog.md create mode 100644 samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/hunting_dog.py create mode 100644 samples/openapi3/client/petstore/python-aiohttp/test/test_hunting_dog.py create mode 100644 samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/docs/HuntingDog.md create mode 100644 samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/hunting_dog.py create mode 100644 samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/test/test_hunting_dog.py create mode 100644 samples/openapi3/client/petstore/python-pydantic-v1/docs/HuntingDog.md create mode 100644 samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/hunting_dog.py create mode 100644 samples/openapi3/client/petstore/python-pydantic-v1/test/test_hunting_dog.py create mode 100644 samples/openapi3/client/petstore/python/docs/HuntingDog.md create mode 100644 samples/openapi3/client/petstore/python/petstore_api/models/hunting_dog.py create mode 100644 samples/openapi3/client/petstore/python/test/test_hunting_dog.py 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 c647aa74f4d8..4da4faef0e53 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 @@ -1957,6 +1957,13 @@ components: properties: breed: type: string + Hunting__Dog: + allOf: + - $ref: '#/components/schemas/Animal' + - type: object + properties: + isTrained: + type: boolean Cat: allOf: - $ref: '#/components/schemas/Animal' diff --git a/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES b/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES index 8558e1d2f58a..0b1874d97aa4 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES @@ -53,6 +53,7 @@ docs/FooGetDefaultResponse.md docs/FormatTest.md docs/HasOnlyReadOnly.md docs/HealthCheckResult.md +docs/HuntingDog.md docs/ImportTestDatetimeApi.md docs/Info.md docs/InnerDictWithProperty.md @@ -178,6 +179,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/hunting_dog.py petstore_api/models/info.py petstore_api/models/inner_dict_with_property.py petstore_api/models/input_all_of.py @@ -246,4 +248,5 @@ setup.cfg setup.py test-requirements.txt test/__init__.py +test/test_hunting_dog.py tox.ini diff --git a/samples/openapi3/client/petstore/python-aiohttp/README.md b/samples/openapi3/client/petstore/python-aiohttp/README.md index d84d9893c842..8c24df52be65 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/README.md +++ b/samples/openapi3/client/petstore/python-aiohttp/README.md @@ -197,6 +197,7 @@ Class | Method | HTTP request | Description - [FormatTest](docs/FormatTest.md) - [HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [HealthCheckResult](docs/HealthCheckResult.md) + - [HuntingDog](docs/HuntingDog.md) - [Info](docs/Info.md) - [InnerDictWithProperty](docs/InnerDictWithProperty.md) - [InputAllOf](docs/InputAllOf.md) diff --git a/samples/openapi3/client/petstore/python-aiohttp/docs/HuntingDog.md b/samples/openapi3/client/petstore/python-aiohttp/docs/HuntingDog.md new file mode 100644 index 000000000000..6db6745dd022 --- /dev/null +++ b/samples/openapi3/client/petstore/python-aiohttp/docs/HuntingDog.md @@ -0,0 +1,29 @@ +# HuntingDog + + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**is_trained** | **bool** | | [optional] + +## Example + +```python +from petstore_api.models.hunting_dog import HuntingDog + +# TODO update the JSON string below +json = "{}" +# create an instance of HuntingDog from a JSON string +hunting_dog_instance = HuntingDog.from_json(json) +# print the JSON string representation of the object +print(HuntingDog.to_json()) + +# convert the object into a dict +hunting_dog_dict = hunting_dog_instance.to_dict() +# create an instance of HuntingDog from a dict +hunting_dog_from_dict = HuntingDog.from_dict(hunting_dog_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-aiohttp/petstore_api/__init__.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py index 27cc58ac25b6..d7871f3e246c 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py @@ -85,6 +85,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py index 12531e2d9063..130555c82361 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py @@ -60,6 +60,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/animal.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/animal.py index 56bec73a9ccd..75629308544e 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/animal.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/animal.py @@ -27,6 +27,7 @@ if TYPE_CHECKING: from petstore_api.models.cat import Cat from petstore_api.models.dog import Dog + from petstore_api.models.hunting_dog import HuntingDog class Animal(BaseModel): """ @@ -48,7 +49,7 @@ class Animal(BaseModel): # discriminator mappings __discriminator_value_class_map: ClassVar[Dict[str, str]] = { - 'Cat': 'Cat','Dog': 'Dog' + 'Cat': 'Cat','Dog': 'Dog','Hunting__Dog': 'HuntingDog' } @classmethod @@ -70,7 +71,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Optional[Union[Cat, Dog]]: + def from_json(cls, json_str: str) -> Optional[Union[Cat, Dog, HuntingDog]]: """Create an instance of Animal from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -95,7 +96,7 @@ def to_dict(self) -> Dict[str, Any]: return _dict @classmethod - def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog]]: + def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog, HuntingDog]]: """Create an instance of Animal from a dict""" # look up the object type based on discriminator mapping object_type = cls.get_discriminator_value(obj) @@ -103,6 +104,8 @@ def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog]]: return import_module("petstore_api.models.cat").Cat.from_dict(obj) if object_type == 'Dog': return import_module("petstore_api.models.dog").Dog.from_dict(obj) + if object_type == 'HuntingDog': + return import_module("petstore_api.models.hunting_dog").HuntingDog.from_dict(obj) raise ValueError("Animal failed to lookup discriminator value from " + json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name + diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/hunting_dog.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/hunting_dog.py new file mode 100644 index 000000000000..f0f8036977ec --- /dev/null +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/hunting_dog.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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +from __future__ import annotations +import pprint +import re # noqa: F401 +import json + +from pydantic import ConfigDict, Field, StrictBool +from typing import Any, ClassVar, Dict, List, Optional +from petstore_api.models.animal import Animal +from typing import Optional, Set +from typing_extensions import Self + +class HuntingDog(Animal): + """ + HuntingDog + """ # noqa: E501 + is_trained: Optional[StrictBool] = Field(default=None, alias="isTrained") + __properties: ClassVar[List[str]] = ["className", "color", "isTrained"] + + model_config = ConfigDict( + populate_by_name=True, + validate_assignment=True, + protected_namespaces=(), + ) + + + def to_str(self) -> str: + """Returns the string representation of the model using alias""" + return pprint.pformat(self.model_dump(by_alias=True)) + + def to_json(self) -> str: + """Returns the JSON representation of the model using alias""" + # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead + return json.dumps(self.to_dict()) + + @classmethod + def from_json(cls, json_str: str) -> Optional[Self]: + """Create an instance of HuntingDog from a JSON string""" + return cls.from_dict(json.loads(json_str)) + + def to_dict(self) -> Dict[str, Any]: + """Return the dictionary representation of the model using alias. + + This has the following differences from calling pydantic's + `self.model_dump(by_alias=True)`: + + * `None` is only added to the output dict for nullable fields that + were set at model initialization. Other fields with value `None` + are ignored. + """ + excluded_fields: Set[str] = set([ + ]) + + _dict = self.model_dump( + by_alias=True, + exclude=excluded_fields, + exclude_none=True, + ) + return _dict + + @classmethod + def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]: + """Create an instance of HuntingDog from a dict""" + if obj is None: + return None + + if not isinstance(obj, dict): + return cls.model_validate(obj) + + _obj = cls.model_validate({ + "className": obj.get("className"), + "color": obj.get("color") if obj.get("color") is not None else 'red', + "isTrained": obj.get("isTrained") + }) + return _obj + + diff --git a/samples/openapi3/client/petstore/python-aiohttp/test/test_hunting_dog.py b/samples/openapi3/client/petstore/python-aiohttp/test/test_hunting_dog.py new file mode 100644 index 000000000000..8d9c106a8721 --- /dev/null +++ b/samples/openapi3/client/petstore/python-aiohttp/test/test_hunting_dog.py @@ -0,0 +1,51 @@ +# 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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +import unittest + +from petstore_api.models.hunting_dog import HuntingDog + +class TestHuntingDog(unittest.TestCase): + """HuntingDog unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional) -> HuntingDog: + """Test HuntingDog + include_optional 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 `HuntingDog` + """ + model = HuntingDog() + if include_optional: + return HuntingDog( + is_trained = True + ) + else: + return HuntingDog( + ) + """ + + def testHuntingDog(self): + """Test HuntingDog""" + # 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-pydantic-v1-aiohttp/.openapi-generator/FILES b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/.openapi-generator/FILES index cafa99c6a715..353fc4ce1797 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/.openapi-generator/FILES @@ -55,6 +55,7 @@ docs/FooGetDefaultResponse.md docs/FormatTest.md docs/HasOnlyReadOnly.md docs/HealthCheckResult.md +docs/HuntingDog.md docs/ImportTestDatetimeApi.md docs/Info.md docs/InnerDictWithProperty.md @@ -180,6 +181,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/hunting_dog.py petstore_api/models/info.py petstore_api/models/inner_dict_with_property.py petstore_api/models/input_all_of.py @@ -246,4 +248,5 @@ setup.cfg setup.py test-requirements.txt test/__init__.py +test/test_hunting_dog.py tox.ini diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/README.md b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/README.md index 7eb5c872b646..66bdc445526c 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/README.md +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/README.md @@ -200,6 +200,7 @@ Class | Method | HTTP request | Description - [FormatTest](docs/FormatTest.md) - [HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [HealthCheckResult](docs/HealthCheckResult.md) + - [HuntingDog](docs/HuntingDog.md) - [Info](docs/Info.md) - [InnerDictWithProperty](docs/InnerDictWithProperty.md) - [InputAllOf](docs/InputAllOf.md) diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/docs/HuntingDog.md b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/docs/HuntingDog.md new file mode 100644 index 000000000000..de912dce0fb5 --- /dev/null +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/docs/HuntingDog.md @@ -0,0 +1,28 @@ +# HuntingDog + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**is_trained** | **bool** | | [optional] + +## Example + +```python +from petstore_api.models.hunting_dog import HuntingDog + +# TODO update the JSON string below +json = "{}" +# create an instance of HuntingDog from a JSON string +hunting_dog_instance = HuntingDog.from_json(json) +# print the JSON string representation of the object +print HuntingDog.to_json() + +# convert the object into a dict +hunting_dog_dict = hunting_dog_instance.to_dict() +# create an instance of HuntingDog from a dict +hunting_dog_from_dict = HuntingDog.from_dict(hunting_dog_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-pydantic-v1-aiohttp/petstore_api/__init__.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/__init__.py index f178ffe52f18..330c529ec6f4 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/__init__.py @@ -87,6 +87,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/__init__.py index b6a5d674a51c..1a3ad2e2949d 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/__init__.py @@ -62,6 +62,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/animal.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/animal.py index d299cee14f7f..e7b1a5a7a94a 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/animal.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/animal.py @@ -26,6 +26,7 @@ if TYPE_CHECKING: from petstore_api.models.cat import Cat from petstore_api.models.dog import Dog + from petstore_api.models.hunting_dog import HuntingDog class Animal(BaseModel): """ @@ -46,7 +47,8 @@ class Config: # discriminator mappings __discriminator_value_class_map = { 'Cat': 'Cat', - 'Dog': 'Dog' + 'Dog': 'Dog', + 'Hunting__Dog': 'HuntingDog' } @classmethod @@ -67,7 +69,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Union(Cat, Dog): + def from_json(cls, json_str: str) -> Union(Cat, Dog, HuntingDog): """Create an instance of Animal from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -80,7 +82,7 @@ def to_dict(self): return _dict @classmethod - def from_dict(cls, obj: dict) -> Union(Cat, Dog): + def from_dict(cls, obj: dict) -> Union(Cat, Dog, HuntingDog): """Create an instance of Animal from a dict""" # look up the object type based on discriminator mapping object_type = cls.get_discriminator_value(obj) @@ -88,6 +90,8 @@ def from_dict(cls, obj: dict) -> Union(Cat, Dog): return import_module("petstore_api.models.cat").Cat.from_dict(obj) if object_type == 'Dog': return import_module("petstore_api.models.dog").Dog.from_dict(obj) + if object_type == 'HuntingDog': + return import_module("petstore_api.models.hunting_dog").HuntingDog.from_dict(obj) raise ValueError("Animal failed to lookup discriminator value from " + json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name + ", mapping: " + json.dumps(cls.__discriminator_value_class_map)) diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/hunting_dog.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/hunting_dog.py new file mode 100644 index 000000000000..28e5d4ca6b93 --- /dev/null +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/models/hunting_dog.py @@ -0,0 +1,74 @@ +# 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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +from __future__ import annotations +import pprint +import re # noqa: F401 +import json + + +from typing import Optional +from pydantic import Field, StrictBool +from petstore_api.models.animal import Animal + +class HuntingDog(Animal): + """ + HuntingDog + """ + is_trained: Optional[StrictBool] = Field(default=None, alias="isTrained") + __properties = ["className", "color", "isTrained"] + + class Config: + """Pydantic configuration""" + 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) -> HuntingDog: + """Create an instance of HuntingDog 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) -> HuntingDog: + """Create an instance of HuntingDog from a dict""" + if obj is None: + return None + + if not isinstance(obj, dict): + return HuntingDog.parse_obj(obj) + + _obj = HuntingDog.parse_obj({ + "class_name": obj.get("className"), + "color": obj.get("color") if obj.get("color") is not None else 'red', + "is_trained": obj.get("isTrained") + }) + return _obj + + diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/test/test_hunting_dog.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/test/test_hunting_dog.py new file mode 100644 index 000000000000..e64da05c0f33 --- /dev/null +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/test/test_hunting_dog.py @@ -0,0 +1,52 @@ +# 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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +import unittest +import datetime + +from petstore_api.models.hunting_dog import HuntingDog # noqa: E501 + +class TestHuntingDog(unittest.TestCase): + """HuntingDog unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional) -> HuntingDog: + """Test HuntingDog + 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 `HuntingDog` + """ + model = HuntingDog() # noqa: E501 + if include_optional: + return HuntingDog( + is_trained = True + ) + else: + return HuntingDog( + ) + """ + + def testHuntingDog(self): + """Test HuntingDog""" + # 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-pydantic-v1/.openapi-generator/FILES b/samples/openapi3/client/petstore/python-pydantic-v1/.openapi-generator/FILES index cafa99c6a715..353fc4ce1797 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python-pydantic-v1/.openapi-generator/FILES @@ -55,6 +55,7 @@ docs/FooGetDefaultResponse.md docs/FormatTest.md docs/HasOnlyReadOnly.md docs/HealthCheckResult.md +docs/HuntingDog.md docs/ImportTestDatetimeApi.md docs/Info.md docs/InnerDictWithProperty.md @@ -180,6 +181,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/hunting_dog.py petstore_api/models/info.py petstore_api/models/inner_dict_with_property.py petstore_api/models/input_all_of.py @@ -246,4 +248,5 @@ setup.cfg setup.py test-requirements.txt test/__init__.py +test/test_hunting_dog.py tox.ini diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/README.md b/samples/openapi3/client/petstore/python-pydantic-v1/README.md index 20f9ebad95dc..b95f0db56260 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/README.md +++ b/samples/openapi3/client/petstore/python-pydantic-v1/README.md @@ -200,6 +200,7 @@ Class | Method | HTTP request | Description - [FormatTest](docs/FormatTest.md) - [HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [HealthCheckResult](docs/HealthCheckResult.md) + - [HuntingDog](docs/HuntingDog.md) - [Info](docs/Info.md) - [InnerDictWithProperty](docs/InnerDictWithProperty.md) - [InputAllOf](docs/InputAllOf.md) diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/docs/HuntingDog.md b/samples/openapi3/client/petstore/python-pydantic-v1/docs/HuntingDog.md new file mode 100644 index 000000000000..de912dce0fb5 --- /dev/null +++ b/samples/openapi3/client/petstore/python-pydantic-v1/docs/HuntingDog.md @@ -0,0 +1,28 @@ +# HuntingDog + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**is_trained** | **bool** | | [optional] + +## Example + +```python +from petstore_api.models.hunting_dog import HuntingDog + +# TODO update the JSON string below +json = "{}" +# create an instance of HuntingDog from a JSON string +hunting_dog_instance = HuntingDog.from_json(json) +# print the JSON string representation of the object +print HuntingDog.to_json() + +# convert the object into a dict +hunting_dog_dict = hunting_dog_instance.to_dict() +# create an instance of HuntingDog from a dict +hunting_dog_from_dict = HuntingDog.from_dict(hunting_dog_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-pydantic-v1/petstore_api/__init__.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/__init__.py index f178ffe52f18..330c529ec6f4 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/__init__.py @@ -87,6 +87,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/__init__.py index b6a5d674a51c..1a3ad2e2949d 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/__init__.py @@ -62,6 +62,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/animal.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/animal.py index 652e2e9a1b51..5d2ab014fac5 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/animal.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/animal.py @@ -26,6 +26,7 @@ if TYPE_CHECKING: from petstore_api.models.cat import Cat from petstore_api.models.dog import Dog + from petstore_api.models.hunting_dog import HuntingDog class Animal(BaseModel): """ @@ -47,7 +48,8 @@ class Config: # discriminator mappings __discriminator_value_class_map = { 'Cat': 'Cat', - 'Dog': 'Dog' + 'Dog': 'Dog', + 'Hunting__Dog': 'HuntingDog' } @classmethod @@ -68,7 +70,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Union(Cat, Dog): + def from_json(cls, json_str: str) -> Union(Cat, Dog, HuntingDog): """Create an instance of Animal from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -87,7 +89,7 @@ def to_dict(self): return _dict @classmethod - def from_dict(cls, obj: dict) -> Union(Cat, Dog): + def from_dict(cls, obj: dict) -> Union(Cat, Dog, HuntingDog): """Create an instance of Animal from a dict""" # look up the object type based on discriminator mapping object_type = cls.get_discriminator_value(obj) @@ -95,6 +97,8 @@ def from_dict(cls, obj: dict) -> Union(Cat, Dog): return import_module("petstore_api.models.cat").Cat.from_dict(obj) if object_type == 'Dog': return import_module("petstore_api.models.dog").Dog.from_dict(obj) + if object_type == 'HuntingDog': + return import_module("petstore_api.models.hunting_dog").HuntingDog.from_dict(obj) raise ValueError("Animal failed to lookup discriminator value from " + json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name + ", mapping: " + json.dumps(cls.__discriminator_value_class_map)) diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/hunting_dog.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/hunting_dog.py new file mode 100644 index 000000000000..6a38e9be278f --- /dev/null +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/models/hunting_dog.py @@ -0,0 +1,86 @@ +# 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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +from __future__ import annotations +import pprint +import re # noqa: F401 +import json + + +from typing import Any, Dict, Optional +from pydantic import Field, StrictBool +from petstore_api.models.animal import Animal + +class HuntingDog(Animal): + """ + HuntingDog + """ + is_trained: Optional[StrictBool] = Field(default=None, alias="isTrained") + additional_properties: Dict[str, Any] = {} + __properties = ["className", "color", "isTrained"] + + class Config: + """Pydantic configuration""" + 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) -> HuntingDog: + """Create an instance of HuntingDog 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) -> HuntingDog: + """Create an instance of HuntingDog from a dict""" + if obj is None: + return None + + if not isinstance(obj, dict): + return HuntingDog.parse_obj(obj) + + _obj = HuntingDog.parse_obj({ + "class_name": obj.get("className"), + "color": obj.get("color") if obj.get("color") is not None else 'red', + "is_trained": obj.get("isTrained") + }) + # 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-pydantic-v1/test/test_hunting_dog.py b/samples/openapi3/client/petstore/python-pydantic-v1/test/test_hunting_dog.py new file mode 100644 index 000000000000..e64da05c0f33 --- /dev/null +++ b/samples/openapi3/client/petstore/python-pydantic-v1/test/test_hunting_dog.py @@ -0,0 +1,52 @@ +# 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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +import unittest +import datetime + +from petstore_api.models.hunting_dog import HuntingDog # noqa: E501 + +class TestHuntingDog(unittest.TestCase): + """HuntingDog unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional) -> HuntingDog: + """Test HuntingDog + 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 `HuntingDog` + """ + model = HuntingDog() # noqa: E501 + if include_optional: + return HuntingDog( + is_trained = True + ) + else: + return HuntingDog( + ) + """ + + def testHuntingDog(self): + """Test HuntingDog""" + # 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/.openapi-generator/FILES b/samples/openapi3/client/petstore/python/.openapi-generator/FILES index 8558e1d2f58a..0b1874d97aa4 100755 --- a/samples/openapi3/client/petstore/python/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python/.openapi-generator/FILES @@ -53,6 +53,7 @@ docs/FooGetDefaultResponse.md docs/FormatTest.md docs/HasOnlyReadOnly.md docs/HealthCheckResult.md +docs/HuntingDog.md docs/ImportTestDatetimeApi.md docs/Info.md docs/InnerDictWithProperty.md @@ -178,6 +179,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/hunting_dog.py petstore_api/models/info.py petstore_api/models/inner_dict_with_property.py petstore_api/models/input_all_of.py @@ -246,4 +248,5 @@ setup.cfg setup.py test-requirements.txt test/__init__.py +test/test_hunting_dog.py tox.ini diff --git a/samples/openapi3/client/petstore/python/README.md b/samples/openapi3/client/petstore/python/README.md index 9e37f108ad97..9baddaa7a072 100755 --- a/samples/openapi3/client/petstore/python/README.md +++ b/samples/openapi3/client/petstore/python/README.md @@ -197,6 +197,7 @@ Class | Method | HTTP request | Description - [FormatTest](docs/FormatTest.md) - [HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [HealthCheckResult](docs/HealthCheckResult.md) + - [HuntingDog](docs/HuntingDog.md) - [Info](docs/Info.md) - [InnerDictWithProperty](docs/InnerDictWithProperty.md) - [InputAllOf](docs/InputAllOf.md) diff --git a/samples/openapi3/client/petstore/python/docs/HuntingDog.md b/samples/openapi3/client/petstore/python/docs/HuntingDog.md new file mode 100644 index 000000000000..6db6745dd022 --- /dev/null +++ b/samples/openapi3/client/petstore/python/docs/HuntingDog.md @@ -0,0 +1,29 @@ +# HuntingDog + + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**is_trained** | **bool** | | [optional] + +## Example + +```python +from petstore_api.models.hunting_dog import HuntingDog + +# TODO update the JSON string below +json = "{}" +# create an instance of HuntingDog from a JSON string +hunting_dog_instance = HuntingDog.from_json(json) +# print the JSON string representation of the object +print(HuntingDog.to_json()) + +# convert the object into a dict +hunting_dog_dict = hunting_dog_instance.to_dict() +# create an instance of HuntingDog from a dict +hunting_dog_from_dict = HuntingDog.from_dict(hunting_dog_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/petstore_api/__init__.py b/samples/openapi3/client/petstore/python/petstore_api/__init__.py index 27cc58ac25b6..d7871f3e246c 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python/petstore_api/__init__.py @@ -85,6 +85,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py index 12531e2d9063..130555c82361 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py @@ -60,6 +60,7 @@ 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.hunting_dog import HuntingDog from petstore_api.models.info import Info from petstore_api.models.inner_dict_with_property import InnerDictWithProperty from petstore_api.models.input_all_of import InputAllOf diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/animal.py b/samples/openapi3/client/petstore/python/petstore_api/models/animal.py index 131ad8d0ed60..c43dca9bba26 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/animal.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/animal.py @@ -27,6 +27,7 @@ if TYPE_CHECKING: from petstore_api.models.cat import Cat from petstore_api.models.dog import Dog + from petstore_api.models.hunting_dog import HuntingDog class Animal(BaseModel): """ @@ -49,7 +50,7 @@ class Animal(BaseModel): # discriminator mappings __discriminator_value_class_map: ClassVar[Dict[str, str]] = { - 'Cat': 'Cat','Dog': 'Dog' + 'Cat': 'Cat','Dog': 'Dog','Hunting__Dog': 'HuntingDog' } @classmethod @@ -71,7 +72,7 @@ def to_json(self) -> str: return json.dumps(self.to_dict()) @classmethod - def from_json(cls, json_str: str) -> Optional[Union[Cat, Dog]]: + def from_json(cls, json_str: str) -> Optional[Union[Cat, Dog, HuntingDog]]: """Create an instance of Animal from a JSON string""" return cls.from_dict(json.loads(json_str)) @@ -103,7 +104,7 @@ def to_dict(self) -> Dict[str, Any]: return _dict @classmethod - def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog]]: + def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog, HuntingDog]]: """Create an instance of Animal from a dict""" # look up the object type based on discriminator mapping object_type = cls.get_discriminator_value(obj) @@ -111,6 +112,8 @@ def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog]]: return import_module("petstore_api.models.cat").Cat.from_dict(obj) if object_type == 'Dog': return import_module("petstore_api.models.dog").Dog.from_dict(obj) + if object_type == 'HuntingDog': + return import_module("petstore_api.models.hunting_dog").HuntingDog.from_dict(obj) raise ValueError("Animal failed to lookup discriminator value from " + json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name + diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/hunting_dog.py b/samples/openapi3/client/petstore/python/petstore_api/models/hunting_dog.py new file mode 100644 index 000000000000..c0463dc20bc0 --- /dev/null +++ b/samples/openapi3/client/petstore/python/petstore_api/models/hunting_dog.py @@ -0,0 +1,103 @@ +# 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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +from __future__ import annotations +import pprint +import re # noqa: F401 +import json + +from pydantic import ConfigDict, Field, StrictBool +from typing import Any, ClassVar, Dict, List, Optional +from petstore_api.models.animal import Animal +from typing import Optional, Set +from typing_extensions import Self + +class HuntingDog(Animal): + """ + HuntingDog + """ # noqa: E501 + is_trained: Optional[StrictBool] = Field(default=None, alias="isTrained") + additional_properties: Dict[str, Any] = {} + __properties: ClassVar[List[str]] = ["className", "color", "isTrained"] + + model_config = ConfigDict( + populate_by_name=True, + validate_assignment=True, + protected_namespaces=(), + ) + + + def to_str(self) -> str: + """Returns the string representation of the model using alias""" + return pprint.pformat(self.model_dump(by_alias=True)) + + def to_json(self) -> str: + """Returns the JSON representation of the model using alias""" + # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead + return json.dumps(self.to_dict()) + + @classmethod + def from_json(cls, json_str: str) -> Optional[Self]: + """Create an instance of HuntingDog from a JSON string""" + return cls.from_dict(json.loads(json_str)) + + def to_dict(self) -> Dict[str, Any]: + """Return the dictionary representation of the model using alias. + + This has the following differences from calling pydantic's + `self.model_dump(by_alias=True)`: + + * `None` is only added to the output dict for nullable fields that + were set at model initialization. Other fields with value `None` + are ignored. + * Fields in `self.additional_properties` are added to the output dict. + """ + excluded_fields: Set[str] = set([ + "additional_properties", + ]) + + _dict = self.model_dump( + by_alias=True, + exclude=excluded_fields, + 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: Optional[Dict[str, Any]]) -> Optional[Self]: + """Create an instance of HuntingDog from a dict""" + if obj is None: + return None + + if not isinstance(obj, dict): + return cls.model_validate(obj) + + _obj = cls.model_validate({ + "className": obj.get("className"), + "color": obj.get("color") if obj.get("color") is not None else 'red', + "isTrained": obj.get("isTrained") + }) + # 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/test/test_hunting_dog.py b/samples/openapi3/client/petstore/python/test/test_hunting_dog.py new file mode 100644 index 000000000000..8d9c106a8721 --- /dev/null +++ b/samples/openapi3/client/petstore/python/test/test_hunting_dog.py @@ -0,0 +1,51 @@ +# 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: \" \\ + + The version of the OpenAPI document: 1.0.0 + Generated by OpenAPI Generator (https://openapi-generator.tech) + + Do not edit the class manually. +""" # noqa: E501 + + +import unittest + +from petstore_api.models.hunting_dog import HuntingDog + +class TestHuntingDog(unittest.TestCase): + """HuntingDog unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional) -> HuntingDog: + """Test HuntingDog + include_optional 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 `HuntingDog` + """ + model = HuntingDog() + if include_optional: + return HuntingDog( + is_trained = True + ) + else: + return HuntingDog( + ) + """ + + def testHuntingDog(self): + """Test HuntingDog""" + # inst_req_only = self.make_instance(include_optional=False) + # inst_req_and_optional = self.make_instance(include_optional=True) + +if __name__ == '__main__': + unittest.main()