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

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

Open
2 tasks done
melroy89 opened this issue Mar 19, 2024 · 6 comments
Open
2 tasks done

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

melroy89 opened this issue Mar 19, 2024 · 6 comments

Comments

@melroy89
Copy link
Contributor

melroy89 commented Mar 19, 2024

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.

@mcollina
Copy link
Member

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

This comment was marked as outdated.

@melroy89
Copy link
Contributor Author

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

@melroy89
Copy link
Contributor Author

melroy89 commented Mar 20, 2024

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
Copy link
Contributor Author

melroy89 commented Apr 8, 2024

Won't fix. No time.

@melroy89 melroy89 closed this as completed Apr 8, 2024
@gurgunday
Copy link
Member

No problem!

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

@gurgunday gurgunday reopened this Apr 8, 2024
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

No branches or pull requests

3 participants