Debian Codegraph @

tests/test_repeated_dependency_schema.py

raw
1 from fastapi import Depends, FastAPI, Header, status
2 from fastapi.testclient import TestClient
3 from inline_snapshot import snapshot
4
5 app = FastAPI()
6
7
8 def get_header(*, someheader: str = Header()):
9 return someheader
10
11
12 def get_something_else(*, someheader: str = Depends(get_header)):
13 return f"{someheader}123"
14
15
16 @app.get("/")
17 def get_deps(dep1: str = Depends(get_header), dep2: str = Depends(get_something_else)):
18 return {"dep1": dep1, "dep2": dep2}
19
20
21 client = TestClient(app)
22
23
24 def test_response():
25 response = client.get("/", headers={"someheader": "hello"})
26 assert response.status_code == status.HTTP_200_OK
27 assert response.json() == {"dep1": "hello", "dep2": "hello123"}
28
29
30 def test_openapi_schema():
31 response = client.get("/openapi.json")
32 assert response.status_code == status.HTTP_200_OK
33 actual_schema = response.json()
34 assert (
35 len(actual_schema["paths"]["/"]["get"]["parameters"]) == 1
36 ) # primary goal of this test
37 assert actual_schema == snapshot(
38 {
39 "components": {
40 "schemas": {
41 "HTTPValidationError": {
42 "properties": {
43 "detail": {
44 "items": {
45 "$ref": "#/components/schemas/ValidationError"
46 },
47 "title": "Detail",
48 "type": "array",
49 }
50 },
51 "title": "HTTPValidationError",
52 "type": "object",
53 },
54 "ValidationError": {
55 "properties": {
56 "ctx": {"title": "Context", "type": "object"},
57 "input": {"title": "Input"},
58 "loc": {
59 "items": {
60 "anyOf": [{"type": "string"}, {"type": "integer"}]
61 },
62 "title": "Location",
63 "type": "array",
64 },
65 "msg": {"title": "Message", "type": "string"},
66 "type": {"title": "Error Type", "type": "string"},
67 },
68 "required": ["loc", "msg", "type"],
69 "title": "ValidationError",
70 "type": "object",
71 },
72 }
73 },
74 "info": {"title": "FastAPI", "version": "0.1.0"},
75 "openapi": "3.1.0",
76 "paths": {
77 "/": {
78 "get": {
79 "operationId": "get_deps__get",
80 "parameters": [
81 {
82 "in": "header",
83 "name": "someheader",
84 "required": True,
85 "schema": {"title": "Someheader", "type": "string"},
86 }
87 ],
88 "responses": {
89 "200": {
90 "content": {"application/json": {"schema": {}}},
91 "description": "Successful Response",
92 },
93 "422": {
94 "content": {
95 "application/json": {
96 "schema": {
97 "$ref": "#/components/schemas/HTTPValidationError"
98 }
99 }
100 },
101 "description": "Validation Error",
102 },
103 },
104 "summary": "Get Deps",
105 }
106 }
107 },
108 }
109 )