mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-06 04:19:26 +00:00
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
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
========================
|
||||
Custom Validator Example
|
||||
========================
|
||||
|
||||
In this example we fill-in non-provided properties with their defaults.
|
||||
Validator code is based on example from `python-jsonschema docs`_.
|
||||
|
||||
Running:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ./enforcedefaults.py
|
||||
|
||||
Now open your browser and go to http://localhost:8080/v1/ui/ to see the Swagger
|
||||
UI. If you send a ``POST`` request with empty body ``{}``, you should receive
|
||||
echo with defaults filled-in.
|
||||
|
||||
.. _python-jsonschema docs: https://python-jsonschema.readthedocs.io/en/latest/faq/#why-doesn-t-my-schema-that-has-a-default-property-actually-set-the-default-on-my-instance
|
||||
@@ -1,42 +0,0 @@
|
||||
swagger: '2.0'
|
||||
info:
|
||||
version: '1'
|
||||
title: Custom Validator Example
|
||||
basePath: '/v1'
|
||||
consumes:
|
||||
- application/json
|
||||
produces:
|
||||
- application/json
|
||||
paths:
|
||||
/echo:
|
||||
post:
|
||||
description: Echo passed data
|
||||
operationId: enforcedefaults.echo
|
||||
parameters:
|
||||
- name: data
|
||||
in: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/Data'
|
||||
responses:
|
||||
'200':
|
||||
description: Data with defaults filled in by validator
|
||||
schema:
|
||||
$ref: '#/definitions/Data'
|
||||
default:
|
||||
description: Unexpected error
|
||||
schema:
|
||||
$ref: '#/definitions/Error'
|
||||
definitions:
|
||||
Data:
|
||||
type: object
|
||||
properties:
|
||||
outer-object:
|
||||
type: object
|
||||
default: {}
|
||||
properties:
|
||||
inner-object:
|
||||
type: string
|
||||
default: foo
|
||||
Error:
|
||||
type: string
|
||||
@@ -1,48 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import connexion
|
||||
import jsonschema
|
||||
import six
|
||||
from connexion.decorators.validation import RequestBodyValidator
|
||||
|
||||
|
||||
def echo(data):
|
||||
return data
|
||||
|
||||
|
||||
# via https://python-jsonschema.readthedocs.io/
|
||||
def extend_with_set_default(validator_class):
|
||||
validate_properties = validator_class.VALIDATORS['properties']
|
||||
|
||||
def set_defaults(validator, properties, instance, schema):
|
||||
for property, subschema in six.iteritems(properties):
|
||||
if 'default' in subschema:
|
||||
instance.setdefault(property, subschema['default'])
|
||||
|
||||
for error in validate_properties(
|
||||
validator, properties, instance, schema):
|
||||
yield error
|
||||
|
||||
return jsonschema.validators.extend(
|
||||
validator_class, {'properties': set_defaults})
|
||||
|
||||
DefaultsEnforcingDraft4Validator = extend_with_set_default(
|
||||
jsonschema.Draft4Validator)
|
||||
|
||||
|
||||
class DefaultsEnforcingRequestBodyValidator(RequestBodyValidator):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DefaultsEnforcingRequestBodyValidator, self).__init__(
|
||||
*args, validator=DefaultsEnforcingDraft4Validator, **kwargs)
|
||||
|
||||
|
||||
validator_map = {
|
||||
'body': DefaultsEnforcingRequestBodyValidator
|
||||
}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = connexion.FlaskApp(
|
||||
__name__, port=8080, specification_dir='.', validator_map=validator_map)
|
||||
app.add_api('enforcedefaults-api.yaml')
|
||||
app.run()
|
||||
Reference in New Issue
Block a user