Skip to content

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

Description

@melroy89

Prerequisites

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

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions