i use @fastify/swagger and @fastify/swagger-ui, my config
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUi from '@fastify/swagger-ui';
import { FastifyInstance } from 'fastify';
import fp from 'fastify-plugin';
const swagger = async (fastify: FastifyInstance) => {
await fastify.register(fastifySwagger, {
mode: 'dynamic',
openapi: {
openapi: '3.0.3',
info: {
title: 'openapi',
version: '0.1.0'
}
}
});
await fastify.register(fastifySwaggerUi, {
routePrefix: '/openapi',
uiConfig: {
deepLinking: true
},
uiHooks: {
onRequest: function (request, reply, next) {
next();
},
preHandler: function (request, reply, next) {
next();
}
},
staticCSP: true,
transformStaticCSP: (header) => header,
transformSpecification: (swaggerObject, request, reply) => {
return swaggerObject;
},
transformSpecificationClone: true
});
};
export default fp(swagger);
my routes like this
export const articleRouter = async (fastify: FastifyInstance) => {
fastify.get(
'/list',
{
schema: {
tags: ['article'],
summary: 'getting artilce list',
description: 'description in routes',
params: {
type: 'object',
description: 'some params',
properties: {
limit: {
type: 'number',
description: 'limit articles'
},
sort: {
type: 'string',
description: 'asc or desc'
}
},
required: ['sort']
},
}
},
async (req, resp) => {
const articles = await articleService.findArticles();
resp.send(articles);
}
);
};
swagger screenshot
enter image description here
All works, but field 'limit' is alway required. Why?
Please, help me.
I'm playing with all properties in config and routes but no one helpe me.
@fastify/swagger @fastify/swagger-ui
There was an error in the choice of property used. I replaced params with querystring, because it's not a path parameter but a query one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With