From 943ca126fc7e944c4106ebc6153b33a630ddec22 Mon Sep 17 00:00:00 2001 From: Artur Maciag Date: Tue, 17 Oct 2017 14:23:26 +0100 Subject: [PATCH] Schema deprecated value --- openapi_core/exceptions.py | 4 ++++ openapi_core/schemas.py | 15 +++++++++++++-- tests/unit/test_schemas.py | 17 ++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/openapi_core/exceptions.py b/openapi_core/exceptions.py index e734b53..5516483 100644 --- a/openapi_core/exceptions.py +++ b/openapi_core/exceptions.py @@ -29,5 +29,9 @@ class InvalidValueType(OpenAPIMappingError): pass +class InvalidValue(OpenAPIMappingError): + pass + + class UndefinedSchemaProperty(OpenAPIMappingError): pass diff --git a/openapi_core/schemas.py b/openapi_core/schemas.py index c5e81a7..0b3d31a 100644 --- a/openapi_core/schemas.py +++ b/openapi_core/schemas.py @@ -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 diff --git a/tests/unit/test_schemas.py b/tests/unit/test_schemas.py index 6e9804b..0dab30c 100644 --- a/tests/unit/test_schemas.py +++ b/tests/unit/test_schemas.py @@ -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)