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:
João Santos
2018-11-05 14:50:42 +01:00
committed by GitHub
parent 08faf2aa86
commit 44ea9336fe
160 changed files with 4561 additions and 39687 deletions

View File

@@ -2,20 +2,24 @@ import json
import jinja2
import yaml
from swagger_spec_validator.common import SwaggerValidationError
from openapi_spec_validator.loaders import ExtendedSafeLoader
import mock
import pytest
from conftest import TEST_FOLDER, build_app_from_fixture
from connexion import App
from connexion.exceptions import InvalidSpecification
SPECS = ["swagger.yaml", "openapi.yaml"]
def test_app_with_relative_path(simple_api_spec_dir):
@pytest.mark.parametrize("spec", SPECS)
def test_app_with_relative_path(simple_api_spec_dir, spec):
# Create the app with a relative path and run the test_app testcase below.
app = App(__name__, port=5001,
specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
debug=True)
app.add_api('swagger.yaml')
app.add_api(spec)
app_client = app.app.test_client()
get_bye = app_client.get('/v1.0/bye/jsantos') # type: flask.Response
@@ -23,13 +27,25 @@ def test_app_with_relative_path(simple_api_spec_dir):
assert get_bye.data == b'Goodbye jsantos'
def test_app_with_different_server_option(simple_api_spec_dir):
@pytest.mark.parametrize("spec", SPECS)
def test_app_with_resolver(simple_api_spec_dir, spec):
from connexion.resolver import Resolver
resolver = Resolver()
app = App(__name__, port=5001,
specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
resolver=resolver)
api = app.add_api(spec)
assert api.resolver is resolver
@pytest.mark.parametrize("spec", SPECS)
def test_app_with_different_server_option(simple_api_spec_dir, spec):
# Create the app with a relative path and run the test_app testcase below.
app = App(__name__, port=5001,
server='gevent',
specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
debug=True)
app.add_api('swagger.yaml')
app.add_api(spec)
app_client = app.app.test_client()
get_bye = app_client.get('/v1.0/bye/jsantos') # type: flask.Response
@@ -38,10 +54,10 @@ def test_app_with_different_server_option(simple_api_spec_dir):
def test_app_with_different_uri_parser(simple_api_spec_dir):
from connexion.decorators.uri_parsing import Swagger2URIParser
from connexion.decorators.uri_parsing import FirstValueURIParser
app = App(__name__, port=5001,
specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
options={"uri_parser_class": Swagger2URIParser},
options={"uri_parser_class": FirstValueURIParser},
debug=True)
app.add_api('swagger.yaml')
@@ -51,92 +67,106 @@ def test_app_with_different_uri_parser(simple_api_spec_dir):
) # type: flask.Response
assert resp.status_code == 200
j = json.loads(resp.get_data(as_text=True))
assert j == ['d', 'e', 'f']
assert j == ['a', 'b', 'c']
def test_no_swagger_ui(simple_api_spec_dir):
@pytest.mark.parametrize("spec", SPECS)
def test_no_swagger_ui(simple_api_spec_dir, spec):
options = {"swagger_ui": False}
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir,
swagger_ui=False, debug=True)
app.add_api('swagger.yaml')
options=options, debug=True)
app.add_api(spec)
app_client = app.app.test_client()
swagger_ui = app_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui.status_code == 404
app2 = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app2.add_api('swagger.yaml', swagger_ui=False)
app2.add_api(spec, options={"swagger_ui": False})
app2_client = app2.app.test_client()
swagger_ui2 = app2_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui2.status_code == 404
def test_swagger_json_app(simple_api_spec_dir):
""" Verify the swagger.json file is returned for default setting passed to app. """
@pytest.mark.parametrize("spec", SPECS)
def test_swagger_json_app(simple_api_spec_dir, spec):
""" Verify the spec json file is returned for default setting passed to app. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api('swagger.yaml')
app.add_api(spec)
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
assert swagger_json.status_code == 200
url = '/v1.0/{spec}'
url = url.format(spec=spec.replace("yaml", "json"))
spec_json = app_client.get(url) # type: flask.Response
assert spec_json.status_code == 200
def test_no_swagger_json_app(simple_api_spec_dir):
""" Verify the swagger.json file is not returned when set to False when creating app. """
@pytest.mark.parametrize("spec", SPECS)
def test_no_swagger_json_app(simple_api_spec_dir, spec):
""" Verify the spec json file is not returned when set to False when creating app. """
options = {"serve_spec": False}
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir,
swagger_json=False, debug=True)
app.add_api('swagger.yaml')
options=options, debug=True)
app.add_api(spec)
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
assert swagger_json.status_code == 404
url = '/v1.0/{spec}'
url = url.format(spec=spec.replace("yaml", "json"))
spec_json = app_client.get(url) # type: flask.Response
assert spec_json.status_code == 404
def test_dict_as_yaml_path(simple_api_spec_dir):
swagger_yaml_path = simple_api_spec_dir / 'swagger.yaml'
@pytest.mark.parametrize("spec", SPECS)
def test_dict_as_yaml_path(simple_api_spec_dir, spec):
openapi_yaml_path = simple_api_spec_dir / spec
with swagger_yaml_path.open(mode='rb') as swagger_yaml:
contents = swagger_yaml.read()
with openapi_yaml_path.open(mode='rb') as openapi_yaml:
contents = openapi_yaml.read()
try:
swagger_template = contents.decode()
openapi_template = contents.decode()
except UnicodeDecodeError:
swagger_template = contents.decode('utf-8', 'replace')
openapi_template = contents.decode('utf-8', 'replace')
swagger_string = jinja2.Template(swagger_template).render({})
specification = yaml.safe_load(swagger_string) # type: dict
openapi_string = jinja2.Template(openapi_template).render({})
specification = yaml.load(openapi_string, ExtendedSafeLoader) # type: dict
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(specification)
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 200
def test_swagger_json_api(simple_api_spec_dir):
""" Verify the swagger.json file is returned for default setting passed to api. """
@pytest.mark.parametrize("spec", SPECS)
def test_swagger_json_api(simple_api_spec_dir, spec):
""" Verify the spec json file is returned for default setting passed to api. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api('swagger.yaml')
app.add_api(spec)
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 200
def test_no_swagger_json_api(simple_api_spec_dir):
""" Verify the swagger.json file is not returned when set to False when adding api. """
@pytest.mark.parametrize("spec", SPECS)
def test_no_swagger_json_api(simple_api_spec_dir, spec):
""" Verify the spec json file is not returned when set to False when adding api. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api('swagger.yaml', swagger_json=False)
app.add_api(spec, options={"serve_spec": False})
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 404
def test_swagger_json_content_type(simple_app):
app_client = simple_app.app.test_client()
response = app_client.get('/v1.0/swagger.json',
data={}) # type: flask.Response
spec = simple_app._spec_file
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
response = app_client.get(url) # type: flask.Response
assert response.status_code == 200
assert response.content_type == 'application/json'
@@ -178,15 +208,16 @@ def test_resolve_classmethod(simple_app):
assert resp.data.decode('utf-8', 'replace') == '"DummyClass"\n'
def test_add_api_with_function_resolver_function_is_wrapped(simple_api_spec_dir):
@pytest.mark.parametrize("spec", SPECS)
def test_add_api_with_function_resolver_function_is_wrapped(simple_api_spec_dir, spec):
app = App(__name__, specification_dir=simple_api_spec_dir)
api = app.add_api('swagger.yaml', resolver=lambda oid: (lambda foo: 'bar'))
api = app.add_api(spec, resolver=lambda oid: (lambda foo: 'bar'))
assert api.resolver.resolve_function_from_operation_id('faux')('bah') == 'bar'
def test_default_query_param_does_not_match_defined_type(
default_param_error_spec_dir):
with pytest.raises(SwaggerValidationError):
with pytest.raises(InvalidSpecification):
build_app_from_fixture(default_param_error_spec_dir, validate_responses=True, debug=False)