Skip to content

Allow per-view Schema Generation customisation #5337

@carltongibson

Description

@carltongibson

A recurring issue is the need to slightly customise the Schema Generation. The default behaviour is correct in the main; what we’re missing is the ability to customise the behaviour for specific views.

For full control it is possible to write schemas by hand, but we want to be able to keep the automatic generation, in the main, whilst tweaking the aspects that are not correct (for our project).

Some proposals involve adopting a specific markdown syntax in order to encode (ever) more complex data structures in docstrings. We would then parse these, using Sphinx or pyparsing or a custom parser.

This is not the way to go. (External dependencies, fragility, and so on.) Better is to use Core Schema to clearly/precisely/etc specify the details we require.

The current implementation is Top Down — the generator inspects each view in turn to create the schema. Its limitation is that it does not (easily) allow customisation on a per-view/serialiser/etc basis.

In order to avoid adding more API to the APIView class, we will move the bulk of the generator’s introspection logic to a descriptor class that will be attached to the view. It will be accessed from the view and perform the necessary schema generation:

view.schema.get_schema()

(Or such)

The descriptor class will expose the required hooks to allow per-view customisation of the schema generation.

For example, it is not currently possible to provide a description for a non-model path parameter on a URL. An implementation of (the equivalent to the current) get_path_fields adding this would go something like:

fields = super().get_path_fields(...)
extra_field = coreapi.Field(...)
fields.append(extra_field)
return fields

i.e. We take the default implementation and make the (small) changes we want.

Users would be free to take whatever steps were needed to keep this DRY in their own projects.

The top-level schema generator will be maintained. It’s responsibilities will be reduced to accepting the patterns to be processed and gathering the resulting schema document.

Tasks

  • Migrate introspection logic from SchemaGenerator to descriptor class for APIView.
    • Make sure it still works.
  • Document interface and usage.

Related Issues

(There are others.)

Related PRs

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy