Coverage for tests / test_union_body.py: 100%

26 statements  

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

1from typing import Optional, Union 1abcd

2 

3from fastapi import FastAPI 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6from pydantic import BaseModel 1abcd

7 

8app = FastAPI() 1abcd

9 

10 

11class Item(BaseModel): 1abcd

12 name: Optional[str] = None 1abcd

13 

14 

15class OtherItem(BaseModel): 1abcd

16 price: int 1abcd

17 

18 

19@app.post("/items/") 1abcd

20def save_union_body(item: Union[OtherItem, Item]): 1abcd

21 return {"item": item} 1efghij

22 

23 

24client = TestClient(app) 1abcd

25 

26 

27def test_post_other_item(): 1abcd

28 response = client.post("/items/", json={"price": 100}) 1fhj

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

30 assert response.json() == {"item": {"price": 100}} 1fhj

31 

32 

33def test_post_item(): 1abcd

34 response = client.post("/items/", json={"name": "Foo"}) 1egi

35 assert response.status_code == 200, response.text 1egi

36 assert response.json() == {"item": {"name": "Foo"}} 1egi

37 

38 

39def test_openapi_schema(): 1abcd

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

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

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

43 { 

44 "openapi": "3.1.0", 

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

46 "paths": { 

47 "/items/": { 

48 "post": { 

49 "responses": { 

50 "200": { 

51 "description": "Successful Response", 

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

53 }, 

54 "422": { 

55 "description": "Validation Error", 

56 "content": { 

57 "application/json": { 

58 "schema": { 

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

60 } 

61 } 

62 }, 

63 }, 

64 }, 

65 "summary": "Save Union Body", 

66 "operationId": "save_union_body_items__post", 

67 "requestBody": { 

68 "content": { 

69 "application/json": { 

70 "schema": { 

71 "title": "Item", 

72 "anyOf": [ 

73 {"$ref": "#/components/schemas/OtherItem"}, 

74 {"$ref": "#/components/schemas/Item"}, 

75 ], 

76 } 

77 } 

78 }, 

79 "required": True, 

80 }, 

81 } 

82 } 

83 }, 

84 "components": { 

85 "schemas": { 

86 "OtherItem": { 

87 "title": "OtherItem", 

88 "required": ["price"], 

89 "type": "object", 

90 "properties": {"price": {"title": "Price", "type": "integer"}}, 

91 }, 

92 "Item": { 

93 "title": "Item", 

94 "type": "object", 

95 "properties": { 

96 "name": { 

97 "title": "Name", 

98 "anyOf": [{"type": "string"}, {"type": "null"}], 

99 } 

100 }, 

101 }, 

102 "ValidationError": { 

103 "title": "ValidationError", 

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

105 "type": "object", 

106 "properties": { 

107 "loc": { 

108 "title": "Location", 

109 "type": "array", 

110 "items": { 

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

112 }, 

113 }, 

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

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

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

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

118 }, 

119 }, 

120 "HTTPValidationError": { 

121 "title": "HTTPValidationError", 

122 "type": "object", 

123 "properties": { 

124 "detail": { 

125 "title": "Detail", 

126 "type": "array", 

127 "items": { 

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

129 }, 

130 } 

131 }, 

132 }, 

133 } 

134 }, 

135 } 

136 )