Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds tests of Shape, Tringle, and Quadrilateral traveling through 2 d…
…iscriminators
  • Loading branch information
spacether committed Apr 30, 2020
commit ffed5dc63f1fb573b08368752f7019eb0f31635c
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,12 @@ class OpenApiModel(object):
# pick a new schema/class to instantiate because a discriminator
# propertyName value was passed in

from_server = kwargs.get('_from_server', False)
visited_composed_classes = kwargs.get('_visited_composed_classes', ())
if (
cls.discriminator is None or
from_server is False or
cls in visited_composed_classes
):
# we don't have a discriminator
# from_server is false which means that we are building this model
# on the client side
# or we have already visited this class before and are sure that we
# want to instantiate it this time

Expand All @@ -84,6 +80,18 @@ class OpenApiModel(object):
oneof_anyof_classes.extend(cls._composed_schemas.get('oneOf', ()))
oneof_anyof_classes.extend(cls._composed_schemas.get('anyOf', ()))
new_cls = cls.get_discriminator_class(kwargs)
if new_cls is None:
disc_prop_name_py = list(cls.discriminator.keys())[0]
disc_prop_name_js = cls.attribute_map[disc_prop_name_py]
path_to_item = kwargs.get('_path_to_item', ())
disc_prop_value = kwargs.get(
disc_prop_name_js, kwargs.get(disc_prop_name_py))
raise ApiValueError(
"Cannot deserialize input data due to invalid discriminator "
"value. The OpenAPI document has no mapping for discriminator "
"property '%s'='%s' at path: %s" %
(disc_prop_name_js, disc_prop_value, path_to_item)
)
oneof_anyof_child = new_cls in oneof_anyof_classes

