Coverage for tests/test_additional_responses_custom_model_in_callback.py: 100%
18 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
1from dirty_equals import IsDict 1abcde
2from fastapi import APIRouter, FastAPI 1abcde
3from fastapi.testclient import TestClient 1abcde
4from pydantic import BaseModel, HttpUrl 1abcde
5from starlette.responses import JSONResponse 1abcde
8class CustomModel(BaseModel): 1abcde
9 a: int 1abcde
12app = FastAPI() 1abcde
14callback_router = APIRouter(default_response_class=JSONResponse) 1abcde
17@callback_router.get( 1abcde
18 "{$callback_url}/callback/", responses={400: {"model": CustomModel}}
19)
20def callback_route(): 1abcde
21 pass # pragma: no cover
24@app.post("/", callbacks=callback_router.routes) 1abcde
25def main_route(callback_url: HttpUrl): 1abcde
26 pass # pragma: no cover
29client = TestClient(app) 1abcde
32def test_openapi_schema(): 1abcde
33 response = client.get("/openapi.json") 1abcde
34 assert response.status_code == 200, response.text 1abcde
35 assert response.json() == { 1abcde
36 "openapi": "3.1.0",
37 "info": {"title": "FastAPI", "version": "0.1.0"},
38 "paths": {
39 "/": {
40 "post": {
41 "summary": "Main Route",
42 "operationId": "main_route__post",
43 "parameters": [
44 {
45 "required": True,
46 "schema": IsDict(
47 {
48 "title": "Callback Url",
49 "minLength": 1,
50 "type": "string",
51 "format": "uri",
52 }
53 )
54 # TODO: remove when deprecating Pydantic v1
55 | IsDict(
56 {
57 "title": "Callback Url",
58 "maxLength": 2083,
59 "minLength": 1,
60 "type": "string",
61 "format": "uri",
62 }
63 ),
64 "name": "callback_url",
65 "in": "query",
66 }
67 ],
68 "responses": {
69 "200": {
70 "description": "Successful Response",
71 "content": {"application/json": {"schema": {}}},
72 },
73 "422": {
74 "description": "Validation Error",
75 "content": {
76 "application/json": {
77 "schema": {
78 "$ref": "#/components/schemas/HTTPValidationError"
79 }
80 }
81 },
82 },
83 },
84 "callbacks": {
85 "callback_route": {
86 "{$callback_url}/callback/": {
87 "get": {
88 "summary": "Callback Route",
89 "operationId": "callback_route__callback_url__callback__get",
90 "responses": {
91 "400": {
92 "content": {
93 "application/json": {
94 "schema": {
95 "$ref": "#/components/schemas/CustomModel"
96 }
97 }
98 },
99 "description": "Bad Request",
100 },
101 "200": {
102 "description": "Successful Response",
103 "content": {
104 "application/json": {"schema": {}}
105 },
106 },
107 },
108 }
109 }
110 }
111 },
112 }
113 }
114 },
115 "components": {
116 "schemas": {
117 "CustomModel": {
118 "title": "CustomModel",
119 "required": ["a"],
120 "type": "object",
121 "properties": {"a": {"title": "A", "type": "integer"}},
122 },
123 "HTTPValidationError": {
124 "title": "HTTPValidationError",
125 "type": "object",
126 "properties": {
127 "detail": {
128 "title": "Detail",
129 "type": "array",
130 "items": {"$ref": "#/components/schemas/ValidationError"},
131 }
132 },
133 },
134 "ValidationError": {
135 "title": "ValidationError",
136 "required": ["loc", "msg", "type"],
137 "type": "object",
138 "properties": {
139 "loc": {
140 "title": "Location",
141 "type": "array",
142 "items": {
143 "anyOf": [{"type": "string"}, {"type": "integer"}]
144 },
145 },
146 "msg": {"title": "Message", "type": "string"},
147 "type": {"title": "Error Type", "type": "string"},
148 },
149 },
150 }
151 },
152 }