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

[zod-openapi] Default response support #739

Open
jstri opened this issue Sep 12, 2024 · 0 comments
Open

[zod-openapi] Default response support #739

jstri opened this issue Sep 12, 2024 · 0 comments

Comments

@jstri
Copy link

jstri commented Sep 12, 2024

OpenAPI supports describing response objects with status codes, status ranges, or using the default field (more in the specification).

The Default Response section of Describing Responses documentation (see here) states the following:

Sometimes, an operation can return multiple errors with different HTTP status codes, but all of them have the same response structure:
(masked example section)
You can use the default response to describe these errors collectively, not individually. “Default” means this response is used for all HTTP codes that are not covered individually for this operation.

This issue was first introduced in #519 with the support of status codes, which was later also extended in #535 to support the status ranges. It is now only missing the support for the default object.


Quick reproduction of an issue:

import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';

export const app = new OpenAPIHono().openapi(
  createRoute({
    summary: 'Test route',
    method: 'post',
    path: '/test',
    responses: {
      200: {
        description: '',
        content: {
          'application/json': {
            schema: z.object({
              name: z.string(),
            }),
          },
        },
      },
      default: {
        description: '',
        content: {
          'application/json': {
            schema: z.object({
              result: z.number(),
            }),
          },
        },
      },
    },
  }),
  (c) => {
    return c.json(
      {
        result: 0
      },
      400
    );
  }
);

Typescript error for the example above:

  Type 'JSONRespondReturn<{ result: number; }, 400>' is not assignable to type 'MaybePromise<TypedResponse<any, 200, "json" | "text">>'.
    Type 'JSONRespondReturn<{ result: number; }, 400>' is not assignable to type 'TypedResponse<any, 200, "json" | "text">'.
      Types of property '_status' are incompatible.
        Type '400' is not assignable to type '200'.ts(2345)
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