Extending generated OpenAPI docs (#799)

* Added login endpoint docs

* make format

* Changed login route into multiple examples.

* Added reset password router docs

* Updated /{id} routes for user

* Updated /me routes

* Fixed user already exists response description

* Updated the /register route

* Updated verify routes

* Updated oauth2 endpoints.

* Applied `make format`

* Renamed Authentication methods for getting their openapi schemas

- `get_login_responses_success` -> `get_openapi_login_responses_success`
- `get_logout_responses_success` -> `get_openapi_logout_responses_success`

* Fixed flake8 errors

* Not using `Final` to keep python37 compatibility

Co-authored-by: François Voron <fvoron@gmail.com>
This commit is contained in:
Matyáš Richter
2021-11-23 13:13:51 +01:00
committed by GitHub
parent 1f5ce51df2
commit c759bb6915
16 changed files with 546 additions and 35 deletions

View File

@ -1,10 +1,19 @@
from fastapi import APIRouter, Depends, HTTPException, Response, status
from fastapi.security import OAuth2PasswordRequestForm
from pydantic import BaseModel
from fastapi_users import models
from fastapi_users.authentication import Authenticator, BaseAuthentication
from fastapi_users.manager import BaseUserManager, UserManagerDependency
from fastapi_users.router.common import ErrorCode
from fastapi_users.router.common import ErrorCode, ErrorModel
class LoginBadCredentials(BaseModel):
detail: ErrorCode.LOGIN_BAD_CREDENTIALS
class LoginUserNotVerified(BaseModel):
detail: ErrorCode.LOGIN_USER_NOT_VERIFIED
def get_auth_router(
@ -19,7 +28,32 @@ def get_auth_router(
active=True, verified=requires_verification
)
@router.post("/login", name="auth:login")
login_responses = {
status.HTTP_400_BAD_REQUEST: {
"model": ErrorModel,
"content": {
"application/json": {
"examples": {
ErrorCode.LOGIN_BAD_CREDENTIALS: {
"summary": "Bad credentials or the user is inactive.",
"value": {"detail": ErrorCode.LOGIN_BAD_CREDENTIALS},
},
ErrorCode.LOGIN_USER_NOT_VERIFIED: {
"summary": "The user is not verified.",
"value": {"detail": ErrorCode.LOGIN_USER_NOT_VERIFIED},
},
}
}
},
},
**backend.get_openapi_login_responses_success(),
}
@router.post(
"/login",
name="auth:login",
responses=login_responses,
)
async def login(
response: Response,
credentials: OAuth2PasswordRequestForm = Depends(),
@ -40,8 +74,16 @@ def get_auth_router(
return await backend.get_login_response(user, response, user_manager)
if backend.logout:
logout_responses = {
**{
status.HTTP_401_UNAUTHORIZED: {
"description": "Missing token or inactive user."
}
},
**backend.get_openapi_logout_responses_success(),
}
@router.post("/logout", name="auth:logout")
@router.post("/logout", name="auth:logout", responses=logout_responses)
async def logout(
response: Response,
user=Depends(get_current_user),