-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PYTHON] Fix for failing to lookup discriminator value using AllOf a…
…nd discriminator (#18498) * fix issue 18495 * add tests and update samples * update samples
- Loading branch information
Showing
56 changed files
with
2,185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
samples/openapi3/client/petstore/python-aiohttp/docs/BaseDiscriminator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# BaseDiscriminator | ||
|
||
|
||
## Properties | ||
|
||
Name | Type | Description | Notes | ||
------------ | ------------- | ------------- | ------------- | ||
**type_name** | **str** | | [optional] | ||
|
||
## Example | ||
|
||
```python | ||
from petstore_api.models.base_discriminator import BaseDiscriminator | ||
|
||
# TODO update the JSON string below | ||
json = "{}" | ||
# create an instance of BaseDiscriminator from a JSON string | ||
base_discriminator_instance = BaseDiscriminator.from_json(json) | ||
# print the JSON string representation of the object | ||
print(BaseDiscriminator.to_json()) | ||
|
||
# convert the object into a dict | ||
base_discriminator_dict = base_discriminator_instance.to_dict() | ||
# create an instance of BaseDiscriminator from a dict | ||
base_discriminator_from_dict = BaseDiscriminator.from_dict(base_discriminator_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) | ||
|
||
|
29 changes: 29 additions & 0 deletions
29
samples/openapi3/client/petstore/python-aiohttp/docs/Info.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Info | ||
|
||
|
||
## Properties | ||
|
||
Name | Type | Description | Notes | ||
------------ | ------------- | ------------- | ------------- | ||
**val** | [**BaseDiscriminator**](BaseDiscriminator.md) | | [optional] | ||
|
||
## Example | ||
|
||
```python | ||
from petstore_api.models.info import Info | ||
|
||
# TODO update the JSON string below | ||
json = "{}" | ||
# create an instance of Info from a JSON string | ||
info_instance = Info.from_json(json) | ||
# print the JSON string representation of the object | ||
print(Info.to_json()) | ||
|
||
# convert the object into a dict | ||
info_dict = info_instance.to_dict() | ||
# create an instance of Info from a dict | ||
info_from_dict = Info.from_dict(info_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) | ||
|
||
|
29 changes: 29 additions & 0 deletions
29
samples/openapi3/client/petstore/python-aiohttp/docs/PrimitiveString.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# PrimitiveString | ||
|
||
|
||
## Properties | ||
|
||
Name | Type | Description | Notes | ||
------------ | ------------- | ------------- | ------------- | ||
**value** | **str** | | [optional] | ||
|
||
## Example | ||
|
||
```python | ||
from petstore_api.models.primitive_string import PrimitiveString | ||
|
||
# TODO update the JSON string below | ||
json = "{}" | ||
# create an instance of PrimitiveString from a JSON string | ||
primitive_string_instance = PrimitiveString.from_json(json) | ||
# print the JSON string representation of the object | ||
print(PrimitiveString.to_json()) | ||
|
||
# convert the object into a dict | ||
primitive_string_dict = primitive_string_instance.to_dict() | ||
# create an instance of PrimitiveString from a dict | ||
primitive_string_from_dict = PrimitiveString.from_dict(primitive_string_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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/base_discriminator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# 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, ConfigDict, 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.primitive_string import PrimitiveString | ||
from petstore_api.models.info import Info | ||
|
||
class BaseDiscriminator(BaseModel): | ||
""" | ||
BaseDiscriminator | ||
""" # noqa: E501 | ||
type_name: Optional[StrictStr] = Field(default=None, alias="_typeName") | ||
__properties: ClassVar[List[str]] = ["_typeName"] | ||
|
||
model_config = ConfigDict( | ||
populate_by_name=True, | ||
validate_assignment=True, | ||
protected_namespaces=(), | ||
) | ||
|
||
|
||
# JSON field name that stores the object type | ||
__discriminator_property_name: ClassVar[str] = '_typeName' | ||
|
||
# discriminator mappings | ||
__discriminator_value_class_map: ClassVar[Dict[str, str]] = { | ||
'string': 'PrimitiveString','Info': 'Info' | ||
} | ||
|
||
@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[PrimitiveString, Info]]: | ||
"""Create an instance of BaseDiscriminator 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[PrimitiveString, Info]]: | ||
"""Create an instance of BaseDiscriminator from a dict""" | ||
# look up the object type based on discriminator mapping | ||
object_type = cls.get_discriminator_value(obj) | ||
if object_type == 'PrimitiveString': | ||
return import_module("petstore_api.models.primitive_string").PrimitiveString.from_dict(obj) | ||
if object_type == 'Info': | ||
return import_module("petstore_api.models.info").Info.from_dict(obj) | ||
|
||
raise ValueError("BaseDiscriminator failed to lookup discriminator value from " + | ||
json.dumps(obj) + ". Discriminator property name: " + cls.__discriminator_property_name + | ||
", mapping: " + json.dumps(cls.__discriminator_value_class_map)) | ||
|
||
|
Oops, something went wrong.