This project demonstrates the fix for guard() schemas not appearing in OpenAPI documentation.
When using guard() to apply validation schemas to multiple routes, those schemas were not included in the generated OpenAPI specification.
- Elysia Core: Added
getFlattenedRoutes()method that merges standaloneValidator arrays into direct hook properties - OpenAPI Plugin: Updated to use
getFlattenedRoutes()instead ofgetGlobalRoutes()
This demo uses local versions of both packages with the fixes applied.
# Install dependencies (links to local elysia and elysia-openapi)
bun install
# Run the server
bun run dev
# Test the fix
bun run test:afterbun run devServer will start at http://localhost:3000
Open http://localhost:3000/docs or fetch the JSON:
curl http://localhost:3000/docs/json | jq '.paths'Check that /sign-up and /sign-in endpoints now include requestBody schemas:
{
"/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"
}
}
}# Test sign-up
curl -X POST http://localhost:3000/sign-up \
-H "Content-Type: application/json" \
-d '{"username":"test","password":"test123"}'
# Test sign-in
curl -X POST http://localhost:3000/sign-in \
-H "Content-Type: application/json" \
-d '{"username":"test","password":"test123"}'Run the test script to see the internal behavior:
bun run test:afterThis shows:
- Normal routes (via
getGlobalRoutes()) have standaloneValidator arrays but no direct body schemas - Flattened routes (via
getFlattenedRoutes()) have body schemas merged into direct properties - OpenAPI plugin can now read these schemas
src/index.ts- Main server demonstrating the guard() usagesrc/test-after-fix.ts- Direct test of getFlattenedRoutes() functionalityADR.md- Technical decision record explaining the implementationpackage.json- Links to local fixed versions of elysia and @elysiajs/openapi
File: src/index.ts
- Added
getFlattenedRoutes()method (lines 343-354) - Added
mergeStandaloneValidators()helper (lines 359-405) - Added
mergeSchemaProperty()helper (lines 410-437) - Added
mergeResponseSchema()helper (lines 442-482)
Tests: test/core/flattened-routes.test.ts (7 tests, all passing)
File: src/openapi.ts
Changed line 298 from:
const routes = app.getGlobalRoutes()To:
const routes = app.getFlattenedRoutes?.() ?? app.getGlobalRoutes()To use this fix in your own projects before it's officially released:
- Clone and build elysia with the changes
- Clone and build elysia-openapi with the changes
- Link them locally or copy the built files
Or wait for the PRs to be merged and new versions to be published.