Skip to content

Failed to handle multiple response codes in schema #24

@BkunS

Description

@BkunS

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.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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions