Schema deprecated value

This commit is contained in:
Artur Maciag 2017-10-17 14:23:26 +01:00
parent 1ca11c9d19
commit 943ca126fc
3 changed files with 33 additions and 3 deletions

View file

@ -29,5 +29,9 @@ class InvalidValueType(OpenAPIMappingError):
pass pass
class InvalidValue(OpenAPIMappingError):
pass
class UndefinedSchemaProperty(OpenAPIMappingError): class UndefinedSchemaProperty(OpenAPIMappingError):
pass pass

View file

@ -1,6 +1,8 @@
"""OpenAPI core schemas module""" """OpenAPI core schemas module"""
import logging import logging
from collections import defaultdict from collections import defaultdict
import warnings
from distutils.util import strtobool from distutils.util import strtobool
from functools import lru_cache from functools import lru_cache
@ -9,6 +11,7 @@ from six import iteritems
from openapi_core.exceptions import ( from openapi_core.exceptions import (
InvalidValueType, UndefinedSchemaProperty, MissingPropertyError, InvalidValueType, UndefinedSchemaProperty, MissingPropertyError,
InvalidValue,
) )
from openapi_core.models import ModelFactory from openapi_core.models import ModelFactory
@ -26,7 +29,8 @@ class Schema(object):
def __init__( def __init__(
self, schema_type, model=None, properties=None, items=None, self, schema_type, model=None, properties=None, items=None,
spec_format=None, required=False, default=None, nullable=False): spec_format=None, required=False, default=None, nullable=False,
enum=None):
self.type = schema_type self.type = schema_type
self.model = model self.model = model
self.properties = properties and dict(properties) or {} self.properties = properties and dict(properties) or {}
@ -35,6 +39,7 @@ class Schema(object):
self.required = required self.required = required
self.default = default self.default = default
self.nullable = nullable self.nullable = nullable
self.enum = enum
def __getitem__(self, name): def __getitem__(self, name):
return self.properties[name] return self.properties[name]
@ -77,6 +82,11 @@ class Schema(object):
if casted is None and not self.required: if casted is None and not self.required:
return None return None
if self.enum and casted not in self.enum:
raise InvalidValue(
"Value of %s not in enum choices: %s", value, str(self.enum),
)
return casted return casted
def _unmarshal_collection(self, value): def _unmarshal_collection(self, value):
@ -138,6 +148,7 @@ class SchemaFactory(object):
properties_spec = schema_deref.get('properties', None) properties_spec = schema_deref.get('properties', None)
items_spec = schema_deref.get('items', None) items_spec = schema_deref.get('items', None)
nullable = schema_deref.get('nullable', False) nullable = schema_deref.get('nullable', False)
enum = schema_deref.get('enum', None)
properties = None properties = None
if properties_spec: if properties_spec:
@ -149,7 +160,7 @@ class SchemaFactory(object):
return Schema( return Schema(
schema_type, model=model, properties=properties, items=items, schema_type, model=model, properties=properties, items=items,
required=required, nullable=nullable, required=required, nullable=nullable, enum=enum,
) )
@property @property

View file

@ -1,7 +1,7 @@
import mock import mock
import pytest import pytest
from openapi_core.exceptions import InvalidValueType from openapi_core.exceptions import InvalidValueType, InvalidValue
from openapi_core.schemas import Schema from openapi_core.schemas import Schema
@ -63,6 +63,21 @@ class TestSchemaUnmarshal(object):
assert result == int(value) assert result == int(value)
def test_integer_enum_invalid(self):
schema = Schema('integer', enum=[1,2,3])
value = '123'
with pytest.raises(InvalidValue):
schema.unmarshal(value)
def test_integer_enum(self):
schema = Schema('integer', enum=[1,2,3])
value = '2'
result = schema.unmarshal(value)
assert result == int(value)
def test_integer_default(self): def test_integer_default(self):
default_value = '123' default_value = '123'
schema = Schema('integer', default=default_value) schema = Schema('integer', default=default_value)