Skip to content

Commit 943ca12

Browse files
committed
Schema deprecated value
1 parent 1ca11c9 commit 943ca12

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

openapi_core/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@ class InvalidValueType(OpenAPIMappingError):
2929
pass
3030

3131

32+
class InvalidValue(OpenAPIMappingError):
33+
pass
34+
35+
3236
class UndefinedSchemaProperty(OpenAPIMappingError):
3337
pass

openapi_core/schemas.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""OpenAPI core schemas module"""
22
import logging
33
from collections import defaultdict
4+
import warnings
5+
46
from distutils.util import strtobool
57
from functools import lru_cache
68

@@ -9,6 +11,7 @@
911

1012
from openapi_core.exceptions import (
1113
InvalidValueType, UndefinedSchemaProperty, MissingPropertyError,
14+
InvalidValue,
1215
)
1316
from openapi_core.models import ModelFactory
1417

@@ -26,7 +29,8 @@ class Schema(object):
2629

2730
def __init__(
2831
self, schema_type, model=None, properties=None, items=None,
29-
spec_format=None, required=False, default=None, nullable=False):
32+
spec_format=None, required=False, default=None, nullable=False,
33+
enum=None):
3034
self.type = schema_type
3135
self.model = model
3236
self.properties = properties and dict(properties) or {}
@@ -35,6 +39,7 @@ def __init__(
3539
self.required = required
3640
self.default = default
3741
self.nullable = nullable
42+
self.enum = enum
3843

3944
def __getitem__(self, name):
4045
return self.properties[name]
@@ -77,6 +82,11 @@ def unmarshal(self, value):
7782
if casted is None and not self.required:
7883
return None
7984

85+
if self.enum and casted not in self.enum:
86+
raise InvalidValue(
87+
"Value of %s not in enum choices: %s", value, str(self.enum),
88+
)
89+
8090
return casted
8191

8292
def _unmarshal_collection(self, value):
@@ -138,6 +148,7 @@ def create(self, schema_spec):
138148
properties_spec = schema_deref.get('properties', None)
139149
items_spec = schema_deref.get('items', None)
140150
nullable = schema_deref.get('nullable', False)
151+
enum = schema_deref.get('enum', None)
141152

142153
properties = None
143154
if properties_spec:
@@ -149,7 +160,7 @@ def create(self, schema_spec):
149160

150161
return Schema(
151162
schema_type, model=model, properties=properties, items=items,
152-
required=required, nullable=nullable,
163+
required=required, nullable=nullable, enum=enum,
153164
)
154165

155166
@property

tests/unit/test_schemas.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import mock
22
import pytest
33

4-
from openapi_core.exceptions import InvalidValueType
4+
from openapi_core.exceptions import InvalidValueType, InvalidValue
55
from openapi_core.schemas import Schema
66

77

@@ -63,6 +63,21 @@ def test_integer_valid(self):
6363

6464
assert result == int(value)
6565

66+
def test_integer_enum_invalid(self):
67+
schema = Schema('integer', enum=[1,2,3])
68+
value = '123'
69+
70+
with pytest.raises(InvalidValue):
71+
schema.unmarshal(value)
72+
73+
def test_integer_enum(self):
74+
schema = Schema('integer', enum=[1,2,3])
75+
value = '2'
76+
77+
result = schema.unmarshal(value)
78+
79+
assert result == int(value)
80+
6681
def test_integer_default(self):
6782
default_value = '123'
6883
schema = Schema('integer', default=default_value)

0 commit comments

Comments
 (0)