Files
connexion/docs/response.rst
João Santos 44ea9336fe Connexion 2.0 (#619)
- App and Api options must be provided through the "options" argument (``old_style_options`` have been removed).
- You must specify a form content-type in 'consumes' in order to consume form data.
- The `Operation` interface has been formalized in the `AbstractOperation` class.
- The `Operation` class has been renamed to `Swagger2Operation`.
- Array parameter deserialization now follows the Swagger 2.0 spec more closely.
  In situations when a query parameter is passed multiple times, and the collectionFormat is either csv or pipes, the right-most value will be used.
  For example, `?q=1,2,3&q=4,5,6` will result in `q = [4, 5, 6]`.
  The old behavior is available by setting the collectionFormat to `multi`, or by importing `decorators.uri_parsing.AlwaysMultiURIParser` and passing `parser_class=AlwaysMultiURIParser` to your Api.
- The spec validator library has changed from `swagger-spec-validator` to `openapi-spec-validator`.
- Errors that previously raised `SwaggerValidationError` now raise the `InvalidSpecification` exception.
  All spec validation errors should be wrapped with `InvalidSpecification`.
- Support for nullable/x-nullable, readOnly and writeOnly/x-writeOnly has been added to the standard json schema validator.
- Custom validators can now be specified on api level (instead of app level).
- Added support for basic authentication and apikey authentication
- If unsupported security requirements are defined or ``x-tokenInfoFunc``/``x-tokenInfoUrl`` is missing, connexion now denies requests instead of allowing access without security-check.
- Accessing ``connexion.request.user`` / ``flask.request.user`` is no longer supported, use ``connexion.context['user']`` instead
2018-11-05 14:50:42 +01:00

94 lines
2.7 KiB
ReStructuredText

Response Handling
=================
Response Serialization
----------------------
If the endpoint returns a `Response` object this response will be used as is.
Otherwise, and by default and if the specification defines that an endpoint
produces only JSON, connexion will automatically serialize the return value
for you and set the right content type in the HTTP header.
If the endpoint produces a single non-JSON mimetype then Connexion will
automatically set the right content type in the HTTP header.
Customizing JSON encoder
^^^^^^^^^^^^^^^^^^^^^^^^
Connexion allows you to customize the `JSONEncoder` class in the Flask app
instance `json_encoder` (`connexion.App:app`). If you wanna reuse the
Connexion's date-time serialization, inherit your custom encoder from
`connexion.apps.flask_app.FlaskJSONEncoder`.
Returning status codes
----------------------
There are two ways of returning a specific status code.
One way is to return a `Response` object that will be used unchanged.
The other is returning it as a second return value in the response. For example
.. code-block:: python
def my_endpoint():
return 'Not Found', 404
Returning Headers
-----------------
There are two ways to return headers from your endpoints.
One way is to return a `Response` object that will be used unchanged.
The other is returning a dict with the header values as the third return value
in the response:
For example
.. code-block:: python
def my_endpoint():
return 'Not Found', 404, {'x-error': 'not found'}
Response Validation
-------------------
While, by default Connexion doesn't validate the responses it's possible to
do so by opting in when adding the API:
.. code-block:: python
import connexion
app = connexion.FlaskApp(__name__, specification_dir='swagger/')
app.add_api('my_api.yaml', validate_responses=True)
app.run(port=8080)
This will validate all the responses using `jsonschema` and is specially useful
during development.
Custom Validator
-----------------
By default, response body contents are validated against OpenAPI schema
via ``connexion.decorators.response.ResponseValidator``, if you want to change
the validation, you can override the default class with:
.. code-block:: python
validator_map = {
'response': CustomResponseValidator
}
app = connexion.FlaskApp(__name__)
app.add_api('api.yaml', ..., validator_map=validator_map)
Error Handling
--------------
By default connexion error messages are JSON serialized according to
`Problem Details for HTTP APIs`_
Application can return errors using ``connexion.problem``.
.. _Problem Details for HTTP APIs: https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-00