Skip to content

Commit

Permalink
Fix swagger UI for split specs
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeSneyders committed Nov 23, 2024
1 parent cef665c commit 224151c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 43 deletions.
2 changes: 1 addition & 1 deletion connexion/middleware/swagger_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _base_path_for_prefix(self, request: StarletteRequest) -> str:
"route_root_path", request.scope.get("root_path", "")
).rstrip("/")

def _spec_for_prefix(self, request):
def _spec_for_prefix(self, request) -> dict:
"""
returns a spec with a modified basePath / servers block
which corresponds to the incoming request path.
Expand Down
2 changes: 1 addition & 1 deletion connexion/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def enforce_string_keys(obj):
return OpenAPISpecification(spec, base_uri=base_uri)

def clone(self):
return type(self)(copy.deepcopy(self._raw_spec))
return type(self)(copy.deepcopy(self._spec))

@classmethod
def load(cls, spec, *, arguments=None):
Expand Down
10 changes: 4 additions & 6 deletions examples/helloworld_async/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
import connexion


async def test():
def pet(id_):
pass


async def post_greeting(name: str):
await test()
return f"Hello {name}", 201
def pets():
return 200


app = connexion.AsyncApp(__name__, specification_dir="spec")
app.add_api("openapi.yaml", arguments={"title": "Hello World Example"})
app.add_api("swagger.yaml", arguments={"title": "Hello World Example"})

# app.add_api("swagger.yaml", arguments={"title": "Hello World Example"})

if __name__ == "__main__":
app.run(f"{Path(__file__).stem}:app", port=8080)
57 changes: 37 additions & 20 deletions examples/helloworld_async/spec/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
openapi: "3.0.0"
openapi: 3.0.0

info:
title: Hello World
version: "1.0"
version: 1.0.0
title: Swagger Petstore
license:
name: MIT

servers:
- url: /openapi

paths:
/greeting/{name}:
post:
summary: Generate greeting
description: Generates a greeting message.
operationId: hello.post_greeting
/pets:
get:
operationId: hello.pets
summary: List all pets
responses:
'200':
description: A paged array of pets
content:
application/json:
schema:
$ref: "components.yaml#/components/schemas/Pets"
default:
description: Unexpected error
content:
application/json:
schema:
$ref: "components.yaml#/components/schemas/Error"

'/pets/{petId}':
get:
operationId: hello.pet
summary: Info for a specific pet
responses:
'200':
description: greeting response
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "components.yaml#/components/schemas/Pet"
default:
description: Unexpected error
content:
text/plain:
application/json:
schema:
type: string
example: "hello dave!"
parameters:
- name: name
in: path
description: Name of the person to greet.
required: true
schema:
type: string
example: "dave"
$ref: "components.yaml#/components/schemas/Error"
41 changes: 26 additions & 15 deletions examples/helloworld_async/spec/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
swagger: "2.0"

info:
title: Hello World
title: "{{title}}"
version: "1.0"

basePath: /swagger

paths:
/greeting/{name}:
post:
summary: Generate greeting
description: Generates a greeting message.
operationId: hello.post_greeting
/pets:
get:
summary: List all pets
responses:
'200':
description: greeting response
description: A paged array of pets
schema:
type: array
items:
$ref: 'definitions.yaml#/definitions/Pets'
default:
description: Unexpected Error
schema:
$ref: 'definitions.yaml#/definitions/Error'

'/pets/{id}':
get:
summary: Info for a specific pet
responses:
'200':
description: Expected response to a valid request
schema:
$ref: 'definitions.yaml#/definitions/Pet'
default:
description: Unexpected Error
schema:
type: string
example: "hello dave!"
parameters:
- name: name
in: path
description: Name of the person to greet.
required: true
type: string
$ref: 'definitions.yaml#/definitions/Error'
21 changes: 21 additions & 0 deletions tests/test_mock.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from connexion.middleware.security import SecurityOperation
from connexion.mock import MockResolver
from connexion.operations import OpenAPIOperation, Swagger2Operation

Expand Down Expand Up @@ -321,3 +322,23 @@ def test_mock_resolver_notimplemented():
"No example response or response schema defined.",
418,
)


def test_mock_resolver_multiple_resolves():
resolver = MockResolver(mock_all=True)

responses = {"default": {"examples": {"application/json": {"foo": "bar"}}}}

operation = SecurityOperation(
method="GET",
path="endpoint",
path_parameters=[],
operation={"responses": responses},
app_produces=["application/json"],
app_consumes=["application/json"],
definitions={},
resolver=resolver,
)

assert resolver.resolve(operation).operation_id == "mock-1"
assert resolver.resolve(operation).operation_id == "mock-1"

0 comments on commit 224151c

Please sign in to comment.