mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-09 04:19:32 +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:
77
tests/test_json_validation.py
Normal file
77
tests/test_json_validation.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import json
|
||||
|
||||
from jsonschema.validators import _utils, extend
|
||||
|
||||
import pytest
|
||||
from conftest import build_app_from_fixture
|
||||
from connexion import App
|
||||
from connexion.decorators.validation import RequestBodyValidator
|
||||
from connexion.json_schema import Draft4RequestValidator
|
||||
|
||||
SPECS = ["swagger.yaml", "openapi.yaml"]
|
||||
|
||||
@pytest.mark.parametrize("spec", SPECS)
|
||||
def test_validator_map(json_validation_spec_dir, spec):
|
||||
def validate_type(validator, types, instance, schema):
|
||||
types = _utils.ensure_list(types)
|
||||
errors = Draft4RequestValidator.VALIDATORS['type'](validator, types, instance, schema)
|
||||
for e in errors:
|
||||
yield e
|
||||
|
||||
if 'string' in types and 'minLength' not in schema:
|
||||
errors = Draft4RequestValidator.VALIDATORS['minLength'](validator, 1, instance, schema)
|
||||
for e in errors:
|
||||
yield e
|
||||
|
||||
MinLengthRequestValidator = extend(Draft4RequestValidator, {'type': validate_type})
|
||||
|
||||
class MyRequestBodyValidator(RequestBodyValidator):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(MyRequestBodyValidator, self).__init__(*args, validator=MinLengthRequestValidator, **kwargs)
|
||||
|
||||
validator_map = {'body': MyRequestBodyValidator}
|
||||
|
||||
app = App(__name__, specification_dir=json_validation_spec_dir)
|
||||
app.add_api(spec, validate_responses=True, validator_map=validator_map)
|
||||
app_client = app.app.test_client()
|
||||
|
||||
res = app_client.post('/v1.0/minlength', data=json.dumps({'foo': 'bar'}), content_type='application/json') # type: flask.Response
|
||||
assert res.status_code == 200
|
||||
|
||||
res = app_client.post('/v1.0/minlength', data=json.dumps({'foo': ''}), content_type='application/json') # type: flask.Response
|
||||
assert res.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.parametrize("spec", SPECS)
|
||||
def test_readonly(json_validation_spec_dir, spec):
|
||||
app = build_app_from_fixture(json_validation_spec_dir, spec, validate_responses=True)
|
||||
app_client = app.app.test_client()
|
||||
|
||||
res = app_client.get('/v1.0/user') # type: flask.Response
|
||||
assert res.status_code == 200
|
||||
assert json.loads(res.data.decode()).get('user_id') == 7
|
||||
|
||||
res = app_client.post('/v1.0/user', data=json.dumps({'name': 'max', 'password': '1234'}), content_type='application/json') # type: flask.Response
|
||||
assert res.status_code == 200
|
||||
assert json.loads(res.data.decode()).get('user_id') == 8
|
||||
|
||||
res = app_client.post('/v1.0/user', data=json.dumps({'user_id': 9, 'name': 'max'}), content_type='application/json') # type: flask.Response
|
||||
assert res.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.parametrize("spec", SPECS)
|
||||
def test_writeonly(json_validation_spec_dir, spec):
|
||||
app = build_app_from_fixture(json_validation_spec_dir, spec, validate_responses=True)
|
||||
app_client = app.app.test_client()
|
||||
|
||||
res = app_client.post('/v1.0/user', data=json.dumps({'name': 'max', 'password': '1234'}), content_type='application/json') # type: flask.Response
|
||||
assert res.status_code == 200
|
||||
assert 'password' not in json.loads(res.data.decode())
|
||||
|
||||
res = app_client.get('/v1.0/user') # type: flask.Response
|
||||
assert res.status_code == 200
|
||||
assert 'password' not in json.loads(res.data.decode())
|
||||
|
||||
res = app_client.get('/v1.0/user_with_password') # type: flask.Response
|
||||
assert res.status_code == 500
|
||||
assert json.loads(res.data.decode())['title'] == 'Response body does not conform to specification'
|
||||
Reference in New Issue
Block a user