Package: drf-yasg-json-api
drf-yasg-json-api -drf-yasgmeetsJSON APIAutomated generation of Swagger/OpenAPI 2.0JSON APIspecifications from Django Rest Framework endpoints.This package makesdrf-yasg Yet Another Swagger GeneratorandDjango REST framework JSON APIplay together.Table of ContentsCompatibilityInstallationQuickstartExtending drg-yasg configurationRenderers and parsersFeaturesMain request/response JSON API schema support:datafield withid,type,relationships,attributesstructureincludedfield andincludequery paramfilterquery parampaginationAdditionalSupport forswagger_auto_schemadecorator ofdrf-yasgStrippingwrite_onlyfields from response andread_onlyfrom requestExtrax-writeOnlyandx-readOnlypropertiesCoexistence of JSON API views with pure REST API viewsCompatibilityDjango REST Framework JSON API:2.8,3.0,3.1,3.2,4.0,4.1,4.2,4.3,5.0Drf-yasg:1.16,1.17.0,1.17.1,1.20Django REST Framework:3.8,3.9,3.10,3.11,3.12,3.13Django:2.0,2.1,2.2,3.0,3.1,3.2,4.0Python:3.6,3.7,3.8,3.9Installationpip install -U drf-yasg-json-apiQuickstartFirst followdrf-yasg quickstart,
then extend the configuration in following way.Extending drg-yasg configurationAssuming you are using drf-yasg configuration like below (which is drf-yasg default):SWAGGER_SETTINGS = {
    'DEFAULT_AUTO_SCHEMA_CLASS': 'drf_yasg.inspectors.SwaggerAutoSchema',

    'DEFAULT_FIELD_INSPECTORS': [
        'drf_yasg.inspectors.CamelCaseJSONFilter',
        'drf_yasg.inspectors.RecursiveFieldInspector',
        'drf_yasg.inspectors.ReferencingSerializerInspector',
        'drf_yasg.inspectors.ChoiceFieldInspector',
        'drf_yasg.inspectors.FileFieldInspector',
        'drf_yasg.inspectors.DictFieldInspector',
        'drf_yasg.inspectors.JSONFieldInspector',
        'drf_yasg.inspectors.HiddenFieldInspector',
        'drf_yasg.inspectors.RelatedFieldInspector',
        'drf_yasg.inspectors.SerializerMethodFieldInspector',
        'drf_yasg.inspectors.SimpleFieldInspector',
        'drf_yasg.inspectors.StringDefaultFieldInspector',
    ],
    'DEFAULT_FILTER_INSPECTORS': [
        'drf_yasg.inspectors.CoreAPICompatInspector',
    ],
    'DEFAULT_PAGINATOR_INSPECTORS': [
        'drf_yasg.inspectors.DjangoRestResponsePagination',
        'drf_yasg.inspectors.CoreAPICompatInspector',
    ],
}Apply following changes:SWAGGER_SETTINGS = {
    'DEFAULT_AUTO_SCHEMA_CLASS': 'drf_yasg_json_api.inspectors.SwaggerAutoSchema',  # Overridden

    'DEFAULT_FIELD_INSPECTORS': [
        'drf_yasg_json_api.inspectors.NamesFormatFilter',  # Replaces CamelCaseJSONFilter
        'drf_yasg.inspectors.RecursiveFieldInspector',  
        'drf_yasg_json_api.inspectors.XPropertiesFilter',  # Added 
        'drf_yasg_json_api.inspectors.JSONAPISerializerSmartInspector',  # Added
        'drf_yasg.inspectors.ReferencingSerializerInspector',
        'drf_yasg_json_api.inspectors.IntegerIDFieldInspector',  # Added
        'drf_yasg.inspectors.ChoiceFieldInspector',
        'drf_yasg.inspectors.FileFieldInspector',
        'drf_yasg.inspectors.DictFieldInspector',
        'drf_yasg.inspectors.JSONFieldInspector',
        'drf_yasg.inspectors.HiddenFieldInspector',
        'drf_yasg_json_api.inspectors.ManyRelatedFieldInspector',  # Added
        'drf_yasg_json_api.inspectors.IntegerPrimaryKeyRelatedFieldInspector',  # Added 
        'drf_yasg.inspectors.RelatedFieldInspector',
        'drf_yasg.inspectors.SerializerMethodFieldInspector',
        'drf_yasg.inspectors.SimpleFieldInspector',
        'drf_yasg.inspectors.StringDefaultFieldInspector',

    ],
    'DEFAULT_FILTER_INSPECTORS': [
        'drf_yasg_json_api.inspectors.DjangoFilterInspector',  # Added (optional), requires django_filter 
        'drf_yasg.inspectors.CoreAPICompatInspector',
    ],
    'DEFAULT_PAGINATOR_INSPECTORS': [
        'drf_yasg_json_api.inspectors.DjangoRestResponsePagination',  # Added
        'drf_yasg.inspectors.DjangoRestResponsePagination',
        'drf_yasg.inspectors.CoreAPICompatInspector',
    ],
}Renderers and parsersJSON API schema of your view's response or request will be generated if you usedjango-rest-framework-json-api'sJSONAPIRendererorJSONAPIParserrespectively.But since you have already used them torenderorparse, not just togenerate schema(haven't you?),
you probably only need to alter the configuration as described above.That's it!FeaturesFields and query params extraction follows Django REST framework JSON API.Main request/response JSON API schema support:datafield withid,type,relationships,attributesstructureSchema based on view's main serializer. It accessed through view'sget_serializermethod,the same waydrf-yasgdoes it.UseGenericAPIVieworAPIViewand defineget_serializermanually.Fields and their source:id–idfieldorother serializer field that matches the modelpkfieldorin-the-fly generated serializer field for modelpktype– serializer's model JSON API resource nameorview's resource name,the same way Django REST framework JSON API does itrelationships– all serializer fields ofRelatedFieldandManyRelatedFieldclassattributes– all other serializer fieldsincludedfield andincludequery paramSchema based on serializers defined inincluded_serializerattribute of view's main serializer where each one is
treated in the same way as view's main serializer (datafield).filterquery paramIf view usesdjango_filters.DjangoFilterBackendas filter backend,
schema offilter[]query param will be generated based on view'sfilterset_fieldsattribute.paginationIf view usesJsonApiPageNumberPaginationorJsonApiLimitOffsetPaginationaspagination_class,
schema oflinksandmeta, consistent with those pagination types, will be generated.AdditionalSupport forswagger_auto_schemadecorator ofdrf-yasgJSON API schema is also generated for success responses (statuses 2XX) defined manually usingresponsesargument
ofswagger_auto_schemadecorator.Strippingwrite_onlyfields from response andread_onlyfrom requestdrf_yasg_json_api.inspectors.InlineSerializerSmartInspectorstrips fields inaccessible in request/response to
provide view of fields that arereallyavailable to use.You can revert to traditionaldrf-yasgview of all serializer fields in both response and request by replacing this
inspector withdrf_yasg_json_api.inspectors.InlineSerializerInspectorExtrax-writeOnlyandx-readOnlypropertiesdrf_yasg_json_api.inspectors.XPropertiesFilteruses:x-readOnlyto mark read only fields even if they are nestedx-witeOonlyadds missing support for write only fieldsCoexistence of JSON API views with pure REST API viewsJSON API docs will be generated bydrf_yasg_json_api.inspectors.JSONAPISerializerInspector,nonJSON API views are ignored by this inspector.Pure REST API docs will be generated bydrf-yasginspectors – eitherdrf_yasg.inspectors.ReferencingSerializerInspectorordrf_yasg.inspectors.InlineSerializerInspectordepending on
which one you prefer to use.RecursiveFieldRecursiveField follows different approach from JSON API, so it cannot be used withJSONAPISerializerInspector, but you can still have pure REST API views that will be documented
usingReferencingSerializerInspector.Alternatively, instead ofRecursiveFieldyou can useincluded_serializerswithself(e.g.included_serializers = {'related-obj': 'self'}) to implement limited in depth recursion
the JSON API way.