fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
941 stars 209 forks source link

Cannot pass definitions field when using openapi configuration #691

Open imjuni opened 1 year ago

imjuni commented 1 year ago

Prerequisites

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.
    • build querystring, build params, build headers, build cookies
    • definitions field remove from the option so that change openapiObject.definitions to openapiObject.components.schemas
    • definitions is always undefined so no way to pass externalSchemas
      // 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.

Steps to Reproduce

Reproducable Repo.

Use this repo.

  1. git clone git@github.com:imjuni/maeum.git
  2. cd maeum
  3. npm install
  4. npm run dev

Expected Behavior

Works with custom schemaController.