Coverage for tests / test_additional_properties_bool.py: 100%

25 statements  

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

1from typing import Union 1abcd

2 

3from fastapi import FastAPI 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6from pydantic import BaseModel, ConfigDict 1abcd

7 

8 

9class FooBaseModel(BaseModel): 1abcd

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

11 

12 

13class Foo(FooBaseModel): 1abcd

14 pass 1abcd

15 

16 

17app = FastAPI() 1abcd

18 

19 

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

21async def post( 1abcd

22 foo: Union[Foo, None] = None, 

23): 

24 return foo 1efg

25 

26 

27client = TestClient(app) 1abcd

28 

29 

30def test_call_invalid(): 1abcd

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

32 assert response.status_code == 422 1klm

33 

34 

35def test_call_valid(): 1abcd

36 response = client.post("/", json={}) 1efg

37 assert response.status_code == 200 1efg

38 assert response.json() == {} 1efg

39 

40 

41def test_openapi_schema(): 1abcd

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

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

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

45 { 

46 "openapi": "3.1.0", 

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

48 "paths": { 

49 "/": { 

50 "post": { 

51 "summary": "Post", 

52 "operationId": "post__post", 

53 "requestBody": { 

54 "content": { 

55 "application/json": { 

56 "schema": { 

57 "anyOf": [ 

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

59 {"type": "null"}, 

60 ], 

61 "title": "Foo", 

62 } 

63 } 

64 } 

65 }, 

66 "responses": { 

67 "200": { 

68 "description": "Successful Response", 

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

70 }, 

71 "422": { 

72 "description": "Validation Error", 

73 "content": { 

74 "application/json": { 

75 "schema": { 

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

77 } 

78 } 

79 }, 

80 }, 

81 }, 

82 } 

83 } 

84 }, 

85 "components": { 

86 "schemas": { 

87 "Foo": { 

88 "properties": {}, 

89 "additionalProperties": False, 

90 "type": "object", 

91 "title": "Foo", 

92 }, 

93 "HTTPValidationError": { 

94 "properties": { 

95 "detail": { 

96 "items": { 

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

98 }, 

99 "type": "array", 

100 "title": "Detail", 

101 } 

102 }, 

103 "type": "object", 

104 "title": "HTTPValidationError", 

105 }, 

106 "ValidationError": { 

107 "properties": { 

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

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

110 "loc": { 

111 "items": { 

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

113 }, 

114 "type": "array", 

115 "title": "Location", 

116 }, 

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

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

119 }, 

120 "type": "object", 

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

122 "title": "ValidationError", 

123 }, 

124 } 

125 }, 

126 } 

127 )