drf-spectacular | flexible OpenAPI 3 schema generation for Django REST | REST library

 by   tfranzel Python Version: 0.27.1 License: BSD-3-Clause

kandi X-RAY | drf-spectacular Summary

kandi X-RAY | drf-spectacular Summary

drf-spectacular is a Python library typically used in Web Services, REST, Swagger applications. drf-spectacular has no bugs, it has no vulnerabilities, it has build file available, it has a Permissive License and it has medium support. You can install using 'pip install drf-spectacular' or download it from GitHub, PyPI.

Sane and flexible OpenAPI 3 schema generation for Django REST framework.

            kandi-support Support

              drf-spectacular has a medium active ecosystem.
              It has 1692 star(s) with 201 fork(s). There are 12 watchers for this library.
              There were 3 major release(s) in the last 6 months.
              There are 61 open issues and 678 have been closed. On average issues are closed in 13 days. There are 6 open pull requests and 0 closed requests.
              It has a neutral sentiment in the developer community.
              The latest version of drf-spectacular is 0.27.1

            kandi-Quality Quality

              drf-spectacular has 0 bugs and 0 code smells.

            kandi-Security Security

              drf-spectacular has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              drf-spectacular code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              drf-spectacular is licensed under the BSD-3-Clause License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              drf-spectacular releases are available to install and integrate.
              Deployable package is available in PyPI.
              Build file is available. You can build the component from source.
              drf-spectacular saves you 3081 person hours of effort in developing the same functionality from scratch.
              It has 11249 lines of code, 885 functions and 87 files.
              It has low code complexity. Code complexity directly impacts maintainability of the code.

            Top functions reviewed by kandi - BETA

            kandi has reviewed drf-spectacular and discovered the below as its top functions. This is intended to give you an instant insight into drf-spectacular implemented functionality, and help decide if they suit your requirements.
            • Postprocess schema enums
            • Log a warning
            • Emit a message
            • Get the override value for a property
            • Map serializer to serializer
            • Return the name of the serializer
            • Force a serializer instance
            • Filter the supported arguments
            • Render the given schema
            • Return a JSON response
            • Create a lazy serializer
            • Maps the serializer to a mapping
            • Check the schema
            • Handle GET requests
            • Builds a GeoFeatureModelSerializerSerializer
            • Get API endpoints
            • Load the target class
            • Returns the serializer for the given auto_schema
            • Return a serializer for the given auto_schema
            • Returns a list of parameters for a given auto_schema
            • Return a fixed view
            • Handles GET request
            • Replacement for view replacement
            • Maps the serializer to the given direction
            • Creates a replacement for SubscriptionSerializer
            • Handle GET request
            Get all kandi verified functions for this library.

            drf-spectacular Key Features

            No Key Features are available at this moment for drf-spectacular.

            drf-spectacular Examples and Code Snippets

            drf-spectacular: Add OpenApiResponse to a serializer-less function-based view
            Pythondot img1Lines of Code : 25dot img1License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            from drf_spectacular.utils import extend_schema, OpenApiParameter, inline_serializer
            # ...
                    OpenApiParameter(name="callsign", required=True, type=str),
                description="Get an APRS-IS passco
            Problem to use @extend_schema over an @actoin in DRF
            Pythondot img2Lines of Code : 16dot img2License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
                @extend_schema(summary="short summary")
                @action(detail=True, methods=["GET"], url_name="command")
                def command(self, request, pk) -> Union[Response, Http404]:  # Retrieve Command
                    data = request.data
                    status = g
            Django-Rest-Framework: documenting parameters of function-based view without model
            Pythondot img3Lines of Code : 25dot img3License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
                    OpenApiParameter(name='callsign', description='', 
                                     required=True, type=str),
                description='More descriptive text'
            def get_passcode(request):
                callsign = reque
            Django Swagger won't allow me to use POST method (no parameters shown)
            Pythondot img4Lines of Code : 13dot img4License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            class ProjectView(mixins.RetrieveModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):
                permission_classes = [permissions.IsAuthenticated, ] 
                serializer_class = ProjectSerializer
                queryset = Project.objects.all()
            drf-spectacular: Specify empty payload using @extend_schema
            Pythondot img5Lines of Code : 5dot img5License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            class EmptyPayloadAPI(APIView):
                @extend_schema(request=None, responses=EmptyPayloadResponseSerializer)
                def post(self, request, *args, **kwargs):
                    # some actions
                    return Response(data={"detail": "Success"})
            drf-spectacular: Add description to HTTP_204
            Pythondot img6Lines of Code : 10dot img6License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            from drf_spectacular.utils import OpenApiResponse, extend_schema
                description='creation description', 
                    201: OpenApiResponse(response=int, description='creation with int response.'),

            Community Discussions


            Django drf-spectacular with FirebaseBackend auth
            Asked 2022-Mar-27 at 23:01

            To give as much context as possible; I have two problems while using drf-spectacular to build my API documentation;

            1. With my default configuration, I cannot even load the documentation's page because I have a custom auth backend (using firebase_admin 5.2.0), the swagger (or ReDoc) are part of my REST API, therefore, would have to pass a valid token to load the page allocated in the documentation's endpoint (which is not possible from a browser).

            2. Second, and most importantly, I am not able to configure my custom firebase auth with drf-spectacular to implement a Swagger authentication method to execute against my API endpoints. I would be just fine having the possibility to add a token in the Swagger doc, do not need to have all Firebase auth credentials, URLs, and flow.

            • api/urls.py


            Answered 2022-Mar-27 at 23:01
            1. By default spectacular uses 'SERVE_PERMISSIONS': ['rest_framework.permissions.AllowAny'], which should allow opening the swagger page even if not authenticated. Maybe FirebaseBackend bails hard which prevents you ever getting to AllowAny. Try this to take Firebase out of the equation for the schema views:

            Source https://stackoverflow.com/questions/71526560


            How to document individual actions of a ViewSet using `drf-spectacular`?
            Asked 2022-Feb-01 at 16:06

            Using DRF's built-in way of documenting the API, I was able to write a docstring like the following and each action was documented by its corresponding line:



            Answered 2022-Feb-01 at 16:06

            I have just found out that the library provides very in-depth documentation functionality via their decorators. Without overriding any of the ViewSet methods, the above docstring could be written as:

            Source https://stackoverflow.com/questions/70941962


            drf-spectacular: Add OpenApiResponse to a serializer-less function-based view
            Asked 2022-Jan-19 at 02:37

            So, I'm documenting the following piece of code using drf-spectacular:



            Answered 2022-Jan-01 at 18:40

            If you don't have a Serializer, you can use inline_serializer:

            Source https://stackoverflow.com/questions/70550665


            Problem to use @extend_schema over an @actoin in DRF
            Asked 2022-Jan-03 at 01:55

            hi I have a @extend_schema of drf_spectacular library in my code I need to use it over my @action to customize the detail in OpenAPI, but I get errors like that



            Answered 2022-Jan-03 at 01:55

            I fixed this problem by adding more @extend_schema to other extra endpoints, if did not that you get an error like that I take

            Source https://stackoverflow.com/questions/70559799


            Schema for Rest Framework serializer with modified to_representation
            Asked 2021-Dec-28 at 23:42

            I implemented a modification of the to_representation method of a serializer in order to flatten a nested relation through



            Answered 2021-Dec-28 at 23:42

            You can try to define fields used in GeneralInformationSerializer and use source property eg.:

            Source https://stackoverflow.com/questions/70510361


            Django Swagger won't allow me to use POST method (no parameters shown)
            Asked 2021-Nov-27 at 18:45

            I'm using djangorestframework together with drf-spectacular modules for a Django project, and I'm trying to build some basic API methods for my Project model. Its structure looks like this:



            Answered 2021-Nov-27 at 18:45

            Swagger Grabs the fields from a serializer_class variable. I really recommend you change the format to Class-Based Views. Something using mixins or generic class.

            Your view could be like

            Source https://stackoverflow.com/questions/70134679


            Django REST drf-spectacular wrong detection of nested fields
            Asked 2021-Nov-24 at 12:21

            if there are more than one fields with relations to other models and depth>=1 is specified on the serializer Meta class, in the example generated for the corresponding url, all of the relation fields have the same object as their value, for example:




            Answered 2021-Nov-24 at 12:21

            This is from the DRF documentation:

            The depth option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation. If you want to customize the way the serialization is done you'll need to define the field yourself.

            The problem is that drf-spectacular has a hard time knowing how the nested fields should be represented beforehand, since there is no "explicit" serializer.

            I believe when depth is used, DRF simply passes nested data through a on-the-fly ModelSerializer and then does json.dump.

            Your second OrderSerializer is the preferred way. It gives you more guarantees for you data exposure, and as an added benefit is parse-able by spectacular.

            TL;DR: depth is not supported at the moment. If you think this should be supported, open a github issue.

            Source https://stackoverflow.com/questions/69916512


            Django drf-spectacular - Can you exclude specific paths?
            Asked 2021-Nov-10 at 12:03

            We have a bunch of api's with different versions in urls.py, eg

            • api/v1
            • api/v2
            • api/v3

            . We want to implement swagger with drf-spectacular, but we only want to expose api/v3 endpoints.

            Is there a way to do this? I cannot make sense of the documentation.




            Answered 2021-Aug-27 at 18:45

            Worked it out. Copied the custom preprocessing hook function, changed the pass statement to filter what I did not require in the endpoints, then mapped the location of the file and function in my spectacular settings for preprocessed hooks.

            Source https://stackoverflow.com/questions/68943065


            How to send custom JSON headers with requests in drf_spectacular (django)?
            Asked 2021-Nov-01 at 15:53

            Can I create an custom description of JSON headers in drf_spectacular without using serializers class in @extend_schema decorator?



            Answered 2021-Nov-01 at 15:53


            Define component schema with drf-spectacular for django API
            Asked 2021-Nov-01 at 15:25

            I am using drf-spectacular to generate an OpenAPI schema for django. As I am not using serializers, I am defining everything in the extend_schema decorator. Now my question is, if it is possible to manually define a component schema.

            Here is an example what my api view looks like:



            Answered 2021-Nov-01 at 15:25

            OpenApiTypes.OBJECT means that the response object can have any amount of fields. Of course Swagger UI cannot know those fields in advance and thus it shows {}, which may be unintuitive but it is correct.

            What you want is to give your response some structure. Everything in spectacular revolves around serializers/components. If you don't want to have explicit serializers, you can create implicit ones with inline_serializer

            Source https://stackoverflow.com/questions/69482097

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network


            No vulnerabilities reported

            Install drf-spectacular

            You can install using 'pip install drf-spectacular' or download it from GitHub, PyPI.
            You can use drf-spectacular like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.


            For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
          • PyPI

            pip install drf-spectacular

          • CLONE
          • HTTPS


          • CLI

            gh repo clone tfranzel/drf-spectacular

          • sshUrl


          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link