Skip to content

Add support for oneOf union properties in addition to anyOf #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions openapi_python_client/parser/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions openapi_python_client/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.**
Expand All @@ -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.**
Expand Down
2 changes: 1 addition & 1 deletion tests/test_openapi_parser/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down