From 19e819d8a9c90e9909402def740cb8cad02e94da Mon Sep 17 00:00:00 2001 From: Stefans Mezulis Date: Sun, 17 Sep 2023 11:59:56 +0100 Subject: [PATCH 1/6] Copy alias to validation_alias --- fastapi/params.py | 6 ++++++ tests/test_openapi_form_alias.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/test_openapi_form_alias.py diff --git a/fastapi/params.py b/fastapi/params.py index b40944dba..5f5a5fe43 100644 --- a/fastapi/params.py +++ b/fastapi/params.py @@ -560,6 +560,12 @@ class Body(FieldInfo): ) current_json_schema_extra = json_schema_extra or extra if PYDANTIC_V2: + if serialization_alias in (_Unset, None) and isinstance(alias, str): + serialization_alias = alias + + if validation_alias in (_Unset, None): + validation_alias = alias + kwargs.update( { "annotation": annotation, diff --git a/tests/test_openapi_form_alias.py b/tests/test_openapi_form_alias.py new file mode 100644 index 000000000..8af2373c0 --- /dev/null +++ b/tests/test_openapi_form_alias.py @@ -0,0 +1,28 @@ +from fastapi import FastAPI, Form +from fastapi.testclient import TestClient + +app = FastAPI() + + +@app.post("/") +def route_with_form(form_param: str = Form(alias="aliased-field")): + return {} + + +client = TestClient(app) + + +def test_get_route(): + response = client.post("/", data={"aliased-field": "Hello, World!"}) + assert response.status_code == 200, response.text + assert response.json() == {} + + +def test_openapi(): + response = client.get("/openapi.json") + assert response.status_code == 200, response.text + form_properties = ( + response.json() + ["components"]["schemas"]["Body_route_with_form__post"]["properties"] + ) + assert "aliased-field" in form_properties From 9382f22468ef418f50bf7dd9f78763c6262ddae3 Mon Sep 17 00:00:00 2001 From: Stefans Mezulis Date: Sun, 17 Sep 2023 12:12:42 +0100 Subject: [PATCH 2/6] Also test annotated param --- tests/test_openapi_form_alias.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/test_openapi_form_alias.py b/tests/test_openapi_form_alias.py index 8af2373c0..bda32d360 100644 --- a/tests/test_openapi_form_alias.py +++ b/tests/test_openapi_form_alias.py @@ -1,28 +1,46 @@ +from typing import Annotated + +import pytest + from fastapi import FastAPI, Form from fastapi.testclient import TestClient app = FastAPI() -@app.post("/") +@app.post("/old-style", operation_id="old_style") def route_with_form(form_param: str = Form(alias="aliased-field")): return {} +@app.post("/annotated", operation_id="annotated") +def route_with_form_annotated(form_param: Annotated[str, Form(alias="aliased-field")]): + return {} + + client = TestClient(app) -def test_get_route(): - response = client.post("/", data={"aliased-field": "Hello, World!"}) +@pytest.mark.parametrize( + "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.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") assert response.status_code == 200, response.text + print(response.json()["components"]["schemas"]) form_properties = ( response.json() - ["components"]["schemas"]["Body_route_with_form__post"]["properties"] + ["components"]["schemas"][schema_obj]["properties"] ) assert "aliased-field" in form_properties From 9cc86f9f74d51cb19c2032702890fa92ecc6ec1e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 17 Sep 2023 11:17:55 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_openapi_form_alias.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/tests/test_openapi_form_alias.py b/tests/test_openapi_form_alias.py index bda32d360..c045c2abc 100644 --- a/tests/test_openapi_form_alias.py +++ b/tests/test_openapi_form_alias.py @@ -1,7 +1,6 @@ from typing import Annotated import pytest - from fastapi import FastAPI, Form from fastapi.testclient import TestClient @@ -21,26 +20,17 @@ def route_with_form_annotated(form_param: Annotated[str, Form(alias="aliased-fie client = TestClient(app) -@pytest.mark.parametrize( - "path", - ["/old-style", "/annotated"] -) +@pytest.mark.parametrize("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.json() == {} -@pytest.mark.parametrize( - "schema_obj", - ["Body_annotated", "Body_old_style"] -) +@pytest.mark.parametrize("schema_obj", ["Body_annotated", "Body_old_style"]) def test_form_alias_is_correct(schema_obj: str): response = client.get("/openapi.json") assert response.status_code == 200, response.text print(response.json()["components"]["schemas"]) - form_properties = ( - response.json() - ["components"]["schemas"][schema_obj]["properties"] - ) + form_properties = response.json()["components"]["schemas"][schema_obj]["properties"] assert "aliased-field" in form_properties From 5581a7db521c45d4453eb19f7dfcebbc5458972e Mon Sep 17 00:00:00 2001 From: Stefans Mezulis Date: Sun, 17 Sep 2023 12:20:11 +0100 Subject: [PATCH 4/6] Use typing_extensions for Python < 3.9 --- tests/test_openapi_form_alias.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_openapi_form_alias.py b/tests/test_openapi_form_alias.py index bda32d360..16f79fa42 100644 --- a/tests/test_openapi_form_alias.py +++ b/tests/test_openapi_form_alias.py @@ -1,4 +1,4 @@ -from typing import Annotated +from typing_extensions import Annotated import pytest From 516fe5adb3c81de64bc60ad8556b65b97a73b363 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 17 Sep 2023 11:21:45 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_openapi_form_alias.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_openapi_form_alias.py b/tests/test_openapi_form_alias.py index 887a756c8..4e9cf839a 100644 --- a/tests/test_openapi_form_alias.py +++ b/tests/test_openapi_form_alias.py @@ -1,8 +1,7 @@ -from typing_extensions import Annotated - import pytest from fastapi import FastAPI, Form from fastapi.testclient import TestClient +from typing_extensions import Annotated app = FastAPI() From f030bb2668a72f4064a61c387fcd659d33fe33f0 Mon Sep 17 00:00:00 2001 From: Stefans Mezulis Date: Sun, 17 Sep 2023 12:22:00 +0100 Subject: [PATCH 6/6] Remove unused print statement --- tests/test_openapi_form_alias.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_openapi_form_alias.py b/tests/test_openapi_form_alias.py index 887a756c8..dc74dab8f 100644 --- a/tests/test_openapi_form_alias.py +++ b/tests/test_openapi_form_alias.py @@ -31,6 +31,5 @@ def test_get_route(path: str): def test_form_alias_is_correct(schema_obj: str): response = client.get("/openapi.json") assert response.status_code == 200, response.text - print(response.json()["components"]["schemas"]) form_properties = response.json()["components"]["schemas"][schema_obj]["properties"] assert "aliased-field" in form_properties