| 1 |
import pytest |
| 2 |
from dirty_equals import IsOneOf |
| 3 |
from fastapi import FastAPI |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
from pydantic import BaseModel |
| 7 |
|
| 8 |
from tests.utils import needs_py310 |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture(name="client") |
| 12 |
def get_client(): |
| 13 |
from enum import Enum |
| 14 |
|
| 15 |
app = FastAPI() |
| 16 |
|
| 17 |
class PlatformRole(str, Enum): |
| 18 |
admin = "admin" |
| 19 |
user = "user" |
| 20 |
|
| 21 |
class OtherRole(str, Enum): ... |
| 22 |
|
| 23 |
class User(BaseModel): |
| 24 |
username: str |
| 25 |
role: PlatformRole | OtherRole |
| 26 |
|
| 27 |
@app.get("/users") |
| 28 |
async def get_user() -> User: |
| 29 |
return {"username": "alice", "role": "admin"} # ty: ignore[invalid-return-type] |
| 30 |
|
| 31 |
client = TestClient(app) |
| 32 |
return client |
| 33 |
|
| 34 |
|
| 35 |
@needs_py310 |
| 36 |
def test_get(client: TestClient): |
| 37 |
response = client.get("/users") |
| 38 |
assert response.json() == {"username": "alice", "role": "admin"} |
| 39 |
|
| 40 |
|
| 41 |
@needs_py310 |
| 42 |
def test_openapi_schema(client: TestClient): |
| 43 |
response = client.get("openapi.json") |
| 44 |
assert response.json() == snapshot( |
| 45 |
{ |
| 46 |
"openapi": "3.1.0", |
| 47 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 48 |
"paths": { |
| 49 |
"/users": { |
| 50 |
"get": { |
| 51 |
"summary": "Get User", |
| 52 |
"operationId": "get_user_users_get", |
| 53 |
"responses": { |
| 54 |
"200": { |
| 55 |
"description": "Successful Response", |
| 56 |
"content": { |
| 57 |
"application/json": { |
| 58 |
"schema": {"$ref": "#/components/schemas/User"} |
| 59 |
} |
| 60 |
}, |
| 61 |
} |
| 62 |
}, |
| 63 |
} |
| 64 |
} |
| 65 |
}, |
| 66 |
"components": { |
| 67 |
"schemas": IsOneOf( |
| 68 |
# Pydantic >= 2.11: no top-level OtherRole |
| 69 |
{ |
| 70 |
"PlatformRole": { |
| 71 |
"type": "string", |
| 72 |
"enum": ["admin", "user"], |
| 73 |
"title": "PlatformRole", |
| 74 |
}, |
| 75 |
"User": { |
| 76 |
"properties": { |
| 77 |
"username": {"type": "string", "title": "Username"}, |
| 78 |
"role": { |
| 79 |
"anyOf": [ |
| 80 |
{"$ref": "#/components/schemas/PlatformRole"}, |
| 81 |
{"enum": [], "title": "OtherRole"}, |
| 82 |
], |
| 83 |
"title": "Role", |
| 84 |
}, |
| 85 |
}, |
| 86 |
"type": "object", |
| 87 |
"required": ["username", "role"], |
| 88 |
"title": "User", |
| 89 |
}, |
| 90 |
}, |
| 91 |
# Pydantic < 2.11: adds a top-level OtherRole schema |
| 92 |
{ |
| 93 |
"OtherRole": { |
| 94 |
"enum": [], |
| 95 |
"title": "OtherRole", |
| 96 |
}, |
| 97 |
"PlatformRole": { |
| 98 |
"type": "string", |
| 99 |
"enum": ["admin", "user"], |
| 100 |
"title": "PlatformRole", |
| 101 |
}, |
| 102 |
"User": { |
| 103 |
"properties": { |
| 104 |
"username": {"type": "string", "title": "Username"}, |
| 105 |
"role": { |
| 106 |
"anyOf": [ |
| 107 |
{"$ref": "#/components/schemas/PlatformRole"}, |
| 108 |
{"enum": [], "title": "OtherRole"}, |
| 109 |
], |
| 110 |
"title": "Role", |
| 111 |
}, |
| 112 |
}, |
| 113 |
"type": "object", |
| 114 |
"required": ["username", "role"], |
| 115 |
"title": "User", |
| 116 |
}, |
| 117 |
}, |
| 118 |
) |
| 119 |
}, |
| 120 |
} |
| 121 |
) |