All Projects → tfranzel → Drf Spectacular

tfranzel / Drf Spectacular

Licence: bsd-3-clause
Sane and flexible OpenAPI 3 schema generation for Django REST framework.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Drf Spectacular

Drf Yasg
Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
Stars: ✭ 2,523 (+509.42%)
Mutual labels:  rest-api, swagger, openapi-specification, documentation-generator, django
Swagger Core
Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
Stars: ✭ 6,898 (+1566.18%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Swagger Editor
Swagger Editor
Stars: ✭ 7,365 (+1678.99%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+100.72%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Swagger Inflector
Stars: ✭ 131 (-68.36%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Swagger Parser
Swagger Spec to Java POJOs
Stars: ✭ 468 (+13.04%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Springdoc Openapi
Library for OpenAPI 3 with spring-boot
Stars: ✭ 1,113 (+168.84%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Swagger Js
Javascript library to connect to swagger-enabled APIs via browser or nodejs
Stars: ✭ 2,319 (+460.14%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Redoc
📘 OpenAPI/Swagger-generated API Reference Documentation
Stars: ✭ 15,935 (+3749.03%)
Mutual labels:  swagger, openapi3, documentation-generator, openapi-specification
Swagger Ui
Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
Stars: ✭ 21,279 (+5039.86%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Swagger Codegen
swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
Stars: ✭ 13,859 (+3247.58%)
Mutual labels:  rest-api, swagger, openapi3, openapi-specification
Openapi To Graphql
Translate APIs described by OpenAPI Specifications (OAS) into GraphQL
Stars: ✭ 973 (+135.02%)
Mutual labels:  rest-api, swagger, openapi-specification
openapi-schemas
JSON Schemas for every version of the OpenAPI Specification
Stars: ✭ 22 (-94.69%)
Mutual labels:  swagger, openapi-specification, openapi3
Drf Autodocs
Ultimately automated DRF documentation rendering(UNMAINTAINED)
Stars: ✭ 82 (-80.19%)
Mutual labels:  rest-api, documentation-generator, django
Django Ninja
💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
Stars: ✭ 875 (+111.35%)
Mutual labels:  rest-api, swagger, django
Safrs
SqlAlchemy Flask-Restful Swagger Json:API OpenAPI
Stars: ✭ 255 (-38.41%)
Mutual labels:  rest-api, swagger, openapi3
Pretty Swag
Pretty UI for Swagger spec
Stars: ✭ 112 (-72.95%)
Mutual labels:  rest-api, swagger, documentation-generator
Openapi.tools
A collection of Editors, Linters, Parsers, Code Generators, Documentation, Testing
Stars: ✭ 257 (-37.92%)
Mutual labels:  swagger, openapi3, openapi-specification
Openapi Directory
🌐 Wikipedia for Web APIs. Directory of REST API definitions in OpenAPI 2.0/3.x format
Stars: ✭ 2,635 (+536.47%)
Mutual labels:  rest-api, swagger, openapi3
Oapi Codegen
Generate Go client and server boilerplate from OpenAPI 3 specifications
Stars: ✭ 806 (+94.69%)
Mutual labels:  rest-api, swagger, openapi3

=============== drf-spectacular

|build-status-image| |codecov| |pypi-version| |docs|

Sane and flexible OpenAPI 3.0 <https://github.com/OAI/OpenAPI-Specification>_ schema generation for Django REST framework <https://www.django-rest-framework.org/>_.

This project has 3 goals: 1. Extract as much schema information from DRF as possible. 2. Provide flexibility to make the schema usable in the real world (not only toy examples). 3. Generate a schema that works well with the most popular client generators.

The code is a heavily modified fork of the DRF OpenAPI generator <https://github.com/encode/django-rest-framework/blob/master/rest_framework/schemas/openapi.py/>_, which is/was lacking all of the below listed features.

Features - Serializers modelled as components. (arbitrary nesting and recursion supported) - @extend_schema <https://drf-spectacular.readthedocs.io/en/latest/drf_spectacular.html#drf_spectacular.utils.extend_schema>_ decorator for customization of APIView, Viewsets, function-based views, and @action - additional parameters - request/response serializer override (with status codes) - polymorphic responses either manually with PolymorphicProxySerializer helper or via rest_polymorphic's PolymorphicSerializer) - ... and more customization options - Authentication support (DRF natives included, easily extendable) - Custom serializer class support (easily extendable) - MethodSerializerField() type via type hinting or @extend_schema_field - i18n support - Tags extraction - Request/response/parameter examples - Description extraction from docstrings - Sane fallbacks - Sane operation_id naming (based on path) - Schema serving with SpectacularAPIView (Redoc and Swagger-UI views are also available) - Optional input/output serializer component split - Included support for: - django-polymorphic <https://github.com/django-polymorphic/django-polymorphic>_ / django-rest-polymorphic <https://github.com/apirobot/django-rest-polymorphic>_ - SimpleJWT <https://github.com/SimpleJWT/django-rest-framework-simplejwt>_ - DjangoOAuthToolkit <https://github.com/jazzband/django-oauth-toolkit>_ - djangorestframework-jwt <https://github.com/jpadilla/django-rest-framework-jwt>_ (tested fork drf-jwt <https://github.com/Styria-Digital/django-rest-framework-jwt>) - dj-rest-auth <https://github.com/jazzband/dj-rest-auth> (maintained fork of django-rest-auth <https://github.com/Tivix/django-rest-auth>) - djangorestframework-camel-case <https://github.com/vbabiy/djangorestframework-camel-case> (via postprocessing hook camelize_serializer_fields) - django-filter <https://github.com/carltongibson/django-filter>_

For more information visit the documentation <https://drf-spectacular.readthedocs.io>_.

License

Provided by T. Franzel <https://github.com/tfranzel>, Cashlink Technologies GmbH. Licensed under 3-Clause BSD <https://github.com/tfranzel/drf-spectacular/blob/master/LICENSE>.

Requirements

  • Python >= 3.6
  • Django (2.2, 3.0, 3.1)
  • Django REST Framework (3.10, 3.11)

Installation

Install using pip\ ...

.. code:: bash

$ pip install drf-spectacular

then add drf-spectacular to installed apps in settings.py

.. code:: python

INSTALLED_APPS = [
    # ALL YOUR APPS
    'drf_spectacular',
]

and finally register our spectacular AutoSchema with DRF

.. code:: python

REST_FRAMEWORK = {
    # YOUR SETTINGS
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

Release management ^^^^^^^^^^^^^^^^^^

drf-spectacular deliberately stays below version 1.x.x to signal that every new version may potentially break you. For production we strongly recommend pinning the version and inspecting a schema diff on update.

With that said, we aim to be extremely defensive w.r.t. breaking API changes. However, we also acknowledge the fact that even slight schema changes may break your toolchain, as any existing bug may somehow also be used as a feature.

We define version increments with the following semantics. y-stream increments may contain potentially breaking changes to both API and schema. z-stream increments will never break the API and may only contain schema changes that should have a low chance of breaking you.

Take it for a spin

Generate your schema with the CLI:

.. code:: bash

$ ./manage.py spectacular --file schema.yml
$ docker run -p 80:8080 -e SWAGGER_JSON=/schema.yml -v ${PWD}/schema.yml:/schema.yml swaggerapi/swagger-ui

If you also want to validate your schema add the --validate flag. Or serve your schema directly from your API. We also provide convenience wrappers for swagger-ui or redoc.

.. code:: python

from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
urlpatterns = [
    # YOUR PATTERNS
    path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
    # Optional UI:
    path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]

Usage

drf-spectacular works pretty well out of the box. You might also want to set some metadata for your API. Just create a SPECTACULAR_SETTINGS dictionary in your settings.py and override the defaults. Have a look at the available settings <https://drf-spectacular.readthedocs.io/en/latest/settings.html>_.

The toy examples do not cover your cases? No problem, you can heavily customize how your schema will be rendered.

Customization by using @extend_schema ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Most customization cases should be covered by the extend_schema decorator. We usually get pretty far with specifying OpenApiParameter and splitting request/response serializers, but the sky is the limit.

.. code:: python

from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiExample
from drf_spectacular.types import OpenApiTypes

class AlbumViewset(viewset.ModelViewset)
    serializer_class = AlbumSerializer

    @extend_schema(
        request=AlbumCreationSerializer
        responses={201: AlbumSerializer},
    )
    def create(self, request):
        # your non-standard behaviour
        return super().create(request)

    @extend_schema(
        # extra parameters added to the schema
        parameters=[
            OpenApiParameter(name='artist', description='Filter by artist', required=False, type=str),
            OpenApiParameter(
                name='release',
                type=OpenApiTypes.DATE,
                location=OpenApiParameter.QUERY,
                description='Filter by release date',
                examples=[
                    OpenApiExample(
                        'Example 1',
                        summary='short optional summary',
                        description='longer description',
                        value='1993-08-23'
                    ),
                    ...
                ],
            ),
        ],
        # override default docstring extraction
        description='More descriptive text',
        # provide Authentication class that deviates from the views default
        auth=None,
        # change the auto-generated operation name
        operation_id=None,
        # or even completely override what AutoSchema would generate. Provide raw Open API spec as Dict.
        operation=None,
        # attach request/response examples to the operation.
        examples=[
            OpenApiExample(
                'Example 1',
                description='longer description',
                value=...
            ),
            ...
        ],
    )
    def list(self, request):
        # your non-standard behaviour
        return super().list(request)

    @extend_schema(
        request=AlbumLikeSerializer
        responses={204: None},
    )
    @action(detail=True, methods=['post'])
    def set_password(self, request, pk=None):
        # your action behaviour

More customization ^^^^^^^^^^^^^^^^^^

Still not satisifed? You want more! We still got you covered. Visit customization <https://drf-spectacular.readthedocs.io/en/latest/customization.html>_ for more information.

Testing

Install testing requirements.

.. code:: bash

$ pip install -r requirements.txt

Run with runtests.

.. code:: bash

$ ./runtests.py

You can also use the excellent tox_ testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:

.. code:: bash

$ tox

.. _tox: http://tox.readthedocs.org/en/latest/

.. |build-status-image| image:: https://api.travis-ci.com/tfranzel/drf-spectacular.svg?branch=master :target: https://travis-ci.com/tfranzel/drf-spectacular?branch=master .. |pypi-version| image:: https://img.shields.io/pypi/v/drf-spectacular.svg :target: https://pypi.python.org/pypi/drf-spectacular .. |codecov| image:: https://codecov.io/gh/tfranzel/drf-spectacular/branch/master/graph/badge.svg :target: https://codecov.io/gh/tfranzel/drf-spectacular .. |docs| image:: https://readthedocs.org/projects/drf-spectacular/badge/ :target: https://drf-spectacular.readthedocs.io/

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].