Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querystring not working with shared schema #224

Closed
eriott opened this issue Feb 12, 2020 · 3 comments
Closed

Querystring not working with shared schema #224

eriott opened this issue Feb 12, 2020 · 3 comments

Comments

@eriott
Copy link

eriott commented Feb 12, 2020

🐛 Bug Report

Generating of swagger docs fails when I use shared schema (replace-way) in querystring.

To Reproduce

  1. Register fastify-swagger plugin.
  2. Register any shared schema with id.
  3. Create a route and use shared schema in querystring definition.
  4. Run app and go to docs page.
  5. An error happens:
 TypeError: Cannot create property 'name' on string 'q'
   at Object.keys.forEach.prop (/home/regina/Projects/test-fastify-swagger/node_modules/fastify-swagger/dynamic.js:220:16)
   at Array.forEach (<anonymous>)
   at getQueryParams (/home/regina/Projects/test-fastify-swagger/node_modules/fastify-swagger/dynamic.js:217:22)
   at Object.swagger (/home/regina/Projects/test-fastify-swagger/node_modules/fastify-swagger/dynamic.js:144:11)
   at Object.handler (/home/regina/Projects/test-fastify-swagger/node_modules/fastify-swagger/routes.js:28:26)
   at preHandlerCallback (/home/regina/Projects/test-fastify-swagger/node_modules/fastify/lib/handleRequest.js:111:30)
   at preValidationCallback (/home/regina/Projects/test-fastify-swagger/node_modules/fastify/lib/handleRequest.js:100:5)
   at handler (/home/regina/Projects/test-fastify-swagger/node_modules/fastify/lib/handleRequest.js:69:5)
   at handleRequest (/home/regina/Projects/test-fastify-swagger/node_modules/fastify/lib/handleRequest.js:18:5)
   at onRunMiddlewares (/home/regina/Projects/test-fastify-swagger/node_modules/fastify/lib/middleware.js:22:5)

Use this code to reproduce error:

const fastify = require('fastify')({
  bodyLimit: 1024 * 1024 * 2.5,
  ignoreTrailingSlash: true,
  logger: true
})

fastify.register(require('fastify-swagger'), {
  routePrefix: '/docs',
  exposeRoute: true,
  swagger: {
    info: {
      title: 'Test swagger',
      description: 'testing the fastify swagger api',
      version: '0.1.0'
    },
    host: 'localhost',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json'],
  }
})

fastify.addSchema({
  $id: 'query-schema',
  type: 'object',
  properties: {
    param1: {
      type: 'number'
    },
    param2: {
      type: 'string'
    }
  }
})

fastify.put('/some-route/:id', {
  schema: {
    description: 'post some data',
    tags: ['user', 'code'],
    summary: 'qwerty',
    querystring: 'query-schema#'
  }
}, (req, reply) => {})

fastify.ready(err => {
  if (err) {
    fastify.log.error('an error happened', err)
    throw err
  }
  fastify.log.info('fastify ready')
})

fastify.listen(4002, '0.0.0.0', async function (err, address) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
  fastify.log.info(`server listening on ${address}`)
})

Expected behavior

I expect to see my shared schema definition in querystring on docs page.

Your Environment

  • node version: 11.15.0
  • fastify version: 2.12.0
  • fastify-swagger version: 2.5.0
  • os: Linux
@climba03003
Copy link
Member

Resolved by fastify/fastify#2108
If it happen again feel free to reopen the issue

@CodeMogul
Copy link

This issue still persists for refs of nested properties of shared schemas.

To Reproduce

  • Register fastify-swagger plugin.
  • Register any shared schema with id.
  • Create a route and use nested property from shared schema in querystring definition.
  • Run app and go to docs page.

Fastify doesn't throw any errors, but parameters documentation is not generated.

Sample Code

const fastify = require('fastify')({ logger: true });

fastify.register(require('fastify-swagger'), {
  routePrefix: '/documentation',
  exposeRoute: true,
  swagger: {
    host: 'localhost',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json'],
  },
});

// Add a shared schema
fastify.addSchema({
  $id: 'http://example.com/',
  type: 'object',
  properties: {
    hello: { type: 'string' },
  },
});

// Generates required Swagger Docs for Parameters
fastify.get('/generates-param-docs', {
  schema: {
    querystring: { $ref: 'http://example.com/#' },
  },
}, (req, reply) => req.query);

// Does not generate required Swagger Docs for Parameters
// If ref is used with nested properties
fastify.get('/does-not-generate-docs', {
  schema: {
    querystring: {
      hello: { $ref: 'http://example.com#/properties/hello' },
    },
  },
}, (req, reply) => req.query);

fastify.listen(9000, (err) => {
  if (err) {
    fastify.log.error(err);
    process.exit(1);
  }
});

Output Screenshot

Screen Shot 2021-05-10 at 10 27 44 PM

@climba03003
Copy link
Member

climba03003 commented May 11, 2021

@CodeMogul Your problem is similar to #337 or #394 but not this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants