Coverage for tests/test_additional_responses_response_class.py: 100%
22 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import typing 1abcde
3from fastapi import FastAPI 1abcde
4from fastapi.responses import JSONResponse 1abcde
5from fastapi.testclient import TestClient 1abcde
6from pydantic import BaseModel 1abcde
8app = FastAPI() 1abcde
11class JsonApiResponse(JSONResponse): 1abcde
12 media_type = "application/vnd.api+json" 1abcde
15class Error(BaseModel): 1abcde
16 status: str 1abcde
17 title: str 1abcde
20class JsonApiError(BaseModel): 1abcde
21 errors: typing.List[Error] 1abcde
24@app.get( 1abcde
25 "/a",
26 response_class=JsonApiResponse,
27 responses={500: {"description": "Error", "model": JsonApiError}},
28)
29async def a(): 1abcde
30 pass # pragma: no cover
33@app.get("/b", responses={500: {"description": "Error", "model": Error}}) 1abcde
34async def b(): 1abcde
35 pass # pragma: no cover
38client = TestClient(app) 1abcde
41def test_openapi_schema(): 1abcde
42 response = client.get("/openapi.json") 1abcde
43 assert response.status_code == 200, response.text 1abcde
44 assert response.json() == { 1abcde
45 "openapi": "3.1.0",
46 "info": {"title": "FastAPI", "version": "0.1.0"},
47 "paths": {
48 "/a": {
49 "get": {
50 "responses": {
51 "500": {
52 "description": "Error",
53 "content": {
54 "application/vnd.api+json": {
55 "schema": {
56 "$ref": "#/components/schemas/JsonApiError"
57 }
58 }
59 },
60 },
61 "200": {
62 "description": "Successful Response",
63 "content": {"application/vnd.api+json": {"schema": {}}},
64 },
65 },
66 "summary": "A",
67 "operationId": "a_a_get",
68 }
69 },
70 "/b": {
71 "get": {
72 "responses": {
73 "500": {
74 "description": "Error",
75 "content": {
76 "application/json": {
77 "schema": {"$ref": "#/components/schemas/Error"}
78 }
79 },
80 },
81 "200": {
82 "description": "Successful Response",
83 "content": {"application/json": {"schema": {}}},
84 },
85 },
86 "summary": "B",
87 "operationId": "b_b_get",
88 }
89 },
90 },
91 "components": {
92 "schemas": {
93 "Error": {
94 "title": "Error",
95 "required": ["status", "title"],
96 "type": "object",
97 "properties": {
98 "status": {"title": "Status", "type": "string"},
99 "title": {"title": "Title", "type": "string"},
100 },
101 },
102 "JsonApiError": {
103 "title": "JsonApiError",
104 "required": ["errors"],
105 "type": "object",
106 "properties": {
107 "errors": {
108 "title": "Errors",
109 "type": "array",
110 "items": {"$ref": "#/components/schemas/Error"},
111 }
112 },
113 },
114 }
115 },
116 }