Skip to content

Add support for one-of with any type #133

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 3 commits into from
Jun 17, 2019
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
35 changes: 27 additions & 8 deletions openapi_core/schema/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import attr
import functools
import logging
from base64 import b64decode, b64encode
from collections import defaultdict
from datetime import date, datetime
from uuid import UUID
Expand Down Expand Up @@ -219,6 +218,9 @@ def _unmarshal_string(self, value, custom_formatters=None, strict=True):
else:
raise InvalidSchemaValue(msg, value, self.format)
else:
if self.enum and value not in self.enum:
raise InvalidSchemaValue(
"Value {value} not in enum choices: {type}", value, self.enum)
formatstring = self.STRING_FORMAT_CALLABLE_GETTER[schema_format]

try:
Expand Down Expand Up @@ -251,13 +253,30 @@ def _unmarshal_any(self, value, custom_formatters=None, strict=True):
SchemaType.INTEGER, SchemaType.NUMBER, SchemaType.STRING,
]
cast_mapping = self.get_cast_mapping()
for schema_type in types_resolve_order:
cast_callable = cast_mapping[schema_type]
try:
return cast_callable(value)
# @todo: remove ValueError when validation separated
except (OpenAPISchemaError, TypeError, ValueError):
continue
if self.one_of:
result = None
for subschema in self.one_of:
try:
casted = subschema.cast(value, custom_formatters)
except (OpenAPISchemaError, TypeError, ValueError):
continue
else:
if result is not None:
raise MultipleOneOfSchema(self.type)
result = casted

if result is None:
raise NoOneOfSchema(self.type)

return result
else:
for schema_type in types_resolve_order:
cast_callable = cast_mapping[schema_type]
try:
return cast_callable(value)
# @todo: remove ValueError when validation separated
except (OpenAPISchemaError, TypeError, ValueError):
continue

raise NoValidSchema(value)

Expand Down
7 changes: 4 additions & 3 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import pytest
from datetime import datetime
from base64 import b64encode
from uuid import UUID
from six import iteritems, text_type
Expand All @@ -19,7 +20,7 @@
from openapi_core.schema.responses.models import Response
from openapi_core.schema.schemas.enums import SchemaType
from openapi_core.schema.schemas.exceptions import (
NoValidSchema, InvalidSchemaProperty, InvalidSchemaValue,
InvalidSchemaProperty, InvalidSchemaValue,
)
from openapi_core.schema.schemas.models import Schema
from openapi_core.schema.servers.exceptions import InvalidServer
Expand Down Expand Up @@ -1213,7 +1214,7 @@ def test_post_tags_created_datetime(

assert parameters == {}
assert isinstance(body, BaseModel)
assert body.created == created
assert body.created == datetime(2016, 4, 16, 16, 6, 5)
assert body.name == pet_name

code = 400
Expand Down Expand Up @@ -1257,7 +1258,7 @@ def test_post_tags_created_invalid_type(
)

parameters = request.get_parameters(spec)
with pytest.raises(NoValidSchema):
with pytest.raises(InvalidMediaTypeValue):
request.get_body(spec)

assert parameters == {}
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/schema/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,33 @@ def test_number_int_invalid(self):
with pytest.raises(InvalidSchemaValue):
schema.unmarshal(value)

def test_schema_any_one_of(self):
schema = Schema(one_of=[
Schema('string'),
Schema('array', items=Schema('string')),
])
assert schema.unmarshal(['hello']) == ['hello']

def test_schema_any_one_of_mutiple(self):
schema = Schema(one_of=[
Schema('array', items=Schema('string')),
Schema('array', items=Schema('number')),
])
with pytest.raises(MultipleOneOfSchema):
schema.unmarshal([])

def test_schema_any_one_of_no_valid(self):
schema = Schema(one_of=[
Schema('array', items=Schema('string')),
Schema('array', items=Schema('number')),
])
with pytest.raises(NoOneOfSchema):
schema.unmarshal({})

def test_schema_any(self):
schema = Schema()
assert schema.unmarshal('string') == 'string'


class TestSchemaValidate(object):

Expand Down