Coverage for tests / test_additional_properties.py: 100%

19 statements  

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

1from fastapi import FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4from pydantic import BaseModel 1abcd

5 

6app = FastAPI() 1abcd

7 

8 

9class Items(BaseModel): 1abcd

10 items: dict[str, int] 1abcd

11 

12 

13@app.post("/foo") 1abcd

14def foo(items: Items): 1abcd

15 return items.items 1efg

16 

17 

18client = TestClient(app) 1abcd

19 

20 

21def test_additional_properties_post(): 1abcd

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

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

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

25 

26 

27def test_openapi_schema(): 1abcd

28 response = client.get("/openapi.json") 1hij

29 assert response.status_code == 200, response.text 1hij

30 assert response.json() == snapshot( 1hij

31 { 

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 "input": {"title": "Input"}, 

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

96 }, 

97 }, 

98 "HTTPValidationError": { 

99 "title": "HTTPValidationError", 

100 "type": "object", 

101 "properties": { 

102 "detail": { 

103 "title": "Detail", 

104 "type": "array", 

105 "items": { 

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

107 }, 

108 } 

109 }, 

110 }, 

111 } 

112 }, 

113 } 

114 )