mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix `allow_inf_nan` option for Param and Body classes (#11867)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: svlandeg <svlandeg@github.com>
This commit is contained in:
parent
48b36f26d8
commit
51b625e127
|
|
@ -91,7 +91,7 @@ class Param(FieldInfo):
|
||||||
max_length=max_length,
|
max_length=max_length,
|
||||||
discriminator=discriminator,
|
discriminator=discriminator,
|
||||||
multiple_of=multiple_of,
|
multiple_of=multiple_of,
|
||||||
allow_nan=allow_inf_nan,
|
allow_inf_nan=allow_inf_nan,
|
||||||
max_digits=max_digits,
|
max_digits=max_digits,
|
||||||
decimal_places=decimal_places,
|
decimal_places=decimal_places,
|
||||||
**extra,
|
**extra,
|
||||||
|
|
@ -547,7 +547,7 @@ class Body(FieldInfo):
|
||||||
max_length=max_length,
|
max_length=max_length,
|
||||||
discriminator=discriminator,
|
discriminator=discriminator,
|
||||||
multiple_of=multiple_of,
|
multiple_of=multiple_of,
|
||||||
allow_nan=allow_inf_nan,
|
allow_inf_nan=allow_inf_nan,
|
||||||
max_digits=max_digits,
|
max_digits=max_digits,
|
||||||
decimal_places=decimal_places,
|
decimal_places=decimal_places,
|
||||||
**extra,
|
**extra,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
import pytest
|
||||||
|
from fastapi import Body, FastAPI, Query
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/")
|
||||||
|
async def get(
|
||||||
|
x: Annotated[float, Query(allow_inf_nan=True)] = 0,
|
||||||
|
y: Annotated[float, Query(allow_inf_nan=False)] = 0,
|
||||||
|
z: Annotated[float, Query()] = 0,
|
||||||
|
b: Annotated[float, Body(allow_inf_nan=False)] = 0,
|
||||||
|
) -> str:
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value,code",
|
||||||
|
[
|
||||||
|
("-1", 200),
|
||||||
|
("inf", 200),
|
||||||
|
("-inf", 200),
|
||||||
|
("nan", 200),
|
||||||
|
("0", 200),
|
||||||
|
("342", 200),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_allow_inf_nan_param_true(value: str, code: int):
|
||||||
|
response = client.post(f"/?x={value}")
|
||||||
|
assert response.status_code == code, response.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value,code",
|
||||||
|
[
|
||||||
|
("-1", 200),
|
||||||
|
("inf", 422),
|
||||||
|
("-inf", 422),
|
||||||
|
("nan", 422),
|
||||||
|
("0", 200),
|
||||||
|
("342", 200),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_allow_inf_nan_param_false(value: str, code: int):
|
||||||
|
response = client.post(f"/?y={value}")
|
||||||
|
assert response.status_code == code, response.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value,code",
|
||||||
|
[
|
||||||
|
("-1", 200),
|
||||||
|
("inf", 200),
|
||||||
|
("-inf", 200),
|
||||||
|
("nan", 200),
|
||||||
|
("0", 200),
|
||||||
|
("342", 200),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_allow_inf_nan_param_default(value: str, code: int):
|
||||||
|
response = client.post(f"/?z={value}")
|
||||||
|
assert response.status_code == code, response.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value,code",
|
||||||
|
[
|
||||||
|
("-1", 200),
|
||||||
|
("inf", 422),
|
||||||
|
("-inf", 422),
|
||||||
|
("nan", 422),
|
||||||
|
("0", 200),
|
||||||
|
("342", 200),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_allow_inf_nan_body(value: str, code: int):
|
||||||
|
response = client.post("/", json=value)
|
||||||
|
assert response.status_code == code, response.text
|
||||||
Loading…
Reference in New Issue