fastify-type-provider-json-schema-to-ts icon indicating copy to clipboard operation
fastify-type-provider-json-schema-to-ts copied to clipboard

Failed to handle multiple response codes in schema

Open BkunS opened this issue 2 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

^2.1.1

Node.js version

18

Operating system

Windows

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

10

Description

It doesn't convert to the intended typing when comes to defining multiple response code in the schema like this:

schema: {
  response: {
    200: ObjectData,
    404: {
        type: 'object',
        properties: {
          statusCode: 'number',
          error: 'string',
          message: 'string',
        }
     }
  }
}

Above will throw TS error like this: Type 'Document<unknown, any, ObjectData> & ObjectData & Required<{ _id: string; }>' is missing the following properties from type '{ [x: string]: unknown; error?: string | undefined; statusCode: number; message: string; }': statusCode, message

When using 'oneOf' in the response like this:

schema: {
  response: {
    oneOf: [
      { 200: ObjectData },
      { 
        404: {
          type: 'object',
          properties: {
            statusCode: 'number',
            error: 'string',
            message: 'string',
          }
        }
      }
    ]
  }
}

It seems to be correctly parsed by schema-to-ts, but the fastify wouldn't allow it by throwing this error: "Failed building the serialization schema for POST: /xxx, due to error response schemas should be nested under a valid status code, e.g { 2xx: { type: \"object\" } }"

Steps to Reproduce

schema: {
  response: {
    200: ObjectData,
    404: {
        type: 'object',
        properties: {
          statusCode: 'number',
          error: 'string',
          message: 'string',
        }
     }
  }
}

Expected Behavior

I don't know how to sort this out. Maybe treat response types differently by using oneOf logic?

BkunS avatar Nov 14 '22 16:11 BkunS