Drop support for "None" and "null" as null value

This commit is contained in:
Robbe Sneyders
2023-03-01 22:26:25 +01:00
parent 840517e8f8
commit 24a9fa3868
5 changed files with 12 additions and 26 deletions

View File

@@ -193,10 +193,7 @@ def is_nullable(param_def):
def is_null(value):
if hasattr(value, "strip") and value.strip() in ["null", "None"]:
return True
if value is None:
if value in [None, ""]:
return True
return False

View File

@@ -10,7 +10,6 @@ from starlette.datastructures import Headers, MutableHeaders
from starlette.types import Receive, Scope, Send
from connexion.exceptions import BadRequestProblem
from connexion.utils import is_null
class AbstractRequestBodyValidator:
@@ -137,7 +136,7 @@ class AbstractRequestBodyValidator:
# The body is parsed and validated
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)
# 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)
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)
while messages:

View File

@@ -12,7 +12,6 @@ from connexion.json_schema import (
Draft4ResponseValidator,
format_error_with_path,
)
from connexion.utils import is_null
from connexion.validators import (
AbstractRequestBodyValidator,
AbstractResponseBodyValidator,
@@ -50,12 +49,12 @@ class JSONRequestBodyValidator(AbstractRequestBodyValidator):
async def _parse(
self, stream: t.AsyncGenerator[bytes, None], scope: Scope
) -> t.Union[dict, str]:
) -> t.Any:
bytes_body = b"".join([message async for message in stream])
body = bytes_body.decode(self._encoding)
if self._nullable and is_null(body):
return body
if not body:
return None
try:
return json.loads(body)
@@ -116,7 +115,7 @@ class JSONResponseBodyValidator(AbstractResponseBodyValidator):
body = b"".join(stream).decode(self._encoding)
if not body:
return body
return None
try:
return json.loads(body)

View File

@@ -426,31 +426,22 @@ def test_array_in_path(simple_app):
def test_nullable_parameter(simple_app):
app_client = simple_app.test_client()
resp = app_client.get("/v1.0/nullable-parameters?time_start=null")
assert resp.json() == "it was None"
resp = app_client.get("/v1.0/nullable-parameters?time_start=None")
resp = app_client.get("/v1.0/nullable-parameters", params={"time_start": None})
assert resp.json() == "it was None"
time_start = 1010
resp = app_client.get(f"/v1.0/nullable-parameters?time_start={time_start}")
assert resp.json() == time_start
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"})
resp = app_client.post("/v1.0/nullable-parameters", data={"post_param": None})
assert resp.json() == "it was None"
headers = {"Content-Type": "application/json"}
resp = app_client.put("/v1.0/nullable-parameters", content="null", headers=headers)
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(
"/v1.0/nullable-parameters-noargs", content="None", headers=headers
"/v1.0/nullable-parameters-noargs", content="null", headers=headers
)
assert resp.json() == "hello"

View File

@@ -34,7 +34,7 @@ def test_get_missing_required_parameter():
def test_get_x_nullable_parameter():
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
@@ -44,7 +44,7 @@ def test_get_nullable_parameter():
"required": True,
"name": "foo",
}
result = ParameterValidator.validate_parameter("query", "null", param)
result = ParameterValidator.validate_parameter("query", None, param)
assert result is None