Browse Source

fixed bug introduced in 0.124.1

pull/14805/head
Lukas Erlbacher 5 months ago
parent
commit
2b549d99b6
  1. 4
      fastapi/_compat/v2.py
  2. 88
      tests/test_union_single_root_model_openapi.py

4
fastapi/_compat/v2.py

@ -22,7 +22,7 @@ from pydantic import ValidationError as ValidationError
from pydantic._internal._schema_generation_shared import ( # type: ignore[attr-defined] from pydantic._internal._schema_generation_shared import ( # type: ignore[attr-defined]
GetJsonSchemaHandler as GetJsonSchemaHandler, GetJsonSchemaHandler as GetJsonSchemaHandler,
) )
from pydantic._internal._typing_extra import eval_type_lenient from pydantic._internal._typing_extra import annotated_type, eval_type_lenient
from pydantic._internal._utils import lenient_issubclass as lenient_issubclass from pydantic._internal._utils import lenient_issubclass as lenient_issubclass
from pydantic.fields import FieldInfo as FieldInfo from pydantic.fields import FieldInfo as FieldInfo
from pydantic.json_schema import GenerateJsonSchema as GenerateJsonSchema from pydantic.json_schema import GenerateJsonSchema as GenerateJsonSchema
@ -460,7 +460,7 @@ def create_body_model(
def get_model_fields(model: type[BaseModel]) -> list[ModelField]: def get_model_fields(model: type[BaseModel]) -> list[ModelField]:
model_fields: list[ModelField] = [] model_fields: list[ModelField] = []
for name, field_info in model.model_fields.items(): for name, field_info in model.model_fields.items():
type_ = field_info.annotation type_ = annotated_type(field_info.annotation) or field_info.annotation
if lenient_issubclass(type_, (BaseModel, dict)) or is_dataclass(type_): if lenient_issubclass(type_, (BaseModel, dict)) or is_dataclass(type_):
model_config = None model_config = None
else: else:

88
tests/test_union_single_root_model_openapi.py

@ -0,0 +1,88 @@
from typing import Annotated, Literal, Union
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
@pytest.fixture(name="client")
def get_client():
from pydantic import BaseModel, Field, RootModel
class Base(BaseModel):
type: Literal["BASE"] = "BASE"
value: str
class MyModel(RootModel[Annotated[Union[Base], Field(discriminator="type")]]):
pass
app = FastAPI()
@app.get("/")
def test() -> MyModel:
return MyModel.model_validate(Base(value="test"))
client = TestClient(app)
return client
def test_get(client: TestClient):
response = client.get("/")
assert response.json() == {"value": "test", "type": "BASE"}
def test_openapi_schema(client: TestClient):
response = client.get("openapi.json")
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/": {
"get": {
"summary": "Test",
"operationId": "test__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyModel"
}
}
},
}
},
}
}
},
"components": {
"schemas": {
"Base": {
"properties": {
"type": {
"type": "string",
"const": "BASE",
"title": "Type",
"default": "BASE",
},
"value": {"type": "string", "title": "Value"},
},
"type": "object",
"required": ["value"],
"title": "Base",
},
"MyModel": {
"oneOf": [{"$ref": "#/components/schemas/Base"}],
"title": "MyModel",
"discriminator": {
"propertyName": "type",
"mapping": {"BASE": "#/components/schemas/Base"},
},
},
}
},
}
)
Loading…
Cancel
Save