litestar-org / litestar

Production-ready, Light, Flexible and Extensible ASGI API framework | Effortlessly Build Performant APIs
https://litestar.dev/
MIT License
5.5k stars 375 forks source link

Enhancement: Serve openapi routes under multiple path prefixes #3339

Open peterschutt opened 6 months ago

peterschutt commented 6 months ago

Summary

We've seen a pattern where an OpenAPIController subclass would be registered directly onto the application, and (prior to 2.8) implicitly via the OpenAPIConfig object, e.g.,:

class MyOpenAPIController(OpenAPIController):
    path = "/schema"

router = Router(path=f"/{URL_PATH_PREFIX}", route_handlers=[MyOpenAPIController])

app = Litestar(
    [router],
    openapi_config=OpenAPIConfig("api", version="0.9.0"),
)

This pattern supports serving the openapi schema routes both under /schema and /some-prefix/schema.

We need to consider how this pattern would be implemented in v3 when the OpenAPIController object doesn't exist.

ref: #3337

Basic Example

OpenAPIConfig(path=["/schema", "/some-prefix/schema"], ...)

Drawbacks and Impact

No response

Unresolved questions

No response


[!NOTE]
While we are open for sponsoring on GitHub Sponsors and OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.

Fund with Polar

guacs commented 6 months ago

I like the approach of allowing the config to take in multiple paths.

SunsetOrange commented 6 months ago

I'd prefer reusing routers rather than hardcoding url paths. Something like...

router_1 = Router(path="/")
router_2 = Router(path=f"/some-path")

app = Litestar(
    [router_1, router_2],
    openapi_config=OpenAPIConfig(routes=[(router_1, "/docs"), (router_2, "/schema")]),
)
peterschutt commented 6 months ago

The OpenAPIController does accept a router - the idea being you can configure other handlers, middleware, guards etc and we stick the handlers for the openapi UI on there. If we accept a sequence of paths, it also would make sense for us to accept a sequence of routers.

OpenAPIConfig(openapi_router=[Router("/schema"), Router("/some-prefix/schema")])