-
-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenAPI schema generation #292
Comments
I was able to do this with the Login view by overriding it. (Should probably make this a pull request). from knox.serializers import UserSerializer as LoginUserSerializer # or whatever serializer you use
# Serializer to mimic the response from a Knox login
class LoginResponseSerializer(serializers.Serializer):
token = serializers.CharField()
expiry = serializers.DateTimeField()
user = LoginUserSerializer() Then extend the schema for the login view: from drf_spectacular.utils import extend_schema
from rest_framework.authtoken.serializers import AuthTokenSerializer
class LoginView(KnoxLoginView):
"""
Login to the API
This endpoint permits a user to login and use the API.
"""
@extend_schema(
request=AuthTokenSerializer, responses={200: LoginResponseSerializer}
)
def post(self, request, format=None):
return super().post(request, format=format) I've added quite a bit more to my login view so I'm not sure if this works exactly as I've written it, but it should get you going in the right direction at least. |
Moving my comments from #310 here. I've recently started using this with Ping me if you want to take a stab at this! I can review/merge. If anyone is also trying to use this with from drf_spectacular.extensions import (
OpenApiAuthenticationExtension,
OpenApiViewExtension,
)
from rest_framework import serializers
class KnoxAuthentication(OpenApiAuthenticationExtension):
"""
Knox authentication Open API definition.
"""
target_class = "knox.auth.TokenAuthentication"
name = "TokenAuthentication"
def get_security_definition(self, auto_schema):
"""
Custom definition for APIView.
"""
return {
"type": "apiKey",
"in": "header",
"name": "Authorization",
}
class LogoutResponseSerializer(serializers.Serializer):
"""
Empty logout response serializer
"""
class FixLogoutView(OpenApiViewExtension):
target_class = "knox.views.LogoutView"
def view_replacement(self):
"""
Fix view
"""
class Fixed(self.target_class):
serializer_class = LogoutResponseSerializer
return Fixed
class FixLogoutAllView(OpenApiViewExtension):
target_class = "knox.views.LogoutAllView"
def view_replacement(self):
"""
Fix view
"""
class Fixed(self.target_class):
serializer_class = LogoutResponseSerializer
return Fixed This doesn't yield a 1 to 1 to the API output response, but it doesn't matter for my usage. Note: be mindful of the settings if you are re-using this, they are specific to my projects. |
What is the recommended way to automatically generate an OpenAPI schema of the knox API endpoints? Neither the in the Django REST framework integrated schema builder (
python manage.py generateschema
) nor drf-spectacular seem to work out of the box.generateschema:
spectacular
The text was updated successfully, but these errors were encountered: