fix: allow_inf_nan option for Param subclasses

There was a typo in the __init__ method of the Param class which
misnamed the allow_inf_nan option in the kwargs passed to the
underlying Pydantic FieldInfo initializer.  This resulted in the
validator failing to enforce the constraint, and hence inf and nan
values being passed to the route handler where they should not have.
This commit is contained in:
Pat Lasswell 2024-05-15 06:53:19 -07:00
parent a32902606e
commit c434bea96a
2 changed files with 59 additions and 1 deletions

View File

@ -91,7 +91,7 @@ class Param(FieldInfo):
max_length=max_length,
discriminator=discriminator,
multiple_of=multiple_of,
allow_nan=allow_inf_nan,
allow_inf_nan=allow_inf_nan,
max_digits=max_digits,
decimal_places=decimal_places,
**extra,

View File

@ -0,0 +1,58 @@
from typing import Optional
from fastapi import FastAPI
from fastapi.params import Query
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/")
def get(
x: Optional[float] = Query(default=0, allow_inf_nan=False),
y: Optional[float] = Query(default=0, allow_inf_nan=True),
z: Optional[float] = Query(default=0)) -> str: # type: ignore
return 'OK'
client = TestClient(app)
def test_allow_inf_nan_false():
response = client.get('/?x=inf')
assert response.status_code == 422, response.text
response = client.get('/?x=-inf')
assert response.status_code == 422, response.text
response = client.get('/?x=nan')
assert response.status_code == 422, response.text
response = client.get('/?x=0')
assert response.status_code == 200, response.text
def test_allow_inf_nan_true():
response = client.get('/?y=inf')
assert response.status_code == 200, response.text
response = client.get('/?y=-inf')
assert response.status_code == 200, response.text
response = client.get('/?y=nan')
assert response.status_code == 200, response.text
response = client.get('/?y=0')
assert response.status_code == 200, response.text
def test_allow_inf_nan_not_specified():
response = client.get('/?z=inf')
assert response.status_code == 200, response.text
response = client.get('/?z=-inf')
assert response.status_code == 200, response.text
response = client.get('/?z=nan')
assert response.status_code == 200, response.text
response = client.get('/?z=0')
assert response.status_code == 200, response.text