new_visited_composed_classes = [cls]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1875,37 +1875,61 @@ components:
type: boolean
required:
- lengthCm
Shapes:
Shape:
properties:
shapeType:
type: string
oneOf:
- $ref: '#/components/schemas/Triangle'
- $ref: '#/components/schemas/Quadrilateral'
discriminator:
propertyName: shapeType
TriangleInterface:
ShapeInterface:
properties:
shapeType:
type: string
triangleType:
type: string
required:
- shapeType
- triangleType
Triangle:
properties:
triangleType:
type: string
oneOf:
- $ref: '#/components/schemas/EquilateralTriangle'
- $ref: '#/components/schemas/IsoscelesTriangle'
- $ref: '#/components/schemas/ScaleneTriangle'
discriminator:
propertyName: triangleType
EquilateralTriangle:
properties:
triangleType:
type: string
enum:
- EquilateralTriangle
required:
- triangleType
allOf:
- $ref: '#/components/schemas/TriangleInterface'
- $ref: '#/components/schemas/ShapeInterface'
IsoscelesTriangle:
properties:
triangleType:
type: string
enum:
- IsoscelesTriangle
required:
- triangleType
allOf:
- $ref: '#/components/schemas/TriangleInterface'
- $ref: '#/components/schemas/ShapeInterface'
ScaleneTriangle:
properties:
triangleType:
type: string
enum:
- ScaleneTriangle
required:
- triangleType
allOf:
- $ref: '#/components/schemas/TriangleInterface'
- $ref: '#/components/schemas/ShapeInterface'
QuadrilateralInterface:
properties:
shapeType:
Expand Down
10 changes: 10 additions & 0 deletions samples/openapi3/client/petstore/python-experimental/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ Class | Method | HTTP request | Description
- [category.Category](docs/Category.md)
- [class_model.ClassModel](docs/ClassModel.md)
- [client.Client](docs/Client.md)
- [complex_quadrilateral.ComplexQuadrilateral](docs/ComplexQuadrilateral.md)
- [dog.Dog](docs/Dog.md)
- [dog_all_of.DogAllOf](docs/DogAllOf.md)
- [enum_arrays.EnumArrays](docs/EnumArrays.md)
- [enum_class.EnumClass](docs/EnumClass.md)
- [enum_test.EnumTest](docs/EnumTest.md)
- [equilateral_triangle.EquilateralTriangle](docs/EquilateralTriangle.md)
- [file.File](docs/File.md)
- [file_schema_test_class.FileSchemaTestClass](docs/FileSchemaTestClass.md)
- [foo.Foo](docs/Foo.md)
Expand All @@ -160,6 +162,7 @@ Class | Method | HTTP request | Description
- [inline_object4.InlineObject4](docs/InlineObject4.md)
- [inline_object5.InlineObject5](docs/InlineObject5.md)
- [inline_response_default.InlineResponseDefault](docs/InlineResponseDefault.md)
- [isosceles_triangle.IsoscelesTriangle](docs/IsoscelesTriangle.md)
- [list.List](docs/List.md)
- [mammal.Mammal](docs/Mammal.md)
- [map_test.MapTest](docs/MapTest.md)
Expand All @@ -176,10 +179,17 @@ Class | Method | HTTP request | Description
- [outer_enum_integer.OuterEnumInteger](docs/OuterEnumInteger.md)
- [outer_enum_integer_default_value.OuterEnumIntegerDefaultValue](docs/OuterEnumIntegerDefaultValue.md)
- [pet.Pet](docs/Pet.md)
- [quadrilateral.Quadrilateral](docs/Quadrilateral.md)
- [quadrilateral_interface.QuadrilateralInterface](docs/QuadrilateralInterface.md)
- [read_only_first.ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [scalene_triangle.ScaleneTriangle](docs/ScaleneTriangle.md)
- [shape.Shape](docs/Shape.md)
- [shape_interface.ShapeInterface](docs/ShapeInterface.md)
- [simple_quadrilateral.SimpleQuadrilateral](docs/SimpleQuadrilateral.md)
- [special_model_name.SpecialModelName](docs/SpecialModelName.md)
- [string_boolean_map.StringBooleanMap](docs/StringBooleanMap.md)
- [tag.Tag](docs/Tag.md)
- [triangle.Triangle](docs/Triangle.md)
- [user.User](docs/User.md)
- [whale.Whale](docs/Whale.md)
- [zebra.Zebra](docs/Zebra.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# complex_quadrilateral.ComplexQuadrilateral

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**quadrilateral_type** | **str** | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# equilateral_triangle.EquilateralTriangle

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**triangle_type** | **str** | | defaults to 'EquilateralTriangle'

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# isosceles_triangle.IsoscelesTriangle

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**triangle_type** | **str** | | defaults to 'IsoscelesTriangle'

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# quadrilateral.Quadrilateral

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**quadrilateral_type** | **str** | |
**shape_type** | **str** | | defaults to nulltype.Null

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# quadrilateral_interface.QuadrilateralInterface

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**quadrilateral_type** | **str** | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# scalene_triangle.ScaleneTriangle

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**triangle_type** | **str** | | defaults to 'ScaleneTriangle'

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


12 changes: 12 additions & 0 deletions samples/openapi3/client/petstore/python-experimental/docs/Shape.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# shape.Shape

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**quadrilateral_type** | **str** | |
**shape_type** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# shape_interface.ShapeInterface

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# simple_quadrilateral.SimpleQuadrilateral

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**quadrilateral_type** | **str** | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# triangle.Triangle

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**triangle_type** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# triangle_interface.TriangleInterface

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**shape_type** | **str** | |
**triangle_type** | **str** | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@
from petstore_api.models.category import Category
from petstore_api.models.class_model import ClassModel
from petstore_api.models.client import Client
from petstore_api.models.complex_quadrilateral import ComplexQuadrilateral
from petstore_api.models.dog import Dog
from petstore_api.models.dog_all_of import DogAllOf
from petstore_api.models.enum_arrays import EnumArrays
from petstore_api.models.enum_class import EnumClass
from petstore_api.models.enum_test import EnumTest
from petstore_api.models.equilateral_triangle import EquilateralTriangle
from petstore_api.models.file import File
from petstore_api.models.file_schema_test_class import FileSchemaTestClass
from petstore_api.models.foo import Foo
Expand All @@ -78,6 +80,7 @@
from petstore_api.models.inline_object4 import InlineObject4
from petstore_api.models.inline_object5 import InlineObject5
from petstore_api.models.inline_response_default import InlineResponseDefault
from petstore_api.models.isosceles_triangle import IsoscelesTriangle
from petstore_api.models.list import List
from petstore_api.models.mammal import Mammal
from petstore_api.models.map_test import MapTest
Expand All @@ -94,10 +97,17 @@
from petstore_api.models.outer_enum_integer import OuterEnumInteger
from petstore_api.models.outer_enum_integer_default_value import OuterEnumIntegerDefaultValue
from petstore_api.models.pet import Pet
from petstore_api.models.quadrilateral import Quadrilateral
from petstore_api.models.quadrilateral_interface import QuadrilateralInterface
from petstore_api.models.read_only_first import ReadOnlyFirst
from petstore_api.models.scalene_triangle import ScaleneTriangle
from petstore_api.models.shape import Shape
from petstore_api.models.shape_interface import ShapeInterface
from petstore_api.models.simple_quadrilateral import SimpleQuadrilateral
from petstore_api.models.special_model_name import SpecialModelName
from petstore_api.models.string_boolean_map import StringBooleanMap
from petstore_api.models.tag import Tag
from petstore_api.models.triangle import Triangle
from petstore_api.models.user import User
from petstore_api.models.whale import Whale
from petstore_api.models.zebra import Zebra
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,12 @@ def __new__(cls, *args, **kwargs):
# pick a new schema/class to instantiate because a discriminator
# propertyName value was passed in

from_server = kwargs.get('_from_server', False)
visited_composed_classes = kwargs.get('_visited_composed_classes', ())
if (
cls.discriminator is None or
from_server is False or
cls in visited_composed_classes
):
# we don't have a discriminator
# from_server is false which means that we are building this model
# on the client side
# or we have already visited this class before and are sure that we
# want to instantiate it this time

Expand All @@ -154,6 +150,18 @@ def __new__(cls, *args, **kwargs):
oneof_anyof_classes.extend(cls._composed_schemas.get('oneOf', ()))
oneof_anyof_classes.extend(cls._composed_schemas.get('anyOf', ()))
new_cls = cls.get_discriminator_class(kwargs)
if new_cls is None:
disc_prop_name_py = list(cls.discriminator.keys())[0]
disc_prop_name_js = cls.attribute_map[disc_prop_name_py]
path_to_item = kwargs.get('_path_to_item', ())
disc_prop_value = kwargs.get(
disc_prop_name_js, kwargs.get(disc_prop_name_py))
raise ApiValueError(
"Cannot deserialize input data due to invalid discriminator "
"value. The OpenAPI document has no mapping for discriminator "
"property '%s'='%s' at path: %s" %
(disc_prop_name_js, disc_prop_value, path_to_item)
)
oneof_anyof_child = new_cls in oneof_anyof_classes

new_visited_composed_classes = [cls]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ def _composed_schemas():
}

@classmethod
def get_discriminator_class(cls, from_server, data):
def get_discriminator_class(cls, data):
"""Returns the child class specified by the discriminator"""
discriminator = cls.discriminator()
discriminator = cls.discriminator
discr_propertyname_py = list(discriminator.keys())[0]
discr_propertyname_js = cls.attribute_map[discr_propertyname_py]
if from_server:
if discr_propertyname_js in data:
class_name = data[discr_propertyname_js]
else:
class_name = data[discr_propertyname_py]
Expand Down
Loading