Coverage for tests/test_additional_properties.py: 100%

19 statements  

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

1from typing import Dict 1abcde

2 

3from fastapi import FastAPI 1abcde

4from fastapi.testclient import TestClient 1abcde

5from pydantic import BaseModel 1abcde

6 

7app = FastAPI() 1abcde

8 

9 

10class Items(BaseModel): 1abcde

11 items: Dict[str, int] 1abcde

12 

13 

14@app.post("/foo") 1abcde

15def foo(items: Items): 1abcde

16 return items.items 1abcde

17 

18 

19client = TestClient(app) 1abcde

20 

21 

22def test_additional_properties_post(): 1abcde

23 response = client.post("/foo", json={"items": {"foo": 1, "bar": 2}}) 1abcde

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

25 assert response.json() == {"foo": 1, "bar": 2} 1abcde

26 

27 

28def test_openapi_schema(): 1abcde

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

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

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

32 "openapi": "3.1.0", 

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

34 "paths": { 

35 "/foo": { 

36 "post": { 

37 "responses": { 

38 "200": { 

39 "description": "Successful Response", 

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

41 }, 

42 "422": { 

43 "description": "Validation Error", 

44 "content": { 

45 "application/json": { 

46 "schema": { 

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

48 } 

49 } 

50 }, 

51 }, 

52 }, 

53 "summary": "Foo", 

54 "operationId": "foo_foo_post", 

55 "requestBody": { 

56 "content": { 

57 "application/json": { 

58 "schema": {"$ref": "#/components/schemas/Items"} 

59 } 

60 }, 

61 "required": True, 

62 }, 

63 } 

64 } 

65 }, 

66 "components": { 

67 "schemas": { 

68 "Items": { 

69 "title": "Items", 

70 "required": ["items"], 

71 "type": "object", 

72 "properties": { 

73 "items": { 

74 "title": "Items", 

75 "type": "object", 

76 "additionalProperties": {"type": "integer"}, 

77 } 

78 }, 

79 }, 

80 "ValidationError": { 

81 "title": "ValidationError", 

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

83 "type": "object", 

84 "properties": { 

85 "loc": { 

86 "title": "Location", 

87 "type": "array", 

88 "items": { 

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

90 }, 

91 }, 

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

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

94 }, 

95 }, 

96 "HTTPValidationError": { 

97 "title": "HTTPValidationError", 

98 "type": "object", 

99 "properties": { 

100 "detail": { 

101 "title": "Detail", 

102 "type": "array", 

103 "items": {"$ref": "#/components/schemas/ValidationError"}, 

104 } 

105 }, 

106 }, 

107 } 

108 }, 

109 }