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

$ref in allOf in query/headers causes Fastify to crash #604

Closed
2 tasks done
HiiiiD opened this issue Jun 2, 2022 · 3 comments · Fixed by #609
Closed
2 tasks done

$ref in allOf in query/headers causes Fastify to crash #604

HiiiiD opened this issue Jun 2, 2022 · 3 comments · Fixed by #609
Labels
bug Confirmed bug good first issue Good for newcomers

Comments

@HiiiiD
Copy link
Contributor

HiiiiD commented Jun 2, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.29.0

Plugin version

6.1.0

Node.js version

16.13.0

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10 Build 19044.1706

Description

allOf keyword put in query or headers schema causes Fastify to crash with this error

/app/node_modules/@fastify/swagger/lib/util/common.js:187
  return resolveLocalRef(externalSchemas[localRef], externalSchemas)
                                        ^

TypeError: Cannot read properties of undefined (reading 'schemas')
    at resolveLocalRef (/app/node_modules/@fastify/swagger/lib/util/common.js:187:41)
    at /app/node_modules/@fastify/swagger/lib/util/common.js:180:20
    at Array.reduce (<anonymous>)
    at resolveLocalRef (/app/node_modules/@fastify/swagger/lib/util/common.js:179:20)
    at plainJsonObjectToOpenapi3 (/app/node_modules/@fastify/swagger/lib/spec/openapi/utils.js:133:41)
    at resolveCommonParams (/app/node_modules/@fastify/swagger/lib/spec/openapi/utils.js:237:15)
    at prepareOpenapiMethod (/app/node_modules/@fastify/swagger/lib/spec/openapi/utils.js:339:29)
    at Object.swagger (/app/node_modules/@fastify/swagger/lib/spec/openapi/index.js:46:29)
    at prepare (/app/index.js:44:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Steps to Reproduce

const fastify = require('fastify')()

await fastify.register(require('@fastify/swagger'), {
    exposeRoute: true,
    routePrefix: '/docs',
    openapi: {
        info: { title: `Test OpenAPI`, version: '1.0.0' }
    }
})

fastify.addSchema({
    $id: 'test',
    type: 'object',
    properties: {
        field1: { type: 'integer' }
    }
})

fastify.put('/test-route', {
  schema: {
    query: {
        allOf: [{
            $ref: 'test'
        }, {
            type: 'object',
            properties: {
                field2: { type: 'string' }
            }
        }]
    },
    response: {
      200: {
          type: 'object',
          properties: {
              result: { type: 'string' }
          }
      }
    }
  }
}, (req, reply) => ({ result: 'OK' }))

await fastify.ready()
fastify.swagger()

Expected Behavior

No error should occur.

@mcollina
Copy link
Member

mcollina commented Jun 5, 2022

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina mcollina added bug Confirmed bug good first issue Good for newcomers labels Jun 5, 2022
@HiiiiD HiiiiD changed the title allOf in query/headers causes Fastify to crash $ref in allOf in query/headers causes Fastify to crash Jun 6, 2022
@HiiiiD
Copy link
Contributor Author

HiiiiD commented Jun 6, 2022

Of course. I'll create it asap

@HiiiiD
Copy link
Contributor Author

HiiiiD commented Jun 6, 2022

@mcollina Should I create the pull-request even though the CI is currently failing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants