|
|
|
@ -1,4 +1,4 @@ |
|
|
|
from typing import Annotated, Any, Union |
|
|
|
from typing import Annotated, Any |
|
|
|
from unittest.mock import Mock, patch |
|
|
|
|
|
|
|
import pytest |
|
|
|
@ -22,10 +22,10 @@ def convert(v: Any) -> Any: |
|
|
|
|
|
|
|
@app.post("/nullable-required") |
|
|
|
async def read_nullable_required( |
|
|
|
int_val: Annotated[Union[int, None], Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
str_val: Annotated[Union[str, None], Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
int_val: Annotated[int | None, Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
str_val: Annotated[str | None, Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
list_val: Annotated[ |
|
|
|
Union[list[int], None], |
|
|
|
list[int] | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
], |
|
|
|
@ -39,9 +39,9 @@ async def read_nullable_required( |
|
|
|
|
|
|
|
|
|
|
|
class ModelNullableRequired(BaseModel): |
|
|
|
int_val: Union[int, None] |
|
|
|
str_val: Union[str, None] |
|
|
|
list_val: Union[list[int], None] |
|
|
|
int_val: int | None |
|
|
|
str_val: str | None |
|
|
|
list_val: list[int] | None |
|
|
|
|
|
|
|
@field_validator("*", mode="before") |
|
|
|
def validate_all(cls, v): |
|
|
|
@ -60,14 +60,14 @@ async def read_model_nullable_required(params: ModelNullableRequired): |
|
|
|
|
|
|
|
@app.post("/nullable-required-str") |
|
|
|
async def read_nullable_required_no_embed_str( |
|
|
|
str_val: Annotated[Union[str, None], Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
str_val: Annotated[str | None, Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
): |
|
|
|
return {"val": str_val} |
|
|
|
|
|
|
|
|
|
|
|
@app.post("/nullable-required-int") |
|
|
|
async def read_nullable_required_no_embed_int( |
|
|
|
int_val: Annotated[Union[int, None], Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
int_val: Annotated[int | None, Body(), BeforeValidator(lambda v: convert(v))], |
|
|
|
): |
|
|
|
return {"val": int_val} |
|
|
|
|
|
|
|
@ -75,7 +75,7 @@ async def read_nullable_required_no_embed_int( |
|
|
|
@app.post("/nullable-required-list") |
|
|
|
async def read_nullable_required_no_embed_list( |
|
|
|
list_val: Annotated[ |
|
|
|
Union[list[int], None], Body(), BeforeValidator(lambda v: convert(v)) |
|
|
|
list[int] | None, Body(), BeforeValidator(lambda v: convert(v)) |
|
|
|
], |
|
|
|
): |
|
|
|
return {"val": list_val} |
|
|
|
@ -398,17 +398,17 @@ def test_nullable_required_no_embed_pass_value(path: str, value: Any): |
|
|
|
@app.post("/nullable-non-required") |
|
|
|
async def read_nullable_non_required( |
|
|
|
int_val: Annotated[ |
|
|
|
Union[int, None], |
|
|
|
int | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = None, |
|
|
|
str_val: Annotated[ |
|
|
|
Union[str, None], |
|
|
|
str | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = None, |
|
|
|
list_val: Annotated[ |
|
|
|
Union[list[int], None], |
|
|
|
list[int] | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = None, |
|
|
|
@ -422,9 +422,9 @@ async def read_nullable_non_required( |
|
|
|
|
|
|
|
|
|
|
|
class ModelNullableNonRequired(BaseModel): |
|
|
|
int_val: Union[int, None] = None |
|
|
|
str_val: Union[str, None] = None |
|
|
|
list_val: Union[list[int], None] = None |
|
|
|
int_val: int | None = None |
|
|
|
str_val: str | None = None |
|
|
|
list_val: list[int] | None = None |
|
|
|
|
|
|
|
@field_validator("*", mode="before") |
|
|
|
def validate_all(cls, v): |
|
|
|
@ -446,7 +446,7 @@ async def read_model_nullable_non_required( |
|
|
|
@app.post("/nullable-non-required-str") |
|
|
|
async def read_nullable_non_required_no_embed_str( |
|
|
|
str_val: Annotated[ |
|
|
|
Union[str, None], |
|
|
|
str | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = None, |
|
|
|
@ -457,7 +457,7 @@ async def read_nullable_non_required_no_embed_str( |
|
|
|
@app.post("/nullable-non-required-int") |
|
|
|
async def read_nullable_non_required_no_embed_int( |
|
|
|
int_val: Annotated[ |
|
|
|
Union[int, None], |
|
|
|
int | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = None, |
|
|
|
@ -468,7 +468,7 @@ async def read_nullable_non_required_no_embed_int( |
|
|
|
@app.post("/nullable-non-required-list") |
|
|
|
async def read_nullable_non_required_no_embed_list( |
|
|
|
list_val: Annotated[ |
|
|
|
Union[list[int], None], |
|
|
|
list[int] | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = None, |
|
|
|
@ -746,17 +746,17 @@ def test_nullable_non_required_no_embed_pass_value(path: str, value: Any): |
|
|
|
async def read_nullable_with_non_null_default( |
|
|
|
*, |
|
|
|
int_val: Annotated[ |
|
|
|
Union[int, None], |
|
|
|
int | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = -1, |
|
|
|
str_val: Annotated[ |
|
|
|
Union[str, None], |
|
|
|
str | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = "default", |
|
|
|
list_val: Annotated[ |
|
|
|
Union[list[int], None], |
|
|
|
list[int] | None, |
|
|
|
Body(default_factory=lambda: [0]), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
], |
|
|
|
@ -770,9 +770,9 @@ async def read_nullable_with_non_null_default( |
|
|
|
|
|
|
|
|
|
|
|
class ModelNullableWithNonNullDefault(BaseModel): |
|
|
|
int_val: Union[int, None] = -1 |
|
|
|
str_val: Union[str, None] = "default" |
|
|
|
list_val: Union[list[int], None] = [0] |
|
|
|
int_val: int | None = -1 |
|
|
|
str_val: str | None = "default" |
|
|
|
list_val: list[int] | None = [0] |
|
|
|
|
|
|
|
@field_validator("*", mode="before") |
|
|
|
def validate_all(cls, v): |
|
|
|
@ -794,7 +794,7 @@ async def read_model_nullable_with_non_null_default( |
|
|
|
@app.post("/nullable-with-non-null-default-str") |
|
|
|
async def read_nullable_with_non_null_default_no_embed_str( |
|
|
|
str_val: Annotated[ |
|
|
|
Union[str, None], |
|
|
|
str | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = "default", |
|
|
|
@ -805,7 +805,7 @@ async def read_nullable_with_non_null_default_no_embed_str( |
|
|
|
@app.post("/nullable-with-non-null-default-int") |
|
|
|
async def read_nullable_with_non_null_default_no_embed_int( |
|
|
|
int_val: Annotated[ |
|
|
|
Union[int, None], |
|
|
|
int | None, |
|
|
|
Body(), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
] = -1, |
|
|
|
@ -816,7 +816,7 @@ async def read_nullable_with_non_null_default_no_embed_int( |
|
|
|
@app.post("/nullable-with-non-null-default-list") |
|
|
|
async def read_nullable_with_non_null_default_no_embed_list( |
|
|
|
list_val: Annotated[ |
|
|
|
Union[list[int], None], |
|
|
|
list[int] | None, |
|
|
|
Body(default_factory=lambda: [0]), |
|
|
|
BeforeValidator(lambda v: convert(v)), |
|
|
|
], |
|
|
|
|