fastify-swagger icon indicating copy to clipboard operation
fastify-swagger copied to clipboard

Cannot pass definitions field when using openapi configuration

Open imjuni opened this issue 3 years ago • 0 comments

Prerequisites

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

Fastify version

4.9.2

Plugin version

8.1.0

Node.js version

14.20.1

Operating system

Linux

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

Ubuntu 20.04.3 LTS

Description

Issue

Cannot found $ref schema when custom SchemaController apply on fastify server instance. @fastify/swagger display error message like that,

{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message": "Cannot read property 'type' of undefined"
}

Custom SchemaController

ajv schema store split from fastify server instance because schema store use other case. eg. redis, rabbitmq pub/sub or file upload etc(share $ref schema).

Analysis

  1. I believe the following code is a bug.
// as-is
if (schema.querystring) resolveCommonParams('query', parameters, schema.querystring, ref, openapiObject.definitions, securityIgnores.query)
if (schema.body) {
  openapiMethod.requestBody = { content: {} }
  resolveBodyParams(openapiMethod.requestBody, schema.body, schema.consumes, ref)
}
if (schema.params) resolveCommonParams('path', parameters, schema.params, ref, openapiObject.definitions)
if (schema.headers) resolveCommonParams('header', parameters, schema.headers, ref, openapiObject.definitions, securityIgnores.header)
// TODO: need to documentation, we treat it same as the querystring
// fastify do not support cookies schema in first place
if (schema.cookies) resolveCommonParams('cookie', parameters, schema.cookies, ref, openapiObject.definitions, securityIgnores.cookie)
// to-be
if (schema.querystring) resolveCommonParams('query', parameters, schema.querystring, ref, openapiObject.components.schemas, securityIgnores.query)
if (schema.body) {
  openapiMethod.requestBody = { content: {} }
  resolveBodyParams(openapiMethod.requestBody, schema.body, schema.consumes, ref)
}
if (schema.params) resolveCommonParams('path', parameters, schema.params, ref, openapiObject.components.schemas)
if (schema.headers) resolveCommonParams('header', parameters, schema.headers, ref, openapiObject.components.schemas, securityIgnores.header)
// TODO: need to documentation, we treat it same as the querystring
// fastify do not support cookies schema in first place
if (schema.cookies) resolveCommonParams('cookie', parameters, schema.cookies, ref, openapiObject.components.schemas, securityIgnores.cookie)

~~2. resolveLocalRef function not search root scope. * fastify document explain to $ref as a root reference * but resolveLocalRef function don't search root scope. Only search inline scope.~~

Steps to Reproduce

Reproducable Repo.

Use this repo.

  1. git clone [email protected]:imjuni/maeum.git
  2. cd maeum
  3. npm install
  4. npm run dev

Expected Behavior

Works with custom schemaController.

imjuni avatar Nov 13 '22 17:11 imjuni