deseasion.backend.openapi.extract_specification module
This module gathers functions/classes to extract openAPI specification from this API.
- class deseasion.backend.openapi.extract_specification.FlaskRestfulPlugin
Bases:
BasePlugin
This class is a
apispec
plugin made to handleflask_restful.Resource
.- apispec_attr2openapi(func) dict
Extract operation openAPI spec from function decorators.
It extract spec from special decorators (those modifying func.__apispec__).
- Parameters:
func – operation function (get, put, post, etc.)
- Returns:
operation openAPI spec
- apispec_parameters2openapi(func) dict
Extract operation parameters openAPI spec from function decorators.
It extract spec from func.__apispec__[‘parameters’].
- Parameters:
func – operation function (get, put, post, etc.)
- Returns:
operation openAPI spec
- apispec_requestBody2openapi(func) dict
Extract operation request body openAPI spec from function decorators.
It extract spec from func.__apispec__[‘requestBody’].
- Parameters:
func – operation function (get, put, post, etc.)
- Returns:
operation openAPI spec
- apispec_responses2openapi(func) dict
Extract operation responses openAPI spec from function decorators.
It extract spec from func.__apispec__[‘responses’].
- Parameters:
func – operation function (get, put, post, etc.)
- Returns:
operation openAPI spec
- apispec_security2openapi(func) dict
Extract operation security openAPI spec from function decorators.
It extract spec from func.__apispec__[‘security’].
- Parameters:
func – operation function (get, put, post, etc.)
- Returns:
operation openAPI spec
- static extract_doc_strings(func)
Extract any openAPI useful information from operation doc-string.
- Parameters:
func – operation function (get, put, post, etc.)
- Returns:
operation openAPI specification
- init_spec(spec: APISpec) None
Initialize plugin with APISpec object
- Parameters:
spec (APISpec) – APISpec object this plugin instance is attached to
- property marshmallow_plugin: MarshmallowPlugin | None
Return
MarshmallowPlugin
plugin if it is used to build the specification.- Returns:
- path_helper(path: str | None = None, operations: dict | None = None, parameters: list[dict] | None = None, *, resource: Type[Resource] | None = None, api: Api | None)
Add path to openAPI specification.
- Parameters:
api – ref to API
path – endpoint of path (unused here)
operations – operations statically defined on path
parameters – parameters statically defined on path
resource – API resource
- Raises:
ValueError – if resource is not found in api
- Returns:
openAPI path specification
- resource2openapi(resource: Type[Resource], endpoint: str)
Convert API resource to openAPI specication.
It extract spec from doc-string and special decorators (those modifying func.__apispec__).
- Parameters:
resource –
endpoint – path endpoint
- Returns:
_description_
- class deseasion.backend.openapi.extract_specification.MyMarshmallowPlugin(schema_name_resolver: Callable[[type[Schema]], str] | None = None)
Bases:
MarshmallowPlugin
This class is a modified version of the basic Marshmallow plugin.
It is capable of handling
api.schemas.OneOfSchema
schemas.- schema_helper(name, _, schema=None, **kwargs)
Definition helper that allows using a marshmallow
Schema
to provide OpenAPI metadata.It was modified to handle correctly
api.schemas.OneOfSchema
schemas.- Parameters:
schema (type|Schema) – A marshmallow Schema class or instance.
- schema_names = {}
- deseasion.backend.openapi.extract_specification.deep_update(dico: dict, new_dico: dict)
Perform a deep update on the first dictionnary with content of second.
It handles nested dictionary structures, and lists. Everything else is updated the regular way.
- Parameters:
dico – dictionnary to update
new_dico – updated data as a dictionnary
- deseasion.backend.openapi.extract_specification.enum_fields2properties(self, field, **kwargs)
Helper function to convert enum field to openAPI spec.
- Parameters:
field –
- Returns:
openAPI spec
- deseasion.backend.openapi.extract_specification.extract_openapi(namespace: str | None = None) APISpec
Extract OpenAPI specification.
- Parameters:
namespace – namespace to limit to when importing schemas
- Returns:
specification
- deseasion.backend.openapi.extract_specification.get_schemas_classes(namespace: str | None = None) dict[str, Type[BaseSchema]]
Get all schema classes from
api
.- Parameters:
namespace – namespace from which to extract specification Defaults:
__name__
- Returns:
schema classes as a dictionary
- deseasion.backend.openapi.extract_specification.method_fields2properties(self, field, **kwargs) dict
Helper function to convert method field to openAPI spec.
It uses the field.field field as the actual field to convert.
- Parameters:
field –
- Returns:
openAPI spec
- deseasion.backend.openapi.extract_specification.pluck_fields2properties(self, field, **kwargs) dict
Helper function to convert pluck field to openAPI spec.
It uses a reference on the plucked field instead of its type.
- Parameters:
field –
- Returns:
openAPI spec