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

Failed to handle multiple response codes in schema #24

Open
2 tasks done
BkunS opened this issue Nov 14, 2022 · 0 comments
Open
2 tasks done

Failed to handle multiple response codes in schema #24

BkunS opened this issue Nov 14, 2022 · 0 comments

Comments

@BkunS
Copy link

BkunS commented Nov 14, 2022

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?

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

1 participant