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

Crash when querystring/params/headers/cookies are references to global schemas #394

Closed
schantaraud opened this issue Apr 7, 2021 · 4 comments
Labels
bug Confirmed bug

Comments

@schantaraud
Copy link

🐛 Bug Report

fastify.swagger() crashes when a route's querystring/params/headers/cookies is a reference to a global schema. e.g.:

headers: {
  '$ref': 'http://example.com/headers#'
}

Error: TypeError: Cannot read property 'schemas' of undefined.
lib/util/common.js:96:

  // $ref is in the format: #/definitions/<resolved definition>/<optional fragment>
  const localRef = jsonSchema.$ref.split('/')[2]
  return resolveLocalRef(externalSchemas[localRef], externalSchemas)

At this point, $ref is #/components/schemas/def-1 and externalSchemas (openapiObject.definitions) is undefined.

I assume that this is not really supported at the moment but would like to help make it work if possible in an attempt to migrate a set of services from fastify-oas to this module. Any pointer would be appreciated. Will open a PR shortly.

May be related to #252

To Reproduce

Steps to reproduce the behavior:

const fastify = require('fastify')();

fastify.register(require('fastify-swagger'), {
  openapi: {},
  exposeRoute: true
});

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

fastify.addSchema({
  $id: 'http://example.com/headers',
  type: 'object',
  properties: {
    mandatory: {
      type: 'boolean'
    }
  },
  required: ['mandatory']
});

fastify.post('/route', {
  schema: {
    body: {
      '$ref': 'http://example.com/body#'
    },
    headers: {
      '$ref': 'http://example.com/headers#'
    },
    // headers: {
    //   $id: 'http://example.com/headers',
    //   type: 'object',
    //   properties: {
    //     mandatory: {
    //       type: 'boolean'
    //     }
    //   },
    //   required: ['mandatory']
    // },
    response: {
      200: {
        '$ref': 'http://example.com/body#'
      }
    }
  }
}, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) });

fastify.listen(3000, err => {
  if (err) throw err
  console.log(JSON.stringify(fastify.swagger()));
});

Result:

/node-sandbox/node_modules/fastify-swagger/lib/util/common.js:98
  return resolveLocalRef(externalSchemas[localRef], externalSchemas)
                                        ^

TypeError: Cannot read property 'schemas' of undefined
    at resolveLocalRef (/node-sandbox/node_modules/fastify-swagger/lib/util/common.js:98:41)
    at plainJsonObjectToOpenapi3 (/node-sandbox/node_modules/fastify-swagger/lib/spec/openapi/utils.js:95:41)
    at resolveCommonParams (/node-sandbox/node_modules/fastify-swagger/lib/spec/openapi/utils.js:185:15)
    at prepareOpenapiMethod (/node-sandbox/node_modules/fastify-swagger/lib/spec/openapi/utils.js:271:25)
    at Object.swagger (/node-sandbox/node_modules/fastify-swagger/lib/spec/openapi/index.js:45:29)
    at /node-sandbox/index.js:55:38
    at Server.wrap (/node-sandbox/node_modules/fastify/lib/server.js:80:9)
    at Object.onceWrapper (events.js:421:28)
    at Server.emit (events.js:315:20)
    at emitListeningNT (net.js:1352:10)

Expected behavior

A clear and concise description of what you expected to happen.

{
  "openapi": "3.0.3",
  "info": {
    "version": "4.5.0",
    "title": "fastify-swagger"
  },
  "components": {
    "schemas": {
      "def-0": {
        "type": "object",
        "properties": {
          "hello": {
            "type": "string"
          }
        }
      },
      "def-1": {
        "type": "object",
        "properties": {
          "mandatory": {
            "type": "boolean"
          }
        },
        "required": [
          "mandatory"
        ]
      }
    }
  },
  "paths": {
    "/route": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/def-0"
              }
            }
          }
        },
        "parameters": [
          {
            "in": "header",
            "name": "mandatory",
            "required": true,
            "schema": {
              "type": "boolean"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Default Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/def-0"
                }
              }
            }
          }
        }
      }
    }
  }
}

Your Environment

@mcollina
Copy link
Member

mcollina commented Apr 7, 2021

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

@anonrig
Copy link
Contributor

anonrig commented Jun 30, 2021

Hi all, is there any progress on this issue?

@anonrig
Copy link
Contributor

anonrig commented Jun 30, 2021

This issue is resolved. We can close it

@schantaraud
Copy link
Author

Thank you @anonrig

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