Coverage for tests / test_additional_responses_custom_model_in_callback.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import APIRouter, FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4from pydantic import BaseModel, HttpUrl 1abcd

5from starlette.responses import JSONResponse 1abcd

6 

7 

8class CustomModel(BaseModel): 1abcd

9 a: int 1abcd

10 

11 

12app = FastAPI() 1abcd

13 

14callback_router = APIRouter(default_response_class=JSONResponse) 1abcd

15 

16 

17@callback_router.get( 1abcd

18 "{$callback_url}/callback/", responses={400: {"model": CustomModel}} 

19) 

20def callback_route(): 1abcd

21 pass # pragma: no cover 

22 

23 

24@app.post("/", callbacks=callback_router.routes) 1abcd

25def main_route(callback_url: HttpUrl): 1abcd

26 pass # pragma: no cover 

27 

28 

29client = TestClient(app) 1abcd

30 

31 

32def test_openapi_schema(): 1abcd

33 response = client.get("/openapi.json") 1efg

34 assert response.status_code == 200, response.text 1efg

35 assert response.json() == snapshot( 1efg

36 { 

37 "openapi": "3.1.0", 

38 "info": {"title": "FastAPI", "version": "0.1.0"}, 

39 "paths": { 

40 "/": { 

41 "post": { 

42 "summary": "Main Route", 

43 "operationId": "main_route__post", 

44 "parameters": [ 

45 { 

46 "required": True, 

47 "schema": { 

48 "title": "Callback Url", 

49 "maxLength": 2083, 

50 "minLength": 1, 

51 "type": "string", 

52 "format": "uri", 

53 }, 

54 "name": "callback_url", 

55 "in": "query", 

56 } 

57 ], 

58 "responses": { 

59 "200": { 

60 "description": "Successful Response", 

61 "content": {"application/json": {"schema": {}}}, 

62 }, 

63 "422": { 

64 "description": "Validation Error", 

65 "content": { 

66 "application/json": { 

67 "schema": { 

68 "$ref": "#/components/schemas/HTTPValidationError" 

69 } 

70 } 

71 }, 

72 }, 

73 }, 

74 "callbacks": { 

75 "callback_route": { 

76 "{$callback_url}/callback/": { 

77 "get": { 

78 "summary": "Callback Route", 

79 "operationId": "callback_route__callback_url__callback__get", 

80 "responses": { 

81 "400": { 

82 "content": { 

83 "application/json": { 

84 "schema": { 

85 "$ref": "#/components/schemas/CustomModel" 

86 } 

87 } 

88 }, 

89 "description": "Bad Request", 

90 }, 

91 "200": { 

92 "description": "Successful Response", 

93 "content": { 

94 "application/json": {"schema": {}} 

95 }, 

96 }, 

97 }, 

98 } 

99 } 

100 } 

101 }, 

102 } 

103 } 

104 }, 

105 "components": { 

106 "schemas": { 

107 "CustomModel": { 

108 "title": "CustomModel", 

109 "required": ["a"], 

110 "type": "object", 

111 "properties": {"a": {"title": "A", "type": "integer"}}, 

112 }, 

113 "HTTPValidationError": { 

114 "title": "HTTPValidationError", 

115 "type": "object", 

116 "properties": { 

117 "detail": { 

118 "title": "Detail", 

119 "type": "array", 

120 "items": { 

121 "$ref": "#/components/schemas/ValidationError" 

122 }, 

123 } 

124 }, 

125 }, 

126 "ValidationError": { 

127 "title": "ValidationError", 

128 "required": ["loc", "msg", "type"], 

129 "type": "object", 

130 "properties": { 

131 "ctx": {"title": "Context", "type": "object"}, 

132 "input": {"title": "Input"}, 

133 "loc": { 

134 "title": "Location", 

135 "type": "array", 

136 "items": { 

137 "anyOf": [{"type": "string"}, {"type": "integer"}] 

138 }, 

139 }, 

140 "msg": {"title": "Message", "type": "string"}, 

141 "type": {"title": "Error Type", "type": "string"}, 

142 }, 

143 }, 

144 } 

145 }, 

146 } 

147 )