forked from loafle/openapi-generator-original
[python] Fix Circular imports on inherited discriminators. (#17886)
* [python] add inheritance discriminators test #16808 * [python] update samples * [python] fix assert in test * [python] fix inheritance discriminators circular import * [python] update samples * [python] undo type changes related to discriminator * [python] remove extraneous processing * [python-pydantic-v1] fix inheritance discriminators circular import * [python] remove type ignore comment * [python] update samples * [python] fix avoid the empty line break * [python] update samples
This commit is contained in:
parent
bf8ee80001
commit
ad08581f16
@ -927,10 +927,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
|
||||
// if super class
|
||||
if (model.getDiscriminator() != null && model.getDiscriminator().getMappedModels() != null) {
|
||||
moduleImports.add("typing", "Union");
|
||||
Set<CodegenDiscriminator.MappedModel> discriminator = model.getDiscriminator().getMappedModels();
|
||||
for (CodegenDiscriminator.MappedModel mappedModel : discriminator) {
|
||||
postponedModelImports.add(mappedModel.getModelName());
|
||||
}
|
||||
moduleImports.add("importlib", "import_module");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -879,10 +879,6 @@ public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen imp
|
||||
// if super class
|
||||
if (model.getDiscriminator() != null && model.getDiscriminator().getMappedModels() != null) {
|
||||
typingImports.add("Union");
|
||||
Set<CodegenDiscriminator.MappedModel> discriminator = model.getDiscriminator().getMappedModels();
|
||||
for (CodegenDiscriminator.MappedModel mappedModel : discriminator) {
|
||||
postponedModelImports.add(mappedModel.getModelName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,18 @@ import json
|
||||
{{{.}}}
|
||||
{{/vendorExtensions.x-py-model-imports}}
|
||||
|
||||
{{#hasChildren}}
|
||||
{{#discriminator}}
|
||||
{{! If this model is a super class, importlib is used. So import the necessary modules for the type here. }}
|
||||
from typing import TYPE_CHECKING
|
||||
from importlib import import_module
|
||||
if TYPE_CHECKING:
|
||||
{{#mappedModels}}
|
||||
from {{packageName}}.models.{{model.classVarName}} import {{modelName}}
|
||||
{{/mappedModels}}
|
||||
|
||||
{{/discriminator}}
|
||||
{{/hasChildren}}
|
||||
class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}):
|
||||
"""
|
||||
{{#description}}{{{description}}} # noqa: E501{{/description}}{{^description}}{{{classname}}}{{/description}}
|
||||
@ -222,13 +234,13 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
|
||||
{{#discriminator}}
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type:
|
||||
klass = globals()[object_type]
|
||||
return klass.from_dict(obj)
|
||||
else:
|
||||
raise ValueError("{{{classname}}} failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
{{#mappedModels}}
|
||||
if object_type == '{{{mappingName}}}':
|
||||
return import_module("{{packageName}}.models.{{model.classVarName}}").{{modelName}}.from_dict(obj)
|
||||
{{/mappedModels}}
|
||||
raise ValueError("{{{classname}}} failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
{{/discriminator}}
|
||||
{{/hasChildren}}
|
||||
{{^hasChildren}}
|
||||
|
@ -12,6 +12,17 @@ import json
|
||||
from typing import Optional, Set
|
||||
from typing_extensions import Self
|
||||
|
||||
{{#hasChildren}}
|
||||
{{#discriminator}}
|
||||
{{! If this model is a super class, importlib is used. So import the necessary modules for the type here. }}
|
||||
from typing import TYPE_CHECKING
|
||||
if TYPE_CHECKING:
|
||||
{{#mappedModels}}
|
||||
from {{packageName}}.models.{{model.classVarName}} import {{modelName}}
|
||||
{{/mappedModels}}
|
||||
|
||||
{{/discriminator}}
|
||||
{{/hasChildren}}
|
||||
class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}):
|
||||
"""
|
||||
{{#description}}{{{description}}}{{/description}}{{^description}}{{{classname}}}{{/description}}
|
||||
@ -113,7 +124,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Optional[{{^hasChildren}}Self{{/hasChildren}}{{#hasChildren}}{{#discriminator}}Union[{{#children}}Self{{^-last}}, {{/-last}}{{/children}}]{{/discriminator}}{{^discriminator}}Self{{/discriminator}}{{/hasChildren}}]:
|
||||
def from_json(cls, json_str: str) -> Optional[{{^hasChildren}}Self{{/hasChildren}}{{#hasChildren}}{{#discriminator}}Union[{{#mappedModels}}{{{modelName}}}{{^-last}}, {{/-last}}{{/mappedModels}}]{{/discriminator}}{{^discriminator}}Self{{/discriminator}}{{/hasChildren}}]:
|
||||
"""Create an instance of {{{classname}}} from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@ -236,18 +247,19 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
|
||||
|
||||
{{#hasChildren}}
|
||||
@classmethod
|
||||
def from_dict(cls, obj: Dict[str, Any]) -> Optional[{{#discriminator}}Union[{{#children}}Self{{^-last}}, {{/-last}}{{/children}}]{{/discriminator}}{{^discriminator}}Self{{/discriminator}}]:
|
||||
def from_dict(cls, obj: Dict[str, Any]) -> Optional[{{#discriminator}}Union[{{#mappedModels}}{{{modelName}}}{{^-last}}, {{/-last}}{{/mappedModels}}]{{/discriminator}}{{^discriminator}}Self{{/discriminator}}]:
|
||||
"""Create an instance of {{{classname}}} from a dict"""
|
||||
{{#discriminator}}
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type:
|
||||
klass = globals()[object_type]
|
||||
return klass.from_dict(obj)
|
||||
else:
|
||||
raise ValueError("{{{classname}}} failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
{{#mappedModels}}
|
||||
if object_type == '{{{mappingName}}}':
|
||||
return import_module("{{packageName}}.models.{{model.classVarName}}").{{modelName}}.from_dict(obj)
|
||||
{{/mappedModels}}
|
||||
|
||||
raise ValueError("{{{classname}}} failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
{{/discriminator}}
|
||||
{{/hasChildren}}
|
||||
{{^hasChildren}}
|
||||
|
@ -1640,6 +1640,20 @@ components:
|
||||
required:
|
||||
- id
|
||||
- activity
|
||||
DiscriminatorAllOfSuper:
|
||||
type: object
|
||||
required:
|
||||
- elementType
|
||||
discriminator:
|
||||
propertyName: elementType
|
||||
properties:
|
||||
elementType:
|
||||
type: string
|
||||
DiscriminatorAllOfSub:
|
||||
allOf:
|
||||
- $ref: '#/components/schemas/DiscriminatorAllOfSuper'
|
||||
- type: object
|
||||
properties: {}
|
||||
Pet:
|
||||
type: object
|
||||
required:
|
||||
|
@ -30,6 +30,8 @@ docs/CreatureInfo.md
|
||||
docs/DanishPig.md
|
||||
docs/DefaultApi.md
|
||||
docs/DeprecatedObject.md
|
||||
docs/DiscriminatorAllOfSub.md
|
||||
docs/DiscriminatorAllOfSuper.md
|
||||
docs/Dog.md
|
||||
docs/DummyModel.md
|
||||
docs/EnumArrays.md
|
||||
@ -140,6 +142,8 @@ petstore_api/models/creature.py
|
||||
petstore_api/models/creature_info.py
|
||||
petstore_api/models/danish_pig.py
|
||||
petstore_api/models/deprecated_object.py
|
||||
petstore_api/models/discriminator_all_of_sub.py
|
||||
petstore_api/models/discriminator_all_of_super.py
|
||||
petstore_api/models/dog.py
|
||||
petstore_api/models/dummy_model.py
|
||||
petstore_api/models/enum_arrays.py
|
||||
|
@ -164,6 +164,8 @@ Class | Method | HTTP request | Description
|
||||
- [CreatureInfo](docs/CreatureInfo.md)
|
||||
- [DanishPig](docs/DanishPig.md)
|
||||
- [DeprecatedObject](docs/DeprecatedObject.md)
|
||||
- [DiscriminatorAllOfSub](docs/DiscriminatorAllOfSub.md)
|
||||
- [DiscriminatorAllOfSuper](docs/DiscriminatorAllOfSuper.md)
|
||||
- [Dog](docs/Dog.md)
|
||||
- [DummyModel](docs/DummyModel.md)
|
||||
- [EnumArrays](docs/EnumArrays.md)
|
||||
|
@ -0,0 +1,28 @@
|
||||
# DiscriminatorAllOfSub
|
||||
|
||||
|
||||
## Properties
|
||||
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSub from a JSON string
|
||||
discriminator_all_of_sub_instance = DiscriminatorAllOfSub.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSub.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_sub_dict = discriminator_all_of_sub_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSub from a dict
|
||||
discriminator_all_of_sub_form_dict = discriminator_all_of_sub.from_dict(discriminator_all_of_sub_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)
|
||||
|
||||
|
@ -0,0 +1,29 @@
|
||||
# DiscriminatorAllOfSuper
|
||||
|
||||
|
||||
## Properties
|
||||
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**element_type** | **str** | |
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSuper from a JSON string
|
||||
discriminator_all_of_super_instance = DiscriminatorAllOfSuper.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSuper.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_super_dict = discriminator_all_of_super_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSuper from a dict
|
||||
discriminator_all_of_super_form_dict = discriminator_all_of_super.from_dict(discriminator_all_of_super_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)
|
||||
|
||||
|
@ -63,6 +63,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -39,6 +39,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -17,11 +17,17 @@ import pprint
|
||||
import re # noqa: F401
|
||||
import json
|
||||
|
||||
from importlib import import_module
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
from typing import Any, ClassVar, Dict, List, Optional, Union
|
||||
from typing import Optional, Set
|
||||
from typing_extensions import Self
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
|
||||
class Animal(BaseModel):
|
||||
"""
|
||||
Animal
|
||||
@ -64,7 +70,7 @@ class Animal(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Optional[Union[Self, Self]]:
|
||||
def from_json(cls, json_str: str) -> Optional[Union[Cat, Dog]]:
|
||||
"""Create an instance of Animal from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@ -89,20 +95,17 @@ class Animal(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Self, Self]]:
|
||||
def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog]]:
|
||||
"""Create an instance of Animal from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type:
|
||||
klass = globals()[object_type]
|
||||
return klass.from_dict(obj)
|
||||
else:
|
||||
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))
|
||||
if object_type == 'Cat':
|
||||
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)
|
||||
|
||||
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))
|
||||
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
# TODO: Rewrite to not use raise_errors
|
||||
Animal.model_rebuild(raise_errors=False)
|
||||
|
||||
|
@ -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, ClassVar, Dict, List
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from typing import Optional, Set
|
||||
from typing_extensions import Self
|
||||
|
||||
class DiscriminatorAllOfSub(DiscriminatorAllOfSuper):
|
||||
"""
|
||||
DiscriminatorAllOfSub
|
||||
""" # noqa: E501
|
||||
__properties: ClassVar[List[str]] = ["elementType"]
|
||||
|
||||
model_config = {
|
||||
"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 DiscriminatorAllOfSub 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 DiscriminatorAllOfSub from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = cls.model_validate({
|
||||
"elementType": obj.get("elementType")
|
||||
})
|
||||
return _obj
|
||||
|
||||
|
@ -0,0 +1,107 @@
|
||||
# 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 importlib import import_module
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
from typing import Any, ClassVar, Dict, List, Union
|
||||
from typing import Optional, Set
|
||||
from typing_extensions import Self
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
class DiscriminatorAllOfSuper(BaseModel):
|
||||
"""
|
||||
DiscriminatorAllOfSuper
|
||||
""" # noqa: E501
|
||||
element_type: StrictStr = Field(alias="elementType")
|
||||
__properties: ClassVar[List[str]] = ["elementType"]
|
||||
|
||||
model_config = {
|
||||
"populate_by_name": True,
|
||||
"validate_assignment": True,
|
||||
"protected_namespaces": (),
|
||||
}
|
||||
|
||||
|
||||
# JSON field name that stores the object type
|
||||
__discriminator_property_name: ClassVar[str] = 'elementType'
|
||||
|
||||
# discriminator mappings
|
||||
__discriminator_value_class_map: ClassVar[Dict[str, str]] = {
|
||||
'DiscriminatorAllOfSub': 'DiscriminatorAllOfSub'
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_discriminator_value(cls, obj: Dict[str, Any]) -> Optional[str]:
|
||||
"""Returns the discriminator value (object type) of the data"""
|
||||
discriminator_value = obj[cls.__discriminator_property_name]
|
||||
if discriminator_value:
|
||||
return cls.__discriminator_value_class_map.get(discriminator_value)
|
||||
else:
|
||||
return None
|
||||
|
||||
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[Union[DiscriminatorAllOfSub]]:
|
||||
"""Create an instance of DiscriminatorAllOfSuper 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: Dict[str, Any]) -> Optional[Union[DiscriminatorAllOfSub]]:
|
||||
"""Create an instance of DiscriminatorAllOfSuper from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type == 'DiscriminatorAllOfSub':
|
||||
return import_module("petstore_api.models.discriminator_all_of_sub").DiscriminatorAllOfSub.from_dict(obj)
|
||||
|
||||
raise ValueError("DiscriminatorAllOfSuper failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
|
||||
|
@ -0,0 +1,50 @@
|
||||
# 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.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
class TestDiscriminatorAllOfSub(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSub unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSub:
|
||||
"""Test DiscriminatorAllOfSub
|
||||
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 `DiscriminatorAllOfSub`
|
||||
"""
|
||||
model = DiscriminatorAllOfSub()
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSub(self):
|
||||
"""Test DiscriminatorAllOfSub"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -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
|
||||
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
class TestDiscriminatorAllOfSuper(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSuper unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSuper:
|
||||
"""Test DiscriminatorAllOfSuper
|
||||
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 `DiscriminatorAllOfSuper`
|
||||
"""
|
||||
model = DiscriminatorAllOfSuper()
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = ''
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = '',
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSuper(self):
|
||||
"""Test DiscriminatorAllOfSuper"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -31,6 +31,8 @@ docs/CreatureInfo.md
|
||||
docs/DanishPig.md
|
||||
docs/DefaultApi.md
|
||||
docs/DeprecatedObject.md
|
||||
docs/DiscriminatorAllOfSub.md
|
||||
docs/DiscriminatorAllOfSuper.md
|
||||
docs/Dog.md
|
||||
docs/DummyModel.md
|
||||
docs/EnumArrays.md
|
||||
@ -141,6 +143,8 @@ petstore_api/models/creature.py
|
||||
petstore_api/models/creature_info.py
|
||||
petstore_api/models/danish_pig.py
|
||||
petstore_api/models/deprecated_object.py
|
||||
petstore_api/models/discriminator_all_of_sub.py
|
||||
petstore_api/models/discriminator_all_of_super.py
|
||||
petstore_api/models/dog.py
|
||||
petstore_api/models/dummy_model.py
|
||||
petstore_api/models/enum_arrays.py
|
||||
|
@ -166,6 +166,8 @@ Class | Method | HTTP request | Description
|
||||
- [CreatureInfo](docs/CreatureInfo.md)
|
||||
- [DanishPig](docs/DanishPig.md)
|
||||
- [DeprecatedObject](docs/DeprecatedObject.md)
|
||||
- [DiscriminatorAllOfSub](docs/DiscriminatorAllOfSub.md)
|
||||
- [DiscriminatorAllOfSuper](docs/DiscriminatorAllOfSuper.md)
|
||||
- [Dog](docs/Dog.md)
|
||||
- [DummyModel](docs/DummyModel.md)
|
||||
- [EnumArrays](docs/EnumArrays.md)
|
||||
|
@ -0,0 +1,27 @@
|
||||
# DiscriminatorAllOfSub
|
||||
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSub from a JSON string
|
||||
discriminator_all_of_sub_instance = DiscriminatorAllOfSub.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSub.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_sub_dict = discriminator_all_of_sub_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSub from a dict
|
||||
discriminator_all_of_sub_form_dict = discriminator_all_of_sub.from_dict(discriminator_all_of_sub_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)
|
||||
|
||||
|
@ -0,0 +1,28 @@
|
||||
# DiscriminatorAllOfSuper
|
||||
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**element_type** | **str** | |
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSuper from a JSON string
|
||||
discriminator_all_of_super_instance = DiscriminatorAllOfSuper.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSuper.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_super_dict = discriminator_all_of_super_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSuper from a dict
|
||||
discriminator_all_of_super_form_dict = discriminator_all_of_super.from_dict(discriminator_all_of_super_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)
|
||||
|
||||
|
@ -64,6 +64,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -40,6 +40,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -21,6 +21,12 @@ import json
|
||||
from typing import Optional, Union
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from importlib import import_module
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
|
||||
class Animal(BaseModel):
|
||||
"""
|
||||
Animal
|
||||
@ -78,15 +84,12 @@ class Animal(BaseModel):
|
||||
"""Create an instance of Animal from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type:
|
||||
klass = globals()[object_type]
|
||||
return klass.from_dict(obj)
|
||||
else:
|
||||
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))
|
||||
if object_type == 'Cat':
|
||||
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)
|
||||
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))
|
||||
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
Animal.update_forward_refs()
|
||||
|
||||
|
@ -0,0 +1,71 @@
|
||||
# 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 petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
class DiscriminatorAllOfSub(DiscriminatorAllOfSuper):
|
||||
"""
|
||||
DiscriminatorAllOfSub
|
||||
"""
|
||||
__properties = ["elementType"]
|
||||
|
||||
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) -> DiscriminatorAllOfSub:
|
||||
"""Create an instance of DiscriminatorAllOfSub 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) -> DiscriminatorAllOfSub:
|
||||
"""Create an instance of DiscriminatorAllOfSub from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return DiscriminatorAllOfSub.parse_obj(obj)
|
||||
|
||||
_obj = DiscriminatorAllOfSub.parse_obj({
|
||||
"element_type": obj.get("elementType")
|
||||
})
|
||||
return _obj
|
||||
|
||||
|
@ -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 typing import Union
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from importlib import import_module
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
class DiscriminatorAllOfSuper(BaseModel):
|
||||
"""
|
||||
DiscriminatorAllOfSuper
|
||||
"""
|
||||
element_type: StrictStr = Field(..., alias="elementType")
|
||||
__properties = ["elementType"]
|
||||
|
||||
class Config:
|
||||
"""Pydantic configuration"""
|
||||
allow_population_by_field_name = True
|
||||
validate_assignment = True
|
||||
|
||||
# JSON field name that stores the object type
|
||||
__discriminator_property_name = 'elementType'
|
||||
|
||||
# discriminator mappings
|
||||
__discriminator_value_class_map = {
|
||||
'DiscriminatorAllOfSub': 'DiscriminatorAllOfSub'
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_discriminator_value(cls, obj: dict) -> str:
|
||||
"""Returns the discriminator value (object type) of the data"""
|
||||
discriminator_value = obj[cls.__discriminator_property_name]
|
||||
if discriminator_value:
|
||||
return cls.__discriminator_value_class_map.get(discriminator_value)
|
||||
else:
|
||||
return None
|
||||
|
||||
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) -> Union(DiscriminatorAllOfSub):
|
||||
"""Create an instance of DiscriminatorAllOfSuper 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) -> Union(DiscriminatorAllOfSub):
|
||||
"""Create an instance of DiscriminatorAllOfSuper from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type == 'DiscriminatorAllOfSub':
|
||||
return import_module("petstore_api.models.discriminator_all_of_sub").DiscriminatorAllOfSub.from_dict(obj)
|
||||
raise ValueError("DiscriminatorAllOfSuper failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
|
||||
|
@ -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
|
||||
import datetime
|
||||
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub # noqa: E501
|
||||
|
||||
class TestDiscriminatorAllOfSub(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSub unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSub:
|
||||
"""Test DiscriminatorAllOfSub
|
||||
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 `DiscriminatorAllOfSub`
|
||||
"""
|
||||
model = DiscriminatorAllOfSub() # noqa: E501
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSub(self):
|
||||
"""Test DiscriminatorAllOfSub"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,53 @@
|
||||
# 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.discriminator_all_of_super import DiscriminatorAllOfSuper # noqa: E501
|
||||
|
||||
class TestDiscriminatorAllOfSuper(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSuper unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSuper:
|
||||
"""Test DiscriminatorAllOfSuper
|
||||
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 `DiscriminatorAllOfSuper`
|
||||
"""
|
||||
model = DiscriminatorAllOfSuper() # noqa: E501
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = ''
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = '',
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSuper(self):
|
||||
"""Test DiscriminatorAllOfSuper"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -31,6 +31,8 @@ docs/CreatureInfo.md
|
||||
docs/DanishPig.md
|
||||
docs/DefaultApi.md
|
||||
docs/DeprecatedObject.md
|
||||
docs/DiscriminatorAllOfSub.md
|
||||
docs/DiscriminatorAllOfSuper.md
|
||||
docs/Dog.md
|
||||
docs/DummyModel.md
|
||||
docs/EnumArrays.md
|
||||
@ -141,6 +143,8 @@ petstore_api/models/creature.py
|
||||
petstore_api/models/creature_info.py
|
||||
petstore_api/models/danish_pig.py
|
||||
petstore_api/models/deprecated_object.py
|
||||
petstore_api/models/discriminator_all_of_sub.py
|
||||
petstore_api/models/discriminator_all_of_super.py
|
||||
petstore_api/models/dog.py
|
||||
petstore_api/models/dummy_model.py
|
||||
petstore_api/models/enum_arrays.py
|
||||
|
@ -166,6 +166,8 @@ Class | Method | HTTP request | Description
|
||||
- [CreatureInfo](docs/CreatureInfo.md)
|
||||
- [DanishPig](docs/DanishPig.md)
|
||||
- [DeprecatedObject](docs/DeprecatedObject.md)
|
||||
- [DiscriminatorAllOfSub](docs/DiscriminatorAllOfSub.md)
|
||||
- [DiscriminatorAllOfSuper](docs/DiscriminatorAllOfSuper.md)
|
||||
- [Dog](docs/Dog.md)
|
||||
- [DummyModel](docs/DummyModel.md)
|
||||
- [EnumArrays](docs/EnumArrays.md)
|
||||
|
@ -0,0 +1,27 @@
|
||||
# DiscriminatorAllOfSub
|
||||
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSub from a JSON string
|
||||
discriminator_all_of_sub_instance = DiscriminatorAllOfSub.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSub.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_sub_dict = discriminator_all_of_sub_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSub from a dict
|
||||
discriminator_all_of_sub_form_dict = discriminator_all_of_sub.from_dict(discriminator_all_of_sub_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)
|
||||
|
||||
|
@ -0,0 +1,28 @@
|
||||
# DiscriminatorAllOfSuper
|
||||
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**element_type** | **str** | |
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSuper from a JSON string
|
||||
discriminator_all_of_super_instance = DiscriminatorAllOfSuper.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSuper.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_super_dict = discriminator_all_of_super_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSuper from a dict
|
||||
discriminator_all_of_super_form_dict = discriminator_all_of_super.from_dict(discriminator_all_of_super_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)
|
||||
|
||||
|
@ -64,6 +64,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -40,6 +40,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -21,6 +21,12 @@ import json
|
||||
from typing import Any, Dict, Optional, Union
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from importlib import import_module
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
|
||||
class Animal(BaseModel):
|
||||
"""
|
||||
Animal
|
||||
@ -85,15 +91,12 @@ class Animal(BaseModel):
|
||||
"""Create an instance of Animal from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type:
|
||||
klass = globals()[object_type]
|
||||
return klass.from_dict(obj)
|
||||
else:
|
||||
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))
|
||||
if object_type == 'Cat':
|
||||
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)
|
||||
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))
|
||||
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
Animal.update_forward_refs()
|
||||
|
||||
|
@ -0,0 +1,83 @@
|
||||
# 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
|
||||
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
class DiscriminatorAllOfSub(DiscriminatorAllOfSuper):
|
||||
"""
|
||||
DiscriminatorAllOfSub
|
||||
"""
|
||||
additional_properties: Dict[str, Any] = {}
|
||||
__properties = ["elementType"]
|
||||
|
||||
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) -> DiscriminatorAllOfSub:
|
||||
"""Create an instance of DiscriminatorAllOfSub 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) -> DiscriminatorAllOfSub:
|
||||
"""Create an instance of DiscriminatorAllOfSub from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return DiscriminatorAllOfSub.parse_obj(obj)
|
||||
|
||||
_obj = DiscriminatorAllOfSub.parse_obj({
|
||||
"element_type": obj.get("elementType")
|
||||
})
|
||||
# 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
|
||||
|
||||
|
@ -0,0 +1,97 @@
|
||||
# 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, Union
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from importlib import import_module
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
class DiscriminatorAllOfSuper(BaseModel):
|
||||
"""
|
||||
DiscriminatorAllOfSuper
|
||||
"""
|
||||
element_type: StrictStr = Field(..., alias="elementType")
|
||||
additional_properties: Dict[str, Any] = {}
|
||||
__properties = ["elementType"]
|
||||
|
||||
class Config:
|
||||
"""Pydantic configuration"""
|
||||
allow_population_by_field_name = True
|
||||
validate_assignment = True
|
||||
|
||||
# JSON field name that stores the object type
|
||||
__discriminator_property_name = 'elementType'
|
||||
|
||||
# discriminator mappings
|
||||
__discriminator_value_class_map = {
|
||||
'DiscriminatorAllOfSub': 'DiscriminatorAllOfSub'
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_discriminator_value(cls, obj: dict) -> str:
|
||||
"""Returns the discriminator value (object type) of the data"""
|
||||
discriminator_value = obj[cls.__discriminator_property_name]
|
||||
if discriminator_value:
|
||||
return cls.__discriminator_value_class_map.get(discriminator_value)
|
||||
else:
|
||||
return None
|
||||
|
||||
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) -> Union(DiscriminatorAllOfSub):
|
||||
"""Create an instance of DiscriminatorAllOfSuper 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) -> Union(DiscriminatorAllOfSub):
|
||||
"""Create an instance of DiscriminatorAllOfSuper from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type == 'DiscriminatorAllOfSub':
|
||||
return import_module("petstore_api.models.discriminator_all_of_sub").DiscriminatorAllOfSub.from_dict(obj)
|
||||
raise ValueError("DiscriminatorAllOfSuper failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
|
||||
|
@ -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
|
||||
import datetime
|
||||
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub # noqa: E501
|
||||
|
||||
class TestDiscriminatorAllOfSub(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSub unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSub:
|
||||
"""Test DiscriminatorAllOfSub
|
||||
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 `DiscriminatorAllOfSub`
|
||||
"""
|
||||
model = DiscriminatorAllOfSub() # noqa: E501
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSub(self):
|
||||
"""Test DiscriminatorAllOfSub"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,53 @@
|
||||
# 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.discriminator_all_of_super import DiscriminatorAllOfSuper # noqa: E501
|
||||
|
||||
class TestDiscriminatorAllOfSuper(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSuper unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSuper:
|
||||
"""Test DiscriminatorAllOfSuper
|
||||
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 `DiscriminatorAllOfSuper`
|
||||
"""
|
||||
model = DiscriminatorAllOfSuper() # noqa: E501
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = ''
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = '',
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSuper(self):
|
||||
"""Test DiscriminatorAllOfSuper"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -30,6 +30,8 @@ docs/CreatureInfo.md
|
||||
docs/DanishPig.md
|
||||
docs/DefaultApi.md
|
||||
docs/DeprecatedObject.md
|
||||
docs/DiscriminatorAllOfSub.md
|
||||
docs/DiscriminatorAllOfSuper.md
|
||||
docs/Dog.md
|
||||
docs/DummyModel.md
|
||||
docs/EnumArrays.md
|
||||
@ -140,6 +142,8 @@ petstore_api/models/creature.py
|
||||
petstore_api/models/creature_info.py
|
||||
petstore_api/models/danish_pig.py
|
||||
petstore_api/models/deprecated_object.py
|
||||
petstore_api/models/discriminator_all_of_sub.py
|
||||
petstore_api/models/discriminator_all_of_super.py
|
||||
petstore_api/models/dog.py
|
||||
petstore_api/models/dummy_model.py
|
||||
petstore_api/models/enum_arrays.py
|
||||
|
@ -164,6 +164,8 @@ Class | Method | HTTP request | Description
|
||||
- [CreatureInfo](docs/CreatureInfo.md)
|
||||
- [DanishPig](docs/DanishPig.md)
|
||||
- [DeprecatedObject](docs/DeprecatedObject.md)
|
||||
- [DiscriminatorAllOfSub](docs/DiscriminatorAllOfSub.md)
|
||||
- [DiscriminatorAllOfSuper](docs/DiscriminatorAllOfSuper.md)
|
||||
- [Dog](docs/Dog.md)
|
||||
- [DummyModel](docs/DummyModel.md)
|
||||
- [EnumArrays](docs/EnumArrays.md)
|
||||
|
@ -0,0 +1,28 @@
|
||||
# DiscriminatorAllOfSub
|
||||
|
||||
|
||||
## Properties
|
||||
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSub from a JSON string
|
||||
discriminator_all_of_sub_instance = DiscriminatorAllOfSub.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSub.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_sub_dict = discriminator_all_of_sub_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSub from a dict
|
||||
discriminator_all_of_sub_form_dict = discriminator_all_of_sub.from_dict(discriminator_all_of_sub_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)
|
||||
|
||||
|
@ -0,0 +1,29 @@
|
||||
# DiscriminatorAllOfSuper
|
||||
|
||||
|
||||
## Properties
|
||||
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**element_type** | **str** | |
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of DiscriminatorAllOfSuper from a JSON string
|
||||
discriminator_all_of_super_instance = DiscriminatorAllOfSuper.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print DiscriminatorAllOfSuper.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
discriminator_all_of_super_dict = discriminator_all_of_super_instance.to_dict()
|
||||
# create an instance of DiscriminatorAllOfSuper from a dict
|
||||
discriminator_all_of_super_form_dict = discriminator_all_of_super.from_dict(discriminator_all_of_super_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)
|
||||
|
||||
|
@ -63,6 +63,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -39,6 +39,8 @@ from petstore_api.models.creature import Creature
|
||||
from petstore_api.models.creature_info import CreatureInfo
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
from petstore_api.models.deprecated_object import DeprecatedObject
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from petstore_api.models.dog import Dog
|
||||
from petstore_api.models.dummy_model import DummyModel
|
||||
from petstore_api.models.enum_arrays import EnumArrays
|
||||
|
@ -17,11 +17,17 @@ import pprint
|
||||
import re # noqa: F401
|
||||
import json
|
||||
|
||||
from importlib import import_module
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
from typing import Any, ClassVar, Dict, List, Optional, Union
|
||||
from typing import Optional, Set
|
||||
from typing_extensions import Self
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
|
||||
class Animal(BaseModel):
|
||||
"""
|
||||
Animal
|
||||
@ -65,7 +71,7 @@ class Animal(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Optional[Union[Self, Self]]:
|
||||
def from_json(cls, json_str: str) -> Optional[Union[Cat, Dog]]:
|
||||
"""Create an instance of Animal from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@ -97,20 +103,17 @@ class Animal(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Self, Self]]:
|
||||
def from_dict(cls, obj: Dict[str, Any]) -> Optional[Union[Cat, Dog]]:
|
||||
"""Create an instance of Animal from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type:
|
||||
klass = globals()[object_type]
|
||||
return klass.from_dict(obj)
|
||||
else:
|
||||
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))
|
||||
if object_type == 'Cat':
|
||||
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)
|
||||
|
||||
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))
|
||||
|
||||
from petstore_api.models.cat import Cat
|
||||
from petstore_api.models.dog import Dog
|
||||
# TODO: Rewrite to not use raise_errors
|
||||
Animal.model_rebuild(raise_errors=False)
|
||||
|
||||
|
@ -0,0 +1,99 @@
|
||||
# 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, ClassVar, Dict, List
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
from typing import Optional, Set
|
||||
from typing_extensions import Self
|
||||
|
||||
class DiscriminatorAllOfSub(DiscriminatorAllOfSuper):
|
||||
"""
|
||||
DiscriminatorAllOfSub
|
||||
""" # noqa: E501
|
||||
additional_properties: Dict[str, Any] = {}
|
||||
__properties: ClassVar[List[str]] = ["elementType"]
|
||||
|
||||
model_config = {
|
||||
"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 DiscriminatorAllOfSub 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 DiscriminatorAllOfSub from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = cls.model_validate({
|
||||
"elementType": obj.get("elementType")
|
||||
})
|
||||
# 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
|
||||
|
||||
|
@ -0,0 +1,115 @@
|
||||
# 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 importlib import import_module
|
||||
from pydantic import BaseModel, Field, StrictStr
|
||||
from typing import Any, ClassVar, Dict, List, Union
|
||||
from typing import Optional, Set
|
||||
from typing_extensions import Self
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
if TYPE_CHECKING:
|
||||
from petstore_api.models.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
class DiscriminatorAllOfSuper(BaseModel):
|
||||
"""
|
||||
DiscriminatorAllOfSuper
|
||||
""" # noqa: E501
|
||||
element_type: StrictStr = Field(alias="elementType")
|
||||
additional_properties: Dict[str, Any] = {}
|
||||
__properties: ClassVar[List[str]] = ["elementType"]
|
||||
|
||||
model_config = {
|
||||
"populate_by_name": True,
|
||||
"validate_assignment": True,
|
||||
"protected_namespaces": (),
|
||||
}
|
||||
|
||||
|
||||
# JSON field name that stores the object type
|
||||
__discriminator_property_name: ClassVar[str] = 'elementType'
|
||||
|
||||
# discriminator mappings
|
||||
__discriminator_value_class_map: ClassVar[Dict[str, str]] = {
|
||||
'DiscriminatorAllOfSub': 'DiscriminatorAllOfSub'
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_discriminator_value(cls, obj: Dict[str, Any]) -> Optional[str]:
|
||||
"""Returns the discriminator value (object type) of the data"""
|
||||
discriminator_value = obj[cls.__discriminator_property_name]
|
||||
if discriminator_value:
|
||||
return cls.__discriminator_value_class_map.get(discriminator_value)
|
||||
else:
|
||||
return None
|
||||
|
||||
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[Union[DiscriminatorAllOfSub]]:
|
||||
"""Create an instance of DiscriminatorAllOfSuper 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: Dict[str, Any]) -> Optional[Union[DiscriminatorAllOfSub]]:
|
||||
"""Create an instance of DiscriminatorAllOfSuper from a dict"""
|
||||
# look up the object type based on discriminator mapping
|
||||
object_type = cls.get_discriminator_value(obj)
|
||||
if object_type == 'DiscriminatorAllOfSub':
|
||||
return import_module("petstore_api.models.discriminator_all_of_sub").DiscriminatorAllOfSub.from_dict(obj)
|
||||
|
||||
raise ValueError("DiscriminatorAllOfSuper failed to lookup discriminator value from " +
|
||||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name +
|
||||
", mapping: " + json.dumps(cls.__discriminator_value_class_map))
|
||||
|
||||
|
@ -0,0 +1,50 @@
|
||||
# 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.discriminator_all_of_sub import DiscriminatorAllOfSub
|
||||
|
||||
class TestDiscriminatorAllOfSub(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSub unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSub:
|
||||
"""Test DiscriminatorAllOfSub
|
||||
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 `DiscriminatorAllOfSub`
|
||||
"""
|
||||
model = DiscriminatorAllOfSub()
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSub(
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSub(self):
|
||||
"""Test DiscriminatorAllOfSub"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -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
|
||||
|
||||
from petstore_api.models.discriminator_all_of_super import DiscriminatorAllOfSuper
|
||||
|
||||
class TestDiscriminatorAllOfSuper(unittest.TestCase):
|
||||
"""DiscriminatorAllOfSuper unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional) -> DiscriminatorAllOfSuper:
|
||||
"""Test DiscriminatorAllOfSuper
|
||||
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 `DiscriminatorAllOfSuper`
|
||||
"""
|
||||
model = DiscriminatorAllOfSuper()
|
||||
if include_optional:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = ''
|
||||
)
|
||||
else:
|
||||
return DiscriminatorAllOfSuper(
|
||||
element_type = '',
|
||||
)
|
||||
"""
|
||||
|
||||
def testDiscriminatorAllOfSuper(self):
|
||||
"""Test DiscriminatorAllOfSuper"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -342,6 +342,10 @@ class ModelTests(unittest.TestCase):
|
||||
self.assertEqual(dog2.breed, 'bulldog')
|
||||
self.assertEqual(dog2.class_name, "dog")
|
||||
self.assertEqual(dog2.color, 'white')
|
||||
|
||||
def test_inheritance_discriminators(self):
|
||||
model = petstore_api.DiscriminatorAllOfSuper.from_dict({"elementType": "DiscriminatorAllOfSub"})
|
||||
self.assertIsInstance(model, petstore_api.DiscriminatorAllOfSub)
|
||||
|
||||
def test_list(self):
|
||||
# should throw exception as var_123_list should be string
|
||||
|
Loading…
x
Reference in New Issue
Block a user