From 401b13bf2a564511217f257536380db2319773ef Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 11:43:46 +0100 Subject: [PATCH 1/8] Specification use raw spec on clone --- connexion/spec.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connexion/spec.py b/connexion/spec.py index 91ff3cd93..be5d653e5 100644 --- a/connexion/spec.py +++ b/connexion/spec.py @@ -196,7 +196,7 @@ def enforce_string_keys(obj): return OpenAPISpecification(spec, base_uri=base_uri) def clone(self): - return type(self)(copy.deepcopy(self._spec)) + return type(self)(copy.deepcopy(self._raw_spec)) @classmethod def load(cls, spec, *, arguments=None): From 622f247fa063c7954d944c0b28bfec2a4fd00dc2 Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 12:32:13 +0100 Subject: [PATCH 2/8] Add tests --- tests/api/test_swagger_ui.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/api/test_swagger_ui.py diff --git a/tests/api/test_swagger_ui.py b/tests/api/test_swagger_ui.py new file mode 100644 index 000000000..4249a3827 --- /dev/null +++ b/tests/api/test_swagger_ui.py @@ -0,0 +1,31 @@ +from connexion.middleware.swagger_ui import SwaggerUIConfig + + +def test_swagger(simple_app): + app_client = simple_app.test_client() + for api in simple_app.middleware.apis: + if api.kwargs['swagger_ui_options']: + swagger_ui_options = api.kwargs['swagger_ui_options'] + else: + swagger_ui_options = simple_app.middleware.options.swagger_ui_options + + options = SwaggerUIConfig( + swagger_ui_options, oas_version=api.specification.version + ) + response = app_client.get(api.specification.base_path+options.openapi_spec_path) + assert response.status_code == 200 + + +def test_openapi(simple_openapi_app): + app_client = simple_openapi_app.test_client() + for api in simple_openapi_app.middleware.apis: + if api.kwargs['swagger_ui_options']: + swagger_ui_options = api.kwargs['swagger_ui_options'] + else: + swagger_ui_options = simple_openapi_app.middleware.options.swagger_ui_options + + options = SwaggerUIConfig( + swagger_ui_options, oas_version=api.specification.version + ) + response = app_client.get(api.specification.base_path+options.openapi_spec_path) + assert response.status_code == 200 From 65e73924af5eaa92d62245a4049f0783fb91b512 Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 12:34:34 +0100 Subject: [PATCH 3/8] Fix naming --- tests/api/test_swagger_ui.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/api/test_swagger_ui.py b/tests/api/test_swagger_ui.py index 4249a3827..57462ebe2 100644 --- a/tests/api/test_swagger_ui.py +++ b/tests/api/test_swagger_ui.py @@ -1,7 +1,7 @@ from connexion.middleware.swagger_ui import SwaggerUIConfig -def test_swagger(simple_app): +def test_simple(simple_app): app_client = simple_app.test_client() for api in simple_app.middleware.apis: if api.kwargs['swagger_ui_options']: @@ -16,7 +16,7 @@ def test_swagger(simple_app): assert response.status_code == 200 -def test_openapi(simple_openapi_app): +def test_simple_openapi(simple_openapi_app): app_client = simple_openapi_app.test_client() for api in simple_openapi_app.middleware.apis: if api.kwargs['swagger_ui_options']: From 3e4f12a11e034432218505ef98361328fd7c5dd2 Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 13:59:30 +0100 Subject: [PATCH 4/8] Cleanup tests --- tests/api/conftest.py | 12 ++++++++++++ tests/api/test_swagger_ui.py | 33 ++++----------------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/tests/api/conftest.py b/tests/api/conftest.py index 0eb14ba54..a1a3360fe 100644 --- a/tests/api/conftest.py +++ b/tests/api/conftest.py @@ -3,6 +3,7 @@ import pytest from connexion.middleware import MiddlewarePosition from starlette.middleware.cors import CORSMiddleware +from connexion.options import SwaggerUIOptions from starlette.types import Receive, Scope, Send from conftest import OPENAPI3_SPEC, build_app_from_fixture @@ -22,6 +23,17 @@ def simple_openapi_app(app_class): ) +@pytest.fixture(scope="session") +def swagger_app(spec, app_class): + return build_app_from_fixture( + "simple", + app_class=app_class, + spec_file=spec, + validate_responses=True, + swagger_ui_options=SwaggerUIOptions(spec_path="/spec.json"), + ) + + @pytest.fixture(scope="session") def cors_openapi_app(app_class): app = build_app_from_fixture( diff --git a/tests/api/test_swagger_ui.py b/tests/api/test_swagger_ui.py index 57462ebe2..2765b94c1 100644 --- a/tests/api/test_swagger_ui.py +++ b/tests/api/test_swagger_ui.py @@ -1,31 +1,6 @@ -from connexion.middleware.swagger_ui import SwaggerUIConfig - -def test_simple(simple_app): - app_client = simple_app.test_client() - for api in simple_app.middleware.apis: - if api.kwargs['swagger_ui_options']: - swagger_ui_options = api.kwargs['swagger_ui_options'] - else: - swagger_ui_options = simple_app.middleware.options.swagger_ui_options - - options = SwaggerUIConfig( - swagger_ui_options, oas_version=api.specification.version - ) - response = app_client.get(api.specification.base_path+options.openapi_spec_path) - assert response.status_code == 200 - - -def test_simple_openapi(simple_openapi_app): - app_client = simple_openapi_app.test_client() - for api in simple_openapi_app.middleware.apis: - if api.kwargs['swagger_ui_options']: - swagger_ui_options = api.kwargs['swagger_ui_options'] - else: - swagger_ui_options = simple_openapi_app.middleware.options.swagger_ui_options - - options = SwaggerUIConfig( - swagger_ui_options, oas_version=api.specification.version - ) - response = app_client.get(api.specification.base_path+options.openapi_spec_path) +def test_simple(swagger_app): + app_client = swagger_app.test_client() + for api in swagger_app.middleware.apis: + response = app_client.get(api.specification.base_path+"/spec.json") assert response.status_code == 200 From 065041bf7228f79babe02159ba96abf8377a984d Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:07:52 +0100 Subject: [PATCH 5/8] Apply suggestions from code review Co-authored-by: Robbe Sneyders --- tests/api/conftest.py | 2 +- tests/api/test_swagger_ui.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/api/conftest.py b/tests/api/conftest.py index a1a3360fe..63ac83288 100644 --- a/tests/api/conftest.py +++ b/tests/api/conftest.py @@ -24,7 +24,7 @@ def simple_openapi_app(app_class): @pytest.fixture(scope="session") -def swagger_app(spec, app_class): +def swagger_ui_app(spec, app_class): return build_app_from_fixture( "simple", app_class=app_class, diff --git a/tests/api/test_swagger_ui.py b/tests/api/test_swagger_ui.py index 2765b94c1..03bcadda2 100644 --- a/tests/api/test_swagger_ui.py +++ b/tests/api/test_swagger_ui.py @@ -1,6 +1,6 @@ def test_simple(swagger_app): - app_client = swagger_app.test_client() + app_client = swagger_ui_app.test_client() for api in swagger_app.middleware.apis: response = app_client.get(api.specification.base_path+"/spec.json") assert response.status_code == 200 From 4e4d8e99d1c56870633250cca8fe4cb43a3a0659 Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:12:26 +0100 Subject: [PATCH 6/8] Cleanup tests --- tests/api/conftest.py | 4 ++-- tests/api/test_swagger_ui.py | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/api/conftest.py b/tests/api/conftest.py index 63ac83288..adeb1e250 100644 --- a/tests/api/conftest.py +++ b/tests/api/conftest.py @@ -24,11 +24,11 @@ def simple_openapi_app(app_class): @pytest.fixture(scope="session") -def swagger_ui_app(spec, app_class): +def swagger_ui_app(app_class): return build_app_from_fixture( "simple", app_class=app_class, - spec_file=spec, + spec_file=OPENAPI3_SPEC, validate_responses=True, swagger_ui_options=SwaggerUIOptions(spec_path="/spec.json"), ) diff --git a/tests/api/test_swagger_ui.py b/tests/api/test_swagger_ui.py index 03bcadda2..346a68f33 100644 --- a/tests/api/test_swagger_ui.py +++ b/tests/api/test_swagger_ui.py @@ -1,6 +1,5 @@ -def test_simple(swagger_app): +def test_simple(swagger_ui_app): app_client = swagger_ui_app.test_client() - for api in swagger_app.middleware.apis: - response = app_client.get(api.specification.base_path+"/spec.json") - assert response.status_code == 200 + response = app_client.get("/v1.0/spec.json") + assert response.status_code == 200 From cf6795adf419d99ada624da5a588157ee1e273ec Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:26:19 +0100 Subject: [PATCH 7/8] formatting --- tests/api/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api/conftest.py b/tests/api/conftest.py index adeb1e250..9249e6126 100644 --- a/tests/api/conftest.py +++ b/tests/api/conftest.py @@ -2,8 +2,8 @@ import pytest from connexion.middleware import MiddlewarePosition -from starlette.middleware.cors import CORSMiddleware from connexion.options import SwaggerUIOptions +from starlette.middleware.cors import CORSMiddleware from starlette.types import Receive, Scope, Send from conftest import OPENAPI3_SPEC, build_app_from_fixture From 1b6f3fe16be9d2dd1774895567dc7523134299b1 Mon Sep 17 00:00:00 2001 From: Niels Dewulf <87133686+nielsbox@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:48:56 +0100 Subject: [PATCH 8/8] formatting --- tests/api/test_swagger_ui.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/api/test_swagger_ui.py b/tests/api/test_swagger_ui.py index 346a68f33..1d787c65a 100644 --- a/tests/api/test_swagger_ui.py +++ b/tests/api/test_swagger_ui.py @@ -1,4 +1,3 @@ - def test_simple(swagger_ui_app): app_client = swagger_ui_app.test_client() response = app_client.get("/v1.0/spec.json")