mirror of https://github.com/tiangolo/fastapi.git
add conditional to testing alias and validation_alias in PYDANTIC_V2 only
This commit is contained in:
parent
ddc04298d9
commit
da6d322e8f
Binary file not shown.
|
|
@ -1,4 +1,9 @@
|
||||||
|
"""
|
||||||
|
testing to fix incompatibility with alias and validation_alias between fastapi and PYDANTIC_V2
|
||||||
|
"""
|
||||||
|
|
||||||
from fastapi import FastAPI, Form
|
from fastapi import FastAPI, Form
|
||||||
|
from fastapi._compat import PYDANTIC_V2
|
||||||
from starlette.testclient import TestClient
|
from starlette.testclient import TestClient
|
||||||
|
|
||||||
app: FastAPI = FastAPI()
|
app: FastAPI = FastAPI()
|
||||||
|
|
@ -24,7 +29,11 @@ def test_get_alias():
|
||||||
|
|
||||||
|
|
||||||
def test_get_validation_alias():
|
def test_get_validation_alias():
|
||||||
response = client.post("/testing_validation_alias", data={"otherId": "1"})
|
if PYDANTIC_V2:
|
||||||
|
data = {"otherId": "1"}
|
||||||
|
else:
|
||||||
|
data = {"id_test": "1"}
|
||||||
|
response = client.post("/testing_validation_alias", data=data)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json() == {"other_id": 1}
|
assert response.json() == {"other_id": 1}
|
||||||
|
|
||||||
|
|
@ -32,18 +41,18 @@ def test_get_validation_alias():
|
||||||
def test_file_alias_schema():
|
def test_file_alias_schema():
|
||||||
response = client.get("/openapi.json")
|
response = client.get("/openapi.json")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
schema = response.json()
|
if PYDANTIC_V2:
|
||||||
|
schema = response.json()
|
||||||
|
assert (
|
||||||
|
"otherId"
|
||||||
|
in schema["components"]["schemas"]["Body_check_alias_testing_alias_post"][
|
||||||
|
"properties"
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
"otherId"
|
"otherId"
|
||||||
in schema["components"]["schemas"]["Body_check_alias_testing_alias_post"][
|
in schema["components"]["schemas"][
|
||||||
"properties"
|
"Body_check_validation_alias_testing_validation_alias_post"
|
||||||
]
|
]["properties"]
|
||||||
)
|
)
|
||||||
|
|
||||||
assert (
|
|
||||||
"otherId"
|
|
||||||
in schema["components"]["schemas"][
|
|
||||||
"Body_check_validation_alias_testing_validation_alias_post"
|
|
||||||
]["properties"]
|
|
||||||
)
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue