Skip to content

Commit

Permalink
🪲 Fix regression in OpenAPI with middleware (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil authored Aug 21, 2023
2 parents 4c5b355 + a333329 commit f39a4aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion esmerald/openapi/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pydantic import AnyUrl
from pydantic.fields import FieldInfo
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
from starlette.middleware import Middleware
from starlette.routing import BaseRoute
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
from typing_extensions import Literal
Expand Down Expand Up @@ -335,6 +336,9 @@ def should_include_in_schema(route: router.Include) -> bool:
if not route.include_in_schema:
return False

if not is_middleware_app(route):
return True

if (
isinstance(route.app, (Esmerald, ChildEsmerald))
or (
Expand All @@ -355,6 +359,15 @@ def should_include_in_schema(route: router.Include) -> bool:
return True


def is_middleware_app(route: router.Include) -> bool:
"""
Checks if the app is a middleware or a router
"""
from esmerald import MiddlewareProtocol

return bool(isinstance(route.app, (Middleware, MiddlewareProtocol)))


def get_openapi(
*,
app: Any,
Expand Down Expand Up @@ -427,7 +440,7 @@ def iterate_routes(
continue

# For external middlewares
if getattr(route.app, "routes", None) is None:
if getattr(route.app, "routes", None) is None and not is_middleware_app(route):
continue

if hasattr(route, "app") and isinstance(route.app, (Esmerald, ChildEsmerald)):
Expand Down
2 changes: 1 addition & 1 deletion esmerald/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ def __init__(

super().__init__(
self.path,
app=self.app,
app=app,
routes=routes,
name=name,
middleware=cast("Sequence[StarletteMiddleware]", include_middleware),
Expand Down

0 comments on commit f39a4aa

Please sign in to comment.