Dependecies overrides are not taken into account into the apenapi document #8392
-
First Check
Commit to Help
Example Codefrom fastapi import Depends, FastAPI, Header
from fastapi.security import OAuth2PasswordBearer
from fastapi.testclient import TestClient
app = FastAPI()
def get_user_id() -> int:
pass
def get_user(user_id=Depends(get_user_id)) -> str:
return f"{user_id}"
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def get_user_id_from_auth_override(token: str = Depends(oauth2_scheme)) -> int:
return 1
def get_user_id_from_header_override(user_id: int = Header()) -> int:
return user_id
@app.get("/user")
def read_user(
user: str = Depends(get_user),
):
return user
client = TestClient(app)
override_with_security_schema = {
"components": {
"securitySchemes": {
"OAuth2PasswordBearer": {
"flows": {"password": {"scopes": {}, "tokenUrl": "token"}},
"type": "oauth2",
}
}
},
"info": {"title": "FastAPI", "version": "0.1.0"},
"openapi": "3.0.2",
"paths": {
"/user": {
"get": {
"operationId": "read_user_user_get",
"responses": {
"200": {
"content": {"application/json": {"schema": {}}},
"description": "Successful " "Response",
}
},
"security": [{"OAuth2PasswordBearer": []}],
"summary": "Read User",
}
}
},
}
app.dependency_overrides[get_user_id] = get_user_id_from_auth_override
app.openapi_schema = None
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == override_with_security_schemaDescriptionDI is a great feature. We use it to develop reusable APIs routers packaged in different python modules. Depending of the customer requirements, the routers are assembled into a dedicated App .Each service requiring to get the user information declares a Operating SystemLinux Operating System DetailsNo response FastAPI Version0.85.0 Python Version3.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Any news about this proposal? It's very useful and allows to build reusable routers into different app.... |
Beta Was this translation helpful? Give feedback.
-
|
I think it works as it should work. |
Beta Was this translation helpful? Give feedback.
I think it works as it should work.
You shouldn't use
dependencies_overrideto change openapi schema. It's only for mocking some dependencies in tests