Debian Codegraph @

tests/test_additional_properties_bool.py

raw
1 from fastapi import FastAPI
2 from fastapi.testclient import TestClient
3 from inline_snapshot import snapshot
4 from pydantic import BaseModel, ConfigDict
5
6
7 class FooBaseModel(BaseModel):
8 model_config = ConfigDict(extra="forbid")
9
10
11 class Foo(FooBaseModel):
12 pass
13
14
15 app = FastAPI()
16
17
18 @app.post("/")
19 async def post(
20 foo: Foo | None = None,
21 ):
22 return foo
23
24
25 client = TestClient(app)
26
27
28 def test_call_invalid():
29 response = client.post("/", json={"foo": {"bar": "baz"}})
30 assert response.status_code == 422
31
32
33 def test_call_valid():
34 response = client.post("/", json={})
35 assert response.status_code == 200
36 assert response.json() == {}
37
38
39 def test_openapi_schema():
40 response = client.get("/openapi.json")
41 assert response.status_code == 200, response.text
42 assert response.json() == snapshot(
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 )