mirror of
https://github.com/correl/openapi-core.git
synced 2025-01-01 11:03:19 +00:00
Schema deprecated value
This commit is contained in:
parent
1ca11c9d19
commit
943ca126fc
3 changed files with 33 additions and 3 deletions
|
@ -29,5 +29,9 @@ class InvalidValueType(OpenAPIMappingError):
|
|||
pass
|
||||
|
||||
|
||||
class InvalidValue(OpenAPIMappingError):
|
||||
pass
|
||||
|
||||
|
||||
class UndefinedSchemaProperty(OpenAPIMappingError):
|
||||
pass
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
"""OpenAPI core schemas module"""
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
import warnings
|
||||
|
||||
from distutils.util import strtobool
|
||||
from functools import lru_cache
|
||||
|
||||
|
@ -9,6 +11,7 @@ from six import iteritems
|
|||
|
||||
from openapi_core.exceptions import (
|
||||
InvalidValueType, UndefinedSchemaProperty, MissingPropertyError,
|
||||
InvalidValue,
|
||||
)
|
||||
from openapi_core.models import ModelFactory
|
||||
|
||||
|
@ -26,7 +29,8 @@ class Schema(object):
|
|||
|
||||
def __init__(
|
||||
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.model = model
|
||||
self.properties = properties and dict(properties) or {}
|
||||
|
@ -35,6 +39,7 @@ class Schema(object):
|
|||
self.required = required
|
||||
self.default = default
|
||||
self.nullable = nullable
|
||||
self.enum = enum
|
||||
|
||||
def __getitem__(self, name):
|
||||
return self.properties[name]
|
||||
|
@ -77,6 +82,11 @@ class Schema(object):
|
|||
if casted is None and not self.required:
|
||||
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
|
||||
|
||||
def _unmarshal_collection(self, value):
|
||||
|
@ -138,6 +148,7 @@ class SchemaFactory(object):
|
|||
properties_spec = schema_deref.get('properties', None)
|
||||
items_spec = schema_deref.get('items', None)
|
||||
nullable = schema_deref.get('nullable', False)
|
||||
enum = schema_deref.get('enum', None)
|
||||
|
||||
properties = None
|
||||
if properties_spec:
|
||||
|
@ -149,7 +160,7 @@ class SchemaFactory(object):
|
|||
|
||||
return Schema(
|
||||
schema_type, model=model, properties=properties, items=items,
|
||||
required=required, nullable=nullable,
|
||||
required=required, nullable=nullable, enum=enum,
|
||||
)
|
||||
|
||||
@property
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import mock
|
||||
import pytest
|
||||
|
||||
from openapi_core.exceptions import InvalidValueType
|
||||
from openapi_core.exceptions import InvalidValueType, InvalidValue
|
||||
from openapi_core.schemas import Schema
|
||||
|
||||
|
||||
|
@ -63,6 +63,21 @@ class TestSchemaUnmarshal(object):
|
|||
|
||||
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):
|
||||
default_value = '123'
|
||||
schema = Schema('integer', default=default_value)
|
||||
|
|
Loading…
Reference in a new issue