Coverage for tests / test_additional_properties.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from fastapi import FastAPI 1adbc

2from fastapi.testclient import TestClient 1adbc

3from inline_snapshot import snapshot 1adbc

4from pydantic import BaseModel 1adbc

5 

6app = FastAPI() 1adbc

7 

8 

9class Items(BaseModel): 1adbc

10 items: dict[str, int] 1abc

11 

12 

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

14def foo(items: Items): 1adbc

15 return items.items 1efgh

16 

17 

18client = TestClient(app) 1adbc

19 

20 

21def test_additional_properties_post(): 1adbc

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

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

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

25 

26 

27def test_openapi_schema(): 1adbc

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

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

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

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 )