Fastify Swagger isn’t displaying any of my routes in the Swagger UI, even though the routes are working fine when accessed directly. I’m using Fastify with the @fastify/swagger
plugin in dynamic mode, expecting it to automatically pick up routes, but the Swagger JSON output shows an empty paths
object.
Setup:
- Fastify version: 4.27.0
- @fastify/swagger version: 8.14.0
- @fastify/swagger-ui version: 3.0.0
- Node.js version: 21.6.2
Description:
Despite setting up everything according to the Fastify Swagger documentation, the Swagger UI is accessible but doesn’t list any routes. The routes themselves are operational as confirmed by direct API calls (e.g., /ping
returns pong
).
Simplified Code Implementation:
// Simplified Fastify setup with Swagger
import fastifySwagger, { type FastifyDynamicSwaggerOptions } from "@fastify/swagger";
import fastifySwaggerUi, { type FastifySwaggerUiOptions } from "@fastify/swagger-ui";
import fastify from "fastify";
// Check if the environment is development or production
const isDevelopment = process.env.NODE_ENV !== "production";
// Create a new instance of fastify
const app = fastify({
logger: isDevelopment
? {
transport: {
target: "pino-pretty",
options: {
translateTime: "HH:MM:ss Z",
ignore: "pid,hostname",
},
},
}
: true,
});
// Define Swagger options
const swaggerOptions: FastifyDynamicSwaggerOptions = {
mode: "dynamic",
openapi: {
openapi: "3.0.0",
info: {
title: "Test API",
description: "API documentation for Test API",
version: "1.0.0",
},
servers: [{ url: "http://localhost:3000", description: "Development server" }],
tags: [{ name: "ping", description: "Ping route to test Swagger integration" }],
},
};
// Define Swagger UI options
const swaggerUiOptions: FastifySwaggerUiOptions = {
routePrefix: "/documentation",
uiConfig: {
docExpansion: "full",
deepLinking: true,
},
};
// ping route
app.get(
"/ping",
{
schema: {
summary: "Ping test",
description: "Ping route to test Swagger integration",
tags: ["ping"],
response: {
200: {
description: "Successful response",
type: "object",
properties: {
pong: {
type: "string",
},
},
},
},
},
},
async (request, reply) => {
return { pong: "pong" };
},
);
// Register Swagger
app.register(fastifySwagger, swaggerOptions);
app.register(fastifySwaggerUi, swaggerUiOptions);
// Start server
app.listen({ port: 3000 }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server listening at ${address}`);
});
The Swagger JSON (/documentation/json
) consistently shows "paths": {}
which indicates no routes are being recognized by Swagger.
{
"openapi":"3.0.0",
"info":{
"title":"Test API",
"description":"API documentation for Test API",
"version":"1.0.0"
},
"components":{
"schemas":{}
},
"paths":{},
"servers":[{
"url":"http://localhost:3000","description":"Development server"
}],
"tags":[{
"name":"ping","description":"Ping route to test Swagger integration"
}]
}
Has anyone faced a similar issue or can spot what might be missing or misconfigured in my setup?
Attempts to Resolve:
- Checked and ensured route schemas are correctly defined.
- Reinstalled Swagger related packages.
- Tried placing the Swagger registration before and after route declarations.
- Stripped down to just a ping route as seen above.
None of these steps have resolved the issue.
Nothin957 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.