Coverage for tests/test_response_class_no_mediatype.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, Response 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=Response,
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/json": {
55 "schema": {
56 "$ref": "#/components/schemas/JsonApiError"
57 }
58 }
59 },
60 },
61 "200": {"description": "Successful Response"},
62 },
63 "summary": "A",
64 "operationId": "a_a_get",
65 }
66 },
67 "/b": {
68 "get": {
69 "responses": {
70 "500": {
71 "description": "Error",
72 "content": {
73 "application/json": {
74 "schema": {"$ref": "#/components/schemas/Error"}
75 }
76 },
77 },
78 "200": {
79 "description": "Successful Response",
80 "content": {"application/json": {"schema": {}}},
81 },
82 },
83 "summary": "B",
84 "operationId": "b_b_get",
85 }
86 },
87 },
88 "components": {
89 "schemas": {
90 "Error": {
91 "title": "Error",
92 "required": ["status", "title"],
93 "type": "object",
94 "properties": {
95 "status": {"title": "Status", "type": "string"},
96 "title": {"title": "Title", "type": "string"},
97 },
98 },
99 "JsonApiError": {
100 "title": "JsonApiError",
101 "required": ["errors"],
102 "type": "object",
103 "properties": {
104 "errors": {
105 "title": "Errors",
106 "type": "array",
107 "items": {"$ref": "#/components/schemas/Error"},
108 }
109 },
110 },
111 }
112 },
113 }