Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle x nullable for enums #191

Merged
merged 4 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions flex/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@


class Empty(object):

def __cmp__(self, other):
raise TypeError('Empty cannot be compared to other values')

Expand Down Expand Up @@ -157,3 +158,6 @@ def __cmp__(self, other):
PATCH = 'patch'

REQUEST_METHODS = (GET, PUT, POST, DELETE, OPTIONS, HEAD, PATCH)

# Environment variables
FLEX_DISABLE_X_NULLABLE = 'FLEX_DISABLE_X_NULLABLE'
6 changes: 5 additions & 1 deletion flex/validation/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import itertools
import json

import os
import six


Expand Down Expand Up @@ -41,6 +42,7 @@
OBJECT,
DELIMETERS,
REQUEST_METHODS,
FLEX_DISABLE_X_NULLABLE,
)
from flex.decorators import (
skip_if_not_of_type,
Expand All @@ -67,7 +69,7 @@ def generate_type_validator(type_, **kwargs):
Generates a callable validator for the given type or iterable of types.
"""
if is_non_string_iterable(type_):
types = type_
types = tuple(type_)
else:
types = (type_,)
# support x-nullable since Swagger 2.0 doesn't support null type
Expand Down Expand Up @@ -297,6 +299,8 @@ def validate_enum(value, options, **kwargs):


def generate_enum_validator(enum, **kwargs):
if FLEX_DISABLE_X_NULLABLE not in os.environ and kwargs.get('x-nullable') is True:
enum.append(None)
return functools.partial(validate_enum, options=enum)


Expand Down
1 change: 0 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import collections
import re
import six

from flex.validation.common import validate_object
from flex.loading.schema.paths.path_item.operation.responses.single.schema import (
schema_validator,
Expand Down
119 changes: 116 additions & 3 deletions tests/validation/parameter/test_enum_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import os

from flex.exceptions import ValidationError
from flex.loading.schema.paths.path_item.operation.parameters import (
Expand All @@ -12,6 +13,7 @@
STRING,
NUMBER,
BOOLEAN,
FLEX_DISABLE_X_NULLABLE
)
from flex.error_messages import MESSAGES

Expand All @@ -27,22 +29,25 @@
([True, False], 0),
([True, False], 1),
([True, False], ''),
([True, False], None),
([0, 1, 2, 3], True),
([0, 1, 2, 3], False),
([0, 1, 2, 3], '1'),
([0, 1, 2, 3], 4),
([0, 1, 2, 3], 1.0),
([0, 1, 2, 3], None),
(['1', '2', 'a', 'b'], 'A'),
(['1', '2', 'a', 'b'], 1),
(['1', '2', 'a', 'b'], 2),
(['1', '2', 'a', 'b'], None),
),
)
def test_enum_validation_with_invalid_values(enum, value):
parameters = parameters_validator([
{
'name': 'id',
'in': PATH,
'description':'id',
'description': 'id',
'type': [STRING, NUMBER, BOOLEAN],
'required': True,
'enum': enum,
Expand All @@ -62,7 +67,6 @@ def test_enum_validation_with_invalid_values(enum, value):
)



@pytest.mark.parametrize(
'enum,value',
(
Expand All @@ -79,7 +83,7 @@ def test_enum_validation_with_allowed_values(enum, value):
{
'name': 'id',
'in': PATH,
'description':'id',
'description': 'id',
'type': [STRING, NUMBER, BOOLEAN],
'required': True,
'enum': enum,
Expand All @@ -90,3 +94,112 @@ def test_enum_validation_with_allowed_values(enum, value):
}

validate_parameters(parameter_values, parameters, {})


@pytest.mark.parametrize(
'enum,value',
(
([True, False], True),
([True, False], None),
([0, 1, 2, 3], 1),
([0, 1, 2, 3], None),
(['1', '2', 'a', 'b'], 'a'),
(['1', '2', 'a', 'b'], None),
),
)
def test_nullable_enum_validation_with_allowed_values(enum, value):
parameters = parameters_validator([
{
'name': 'id',
'in': PATH,
'description': 'id',
'type': [STRING, NUMBER, BOOLEAN],
'required': True,
'enum': enum,
'x-nullable': True
},
])
parameter_values = {
'id': value,
}

validate_parameters(parameter_values, parameters, {})


@pytest.mark.parametrize(
'enum,value',
(
([True, False], None),
([0, 1, 2, 3], None),
(['1', '2', 'a', 'b'], None),
),
)
def test_nullable_enum_with_null_values_strict(enum, value, monkeypatch):

parameters = parameters_validator([
{
'name': 'id',
'in': PATH,
'description': 'id',
'type': [STRING, NUMBER, BOOLEAN],
'required': True,
'enum': enum,
'x-nullable': True
},
])
parameter_values = {
'id': value,
}

monkeypatch.setattr(os, 'environ', {FLEX_DISABLE_X_NULLABLE: '1'})
with pytest.raises(ValidationError) as err:
validate_parameters(parameter_values, parameters, {})

assert_message_in_errors(
MESSAGES['enum']['invalid'],
err.value.detail,
'id.enum',
)


@pytest.mark.parametrize(
'enum,value',
(
([True, False], 0),
([True, False], 1),
([True, False], ''),
([0, 1, 2, 3], True),
([0, 1, 2, 3], False),
([0, 1, 2, 3], '1'),
([0, 1, 2, 3], 4),
([0, 1, 2, 3], 1.0),
(['1', '2', 'a', 'b'], 'A'),
(['1', '2', 'a', 'b'], 1),
(['1', '2', 'a', 'b'], 2),
),
)
def test_nullable_enum_with_invalid_values(enum, value):

parameters = parameters_validator([
{
'name': 'id',
'in': PATH,
'description': 'id',
'type': [STRING, NUMBER, BOOLEAN],
'required': True,
'enum': enum,
'x-nullable': True
},
])
parameter_values = {
'id': value,
}

with pytest.raises(ValidationError) as err:
validate_parameters(parameter_values, parameters, {})

assert_message_in_errors(
MESSAGES['enum']['invalid'],
err.value.detail,
'id.enum',
)