Debian Codegraph @

tests/test_additional_properties.py

raw
1 from fastapi import FastAPI
2 from fastapi.testclient import TestClient
3 from inline_snapshot import snapshot
4 from pydantic import BaseModel
5
6 app = FastAPI()
7
8
9 class Items(BaseModel):
10 items: dict[str, int]
11
12
13 @app.post("/foo")
14 def foo(items: Items):
15 return items.items
16
17
18 client = TestClient(app)
19
20
21 def test_additional_properties_post():
22 response = client.post("/foo", json={"items": {"foo": 1, "bar": 2}})
23 assert response.status_code == 200, response.text
24 assert response.json() == {"foo": 1, "bar": 2}
25
26
27 def test_openapi_schema():
28 response = client.get("/openapi.json")
29 assert response.status_code == 200, response.text
30 assert response.json() == snapshot(
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 )