1 changed files with 23 additions and 5 deletions
@ -1,28 +1,46 @@ |
|||||
|
from typing import Annotated |
||||
|
|
||||
|
import pytest |
||||
|
|
||||
from fastapi import FastAPI, Form |
from fastapi import FastAPI, Form |
||||
from fastapi.testclient import TestClient |
from fastapi.testclient import TestClient |
||||
|
|
||||
app = FastAPI() |
app = FastAPI() |
||||
|
|
||||
|
|
||||
@app.post("/") |
@app.post("/old-style", operation_id="old_style") |
||||
def route_with_form(form_param: str = Form(alias="aliased-field")): |
def route_with_form(form_param: str = Form(alias="aliased-field")): |
||||
return {} |
return {} |
||||
|
|
||||
|
|
||||
|
@app.post("/annotated", operation_id="annotated") |
||||
|
def route_with_form_annotated(form_param: Annotated[str, Form(alias="aliased-field")]): |
||||
|
return {} |
||||
|
|
||||
|
|
||||
client = TestClient(app) |
client = TestClient(app) |
||||
|
|
||||
|
|
||||
def test_get_route(): |
@pytest.mark.parametrize( |
||||
response = client.post("/", data={"aliased-field": "Hello, World!"}) |
"path", |
||||
|
["/old-style", "/annotated"] |
||||
|
) |
||||
|
def test_get_route(path: str): |
||||
|
response = client.post(path, data={"aliased-field": "Hello, World!"}) |
||||
assert response.status_code == 200, response.text |
assert response.status_code == 200, response.text |
||||
assert response.json() == {} |
assert response.json() == {} |
||||
|
|
||||
|
|
||||
def test_openapi(): |
@pytest.mark.parametrize( |
||||
|
"schema_obj", |
||||
|
["Body_annotated", "Body_old_style"] |
||||
|
) |
||||
|
def test_form_alias_is_correct(schema_obj: str): |
||||
response = client.get("/openapi.json") |
response = client.get("/openapi.json") |
||||
assert response.status_code == 200, response.text |
assert response.status_code == 200, response.text |
||||
|
print(response.json()["components"]["schemas"]) |
||||
form_properties = ( |
form_properties = ( |
||||
response.json() |
response.json() |
||||
["components"]["schemas"]["Body_route_with_form__post"]["properties"] |
["components"]["schemas"][schema_obj]["properties"] |
||||
) |
) |
||||
assert "aliased-field" in form_properties |
assert "aliased-field" in form_properties |
||||
|
Loading…
Reference in new issue