Skip to main content

PIK DRF openapi extensions

Project description

PIK OPENAPI Tools

Some PIK protocol DRF openapi extensions:

  • inspection
    • Schema version fetching from RELEASE env variable.
    • Schema title generation as f'{settings.SERVICE_TITLE} API'.
    • Components schemas generation from ModelSerializers.
    • Schema description fetching from settings.SERVICE_DESCRIPTION
    • SerializerMethodField type hint introspection.
    • type field enum
    • model introspection: -label as title
      • help_text as description
      • label as x-title-plural
    • correct ListField item typing
    • choice labels as x-enumNames
    • deprecated fields marking
    • NullBooleanField type fix
    • Type hints for SerializerMethodField
    • JSONField schema
    • inspect filters for list endpoint only
    • Schema customization with update_schema property or method
    • Operation labels & description
    • RESTQL parameter schema
  • etc
    • Automatic camelCase translation
      • schema
      • filters
      • params
      • restql
    • PIK OpenID Schema
  • renderers
    • CachedRenderer mixin - returns pre-rendenred file if exists:
      • replaces "/" with "_" to get filename
      • /api/v1/schema/ searches {STATIC_ROOT}/_api_v1_schema_.html
      • /api/v1/schema/?format=openapi searches {STATIC_ROOT}/_api_v1_schema_.yaml
      • /api/v1/schema/?format=openapi.json searches {STATIC_ROOT}/_api_v1_schema_.json
    • JSONOpenPrettyRenderer
    • RedocOpenAPIRenderer
  • generateopenapi management command

Installation

  • Add pik_openapi to INSTALLED_APPS in settings.py
INSTALLED_APPS = [
    ...
    'pik_openapi',
]
  • Setup default schema inspector class with settings.py
REST_FRAMEWORK = {
  'DEFAULT_SCHEMA_CLASS': 'core.api.openapi.openapi.PIKAutoSchema',
  ...
}
  • Setup schema url with urls.py
from core.api.openapi.views import get_pik_schema_view

router_api_v1 = DefaultRouter()
api_v1_path = [path(
    'api/v1/', include((router_api_v1.urls, 'api_v1')))]

urlpatterns = [
    *api_v1_path,
    
    path('api/v1/schema/',
         get_pik_schema_view(
             title=f'API Schema',
             patterns=api_v1_path),
         name='api_v1_schema'),]

Usage

  • Redoc schema is available at /api/v1/schema/ and /api/v1/schema/?format=redoc
  • Json schema format available at /api/v1/schema/?format=openapi-json
  • YAML schema format available at /api/v1/schema/?format=openapi
  • Generate pre-rendered json schema file:
  ./manage.py generateopenapi \
    --format=openapi-json \
  --urlpatterns=_project_.urls.api_v1_path \
  > ${STATIC_ROOT}/_api_v1_schema_.json
  • Generate pre-rendered openapi schema file:
  ./manage.py generateopenapi \
    --format=openapi \
  --urlpatterns=_project_.urls.api_v1_path \
  > ${STATIC_ROOT}/_api_v1_schema_.yaml
  redoc-cli bundle 
  ${STATIC_ROOT}/_api_v1_schema_.json \
  --output ${STATIC_ROOT}/_api_v1_schema_.html \
  --options.showExtensions=true

Features

Schema Customization

Serializer schema customization

Inject schema customization, through update_schema dict:

class MySerializer(ModelSerializer):
    update_schema = {
        'properties': {
            'uid': {'deprecated': True}
        }
    }

Inject schema customization, though update_schema callback:

class MySerializer(ModelSerializer):
    def update_schema(self, schema):
        schema['properties']['_uid']['deprecated'] = True
        return schema

ViewSet Schema customization

Inject schema customization, through update_schema dict:

class MyViewSet(ViewSet):
    update_schema = {
        '/api/v1/comment-list/': {
            'get': {
                'deprecated': True,
            }
        }
    }

Inject schema customization, though update_schema callback:

class MyViewSet(ViewSet):
    def update_schema(self, schema):
        schema['/api/v1/comment-list/']['get']['deprecated'] = True
        return schema

Generate Schema as HTML File


For generate schema as HTML file:

npm install -g redoc-cli
  • Get your schema file
  • Generate HTML file with redoc-cli
redoc-cli bundle static/_api_v1_schema_.json \
--output static/_api_v1_schema_.html

More info ``

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pik-openapi-1.0.3.tar.gz (11.1 kB view details)

Uploaded Source

File details

Details for the file pik-openapi-1.0.3.tar.gz.

File metadata

  • Download URL: pik-openapi-1.0.3.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for pik-openapi-1.0.3.tar.gz
Algorithm Hash digest
SHA256 d64dd522865ff72f4482cca6a329a705425d90243ba419a46fcfeadd86ec727c
MD5 184f05d072a75e8951508ee8b8b4c5fa
BLAKE2b-256 ac8ee66c5921a6f528eeb12fddc20e8ff6b8758f34b3de04452acbecf6d5d5ad

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page