API Reference

OpenAPI 3.0 Schema Objects

For official specs, see https://swagger.io/specification/

class djagger.openapi.Contact

OpenAPI contact object

class djagger.openapi.Header

The Header Object follows the structure of the Parameter Object with the following changes: 1. name MUST NOT be specified, it is given in the corresponding headers map. 2. in MUST NOT be specified, it is implicitly in header. 3. All traits that are affected by the location MUST be applicable to a location of header (for example, style).

class djagger.openapi.Info

OpenAPI document information

class djagger.openapi.License

OpenAPI license object

Logo image for display on redoc documents.

class djagger.openapi.Tag

OpenAPI tags

class djagger.openapi.TagGroup

Tag grouping for x-tagGroups setting in redoc. This beyond the OpenAPI 3.0 specs but is included for redoc.

Enums

class djagger.enums.DjaggerAttributeEnumType

Enum type with helper class methods to initialize View-level and operation-level djagger view attributes as enums

classmethod items()

List of tuples of the enum attr names and the corresponding value

location()

Returns the ‘in’ location value for parameters

Return type

Optional[str]

classmethod values()

List of enum attr string values

class djagger.enums.DjaggerViewAttributes

Contains enums for djagger attributes that can be extracted from a view class of function

__init__(custom_prefix, *http_methods)
Parameters

custom_prefix (str) –

from_view(view, attr, http_method=None)

Given a FBV view or CBV view, the general attribute name, and a http method, extracts the attribute from the view starting at the operation-level attribute i.e. get_body_params. If it does not exist, try to extract at the API-level attribute i.e. body_params. If the attribute still does not exist, return None. If http_method is not passed, only extract the API-level attribute.

Example::
>>> ViewAttrs = DjaggerViewAttributes("get")
>>> ViewAttrs.from_view(view, 'operation_id', HttpMethod.GET)
"get_operation_id"
Parameters
Return type

Any

operation(http_method)

Returns the DjaggerAttributeEnumType value for the corresponding HttpMethod

Parameters

http_method (djagger.enums.HttpMethod) –

Return type

djagger.enums.DjaggerAttributeEnumType

classmethod prefix_attrs(method_prefix, custom_prefix='')

Prefixes view_attrs values with string prefix e.g. get_ in ‘get_operation_id’

Parameters
  • method_prefix (str) –

  • custom_prefix (str) –

retrieve_operation_attr_value(attr, http_method)

Converts an API-level attribute value to the operation-level equivalent given a string attribute value attr and a given http method.

Example:

>>> self.retrieve_operation_attr_value('summary', HttMethod.GET)
'get_summary'
Parameters
Return type

str

class djagger.enums.HttpMethod

An enumeration.

classmethod values()

Returns list of http method strings [ ‘get’, ‘post’, … ]

class djagger.enums.ParameterLocation

An enumeration.

Utility functions

The core module of Djagger project

djagger.utils.clean_regex_string(s)

Converts regex string pattern for a path into OpenAPI format.

Example:

>>s = '^toy\/^(?P<toyId>[a-zA-Z0-9-_]+)\/details'
>>clean_regex_string(s)
'toy/{toyId}/details'
Parameters

s (str) –

Return type

str

djagger.utils.clean_resolver_url_pattern(route)

Cleans the full url path pattern from a url resolver into a OpenAPI schema url pattern.

Parameters

route (str) – Full URL path pattern including any prefixed paths.

Returns

str – OpenAPI path format

Return type

str

Example:

>>clean_resolver_url_pattern("toy/%(toyId)s/uploadImage")
toy/{toyId}/uploadImage
djagger.utils.clean_route_url_pattern(route)

Converts a django path route string format into an openAPI route format.

Parameters

route (str) – URL path pattern from URLPattern object.

Returns

str – OpenAPI path format

Return type

str

Example:

>>clean_route_url_pattern("/list/<int:pk>")
/list/{pk}
djagger.utils.field_to_pydantic_args(f)

Given a DRF Field, returns a dictionary of arguments to be passed to pydantic.create_model() field configs.

Parameters

f (rest_framework.fields.Field) –

Return type

Dict

djagger.utils.get_app_name(module)

Given the value of __module__ dunder attr, return the name of the top level module i.e. the app name.

Example:

>>get_app_name('MyApp.urls.resolvers')
'MyApp'
Parameters

module (str) –

Return type

str

djagger.utils.get_pattern_str(pattern)

Given a URLPattern.pattern, or a URLResolver.pattern, return the path string in regex form.

A pattern can exist as RegexPattern or RoutePattern. The string returned will be in the regex pattern form for consistency

Parameters

pattern (Union[django.urls.resolvers.RegexPattern, django.urls.resolvers.RoutePattern]) –

Return type

str

djagger.utils.get_url_patterns(app_names, url_names=[])

Given a list of app names in the project, return a filtered list of URLPatterns that contains a view function or class that are part of the listed apps. Include all apps, less djagger if app_names is an empty list.

If url_names is provided, will further filter the list to only include URLPatterns that match the URL names as provided in the url_names list.

Parameters
  • app_names (List[str]) –

  • url_names (List[str]) –

Return type

List[Tuple[str, django.urls.resolvers.URLPattern]]

djagger.utils.infer_field_type(field)

Classifies DRF Field types into primitive python types or creates an appropriate pydantic model metaclass types if the field itself is a Serializer class.

Parameters

field (rest_framework.fields.Field) –

djagger.utils.list_urls(resolver, prefix='')

Returns a list of tuples containing the ‘cleaned’ full url path and the corresponding URLPattern object

Parameters

resolver (django.urls.resolvers.URLResolver) –

Return type

List[Tuple[str, django.urls.resolvers.URLPattern]]

djagger.utils.model_field_schemas(model)

Returns list of tuple with a JSON Schema for it as the first item. Also return a dictionary of definitions with models as keys and their schemas as values. Refer to pydantic.fields.field_schema for reference

Parameters

model (Any) –

Return type

List[Tuple[Dict, Dict]]

djagger.utils.schema_from_serializer(s)

Converts a DRF Serializer type into a pydantic model.

Parameters

s (rest_framework.serializers.Serializer) –

Return type

pydantic.main.ModelMetaclass

djagger.utils.schema_set_examples(schema, model)

Check if a class has callable example() and if so, sets the schema ‘example’ field to the result of example() callable. The callable should return an instance of the pydantic base model type. example() should return a single instance of the pydantic base model type.

Parameters
  • schema (Dict) –

  • model (Any) –

Decorators

djagger.decorators.schema(methods, **attrs)

Decorator for function based views to set Djagger attributes into the view. A list of http method strings are needed to inform Djagger which endpoint schemas to create I.e. GET POST PUT PATCH DELETE

Example:

@schema(
    methods=["GET", "POST"],
    summary="My FBV Endpoint",
    get_response_schema=MyFBVResponseSchema
)
def my_fb_view(request):
    # Your code logic
    return JsonResponse({"foo":"bar"})
Parameters

methods (List[str]) –

djagger.views.open_api_json(request)

View for auto generated OpenAPI JSON document

Parameters

request (django.http.request.HttpRequest) –

djagger.views.redoc(request)

Redoc openAPI document that is initialized from the JSON output of open_api_json() See https://github.com/Redocly/redoc

Parameters

request (django.http.request.HttpRequest) –