-
-
Notifications
You must be signed in to change notification settings - Fork 762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforcedefaults aiohttp #1163
Merged
RobbeSneyders
merged 3 commits into
spec-first:main
from
p4l1ly:enforcedefaults_aiohttp
Feb 23, 2022
Merged
Enforcedefaults aiohttp #1163
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
39 changes: 39 additions & 0 deletions
39
examples/openapi3/enforcedefaults_aiohttp/enforcedefaults-api.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 56 additions & 0 deletions
56
examples/openapi3/enforcedefaults_aiohttp/enforcedefaults.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change is unrelated to your example, why did you add it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is related. Actually, it fixes the issue #1162. As opposed to FlaskApp, this code says that the body is recomputed each time we access
ConnexionRequest.json
. So if a validator changes the json, it is not reflected in thebody
argument because here it is recomputed again. This change avoids the recomputation and uses the same reference (which has been modified by the validator) again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really, please try the example without this change and see that it behaves differently (the
foo
attribute is not added with the default value).