mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-28 16:49:26 -05:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
942fce394b | ||
|
|
13b067c9b6 | ||
|
|
185cecd891 | ||
|
|
27c0f7e75f | ||
|
|
05dbfebce5 |
4
.github/workflows/build-docs.yml
vendored
4
.github/workflows/build-docs.yml
vendored
@@ -54,7 +54,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
@@ -96,7 +96,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/contributors.yml
vendored
2
.github/workflows/contributors.yml
vendored
@@ -30,7 +30,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/deploy-docs.yml
vendored
2
.github/workflows/deploy-docs.yml
vendored
@@ -29,7 +29,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/label-approved.yml
vendored
2
.github/workflows/label-approved.yml
vendored
@@ -26,7 +26,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/notify-translations.yml
vendored
2
.github/workflows/notify-translations.yml
vendored
@@ -34,7 +34,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/people.yml
vendored
2
.github/workflows/people.yml
vendored
@@ -30,7 +30,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/smokeshow.yml
vendored
2
.github/workflows/smokeshow.yml
vendored
@@ -26,7 +26,7 @@ jobs:
|
||||
with:
|
||||
python-version: '3.9'
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/sponsors.yml
vendored
2
.github/workflows/sponsors.yml
vendored
@@ -30,7 +30,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
6
.github/workflows/test.yml
vendored
6
.github/workflows/test.yml
vendored
@@ -29,7 +29,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
@@ -67,7 +67,7 @@ jobs:
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
@@ -112,7 +112,7 @@ jobs:
|
||||
with:
|
||||
python-version: '3.8'
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/topic-repos.yml
vendored
2
.github/workflows/topic-repos.yml
vendored
@@ -25,7 +25,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
2
.github/workflows/translate.yml
vendored
2
.github/workflows/translate.yml
vendored
@@ -48,7 +48,7 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- name: Setup uv
|
||||
uses: astral-sh/setup-uv@v6
|
||||
uses: astral-sh/setup-uv@v7
|
||||
with:
|
||||
version: "0.4.15"
|
||||
enable-cache: true
|
||||
|
||||
@@ -7,6 +7,16 @@ hide:
|
||||
|
||||
## Latest Changes
|
||||
|
||||
## 0.118.2
|
||||
|
||||
### Fixes
|
||||
|
||||
* 🐛 Fix tagged discriminated union not recognized as body field. PR [#12942](https://github.com/fastapi/fastapi/pull/12942) by [@frankie567](https://github.com/frankie567).
|
||||
|
||||
### Internal
|
||||
|
||||
* ⬆ Bump astral-sh/setup-uv from 6 to 7. PR [#14167](https://github.com/fastapi/fastapi/pull/14167) by [@dependabot[bot]](https://github.com/apps/dependabot).
|
||||
|
||||
## 0.118.1
|
||||
|
||||
### Upgrades
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""FastAPI framework, high performance, easy to learn, fast to code, ready for production"""
|
||||
|
||||
__version__ = "0.118.1"
|
||||
__version__ = "0.118.2"
|
||||
|
||||
from starlette import status as status
|
||||
|
||||
|
||||
@@ -590,6 +590,9 @@ def field_annotation_is_complex(annotation: Union[Type[Any], None]) -> bool:
|
||||
if origin is Union or origin is UnionType:
|
||||
return any(field_annotation_is_complex(arg) for arg in get_args(annotation))
|
||||
|
||||
if origin is Annotated:
|
||||
return field_annotation_is_complex(get_args(annotation)[0])
|
||||
|
||||
return (
|
||||
_annotation_is_complex(annotation)
|
||||
or _annotation_is_complex(origin)
|
||||
|
||||
188
tests/test_union_body_discriminator.py
Normal file
188
tests/test_union_body_discriminator.py
Normal file
@@ -0,0 +1,188 @@
|
||||
from typing import Any, Dict, Union
|
||||
|
||||
from dirty_equals import IsDict
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
from inline_snapshot import snapshot
|
||||
from pydantic import BaseModel, Field
|
||||
from typing_extensions import Annotated, Literal
|
||||
|
||||
from .utils import needs_pydanticv2
|
||||
|
||||
|
||||
@needs_pydanticv2
|
||||
def test_discriminator_pydantic_v2() -> None:
|
||||
from pydantic import Tag
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
class FirstItem(BaseModel):
|
||||
value: Literal["first"]
|
||||
price: int
|
||||
|
||||
class OtherItem(BaseModel):
|
||||
value: Literal["other"]
|
||||
price: float
|
||||
|
||||
Item = Annotated[
|
||||
Union[Annotated[FirstItem, Tag("first")], Annotated[OtherItem, Tag("other")]],
|
||||
Field(discriminator="value"),
|
||||
]
|
||||
|
||||
@app.post("/items/")
|
||||
def save_union_body_discriminator(
|
||||
item: Item, q: Annotated[str, Field(description="Query string")]
|
||||
) -> Dict[str, Any]:
|
||||
return {"item": item}
|
||||
|
||||
client = TestClient(app)
|
||||
response = client.post("/items/?q=first", json={"value": "first", "price": 100})
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item": {"value": "first", "price": 100}}
|
||||
|
||||
response = client.post("/items/?q=other", json={"value": "other", "price": 100.5})
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"item": {"value": "other", "price": 100.5}}
|
||||
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/items/": {
|
||||
"post": {
|
||||
"summary": "Save Union Body Discriminator",
|
||||
"operationId": "save_union_body_discriminator_items__post",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "q",
|
||||
"in": "query",
|
||||
"required": True,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"description": "Query string",
|
||||
"title": "Q",
|
||||
},
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"oneOf": [
|
||||
{"$ref": "#/components/schemas/FirstItem"},
|
||||
{"$ref": "#/components/schemas/OtherItem"},
|
||||
],
|
||||
"discriminator": {
|
||||
"propertyName": "value",
|
||||
"mapping": {
|
||||
"first": "#/components/schemas/FirstItem",
|
||||
"other": "#/components/schemas/OtherItem",
|
||||
},
|
||||
},
|
||||
"title": "Item",
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": IsDict(
|
||||
{
|
||||
# Pydantic 2.10, in Python 3.8
|
||||
# TODO: remove when dropping support for Python 3.8
|
||||
"type": "object",
|
||||
"title": "Response Save Union Body Discriminator Items Post",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
{
|
||||
"type": "object",
|
||||
"additionalProperties": True,
|
||||
"title": "Response Save Union Body Discriminator Items Post",
|
||||
}
|
||||
)
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"FirstItem": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"type": "string",
|
||||
"const": "first",
|
||||
"title": "Value",
|
||||
},
|
||||
"price": {"type": "integer", "title": "Price"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["value", "price"],
|
||||
"title": "FirstItem",
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"properties": {
|
||||
"detail": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/ValidationError"
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Detail",
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"title": "HTTPValidationError",
|
||||
},
|
||||
"OtherItem": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"type": "string",
|
||||
"const": "other",
|
||||
"title": "Value",
|
||||
},
|
||||
"price": {"type": "number", "title": "Price"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["value", "price"],
|
||||
"title": "OtherItem",
|
||||
},
|
||||
"ValidationError": {
|
||||
"properties": {
|
||||
"loc": {
|
||||
"items": {
|
||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Location",
|
||||
},
|
||||
"msg": {"type": "string", "title": "Message"},
|
||||
"type": {"type": "string", "title": "Error Type"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["loc", "msg", "type"],
|
||||
"title": "ValidationError",
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user