Skip to content

Commit 9f01a88

Browse files
committed
BooleanCaster consistent boolean values fix
1 parent 5d0a7d0 commit 9f01a88

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

openapi_core/casting/schemas/casters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from openapi_core.casting.schemas.exceptions import CastError
1313
from openapi_core.schema.schemas import get_properties
14+
from openapi_core.util import BOOLEAN_FALSE_VALUES
15+
from openapi_core.util import BOOLEAN_TRUE_VALUES
1416
from openapi_core.util import forcebool
1517
from openapi_core.validation.schemas.validators import SchemaValidator
1618

@@ -65,7 +67,7 @@ def validate(self, value: Any) -> None:
6567
if isinstance(value, bool):
6668
return
6769

68-
if value.lower() not in ["false", "true"]:
70+
if value.lower() not in BOOLEAN_TRUE_VALUES + BOOLEAN_FALSE_VALUES:
6971
raise ValueError("not a boolean format")
7072

7173
def cast(self, value: Union[str, bytes]) -> bool:

openapi_core/util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
from typing import Any
55
from typing import Iterable
66

7+
BOOLEAN_TRUE_VALUES = ("y", "yes", "t", "true", "on", "1")
8+
BOOLEAN_FALSE_VALUES = ("n", "no", "f", "false", "off", "0")
9+
710

811
def forcebool(val: Any) -> bool:
912
if isinstance(val, str):
1013
val = val.lower()
11-
if val in ("y", "yes", "t", "true", "on", "1"):
14+
if val in BOOLEAN_TRUE_VALUES:
1215
return True
13-
elif val in ("n", "no", "f", "false", "off", "0"):
16+
elif val in BOOLEAN_FALSE_VALUES:
1417
return False
1518
else:
1619
raise ValueError(f"invalid truth value {val!r}")

tests/integration/data/v3.0/petstore.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ paths:
286286
operationId: deleteTag
287287
tags:
288288
- tags
289+
parameters:
290+
- name: x-delete-force
291+
in: header
292+
schema:
293+
type: boolean
294+
required: false
289295
requestBody:
290296
required: false
291297
content:

tests/integration/test_petstore.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,51 @@ def test_delete_tags_no_requestbody(self, spec):
20912091

20922092
assert result.body is None
20932093

2094+
@pytest.mark.parametrize(
2095+
"header_value,expexted_value",
2096+
[
2097+
("y", True),
2098+
("t", True),
2099+
("yes", True),
2100+
("on", True),
2101+
("true", True),
2102+
("1", True),
2103+
("n", False),
2104+
("f", False),
2105+
("no", False),
2106+
("off", False),
2107+
("false", False),
2108+
("0", False),
2109+
],
2110+
)
2111+
def test_delete_tags_header(self, spec, header_value, expexted_value):
2112+
host_url = "http://petstore.swagger.io/v1"
2113+
path_pattern = "/v1/tags"
2114+
headers = {
2115+
"x-delete-force": header_value,
2116+
}
2117+
request = MockRequest(
2118+
host_url,
2119+
"DELETE",
2120+
"/tags",
2121+
headers=headers,
2122+
path_pattern=path_pattern,
2123+
)
2124+
2125+
validate_request(request, spec=spec)
2126+
2127+
result = unmarshal_request(
2128+
request,
2129+
spec=spec,
2130+
cls=V30RequestParametersUnmarshaller,
2131+
)
2132+
2133+
assert result.parameters == Parameters(
2134+
header={
2135+
"x-delete-force": expexted_value,
2136+
},
2137+
)
2138+
20942139
def test_delete_tags_raises_missing_required_response_header(
20952140
self, spec, response_unmarshaller
20962141
):

0 commit comments

Comments
 (0)