Context

The ContextMiddleware included in Connexion provides some information about the current request context as thread-safe request-level global variables.

You can access them by importing them from connexion.context:

from connexion.context import context, operation, receive, request, scope
from connexion import request  # alias for connexion.context.request

Note that when trying to access these context variables outside of the request handling flow, or without running the ContextMiddleware, the following RuntimeError will be thrown:

RuntimeError: Working outside of operation context. Make sure your app is wrapped in a
ContextMiddleware and you're processing a request while accessing the context.

See below for an explanation of the different variables.

request

A Request object representing the incoming request. This is an instance of the ConnexionRequest.

View a detailed reference of the ConnexionRequest class
class connexion.lifecycle.ConnexionRequest(*args, uri_parser=None, **kwargs)

Implementation of the Connexion _RequestInterface representing an ASGI request.

_starlette_request

This class wraps a Starlette Request, and provides access to its attributes by proxy.

property content_type

The content type included in the request headers.

property context

The connexion context of the current request cycle.

async files()

Files included in the request.

async form()

Form data, including files.

classmethod from_starlette_request(request: Request, uri_parser=None) ConnexionRequest
async get_body()

Get body based on the content type. This returns json data for json content types, form data for form content types, and bytes for all others. If the bytes data is empty, None is returned instead.

async json()

Json data included in the request.

property mimetype

The content type included in the request headers stripped from any optional character set encoding

property path_params: Dict[str, Any]

Path parameters exposed as a dictionary

property query_params

Query parameters exposed as a dictionary

Some of the methods and attributes are coroutines that can only be accessed from an async context. When using the FlaskApp, you might want to import the Flask request instead:

from flask import request

operation

An Operation object representing the matched operation from your OpenAPI specification.

When using OpenAPI 3, this is an instance of the OpenAPIOperation class.

View a detailed reference of the OpenAPIOperation class
class connexion.operations.OpenAPIOperation(method, path, operation, resolver, path_parameters=None, app_security=None, security_schemes=None, components=None, randomize_endpoint=None, uri_parser_class=None)

A single API operation on a path.

This class uses the OperationID identify the module and function that will handle the operation

From Swagger Specification:

OperationID

A friendly name for the operation. The id MUST be unique among all operations described in the API. Tools and libraries MAY use the operation id to uniquely identify an operation.

Parameters:
  • method (str) – HTTP method

  • path (str) –

  • operation (dict) – swagger operation object

  • resolver – Callable that maps operationID to a function

  • path_parameters (list) – Parameters defined in the path level

  • app_security (list) – list of security rules the application uses by default

  • security_schemes (dict) – Security Definitions Object

  • components (dict) – Components Object

  • randomize_endpoint (integer) – number of random characters to append to operation name

  • uri_parser_class (AbstractURIParser) – class to use for uri parsing

body_definition(content_type: Optional[str] = None) dict

The body complete definition for this operation.

There can be one “body” parameter at most.

body_name(_content_type: str) str

Name of the body in the spec.

body_schema(content_type: Optional[str] = None) dict

The body schema definition for this operation.

property consumes

Content-Types that the operation consumes

example_response(status_code=None, content_type=None)

Returns example response from spec

classmethod from_spec(spec, *args, path, method, resolver, **kwargs)
property function

Resolved function.

Return type:

types.FunctionType

get_mimetype()

If the endpoint has no ‘produces’ then the default is ‘application/json’.

:rtype str

get_path_parameter_types()

Returns the types for parameters in the path

property is_request_body_defined: bool

Whether the request body is defined for this operation

property method

The HTTP method for this operation (ex. GET, POST)

property operation_id

The operation id used to identify the operation internally to the app

property parameters

Returns the parameters for this operation

property path

The path of the operation, relative to the API base path

property produces

Content-Types that the operation produces

property randomize_endpoint

number of random digits to generate and append to the operation_id.

property request_body

The request body for this operation

response_definition(status_code=None, content_type=None)

response definition for this endpoint

response_schema(status_code=None, content_type=None)

