fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
889 stars 199 forks source link

Shared JSON schema: Cannot create property 'in' on string 'object' #791

Open melroy89 opened 4 months ago

melroy89 commented 4 months ago

Prerequisites

Fastify version

4.26.2

Plugin version

8.14.0

Node.js version

20.11.1

Operating system

Linux

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

Linux Mint 23.1

Description

When going to the documentation page (Swagger), I get:

[17:58:43 UTC] ERROR: Cannot create property 'in' on string 'object'
    reqId: "req-8"
    req: {
      "method": "GET",
      "url": "/docs/json",
      "hostname": "0.0.0.0:3001",
      "remoteAddress": "127.0.0.1",
      "remotePort": 36828
    }
    res: {
      "statusCode": 500
    }
    err: {
      "type": "TypeError",
      "message": "Cannot create property 'in' on string 'object'",
      "stack":
          TypeError: Cannot create property 'in' on string 'object'
              at toSwaggerProp (/media/melroy/Data/Projects/erp-backend/node_modules/@fastify/swagger/lib/spec/swagger/utils.js:120:30)
              at /media/melroy/Data/Projects/erp-backend/node_modules/@fastify/swagger/lib/spec/swagger/utils.js:153:14
              at Array.map (<anonymous>)
              at plainJsonObjectToSwagger2 (/media/melroy/Data/Projects/erp-backend/node_modules/@fastify/swagger/lib/spec/swagger/utils.js:152:6)
              at resolveCommonParams (/media/melroy/Data/Projects/erp-backend/node_modules/@fastify/swagger/lib/spec/swagger/utils.js:204:15)
              at prepareSwaggerMethod (/media/melroy/Data/Projects/erp-backend/node_modules/@fastify/swagger/lib/spec/swagger/utils.js:291:29)
              at Object.swagger (/media/melroy/Data/Projects/erp-backend/node_modules/@fastify/swagger/lib/spec/swagger/index.js:49:29)
              at fastify.route.handler (/media/melroy/Data/Projects/erp-backend/node_modules/@fastify/swagger-ui/lib/routes.js:175:28)
              at preHandlerCallback (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/handleRequest.js:137:37)
              at next (/media/melroy/Data/Projects/erp-backend/node_modules/fastify/lib/hooks.js:233:9)
    }

Steps to Reproduce

Code/repo: https://github.com/melroy89/fastify-shared-schema-issue

My shared schema, I add during start-up:

fastify.addSchema({
  $id: 'shared',
  type: 'object',
  definitions: {
    genericQueryString: {
      type: 'object',
      properties: {
        order: { type: 'string', description: 'Order by a specific field' },
        limit: { type: 'number', description: 'Limit the results' },
      }
    }
  }
})

Using the JSON schema in Fastify with the shared schema reference from above:

{
  description: 'Get all users',
  tags: ['User'],
  summary: 'Get users',
  querystring: {
    $ref: 'shared#/definitions/genericQueryString'
  },
  response: {
      ....
      ...
  }
}

Ps. I also want to keep it extensible, so if a specific API end point needs additional querystrings I should be able to do:

  querystring: {
    $ref: 'shared#/definitions/genericQueryString',
    extra: { type: 'string', description: 'Extra query string' },
  },

Expected Behavior

No errors :). I think my shared schema is correct and should just work.

mcollina commented 4 months ago

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

melroy89 commented 4 months ago

Owh, I thought I gave already some code snippets in the reproducible section above.

I will make a new GitHub repo in that case, if really needed.

melroy89 commented 4 months ago

Here you go a reproducible example: https://github.com/melroy89/fastify-shared-schema-issue. I can't make it easier for you 😆

melroy89 commented 4 months ago

The bug is only present in OpenAPI v2 it seems (in @fastify/swagger/lib/spec/swagger/utils.js). When using OpenAPI v3 specification in Swagger (which is NOT the default), the error described above does no longer appear. Meaning the problem is only in v2 aka 'Swagger' API.

melroy89 commented 3 months ago

Won't fix. No time.

gurgunday commented 3 months ago

No problem!

But no need to close the issue if the issue still exists