diff --git a/CHANGELOG.md b/CHANGELOG.md index 620795695..134ec4818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 0.5.4 - Unreleased ### Additions - Added support for octet-stream content type (#116) - +- Union properties defined using oneOf (#98) ## 0.5.3 - 2020-08-13 ### Security diff --git a/openapi_python_client/parser/properties.py b/openapi_python_client/parser/properties.py index fb2d2d6cc..7ee7277ff 100644 --- a/openapi_python_client/parser/properties.py +++ b/openapi_python_client/parser/properties.py @@ -2,6 +2,7 @@ from dataclasses import InitVar, dataclass, field from datetime import date, datetime +from itertools import chain from typing import Any, ClassVar, Dict, Generic, List, Optional, Set, TypeVar, Union from .. import schema as oai @@ -461,9 +462,9 @@ def _property_from_data( title=data.title or name, default=data.default, ) - if data.anyOf: + if data.anyOf or data.oneOf: sub_properties: List[Property] = [] - for sub_prop_data in data.anyOf: + for sub_prop_data in chain(data.anyOf, data.oneOf): sub_prop = property_from_data(name=name, required=required, data=sub_prop_data) if isinstance(sub_prop, PropertyError): return PropertyError(detail=f"Invalid property in union {name}", data=sub_prop_data) diff --git a/openapi_python_client/schema/schema.py b/openapi_python_client/schema/schema.py index 962eba72b..8db8c165a 100644 --- a/openapi_python_client/schema/schema.py +++ b/openapi_python_client/schema/schema.py @@ -241,7 +241,7 @@ class Schema(BaseModel): value. """ - oneOf: Optional[List[Union[Reference, "Schema"]]] = None + oneOf: List[Union[Reference, "Schema"]] = [] """ **From OpenAPI spec: Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard JSON Schema.** @@ -258,7 +258,7 @@ class Schema(BaseModel): keyword's value. """ - anyOf: Optional[List[Union[Reference, "Schema"]]] = None + anyOf: List[Union[Reference, "Schema"]] = [] """ **From OpenAPI spec: Inline or referenced schema MUST be of a [Schema Object](#schemaObject) and not a standard JSON Schema.** diff --git a/tests/test_openapi_parser/test_properties.py b/tests/test_openapi_parser/test_properties.py index d6833adf6..5844a64cd 100644 --- a/tests/test_openapi_parser/test_properties.py +++ b/tests/test_openapi_parser/test_properties.py @@ -710,7 +710,7 @@ def test_property_from_data_array_invalid_items(self, mocker): def test_property_from_data_union(self, mocker): name = mocker.MagicMock() required = mocker.MagicMock() - data = oai.Schema(anyOf=[{"type": "number", "default": "0.0"}, {"type": "integer", "default": "0"},]) + data = oai.Schema(anyOf=[{"type": "number", "default": "0.0"}], oneOf=[{"type": "integer", "default": "0"},]) UnionProperty = mocker.patch(f"{MODULE_NAME}.UnionProperty") FloatProperty = mocker.patch(f"{MODULE_NAME}.FloatProperty") IntProperty = mocker.patch(f"{MODULE_NAME}.IntProperty")