mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-10 12:27:46 +00:00
Support x-nullable parameters
This commit is contained in:
@@ -6,7 +6,7 @@ import inspect
|
|||||||
import logging
|
import logging
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from ..utils import boolean
|
from ..utils import boolean, is_nullable, is_null
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -38,6 +38,9 @@ def make_type(value, type):
|
|||||||
|
|
||||||
|
|
||||||
def get_val_from_param(value, query_param):
|
def get_val_from_param(value, query_param):
|
||||||
|
if is_nullable(query_param) and is_null(value):
|
||||||
|
return None
|
||||||
|
|
||||||
if query_param["type"] == "array": # then logic is more complex
|
if query_param["type"] == "array": # then logic is more complex
|
||||||
if query_param.get("collectionFormat") and query_param.get("collectionFormat") == "pipes":
|
if query_param.get("collectionFormat") and query_param.get("collectionFormat") == "pipes":
|
||||||
parts = value.split("|")
|
parts = value.split("|")
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import sys
|
|||||||
from jsonschema import draft4_format_checker, validate, ValidationError
|
from jsonschema import draft4_format_checker, validate, ValidationError
|
||||||
|
|
||||||
from ..problem import problem
|
from ..problem import problem
|
||||||
from ..utils import boolean
|
from ..utils import boolean, is_nullable, is_null
|
||||||
|
|
||||||
logger = logging.getLogger('connexion.decorators.validation')
|
logger = logging.getLogger('connexion.decorators.validation')
|
||||||
|
|
||||||
@@ -155,6 +155,9 @@ class ParameterValidator(object):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def validate_parameter(parameter_type, value, param):
|
def validate_parameter(parameter_type, value, param):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
|
if is_nullable(param) and is_null(value):
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
converted_value = validate_type(param, value, parameter_type)
|
converted_value = validate_type(param, value, parameter_type)
|
||||||
except TypeValidationError as e:
|
except TypeValidationError as e:
|
||||||
|
|||||||
@@ -163,3 +163,17 @@ def boolean(s):
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
raise ValueError('Invalid boolean value')
|
raise ValueError('Invalid boolean value')
|
||||||
|
|
||||||
|
|
||||||
|
def is_nullable(param_def):
|
||||||
|
return param_def.get('x-nullable', False)
|
||||||
|
|
||||||
|
|
||||||
|
def is_null(value):
|
||||||
|
if hasattr(value, 'strip') and value.strip() in ['null', 'None']:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|||||||
@@ -181,3 +181,23 @@ def test_array_in_path(simple_app):
|
|||||||
|
|
||||||
resp = app_client.get('/v1.0/test-array-in-path/one_item,another_item')
|
resp = app_client.get('/v1.0/test-array-in-path/one_item,another_item')
|
||||||
assert json.loads(resp.data.decode()) == ["one_item", "another_item"]
|
assert json.loads(resp.data.decode()) == ["one_item", "another_item"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_nullable_parameter(simple_app):
|
||||||
|
app_client = simple_app.app.test_client()
|
||||||
|
resp = app_client.get('/v1.0/nullable-parameters?time_start=null')
|
||||||
|
assert json.loads(resp.data.decode()) == 'it was None'
|
||||||
|
|
||||||
|
resp = app_client.get('/v1.0/nullable-parameters?time_start=None')
|
||||||
|
assert json.loads(resp.data.decode()) == 'it was None'
|
||||||
|
|
||||||
|
time_start = 1010
|
||||||
|
resp = app_client.get(
|
||||||
|
'/v1.0/nullable-parameters?time_start={}'.format(time_start))
|
||||||
|
assert json.loads(resp.data.decode()) == time_start
|
||||||
|
|
||||||
|
resp = app_client.post('/v1.0/nullable-parameters', data={"post_param": 'None'})
|
||||||
|
assert json.loads(resp.data.decode()) == 'it was None'
|
||||||
|
|
||||||
|
resp = app_client.post('/v1.0/nullable-parameters', data={"post_param": 'null'})
|
||||||
|
assert json.loads(resp.data.decode()) == 'it was None'
|
||||||
|
|||||||
@@ -289,3 +289,15 @@ def test_array_in_path(names):
|
|||||||
|
|
||||||
def test_global_response_definition():
|
def test_global_response_definition():
|
||||||
return ['general', 'list'], 200
|
return ['general', 'list'], 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_nullable_parameters(time_start):
|
||||||
|
if time_start is None:
|
||||||
|
return 'it was None'
|
||||||
|
return time_start
|
||||||
|
|
||||||
|
|
||||||
|
def test_nullable_param_post(post_param):
|
||||||
|
if post_param is None:
|
||||||
|
return 'it was None'
|
||||||
|
return post_param
|
||||||
|
|||||||
34
tests/fixtures/simple/swagger.yaml
vendored
34
tests/fixtures/simple/swagger.yaml
vendored
@@ -496,6 +496,40 @@ paths:
|
|||||||
items:
|
items:
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
|
/nullable-parameters:
|
||||||
|
post:
|
||||||
|
operationId: fakeapi.hello.test_nullable_param_post
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
parameters:
|
||||||
|
- name: post_param
|
||||||
|
description: Just a testing parameter.
|
||||||
|
in: formData
|
||||||
|
type: number
|
||||||
|
format: int32
|
||||||
|
x-nullable: true
|
||||||
|
required: true
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: OK
|
||||||
|
|
||||||
|
|
||||||
|
get:
|
||||||
|
operationId: fakeapi.hello.test_nullable_parameters
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
parameters:
|
||||||
|
- name: time_start
|
||||||
|
description: Just a testing parameter.
|
||||||
|
in: query
|
||||||
|
type: integer
|
||||||
|
format: int32
|
||||||
|
x-nullable: true
|
||||||
|
required: true
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: OK
|
||||||
|
|
||||||
|
|
||||||
definitions:
|
definitions:
|
||||||
new_stack:
|
new_stack:
|
||||||
|
|||||||
Reference in New Issue
Block a user