| 1 |
from typing import Annotated, Any, Literal |
| 2 |
|
| 3 |
from dirty_equals import IsOneOf |
| 4 |
from fastapi import FastAPI |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from inline_snapshot import snapshot |
| 7 |
from pydantic import BaseModel, Field |
| 8 |
|
| 9 |
|
| 10 |
def test_discriminator_pydantic_v2() -> None: |
| 11 |
from pydantic import Tag |
| 12 |
|
| 13 |
app = FastAPI() |
| 14 |
|
| 15 |
class FirstItem(BaseModel): |
| 16 |
value: Literal["first"] |
| 17 |
price: int |
| 18 |
|
| 19 |
class OtherItem(BaseModel): |
| 20 |
value: Literal["other"] |
| 21 |
price: float |
| 22 |
|
| 23 |
Item = Annotated[ |
| 24 |
Annotated[FirstItem, Tag("first")] | Annotated[OtherItem, Tag("other")], |
| 25 |
Field(discriminator="value"), |
| 26 |
] |
| 27 |
|
| 28 |
@app.post("/items/") |
| 29 |
def save_union_body_discriminator( |
| 30 |
item: Item, q: Annotated[str, Field(description="Query string")] |
| 31 |
) -> dict[str, Any]: |
| 32 |
return {"item": item} |
| 33 |
|
| 34 |
client = TestClient(app) |
| 35 |
response = client.post("/items/?q=first", json={"value": "first", "price": 100}) |
| 36 |
assert response.status_code == 200, response.text |
| 37 |
assert response.json() == {"item": {"value": "first", "price": 100}} |
| 38 |
|
| 39 |
response = client.post("/items/?q=other", json={"value": "other", "price": 100.5}) |
| 40 |
assert response.status_code == 200, response.text |
| 41 |
assert response.json() == {"item": {"value": "other", "price": 100.5}} |
| 42 |
|
| 43 |
response = client.get("/openapi.json") |
| 44 |
assert response.status_code == 200, response.text |
| 45 |
assert response.json() == snapshot( |
| 46 |
{ |
| 47 |
"openapi": "3.1.0", |
| 48 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 49 |
"paths": { |
| 50 |
"/items/": { |
| 51 |
"post": { |
| 52 |
"summary": "Save Union Body Discriminator", |
| 53 |
"operationId": "save_union_body_discriminator_items__post", |
| 54 |
"parameters": [ |
| 55 |
{ |
| 56 |
"name": "q", |
| 57 |
"in": "query", |
| 58 |
"required": True, |
| 59 |
"schema": { |
| 60 |
"type": "string", |
| 61 |
"description": "Query string", |
| 62 |
"title": "Q", |
| 63 |
}, |
| 64 |
} |
| 65 |
], |
| 66 |
"requestBody": { |
| 67 |
"required": True, |
| 68 |
"content": { |
| 69 |
"application/json": { |
| 70 |
"schema": { |
| 71 |
"oneOf": [ |
| 72 |
{"$ref": "#/components/schemas/FirstItem"}, |
| 73 |
{"$ref": "#/components/schemas/OtherItem"}, |
| 74 |
], |
| 75 |
"discriminator": { |
| 76 |
"propertyName": "value", |
| 77 |
"mapping": { |
| 78 |
"first": "#/components/schemas/FirstItem", |
| 79 |
"other": "#/components/schemas/OtherItem", |
| 80 |
}, |
| 81 |
}, |
| 82 |
"title": "Item", |
| 83 |
} |
| 84 |
} |
| 85 |
}, |
| 86 |
}, |
| 87 |
"responses": { |
| 88 |
"200": { |
| 89 |
"description": "Successful Response", |
| 90 |
"content": { |
| 91 |
"application/json": { |
| 92 |
"schema": IsOneOf( |
| 93 |
# Pydantic < 2.11: no additionalProperties |
| 94 |
{ |
| 95 |
"type": "object", |
| 96 |
"title": "Response Save Union Body Discriminator Items Post", |
| 97 |
}, |
| 98 |
# Pydantic >= 2.11: has additionalProperties |
| 99 |
{ |
| 100 |
"type": "object", |
| 101 |
"additionalProperties": True, |
| 102 |
"title": "Response Save Union Body Discriminator Items Post", |
| 103 |
}, |
| 104 |
) |
| 105 |
} |
| 106 |
}, |
| 107 |
}, |
| 108 |
"422": { |
| 109 |
"description": "Validation Error", |
| 110 |
"content": { |
| 111 |
"application/json": { |
| 112 |
"schema": { |
| 113 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 114 |
} |
| 115 |
} |
| 116 |
}, |
| 117 |
}, |
| 118 |
}, |
| 119 |
} |
| 120 |
} |
| 121 |
}, |
| 122 |
"components": { |
| 123 |
"schemas": { |
| 124 |
"FirstItem": { |
| 125 |
"properties": { |
| 126 |
"value": IsOneOf( |
| 127 |
# Pydantic >= 2.10: const only |
| 128 |
{ |
| 129 |
"type": "string", |
| 130 |
"const": "first", |
| 131 |
"title": "Value", |
| 132 |
}, |
| 133 |
# Pydantic 2.9: const + enum |
| 134 |
{ |
| 135 |
"type": "string", |
| 136 |
"const": "first", |
| 137 |
"enum": ["first"], |
| 138 |
"title": "Value", |
| 139 |
}, |
| 140 |
), |
| 141 |
"price": {"type": "integer", "title": "Price"}, |
| 142 |
}, |
| 143 |
"type": "object", |
| 144 |
"required": ["value", "price"], |
| 145 |
"title": "FirstItem", |
| 146 |
}, |
| 147 |
"HTTPValidationError": { |
| 148 |
"properties": { |
| 149 |
"detail": { |
| 150 |
"items": { |
| 151 |
"$ref": "#/components/schemas/ValidationError" |
| 152 |
}, |
| 153 |
"type": "array", |
| 154 |
"title": "Detail", |
| 155 |
} |
| 156 |
}, |
| 157 |
"type": "object", |
| 158 |
"title": "HTTPValidationError", |
| 159 |
}, |
| 160 |
"OtherItem": { |
| 161 |
"properties": { |
| 162 |
"value": IsOneOf( |
| 163 |
# Pydantic >= 2.10.0: const only |
| 164 |
{ |
| 165 |
"type": "string", |
| 166 |
"const": "other", |
| 167 |
"title": "Value", |
| 168 |
}, |
| 169 |
# Pydantic 2.9.x: const + enum |
| 170 |
{ |
| 171 |
"type": "string", |
| 172 |
"const": "other", |
| 173 |
"enum": ["other"], |
| 174 |
"title": "Value", |
| 175 |
}, |
| 176 |
), |
| 177 |
"price": {"type": "number", "title": "Price"}, |
| 178 |
}, |
| 179 |
"type": "object", |
| 180 |
"required": ["value", "price"], |
| 181 |
"title": "OtherItem", |
| 182 |
}, |
| 183 |
"ValidationError": { |
| 184 |
"properties": { |
| 185 |
"ctx": {"title": "Context", "type": "object"}, |
| 186 |
"input": {"title": "Input"}, |
| 187 |
"loc": { |
| 188 |
"items": { |
| 189 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 190 |
}, |
| 191 |
"type": "array", |
| 192 |
"title": "Location", |
| 193 |
}, |
| 194 |
"msg": {"type": "string", "title": "Message"}, |
| 195 |
"type": {"type": "string", "title": "Error Type"}, |
| 196 |
}, |
| 197 |
"type": "object", |
| 198 |
"required": ["loc", "msg", "type"], |
| 199 |
"title": "ValidationError", |
| 200 |
}, |
| 201 |
} |
| 202 |
}, |
| 203 |
} |
| 204 |
) |