Coverage for tests / test_additional_properties_bool.py: 100%

24 statements  

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

1from fastapi import FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4from pydantic import BaseModel, ConfigDict 1abcd

5 

6 

7class FooBaseModel(BaseModel): 1abcd

8 model_config = ConfigDict(extra="forbid") 1abcd

9 

10 

11class Foo(FooBaseModel): 1abcd

12 pass 1abcd

13 

14 

15app = FastAPI() 1abcd

16 

17 

18@app.post("/") 1abcd

19async def post( 1abcd

20 foo: Foo | None = None, 

21): 

22 return foo 1efgh

23 

24 

25client = TestClient(app) 1abcd

26 

27 

28def test_call_invalid(): 1abcd

29 response = client.post("/", json={"foo": {"bar": "baz"}}) 1mnop

30 assert response.status_code == 422 1mnop

31 

32 

33def test_call_valid(): 1abcd

34 response = client.post("/", json={}) 1efgh

35 assert response.status_code == 200 1efgh

36 assert response.json() == {} 1efgh

37 

38 

39def test_openapi_schema(): 1abcd

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

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

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

43 { 

44 "openapi": "3.1.0", 

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

46 "paths": { 

47 "/": { 

48 "post": { 

49 "summary": "Post", 

50 "operationId": "post__post", 

51 "requestBody": { 

52 "content": { 

53 "application/json": { 

54 "schema": { 

55 "anyOf": [ 

56 {"$ref": "#/components/schemas/Foo"}, 

57 {"type": "null"}, 

58 ], 

59 "title": "Foo", 

60 } 

61 } 

62 } 

63 }, 

64 "responses": { 

65 "200": { 

66 "description": "Successful Response", 

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

68 }, 

69 "422": { 

70 "description": "Validation Error", 

71 "content": { 

72 "application/json": { 

73 "schema": { 

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

75 } 

76 } 

77 }, 

78 }, 

79 }, 

80 } 

81 } 

82 }, 

83 "components": { 

84 "schemas": { 

85 "Foo": { 

86 "properties": {}, 

87 "additionalProperties": False, 

88 "type": "object", 

89 "title": "Foo", 

90 }, 

91 "HTTPValidationError": { 

92 "properties": { 

93 "detail": { 

94 "items": { 

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

96 }, 

97 "type": "array", 

98 "title": "Detail", 

99 } 

100 }, 

101 "type": "object", 

102 "title": "HTTPValidationError", 

103 }, 

104 "ValidationError": { 

105 "properties": { 

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

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

108 "loc": { 

109 "items": { 

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

111 }, 

112 "type": "array", 

113 "title": "Location", 

114 }, 

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

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

117 }, 

118 "type": "object", 

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

120 "title": "ValidationError", 

121 }, 

122 } 

123 }, 

124 } 

125 )