0

Is it possible to document dynamic query parameter using Fastify on Swagger, that allow client to pass the param value inside text field on Swagger UI using Swagger v1.0.0?, in my case is to input dynamic value of conversationId parameter.

enter image description here

Here is my swagger.js file in config folder.

exports.options = {
    routePrefix: '/documentation',
    exposeRoute: true,
    swagger: {
      info: {
        title: 'Wrapper API',
        description: 'Building a wrapper api',
        version: '1.0.0'
      },
      externalDocs: {
        url: 'https://swagger.io',
        description: 'Find more info here'
      },
      host: 'localhost:3000',
      schemes: [
        'http',
        'https'
      ], 
      consumes: ['application/json'],
      produces: ['application/json'], 
    }
  }

+ Here is my route

const healthBotController = require('../controllers/healthBotWrapperController')

const routes = [
  {
    method: 'GET',
    url: '/',
    handler: healthBotController.getEndpoints
  },
]

module.exports = routes;

I tried to search and read document but I couldn't find solution to my problem yet.
Thank you in advanced.

Chanrithisak Phok
  • 1,590
  • 1
  • 19
  • 29

1 Answers1

2

To add the query parameters in swagger, you need to define a JSON Schema in the route configuration:



const fastify = require('fastify')()

fastify.register(require('fastify-swagger'), {
  routePrefix: '/documentation',
  exposeRoute: true,
  swagger: {
    info: {
      title: 'Wrapper API',
      description: 'Building a wrapper api',
      version: '1.0.0'
    },
    externalDocs: {
      url: 'https://swagger.io',
      description: 'Find more info here'
    },
    host: 'localhost:3000',
    schemes: [
      'http',
      'https'
    ],
    consumes: ['application/json'],
    produces: ['application/json'],
    securityDefinitions: {
      ApiToken: {
        description: 'Authorization header token, sample: "Bearer #TOKEN#"',
        type: 'apiKey',
        name: 'Authorization',
        in: 'header'
      },
      Oauth2Token: {
        description: 'OAUTH2',
        type: 'oauth2',
        flow: 'accessCode',
        authorizationUrl: 'https://example.com/oauth/authorize',
        tokenUrl: 'https://example.com/oauth/token',
        scopes: {
          read: 'Grants read access',
          foo: 'Grants foao scope'
        }
      }
    }
  }
})

fastify.route({
  method: 'GET',
  url: '/',
  schema: {
    security: [{ ApiToken: [], Oauth2Token: [] }],
    querystring: {
      foo: { type: 'number' },
      bar: { type: 'string' }
    }
  },
  handler: function foo () {}
})

fastify.listen(8080)

Then in http://localhost:8080/documentation you will see:

query params

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • Thank you Mr. Manuel for your answer, Could you let me know where can you find this solution, are there any document reference of this answer? – Chanrithisak Phok May 05 '21 at 08:39
  • This is the [`dynamic` mode](https://github.com/fastify/fastify-swagger/#dynamic) in the docs – Manuel Spigolon May 05 '21 at 08:50
  • I see Mr. Maneul. You mentioned about the parameter, so how about putting the Authoization header like Bearer since I check my Swagger is version 1.0.0. I have done it but it return [object object]. – Chanrithisak Phok May 05 '21 at 08:57
  • Work like charm sir. Thank you. By the way it would be great if you could provide where references to learn all of these things from starting point to be more senior level of doing with things like this with Swagger. Kind of I want to learn how to fish. – Chanrithisak Phok May 05 '21 at 09:21