SpecTree

class spectree.spec.SpecTree[source]

Interface

Parameters:
  • backend_name (str) – choose from (‘flask’, ‘quart’, ‘falcon’, ‘falcon-asgi’, ‘starlette’)

  • backend – a backend that inherit SpecTree.plugins.base.BasePlugin, this will override the backend_name if provided

  • app – backend framework application instance (can be registered later)

  • before – a callback function of the form spectree.utils.default_before_handler() func(req, resp, req_validation_error, instance) that will be called after the request validation before the endpoint function

  • after – a callback function of the form spectree.utils.default_after_handler() func(req, resp, resp_validation_error, instance) that will be called after the response validation

  • validation_error_status – The default response status code to use in the event of a validation error. This value can be overridden for specific endpoints if needed.

  • kwargs – init spectree.config.Configuration, they can also be configured through the environment variables with prefix spectree_

__init__(backend_name: str = 'base', backend: ~typing.Type[~spectree.plugins.base.BasePlugin] | None = None, app: ~typing.Any = None, before: ~typing.Callable = <function default_before_handler>, after: ~typing.Callable = <function default_after_handler>, validation_error_status: int = 422, validation_error_model: ~typing.Type[~spectree._types.BaseModelSubclassType] | None = None, naming_strategy: ~typing.Callable[[~typing.Type[~spectree._types.BaseModelSubclassType]], str] = <function get_model_key>, nested_naming_strategy: ~typing.Callable[[str, str], str] = <function get_nested_key>, **kwargs: ~typing.Any)[source]
bypass(func: Callable)[source]

bypass rules for routes (mode defined in config)

Normal:

collect all the routes exclude those decorated by other SpecTree instance

Greedy:

collect all the routes

Strict:

collect all the routes decorated by this instance

register(app: Any)[source]

register to backend application

This will be automatically triggered if the app is passed into the init step.

property spec

get the OpenAPI spec

validate(query: Type[BaseModelSubclassType] | None = None, json: Type[BaseModelSubclassType] | None = None, form: Type[BaseModelSubclassType] | None = None, headers: Type[BaseModelSubclassType] | None = None, cookies: Type[BaseModelSubclassType] | None = None, resp: Response | None = None, tags: Sequence = (), security: Any = None, deprecated: bool = False, before: Callable | None = None, after: Callable | None = None, validation_error_status: int = 0, path_parameter_descriptions: Mapping[str, str] | None = None, skip_validation: bool = False, operation_id: str | None = None) Callable[source]
  • validate query, json, headers in request

  • validate response body and status code

  • add tags to this API route

  • add security to this API route

Parameters:
  • querypydantic.BaseModel, query in uri like ?name=value

  • jsonpydantic.BaseModel, JSON format request body

  • formpydantic.BaseModel, form-data request body

  • headerspydantic.BaseModel, if you have specific headers

  • cookiespydantic.BaseModel, if you have cookies for this route

  • respspectree.Response

  • tags – a tuple of strings or spectree.models.Tag

  • security – dict with security config for current route and method

  • deprecated – bool, if endpoint is marked as deprecated

  • beforespectree.utils.default_before_handler() for specific endpoint

  • afterspectree.utils.default_after_handler() for specific endpoint

  • validation_error_status – The response status code to use for the specific endpoint, in the event of a validation error. If not specified, the global validation_error_status is used instead, defined in spectree.spec.SpecTree().

  • path_parameter_descriptions – A dictionary of path parameter names and their description.

  • operation_id – a string override for operationId for the given endpoint