Skip to main content

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 ``

Metadata

Release files for pik-openapi 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pik-openapi 1.0.3
File Size Uploaded Provenance Actions
pik-openapi-1.0.3.tar.gz 11.1 kB Without provenance Details for pik-openapi-1.0.3.tar.gz

Release files / pik-openapi-1.0.3.tar.gz

Download URL pik-openapi-1.0.3.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d64dd522865ff72f4482cca6a329a705425d90243ba419a46fcfeadd86ec727c
BLAKE2b-256 checksum
How to use checksums
ac8ee66c5921a6f528eeb12fddc20e8ff6b8758f34b3de04452acbecf6d5d5ad
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for pik-openapi 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page