Coverage for tests/test_additional_responses_custom_validationerror.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import typing 1abcde

2 

3from fastapi import FastAPI 1abcde

4from fastapi.responses import JSONResponse 1abcde

5from fastapi.testclient import TestClient 1abcde

6from pydantic import BaseModel 1abcde

7 

8app = FastAPI() 1abcde

9 

10 

11class JsonApiResponse(JSONResponse): 1abcde

12 media_type = "application/vnd.api+json" 1abcde

13 

14 

15class Error(BaseModel): 1abcde

16 status: str 1abcde

17 title: str 1abcde

18 

19 

20class JsonApiError(BaseModel): 1abcde

21 errors: typing.List[Error] 1abcde

22 

23 

24@app.get( 1abcde

25 "/a/{id}", 

26 response_class=JsonApiResponse, 

27 responses={422: {"description": "Error", "model": JsonApiError}}, 

28) 

29async def a(id): 1abcde

30 pass # pragma: no cover 

31 

32 

33client = TestClient(app) 1abcde

34 

35 

36def test_openapi_schema(): 1abcde

37 response = client.get("/openapi.json") 1abcde

38 assert response.status_code == 200, response.text 1abcde

39 assert response.json() == { 1abcde

40 "openapi": "3.1.0", 

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

42 "paths": { 

43 "/a/{id}": { 

44 "get": { 

45 "responses": { 

46 "422": { 

47 "description": "Error", 

48 "content": { 

49 "application/vnd.api+json": { 

50 "schema": { 

51 "$ref": "#/components/schemas/JsonApiError" 

52 } 

53 } 

54 }, 

55 }, 

56 "200": { 

57 "description": "Successful Response", 

58 "content": {"application/vnd.api+json": {"schema": {}}}, 

59 }, 

60 }, 

61 "summary": "A", 

62 "operationId": "a_a__id__get", 

63 "parameters": [ 

64 { 

65 "required": True, 

66 "schema": {"title": "Id"}, 

67 "name": "id", 

68 "in": "path", 

69 } 

70 ], 

71 } 

72 } 

73 }, 

74 "components": { 

75 "schemas": { 

76 "Error": { 

77 "title": "Error", 

78 "required": ["status", "title"], 

79 "type": "object", 

80 "properties": { 

81 "status": {"title": "Status", "type": "string"}, 

82 "title": {"title": "Title", "type": "string"}, 

83 }, 

84 }, 

85 "JsonApiError": { 

86 "title": "JsonApiError", 

87 "required": ["errors"], 

88 "type": "object", 

89 "properties": { 

90 "errors": { 

91 "title": "Errors", 

92 "type": "array", 

93 "items": {"$ref": "#/components/schemas/Error"}, 

94 } 

95 }, 

96 }, 

97 } 

98 }, 

99 }