mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-06 04:19:26 +00:00
Enforcedefaults aiohttp (#1163)
* cache ConnexionRequest.json * add openapi3 enforcedefaults_aiohttp example Co-authored-by: Robbe Sneyders <robbe.sneyders@ml6.eu>
This commit is contained in:
@@ -30,7 +30,9 @@ class ConnexionRequest:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def json(self):
|
def json(self):
|
||||||
return self.json_getter()
|
if not hasattr(self, '_json'):
|
||||||
|
self._json = self.json_getter()
|
||||||
|
return self._json
|
||||||
|
|
||||||
|
|
||||||
class ConnexionResponse:
|
class ConnexionResponse:
|
||||||
|
|||||||
16
examples/openapi3/enforcedefaults_aiohttp/README.rst
Normal file
16
examples/openapi3/enforcedefaults_aiohttp/README.rst
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
========================
|
||||||
|
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.
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
openapi: '3.0.0'
|
||||||
|
info:
|
||||||
|
version: '1'
|
||||||
|
title: Custom Validator Example
|
||||||
|
servers:
|
||||||
|
- url: http://localhost:8080/{basePath}
|
||||||
|
variables:
|
||||||
|
basePath:
|
||||||
|
default: api
|
||||||
|
paths:
|
||||||
|
/echo:
|
||||||
|
post:
|
||||||
|
description: Echo passed data
|
||||||
|
operationId: enforcedefaults.echo
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/Data'
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Data with defaults filled in by validator
|
||||||
|
default:
|
||||||
|
description: Unexpected error
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/Error'
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Data:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
foo:
|
||||||
|
type: string
|
||||||
|
default: foo
|
||||||
|
Error:
|
||||||
|
type: string
|
||||||
56
examples/openapi3/enforcedefaults_aiohttp/enforcedefaults.py
Executable file
56
examples/openapi3/enforcedefaults_aiohttp/enforcedefaults.py
Executable file
@@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import connexion
|
||||||
|
import jsonschema
|
||||||
|
import six
|
||||||
|
from connexion.decorators.validation import RequestBodyValidator
|
||||||
|
from connexion.json_schema import Draft4RequestValidator
|
||||||
|
|
||||||
|
|
||||||
|
async def echo(body):
|
||||||
|
return body
|
||||||
|
|
||||||
|
|
||||||
|
# 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(Draft4RequestValidator)
|
||||||
|
|
||||||
|
|
||||||
|
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.AioHttpApp(
|
||||||
|
__name__,
|
||||||
|
port=8080,
|
||||||
|
specification_dir='.',
|
||||||
|
options={'swagger_ui': True}
|
||||||
|
)
|
||||||
|
app.add_api(
|
||||||
|
'enforcedefaults-api.yaml',
|
||||||
|
arguments={'title': 'Hello World Example'},
|
||||||
|
validator_map=validator_map,
|
||||||
|
)
|
||||||
|
app.run()
|
||||||
Reference in New Issue
Block a user