mirror of
https://github.com/correl/openapi-core.git
synced 2025-01-04 03:00:15 +00:00
Object additionalProperties support
* Default is true like in specification * When is set false, it works like in past * Object with types works
This commit is contained in:
parent
395f68b234
commit
83b9c37915
5 changed files with 40 additions and 15 deletions
|
@ -6,7 +6,7 @@ from openapi_core.shortcuts import (
|
||||||
|
|
||||||
__author__ = 'Artur Maciag'
|
__author__ = 'Artur Maciag'
|
||||||
__email__ = 'maciag.artur@gmail.com'
|
__email__ = 'maciag.artur@gmail.com'
|
||||||
__version__ = '0.8.0'
|
__version__ = '0.8.1'
|
||||||
__url__ = 'https://github.com/p1c2u/openapi-core'
|
__url__ = 'https://github.com/p1c2u/openapi-core'
|
||||||
__license__ = 'BSD 3-Clause License'
|
__license__ = 'BSD 3-Clause License'
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,8 @@ class SchemaFactory(object):
|
||||||
deprecated = schema_deref.get('deprecated', False)
|
deprecated = schema_deref.get('deprecated', False)
|
||||||
all_of_spec = schema_deref.get('allOf', None)
|
all_of_spec = schema_deref.get('allOf', None)
|
||||||
one_of_spec = schema_deref.get('oneOf', None)
|
one_of_spec = schema_deref.get('oneOf', None)
|
||||||
additional_properties_spec = schema_deref.get('additionalProperties')
|
additional_properties_spec = schema_deref.get('additionalProperties',
|
||||||
|
True)
|
||||||
min_items = schema_deref.get('minItems', None)
|
min_items = schema_deref.get('minItems', None)
|
||||||
max_items = schema_deref.get('maxItems', None)
|
max_items = schema_deref.get('maxItems', None)
|
||||||
min_length = schema_deref.get('minLength', None)
|
min_length = schema_deref.get('minLength', None)
|
||||||
|
@ -59,8 +60,8 @@ class SchemaFactory(object):
|
||||||
if items_spec:
|
if items_spec:
|
||||||
items = self._create_items(items_spec)
|
items = self._create_items(items_spec)
|
||||||
|
|
||||||
additional_properties = None
|
additional_properties = additional_properties_spec
|
||||||
if additional_properties_spec:
|
if isinstance(additional_properties_spec, dict):
|
||||||
additional_properties = self.create(additional_properties_spec)
|
additional_properties = self.create(additional_properties_spec)
|
||||||
|
|
||||||
return Schema(
|
return Schema(
|
||||||
|
|
|
@ -68,7 +68,7 @@ class Schema(object):
|
||||||
self, schema_type=None, model=None, properties=None, items=None,
|
self, schema_type=None, model=None, properties=None, items=None,
|
||||||
schema_format=None, required=None, default=None, nullable=False,
|
schema_format=None, required=None, default=None, nullable=False,
|
||||||
enum=None, deprecated=False, all_of=None, one_of=None,
|
enum=None, deprecated=False, all_of=None, one_of=None,
|
||||||
additional_properties=None, min_items=None, max_items=None,
|
additional_properties=True, min_items=None, max_items=None,
|
||||||
min_length=None, max_length=None, pattern=None, unique_items=False,
|
min_length=None, max_length=None, pattern=None, unique_items=False,
|
||||||
minimum=None, maximum=None, multiple_of=None,
|
minimum=None, maximum=None, multiple_of=None,
|
||||||
exclusive_minimum=False, exclusive_maximum=False,
|
exclusive_minimum=False, exclusive_maximum=False,
|
||||||
|
@ -284,14 +284,15 @@ class Schema(object):
|
||||||
|
|
||||||
value_props_names = value.keys()
|
value_props_names = value.keys()
|
||||||
extra_props = set(value_props_names) - set(all_props_names)
|
extra_props = set(value_props_names) - set(all_props_names)
|
||||||
if extra_props and self.additional_properties is None:
|
if extra_props and self.additional_properties is False:
|
||||||
raise UndefinedSchemaProperty(extra_props)
|
raise UndefinedSchemaProperty(extra_props)
|
||||||
|
|
||||||
properties = {}
|
properties = {}
|
||||||
for prop_name in extra_props:
|
if self.additional_properties is not True:
|
||||||
prop_value = value[prop_name]
|
for prop_name in extra_props:
|
||||||
properties[prop_name] = self.additional_properties.unmarshal(
|
prop_value = value[prop_name]
|
||||||
prop_value, custom_formatters=custom_formatters)
|
properties[prop_name] = self.additional_properties.unmarshal(
|
||||||
|
prop_value, custom_formatters=custom_formatters)
|
||||||
|
|
||||||
for prop_name, prop in iteritems(all_props):
|
for prop_name, prop in iteritems(all_props):
|
||||||
try:
|
try:
|
||||||
|
@ -515,13 +516,14 @@ class Schema(object):
|
||||||
|
|
||||||
value_props_names = value.keys()
|
value_props_names = value.keys()
|
||||||
extra_props = set(value_props_names) - set(all_props_names)
|
extra_props = set(value_props_names) - set(all_props_names)
|
||||||
if extra_props and self.additional_properties is None:
|
if extra_props and self.additional_properties is False:
|
||||||
raise UndefinedSchemaProperty(extra_props)
|
raise UndefinedSchemaProperty(extra_props)
|
||||||
|
|
||||||
for prop_name in extra_props:
|
if self.additional_properties is not True:
|
||||||
prop_value = value[prop_name]
|
for prop_name in extra_props:
|
||||||
self.additional_properties.validate(
|
prop_value = value[prop_name]
|
||||||
prop_value, custom_formatters=custom_formatters)
|
self.additional_properties.validate(
|
||||||
|
prop_value, custom_formatters=custom_formatters)
|
||||||
|
|
||||||
for prop_name, prop in iteritems(all_props):
|
for prop_name, prop in iteritems(all_props):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -295,6 +295,7 @@ components:
|
||||||
$ref: "#/components/schemas/Utctime"
|
$ref: "#/components/schemas/Utctime"
|
||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
|
additionalProperties: false
|
||||||
TagList:
|
TagList:
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
|
|
|
@ -7,6 +7,7 @@ import pytest
|
||||||
from openapi_core.extensions.models.models import Model
|
from openapi_core.extensions.models.models import Model
|
||||||
from openapi_core.schema.schemas.exceptions import (
|
from openapi_core.schema.schemas.exceptions import (
|
||||||
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema, OpenAPISchemaError,
|
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema, OpenAPISchemaError,
|
||||||
|
UndefinedSchemaProperty
|
||||||
)
|
)
|
||||||
from openapi_core.schema.schemas.models import Schema
|
from openapi_core.schema.schemas.models import Schema
|
||||||
|
|
||||||
|
@ -697,6 +698,26 @@ class TestSchemaValidate(object):
|
||||||
|
|
||||||
assert result == value
|
assert result == value
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('value', [Model({'additional': 1}), ])
|
||||||
|
def test_object_additional_propetries(self, value):
|
||||||
|
schema = Schema('object')
|
||||||
|
|
||||||
|
schema.validate(value)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('value', [Model({'additional': 1}), ])
|
||||||
|
def test_object_additional_propetries_false(self, value):
|
||||||
|
schema = Schema('object', additional_properties=False)
|
||||||
|
|
||||||
|
with pytest.raises(UndefinedSchemaProperty):
|
||||||
|
schema.validate(value)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('value', [Model({'additional': 1}), ])
|
||||||
|
def test_object_additional_propetries_object(self, value):
|
||||||
|
additional_properties = Schema('integer')
|
||||||
|
schema = Schema('object', additional_properties=additional_properties)
|
||||||
|
|
||||||
|
schema.validate(value)
|
||||||
|
|
||||||
@pytest.mark.parametrize('value', [[], ])
|
@pytest.mark.parametrize('value', [[], ])
|
||||||
def test_list_min_items_invalid_schema(self, value):
|
def test_list_min_items_invalid_schema(self, value):
|
||||||
schema = Schema(
|
schema = Schema(
|
||||||
|
|
Loading…
Reference in a new issue