Skip to content

Commit

Permalink
Add a OpenAPIProvider to allow for customisation
Browse files Browse the repository at this point in the history
By overridding and changing the OpenAPIProvider the generated OpenAPI
can be customised as desired. This should, I hope, fulfill a number of
feature requests for various customisation aspects.
  • Loading branch information
pgjones committed Jan 1, 2025
1 parent 2c73e19 commit 7e3ee02
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 252 deletions.
57 changes: 57 additions & 0 deletions docs/how_to_guides/customising.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Customising the OpenAPI schema
==============================

The generation of the OpenAPI schema by Quart-Schema can be customised
by changing the :attr:`QuartSchema.openapi_provider_class`. For
example to only include routes within a specific blueprint named
``bp``,

.. code-block:: python
from quart_schema import OpenAPIProvider, QuartSchema
class BlueprintOnlyOpenAPIProvider(OpenAPIProvider):
def __init__(self, blueprint_name: str, app: Quart, extension: QuartSchema) -> None:
super().__init__(app, extension)
self._blueprint_prefix = f"{blueprint_name}."
def generate_rules(self) -> Iterable[Rule]:
for rule in self._app.url_map.iter_rules():
hidden = getattr(
self._app.view_functions[rule.endpoint], QUART_SCHEMA_HIDDEN_ATTRIBUTE, False
)
if rule.endpoint.beginswith(self._blueprint_prefix) and not hidden and not rule.websocket:
yield rule
quart_schema = QuartSchema(app, openapi_provider_class=CustomerOpenAPIProvider)
It is also possible to alter how the operation ID is generated,

.. code-block:: python
from quart_schema import OpenAPIProvider, QuartSchema
class CustomOperationIdOpenAPIProvider(OpenAPIProvider):
def operation_id(self, method: str, func: Callable) -> Optional[str]:
return func.__name__
quart_schema = QuartSchema(app, openapi_provider_class=CustomerOpenAPIProvider)
There are many more aspects that can be customised, see the
:class:`OpenAPIProvider` for options.

Custom routes
-------------

It is also possible to combine the above into a custom route,

.. code-block:: python
@blueprint.get("/custom.json")
async def custom_openapi():
schema = BlueprintOnlyOpenAPIProvider(blueprint.name, current_app, quart_schema).schema()
return current_app.json.response(schema)
Please note this assumes the blueprint is not nested as the name will
be prefixed with the parents blueprint's names.
1 change: 1 addition & 0 deletions docs/how_to_guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ How to guides

casing.rst
configuration.rst
customising.rst
documenting.rst
error_handling.rst
headers_validation.rst
Expand Down
2 changes: 2 additions & 0 deletions src/quart_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Info,
License,
OAuth2SecurityScheme,
OpenAPIProvider,
OpenIdSecurityScheme,
Server,
ServerVariable,
Expand Down Expand Up @@ -48,6 +49,7 @@
"Info",
"License",
"OAuth2SecurityScheme",
"OpenAPIProvider",
"OpenIdSecurityScheme",
"operation_id",
"PydanticDumpOptions",
Expand Down
Loading

0 comments on commit 7e3ee02

Please sign in to comment.