Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastify openapi 3 create alway required fields

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

like image 730
Eldar Nasyrov Avatar asked Nov 23 '25 15:11

Eldar Nasyrov


1 Answers

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.

like image 169
Wkyubi Avatar answered Nov 25 '25 11:11

Wkyubi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!