Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion connexion/middleware/swagger_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ def _spec_for_prefix(self, request) -> dict:
This is needed when behind a path-altering reverse proxy.
"""
base_path = self._base_path_for_prefix(request)
return self.specification.with_base_path(base_path).spec
specification = self.specification.with_base_path(base_path)
if self.options.resolve_spec_refs:
return specification.spec
return specification.raw

def add_openapi_json(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions connexion/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class SwaggerUIOptions:

:param serve_spec: Whether to serve the Swagger / OpenAPI Specification
:param spec_path: Where to serve the Swagger / OpenAPI Specification
:param resolve_spec_refs: Whether to resolve references in the served Swagger / OpenAPI
Specification.

:param swagger_ui: Whether to serve the Swagger UI
:param swagger_ui_path: Where to serve the Swagger UI
Expand All @@ -37,6 +39,7 @@ class SwaggerUIOptions:

serve_spec: bool = True
spec_path: t.Optional[str] = None
resolve_spec_refs: bool = True

swagger_ui: bool = True
swagger_ui_config: dict = dataclasses.field(default_factory=dict)
Expand Down Expand Up @@ -76,6 +79,11 @@ def openapi_spec_path(self) -> str:
"""Path to host the Swagger UI."""
return self._options.spec_path or self.spec_path

@property
def resolve_spec_refs(self) -> bool:
"""Whether to resolve references in the served Swagger / OpenAPI Specification."""
return self._options.resolve_spec_refs

@property
def swagger_ui_available(self) -> bool:
"""Whether to make the Swagger UI available."""
Expand Down
13 changes: 13 additions & 0 deletions tests/api/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ def test_swagger_json_app(simple_api_spec_dir, spec):
url = url.format(spec=spec.replace("yaml", "json"))
spec_json = app_client.get(url)
assert spec_json.status_code == 200
assert '"$ref"' not in spec_json.text


def test_swagger_json_can_preserve_refs(simple_api_spec_dir, spec):
"""Verify the spec json file can preserve the authored references."""
app = App(__name__, specification_dir=simple_api_spec_dir)
app.add_api(spec, swagger_ui_options=SwaggerUIOptions(resolve_spec_refs=False))
app_client = app.test_client()
url = "/v1.0/{spec}"
url = url.format(spec=spec.replace("yaml", "json"))
spec_json = app_client.get(url)
assert spec_json.status_code == 200
assert '"$ref"' in spec_json.text


def test_swagger_yaml_app(simple_api_spec_dir, spec):
Expand Down
Loading