tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.34k stars 259 forks source link

Request: Add `permission_classes` to responses #1152

Open jvacek opened 8 months ago

jvacek commented 8 months ago

I've got a couple permissions.BasePermission classes that have the message field set on them.

class MyAccessPermissions(permissions.BasePermission):
    """Checks if the instance is accessible"""

    message = "instance does not own a subscription"

    def has_permission(self, request, view) -> bool:
        #code

It would be nice if these could be included under 403 in the responses list, with the messages in the examples.

tfranzel commented 8 months ago

Due to the inherent complexities and generic nature of DRF error handling, we cannot provide that feature in a reasonable manner. If you want to have that information in the schema, you need to use @extend_schema.

jvacek commented 8 months ago

Is there a way that I can annotate my permission classes directly at least, without having to annotate all the views that use them? I did not find any such mention in the documentation.

tfranzel commented 8 months ago

No, because permission classes would be only a part of the solution.

You can however, save the common stuff and e.g. reuse the list of responses for example.

People also have customized the AutoSchema: https://github.com/tfranzel/drf-spectacular/issues/101#issuecomment-1069383391

alxvallejo commented 3 months ago

you need to use @extend_schema

How do we use extend_schema to document permissions?