-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a OpenAPIProvider to allow for customisation
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
Showing
6 changed files
with
497 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.