-
Notifications
You must be signed in to change notification settings - Fork 90
🔧 fix: use getFlattenedRoutes() to include guard() schemas #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
🔧 fix: use getFlattenedRoutes() to include guard() schemas #300
Conversation
… spec Fixes guard() schemas not appearing in generated OpenAPI documentation. Changes: - Replace app.getGlobalRoutes() with app.getFlattenedRoutes?.() ?? app.getGlobalRoutes() - Maintains backward compatibility with older Elysia versions This allows the OpenAPI plugin to access guard() schemas that are now exposed via Elysia's getFlattenedRoutes() method. Requires: elysiajs/elysia#1533
WalkthroughThe code updates route retrieval in Changes
Sequence Diagram(s)sequenceDiagram
participant OpenAPI as toOpenAPISchema
participant Server as FastifyInstance
participant Routes as RouteCollection
OpenAPI->>Server: request routes
alt getFlattenedRoutes exists
Server->>Routes: getFlattenedRoutes()
note right of Routes `#DFF0D8`: flattened routes returned
else fallback
Server->>Routes: getGlobalRoutes()
note right of Routes `#FFF3CD`: legacy routes returned
end
Routes-->>OpenAPI: route list
OpenAPI->>OpenAPI: extract guards/schemas
OpenAPI-->>Caller: OpenAPI schema
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/openapi.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/openapi.ts (2)
example/gen.ts (1)
app(6-77)test/gen/sample.ts (1)
app(5-61)
fix: use @ts-expect-error instead of @ts-ignore Addresses coderabbitai feedback on PR elysiajs#300. Changed from @ts-ignore to @ts-expect-error for better type safety. This ensures the suppression is still necessary - if the error goes away in the future (e.g., after types are updated), @ts-expect-error will fail the build, alerting us to remove it. Also improved the comment to be more descriptive about why the suppression is needed.
Addresses coderabbitai feedback on PR elysiajs#300. Changed from @ts-ignore to @ts-expect-error for better type safety. This ensures the suppression is still necessary - if the error goes away in the future (e.g., after types are updated), @ts-expect-error will fail the build, alerting us to remove it. Also improved the comment to be more descriptive about why the suppression is needed.
1fcdb20 to
da02e4d
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
1 similar comment
✅ Actions performedReview triggered.
|
Problem
Guard schemas defined via
guard()do not appear in the generated OpenAPI specification. Routes exist but lackrequestBodyand parameter schemas.Current Behavior
OpenAPI output:
{ "/sign-up": { "post": { "operationId": "postSign-up" // No requestBody! } } }Root Cause
The plugin reads routes via
app.getGlobalRoutes(), which returns guard schemas instandaloneValidatorarrays. The plugin only checks direct schema properties likeroute.hooks.body.Solution
Use
app.getFlattenedRoutes()(added in elysiajs/elysia#1533) which mergesstandaloneValidatorarrays into direct hook properties.Changes
File:
src/openapi.ts, line 298Backward Compatibility
The optional chaining (
?.()) ensures compatibility with Elysia versions that don't havegetFlattenedRoutes()yet.After Fix
OpenAPI output:
{ "/sign-up": { "post": { "requestBody": { "content": { "application/json": { "schema": { "type": "object", "properties": { "username": { "type": "string" }, "password": { "type": "string" } }, "required": ["username", "password"] } } }, "required": true }, "operationId": "postSign-up" } } }Dependencies
Requires: elysiajs/elysia#1533 (adds
getFlattenedRoutes()method)Note on versions:
peerDependenciesremains>= 1.4.0for backward compatibilitygetGlobalRoutes())devDependencieswill be updated to Elysia 1.4.16+ after 🔧 fix: expose guard() schemas to plugins via getFlattenedRoutes() elysia#1533 is merged and publishedDemo
Reproduction case and full documentation: https://github.com/MarcelOlsen/elysia-guard-openapi-fix-demo
Testing
The fix has been tested with:
All schemas now appear correctly in the OpenAPI documentation.
Summary by CodeRabbit