Skip to content

Schema any type support #72

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions openapi_core/schema/schemas/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class SchemaType(Enum):

ANY = None
INTEGER = 'integer'
NUMBER = 'number'
STRING = 'string'
Expand Down
4 changes: 4 additions & 0 deletions openapi_core/schema/schemas/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class OpenAPISchemaError(OpenAPIMappingError):
pass


class NoValidSchema(OpenAPISchemaError):
pass


class InvalidSchemaValue(OpenAPISchemaError):
pass

Expand Down
2 changes: 1 addition & 1 deletion openapi_core/schema/schemas/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, dereferencer):
def create(self, schema_spec):
schema_deref = self.dereferencer.dereference(schema_spec)

schema_type = schema_deref.get('type', 'object')
schema_type = schema_deref.get('type')
schema_format = schema_deref.get('format')
model = schema_deref.get('x-model', None)
required = schema_deref.get('required', False)
Expand Down
43 changes: 38 additions & 5 deletions openapi_core/schema/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from openapi_core.schema.schemas.exceptions import (
InvalidSchemaValue, UndefinedSchemaProperty, MissingSchemaProperty,
OpenAPISchemaError, NoOneOfSchema, MultipleOneOfSchema,
NoValidSchema,
)
from openapi_core.schema.schemas.util import forcebool

Expand All @@ -30,7 +31,7 @@ def __init__(
schema_format=None, required=None, default=None, nullable=False,
enum=None, deprecated=False, all_of=None, one_of=None,
additional_properties=None):
self.type = schema_type and SchemaType(schema_type)
self.type = SchemaType(schema_type)
self.model = model
self.properties = properties and dict(properties) or {}
self.items = items
Expand All @@ -42,10 +43,12 @@ def __init__(
self.deprecated = deprecated
self.all_of = all_of and list(all_of) or []
self.one_of = one_of and list(one_of) or []
self.any_of = []
self.additional_properties = additional_properties

self._all_required_properties_cache = None
self._all_optional_properties_cache = None
self._schemas_permutations_cache = None

def __getitem__(self, name):
return self.properties[name]
Expand Down Expand Up @@ -76,7 +79,7 @@ def _get_all_required_properties(self):

return dict(
(prop_name, val)
for prop_name, val in all_properties.items()
for prop_name, val in iteritems(all_properties)
if prop_name in required
)

Expand All @@ -92,6 +95,7 @@ def get_all_required_properties_names(self):
def get_cast_mapping(self):
mapping = self.DEFAULT_CAST_CALLABLE_GETTER.copy()
mapping.update({
SchemaType.ANY: self._unmarshal_any,
SchemaType.ARRAY: self._unmarshal_collection,
SchemaType.OBJECT: self._unmarshal_object,
})
Expand All @@ -105,9 +109,6 @@ def cast(self, value):
raise InvalidSchemaValue("Null value for non-nullable schema")
return self.default

if self.type is None:
return value

cast_mapping = self.get_cast_mapping()

if self.type in cast_mapping and value == '':
Expand Down Expand Up @@ -139,6 +140,38 @@ def unmarshal(self, value):

return casted

def get_schemas_permutations(self):
if self._schemas_permutations_cache is None:
self._schemas_permutations_cache =\
self._get_schemas_permutations()

return self._schemas_permutations_cache

def _get_schemas_permutations(self):
if not self.one_of and not self.any_of:
return [self.all_of, ]

if self.one_of:
for one_of_schema in self.one_of:
if self.any_of:
for any_of_schema in self.any_of:
yield self.all_of + [one_of_schema, any_of_schema]
else:
yield self.all_of + [one_of_schema, ]
else:
for any_of_schema in self.any_of:
yield self.all_of + [any_of_schema, ]

def _unmarshal_any(self, value):
cast_mapping = self.get_cast_mapping()
for schema_type, cast_callable in iteritems(cast_mapping):
try:
return cast_callable(value)
except TypeError:
pass
raise NoValidSchema(
"No valid schema found for value {0}".format(value))

def _unmarshal_collection(self, value):
return list(map(self.items.unmarshal, value))

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/data/v3.0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ components:
rootCause:
type: string
additionalProperties:
type: string
description: Any type of information
responses:
ErrorResponse:
description: unexpected error
Expand Down