1

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

  • this answer might help [How to specify if a field is optional or required in OpenAPI/Swagger?](https://stackoverflow.com/questions/40113049/how-to-specify-if-a-field-is-optional-or-required-in-openapi-swagger#answer-40113571) - use `required: true` – Michal Miky Jankovský Jan 10 '23 at 21:06
  • Thank you, but I saw this issue. For some reason, fields is required by default and not changing. – ILDAR Nasyrov Jan 12 '23 at 06:38

0 Answers0