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 1 commit
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
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'
5 changes: 3 additions & 2 deletions flex/validation/common.py
Expand Up @@ -6,7 +6,7 @@
import collections
import itertools
import json

import os
import six


Expand Down Expand Up @@ -41,6 +41,7 @@
OBJECT,
DELIMETERS,
REQUEST_METHODS,
FLEX_DISABLE_X_NULLABLE,
)
from flex.decorators import (
skip_if_not_of_type,
Expand Down Expand Up @@ -297,7 +298,7 @@ def validate_enum(value, options, **kwargs):


def generate_enum_validator(enum, **kwargs):
if kwargs.get('x-nullable') is True:
if not os.environ.get(FLEX_DISABLE_X_NULLABLE) and kwargs.get('x-nullable') is True:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should probably be if FLEX_DISABLE_X_NULLABLE not in os.environ and ... for the case where it's set to an empty value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

enum.append(None)
return functools.partial(validate_enum, options=enum)

Expand Down
14 changes: 13 additions & 1 deletion tests/utils.py
Expand Up @@ -2,7 +2,8 @@
import collections
import re
import six

import contextlib
import os
from flex.validation.common import validate_object
from flex.loading.schema.paths.path_item.operation.responses.single.schema import (
schema_validator,
Expand Down Expand Up @@ -164,3 +165,14 @@ def generate_validator_from_schema(raw_schema, **kwargs):
schema = schema_validator(raw_schema, **kwargs)
validator = functools.partial(validate_object, schema=schema, **kwargs)
return validator


@contextlib.contextmanager
def set_env(**environ):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this to use the monkeypatch fixture provided by pytest. It automatically cleans up anything that you patch during test runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

old_environ = dict(os.environ)
os.environ.update(environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(old_environ)
82 changes: 81 additions & 1 deletion tests/validation/parameter/test_enum_validation.py
Expand Up @@ -12,10 +12,11 @@
STRING,
NUMBER,
BOOLEAN,
FLEX_DISABLE_X_NULLABLE
)
from flex.error_messages import MESSAGES

from tests.utils import assert_message_in_errors
from tests.utils import assert_message_in_errors, set_env


#
Expand Down Expand Up @@ -122,3 +123,82 @@ def test_nullable_enum_validation_with_allowed_values(enum, 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):

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 set_env(**{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',
)