This commit is contained in:
JONEMI21 2025-11-10 10:29:45 +00:00
parent 03d445ad5e
commit 1121414bf2
3 changed files with 20 additions and 7 deletions

View File

@ -510,10 +510,13 @@ if shared.PYDANTIC_VERSION_MINOR_TUPLE >= (2, 6):
def omit_by_default(field_info: FieldInfo) -> FieldInfo:
"""Set omit by default on a FieldInfo's annotation."""
new_annotation = _omit_by_default(field_info.annotation)
new_field_info = copy_field_info(field_info=field_info, annotation=new_annotation)
new_field_info = copy_field_info(
field_info=field_info, annotation=new_annotation
)
return new_field_info
else:
def ignore_invalid(v: Any, handler: Callable[[Any], Any]) -> Any:
try:
return handler(v)
@ -531,7 +534,12 @@ else:
# Handle nested list validation errors (e.g., dict[str, list[str]])
elif isinstance(loc[0], str) and isinstance(v, dict):
key = loc[0]
if len(loc) > 1 and isinstance(loc[1], int) and key in v and isinstance(v[key], list):
if (
len(loc) > 1
and isinstance(loc[1], int)
and key in v
and isinstance(v[key], list)
):
list_index = loc[1]
v[key][list_index] = None
elif key in v:
@ -549,5 +557,7 @@ else:
def omit_by_default(field_info: FieldInfo) -> FieldInfo:
"""add a wrap validator to omit invalid values by default."""
field_info.metadata = field_info.metadata or [] + [WrapValidator(ignore_invalid)]
field_info.metadata = field_info.metadata or [] + [
WrapValidator(ignore_invalid)
]
return field_info

View File

@ -1,5 +1,5 @@
import http
from typing import Dict, FrozenSet, List, Optional, Union
from typing import Dict, FrozenSet, List, Optional
from fastapi import FastAPI, Path, Query

View File

@ -55,7 +55,7 @@ def test_enum_status_code_response():
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json()["paths"] == {
assert response.json() == {
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"externalDocs": {
@ -986,7 +986,10 @@ def test_openapi_schema():
"required": False,
"schema": IsDict(
{
"anyOf": [{"type": "integer"}, {"type": "null"}],
"anyOf": [
{"type": "integer"},
{"type": "null"},
],
"title": "Query",
}
)
@ -1520,4 +1523,4 @@ def test_openapi_schema():
},
}
},
}["paths"]
}