OpenAPI Specification Generation#
Strapi's @strapi/openapi package (located at packages/core/openapi/) provides a CLI tool to generate OpenAPI 3.1.0 specifications for all content-API routes. It is currently experimental .
yarn strapi openapi generate
# or with custom output
yarn strapi openapi generate --output ./docs/api-spec.json
The generated specification.json (default: ./openapi-spec.json) covers CRUD routes for content types, custom routes, authentication endpoints, file upload endpoints, and plugin routes .
Assembly pipeline#
The generator assembles routes into an OpenAPI document through a staged pipeline :
DocumentPathsAssemblerā iterates all collected routes and creates path contexts.PathItemAssemblerā groups routes by their raw path string, then converts Express-style:paramplaceholders to OpenAPI{param}format via_formatPath.OperationParametersAssemblerā for each operation, readsroute.request.paramsandroute.request.queryand emits the corresponding OpenAPI parameter objects .
How path parameters are resolved#
Core.Route (defined in packages/core/types/src/core/route.ts) carries an optional request field of type RouteRequest:
interface RouteRequest {
query?: Record<string, z.ZodType>;
params?: Record<string, z.ZodType>; // ā path parameters
body?: HTTPMediaRecord;
}
OperationParametersAssembler._getPathParameters iterates route.request.params and converts each Zod schema to an OpenAPI in: 'path' parameter object, marking it required or optional based on zodSchema.isOptional() . If request.params is absent, the method returns an empty array immediately .
// packages/core/openapi/src/assemblers/ā¦/parameters.ts (lines 40ā45)
const { params } = route.request ?? {};
// TODO: Allow auto inference (from path) if enabled through configuration
if (!params) {
return [];
}
There is currently no auto-inference of path parameters from the route path string.
The invalid-spec bug#
PathItemAssembler._formatPath always converts :param ā {param} in the emitted path , regardless of whether a matching request.params entry exists. When a route uses a path parameter but omits request.params, the generated document contains {paramName} in the path but an empty parameters array ā which is invalid per the OpenAPI 3.1 spec and rejected by validators and codegen tools (e.g., orval).
Known affected route (as of Strapi 5.56.0): DELETE /auth/sessions/:sessionId in the users-permissions plugin has no request.params , while sibling routes like GET /auth/:provider/callback correctly declare theirs . This was reported in issue #27882 .
Fix and workaround#
Proper fix: Add request.params with a Zod schema for each path segment on the route definition:
{
method: 'DELETE',
path: '/auth/sessions/:sessionId',
handler: 'auth.revokeSession',
config: { prefix: '' },
request: {
params: { sessionId: z.string() },
},
},
Workaround (plugin extensions): Patch the route at runtime in src/extensions/users-permissions/strapi-server.js:
const { z } = require('@strapi/utils');
module.exports = (plugin) => {
const revokeSession = plugin.routes['content-api'].routes.find(
(route) => route.method === 'DELETE' && route.path === '/auth/sessions/:sessionId'
);
if (revokeSession && !revokeSession.request?.params) {
revokeSession.request = { ...revokeSession.request, params: { sessionId: z.string() } };
}
return plugin;
};
The same fix pattern applies to any custom or plugin route with a parameterized path that is missing request.params.
Key source files#
| File | Purpose |
|---|---|
packages/core/openapi/src/assemblers/document/path/path-item/operation/parameters.ts | Converts route.request.params / route.request.query to OpenAPI parameter objects |
packages/core/openapi/src/assemblers/document/path/path-item/path-item.ts | Groups routes and formats :param ā {param} |
packages/core/types/src/core/route.ts | Core.Route type ā defines request.params as Record<string, z.ZodType> |
packages/plugins/users-permissions/server/src/routes/content-api/auth.js | Auth routes; DELETE /auth/sessions/:sessionId is the known missing-params case |