|
|
@ -3,32 +3,28 @@ from typing import Any, Dict, Union |
|
|
from fastapi import FastAPI |
|
|
from fastapi import FastAPI |
|
|
from fastapi.testclient import TestClient |
|
|
from fastapi.testclient import TestClient |
|
|
from inline_snapshot import snapshot |
|
|
from inline_snapshot import snapshot |
|
|
from pydantic import BaseModel, Discriminator, Tag |
|
|
from pydantic import BaseModel, Field, Tag |
|
|
from typing_extensions import Annotated |
|
|
from typing_extensions import Annotated, Literal |
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FirstItem(BaseModel): |
|
|
class FirstItem(BaseModel): |
|
|
value: str |
|
|
value: Literal["first"] |
|
|
price: int |
|
|
price: int |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OtherItem(BaseModel): |
|
|
class OtherItem(BaseModel): |
|
|
value: str |
|
|
value: Literal["second"] |
|
|
price: float |
|
|
price: float |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_discriminator_value(v: Any) -> str: |
|
|
|
|
|
return v.get("value") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Item = Annotated[ |
|
|
Item = Annotated[ |
|
|
Union[ |
|
|
Union[ |
|
|
Annotated[FirstItem, Tag("first")], |
|
|
Annotated[FirstItem, Tag("first")], |
|
|
Annotated[OtherItem, Tag("other")], |
|
|
Annotated[OtherItem, Tag("other")], |
|
|
], |
|
|
], |
|
|
Discriminator(get_discriminator_value), |
|
|
Field(discriminator="value"), |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -55,6 +51,13 @@ def test_openapi_schema() -> None: |
|
|
{"$ref": "#/components/schemas/FirstItem"}, |
|
|
{"$ref": "#/components/schemas/FirstItem"}, |
|
|
{"$ref": "#/components/schemas/OtherItem"}, |
|
|
{"$ref": "#/components/schemas/OtherItem"}, |
|
|
], |
|
|
], |
|
|
|
|
|
"discriminator": { |
|
|
|
|
|
"propertyName": "value", |
|
|
|
|
|
"mapping": { |
|
|
|
|
|
"first": "#/components/schemas/FirstItem", |
|
|
|
|
|
"second": "#/components/schemas/OtherItem", |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
"title": "Item", |
|
|
"title": "Item", |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|