response schema for this endpoint

property responses

Returns the responses for this operation

property router_controller

The router controller to use (python module where handler functions live)

property security
property security_schemes
property uri_parser_class

The uri parser class for this operation.

with_definitions(schema: dict)

Returns the given schema, but with the definitions from the spec attached. This allows any remaining references to be resolved by a validator (for example).

When using Swagger 2, this is an instance of the Swagger2Operation class.

View a detailed reference of the Swagger2Operation class
class connexion.operations.Swagger2Operation(method, path, operation, resolver, app_produces, app_consumes, path_parameters=None, app_security=None, security_schemes=None, definitions=None, randomize_endpoint=None, uri_parser_class=None)

Exposes a Swagger 2.0 operation under the AbstractOperation interface. The primary purpose of this class is to provide the function() method to the API. A Swagger2Operation is plugged into the API with the provided (path, method) pair. It resolves the handler function for this operation with the provided resolver, and wraps the handler function with multiple decorators that provide security, validation, serialization, and deserialization.

Parameters:
  • method (str) – HTTP method

  • path (str) – relative path to this operation

  • operation (dict) – swagger operation object

  • resolver (resolver.Resolver) – Callable that maps operationID to a function

  • app_produces (list) – list of content types the application can return by default

  • app_consumes (list) – list of content types the application consumes by default

  • path_parameters (list) – Parameters defined in the path level

  • app_security (list) – list of security rules the application uses by default

  • security_schemes (dict) –

    Security Definitions Object

  • definitions (dict) – Definitions Object

  • randomize_endpoint (integer) – number of random characters to append to operation name

  • uri_parser_class (AbstractURIParser) – class to use for uri parsing

body_definition(content_type: Optional[str] = None) dict

The body complete definition for this operation.

There can be one “body” parameter at most.

body_name(content_type: Optional[str] = None) str

Name of the body in the spec.

body_schema(content_type: Optional[str] = None) dict

The body schema definition for this operation.

property consumes

Content-Types that the operation consumes

example_response(status_code=None, *args, **kwargs)

Returns example response from spec

classmethod from_spec(spec, *args, path, method, resolver, **kwargs)
property function

Resolved function.

Return type:

types.FunctionType

get_mimetype()

If the endpoint has no ‘produces’ then the default is ‘application/json’.

:rtype str

get_path_parameter_types()

Returns the types for parameters in the path

property is_request_body_defined: bool

Whether the request body is defined for this operation

property method

The HTTP method for this operation (ex. GET, POST)

property operation_id

The operation id used to identify the operation internally to the app

property parameters

Returns the parameters for this operation

property path

The path of the operation, relative to the API base path

property produces

Content-Types that the operation produces

property randomize_endpoint

number of random digits to generate and append to the operation_id.

property request_body: dict

The request body for this operation

response_definition(status_code=None, content_type=None)

response definition for this endpoint

response_schema(status_code=None, content_type=None)

response schema for this endpoint

property responses

Returns the responses for this operation

property router_controller

The router controller to use (python module where handler functions live)

property security
property security_schemes
property uri_parser_class

The uri parser class for this operation.

with_definitions(schema)

Returns the given schema, but with the definitions from the spec attached. This allows any remaining references to be resolved by a validator (for example).

scope

The ASGI scope as received by the ContextMiddleware, thus containing any changes propagated by upstream middleware. The ASGI scope is presented as a dict. Please refer to the ASGI spec for more information on its contents.

context.context

A dict containing the information from the security middleware:

{
    "user": ...  # User information from authentication
    "token_info": ...  # Token information from authentication
}

Third party or custom middleware might add additional fields to this.

receive

Warning

Advanced usage

The receive channel as received by the ContextMiddleware. Note that the receive channel might already be read by other parts of Connexion (eg. when accessing the body via the Request, or when it is injected into your Python function), and that reading it yourself might make it unavailable for those parts of the application.

The receive channel can only be accessed from an async context and is therefore not relevant when using the FlaskApp.