mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-09 20:37:46 +00:00
Drop support for "None" and "null" as null value
This commit is contained in:
@@ -193,10 +193,7 @@ def is_nullable(param_def):
|
|||||||
|
|
||||||
|
|
||||||
def is_null(value):
|
def is_null(value):
|
||||||
if hasattr(value, "strip") and value.strip() in ["null", "None"]:
|
if value in [None, ""]:
|
||||||
return True
|
|
||||||
|
|
||||||
if value is None:
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from starlette.datastructures import Headers, MutableHeaders
|
|||||||
from starlette.types import Receive, Scope, Send
|
from starlette.types import Receive, Scope, Send
|
||||||
|
|
||||||
from connexion.exceptions import BadRequestProblem
|
from connexion.exceptions import BadRequestProblem
|
||||||
from connexion.utils import is_null
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractRequestBodyValidator:
|
class AbstractRequestBodyValidator:
|
||||||
@@ -137,7 +136,7 @@ class AbstractRequestBodyValidator:
|
|||||||
|
|
||||||
# The body is parsed and validated
|
# The body is parsed and validated
|
||||||
body = await self._parse(stream(), scope=scope)
|
body = await self._parse(stream(), scope=scope)
|
||||||
if not (self._nullable and is_null(body)):
|
if not (body is None and self._nullable):
|
||||||
self._validate(body)
|
self._validate(body)
|
||||||
|
|
||||||
# If MUTABLE_VALIDATION is enabled, include any changes made during validation in the messages to send
|
# If MUTABLE_VALIDATION is enabled, include any changes made during validation in the messages to send
|
||||||
@@ -197,7 +196,7 @@ class AbstractResponseBodyValidator:
|
|||||||
stream = (message.get("body", b"") for message in messages)
|
stream = (message.get("body", b"") for message in messages)
|
||||||
body = self._parse(stream)
|
body = self._parse(stream)
|
||||||
|
|
||||||
if body is not None and not (self._nullable and is_null(body)):
|
if not (body is None and self._nullable):
|
||||||
self._validate(body)
|
self._validate(body)
|
||||||
|
|
||||||
while messages:
|
while messages:
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ from connexion.json_schema import (
|
|||||||
Draft4ResponseValidator,
|
Draft4ResponseValidator,
|
||||||
format_error_with_path,
|
format_error_with_path,
|
||||||
)
|
)
|
||||||
from connexion.utils import is_null
|
|
||||||
from connexion.validators import (
|
from connexion.validators import (
|
||||||
AbstractRequestBodyValidator,
|
AbstractRequestBodyValidator,
|
||||||
AbstractResponseBodyValidator,
|
AbstractResponseBodyValidator,
|
||||||
@@ -50,12 +49,12 @@ class JSONRequestBodyValidator(AbstractRequestBodyValidator):
|
|||||||
|
|
||||||
async def _parse(
|
async def _parse(
|
||||||
self, stream: t.AsyncGenerator[bytes, None], scope: Scope
|
self, stream: t.AsyncGenerator[bytes, None], scope: Scope
|
||||||
) -> t.Union[dict, str]:
|
) -> t.Any:
|
||||||
bytes_body = b"".join([message async for message in stream])
|
bytes_body = b"".join([message async for message in stream])
|
||||||
body = bytes_body.decode(self._encoding)
|
body = bytes_body.decode(self._encoding)
|
||||||
|
|
||||||
if self._nullable and is_null(body):
|
if not body:
|
||||||
return body
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return json.loads(body)
|
return json.loads(body)
|
||||||
@@ -116,7 +115,7 @@ class JSONResponseBodyValidator(AbstractResponseBodyValidator):
|
|||||||
body = b"".join(stream).decode(self._encoding)
|
body = b"".join(stream).decode(self._encoding)
|
||||||
|
|
||||||
if not body:
|
if not body:
|
||||||
return body
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return json.loads(body)
|
return json.loads(body)
|
||||||
|
|||||||
@@ -426,31 +426,22 @@ def test_array_in_path(simple_app):
|
|||||||
|
|
||||||
def test_nullable_parameter(simple_app):
|
def test_nullable_parameter(simple_app):
|
||||||
app_client = simple_app.test_client()
|
app_client = simple_app.test_client()
|
||||||
resp = app_client.get("/v1.0/nullable-parameters?time_start=null")
|
resp = app_client.get("/v1.0/nullable-parameters", params={"time_start": None})
|
||||||
assert resp.json() == "it was None"
|
|
||||||
|
|
||||||
resp = app_client.get("/v1.0/nullable-parameters?time_start=None")
|
|
||||||
assert resp.json() == "it was None"
|
assert resp.json() == "it was None"
|
||||||
|
|
||||||
time_start = 1010
|
time_start = 1010
|
||||||
resp = app_client.get(f"/v1.0/nullable-parameters?time_start={time_start}")
|
resp = app_client.get(f"/v1.0/nullable-parameters?time_start={time_start}")
|
||||||
assert resp.json() == time_start
|
assert resp.json() == time_start
|
||||||
|
|
||||||
resp = app_client.post("/v1.0/nullable-parameters", data={"post_param": "None"})
|
resp = app_client.post("/v1.0/nullable-parameters", data={"post_param": None})
|
||||||
assert resp.json() == "it was None"
|
|
||||||
|
|
||||||
resp = app_client.post("/v1.0/nullable-parameters", data={"post_param": "null"})
|
|
||||||
assert resp.json() == "it was None"
|
assert resp.json() == "it was None"
|
||||||
|
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
resp = app_client.put("/v1.0/nullable-parameters", content="null", headers=headers)
|
resp = app_client.put("/v1.0/nullable-parameters", content="null", headers=headers)
|
||||||
assert resp.json() == "it was None"
|
assert resp.json() == "it was None"
|
||||||
|
|
||||||
resp = app_client.put("/v1.0/nullable-parameters", content="None", headers=headers)
|
|
||||||
assert resp.json() == "it was None"
|
|
||||||
|
|
||||||
resp = app_client.put(
|
resp = app_client.put(
|
||||||
"/v1.0/nullable-parameters-noargs", content="None", headers=headers
|
"/v1.0/nullable-parameters-noargs", content="null", headers=headers
|
||||||
)
|
)
|
||||||
assert resp.json() == "hello"
|
assert resp.json() == "hello"
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ def test_get_missing_required_parameter():
|
|||||||
|
|
||||||
def test_get_x_nullable_parameter():
|
def test_get_x_nullable_parameter():
|
||||||
param = {"type": "number", "required": True, "name": "foo", "x-nullable": True}
|
param = {"type": "number", "required": True, "name": "foo", "x-nullable": True}
|
||||||
result = ParameterValidator.validate_parameter("formdata", "None", param)
|
result = ParameterValidator.validate_parameter("formdata", None, param)
|
||||||
assert result is None
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ def test_get_nullable_parameter():
|
|||||||
"required": True,
|
"required": True,
|
||||||
"name": "foo",
|
"name": "foo",
|
||||||
}
|
}
|
||||||
result = ParameterValidator.validate_parameter("query", "null", param)
|
result = ParameterValidator.validate_parameter("query", None, param)
|
||||||
assert result is None
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user