Skip to content

Why is it not possible to provide description and content for 400 status code? #13

@GorgeousPuree

Description

@GorgeousPuree
export const { PUT } = defineRoute({
  operationId: 'updateOrder',
  method: 'PUT',
  summary: 'Update order',
  description:
    'Update order text fields. To update order images use /api/orders/images',
  tags: ['Orders'],
  requestBody: UpdatedOrderFieldsApiDTO,
  action: async ({}) => {
    // return handleServerActionResponseInApiRoute(
    //   await updateOrderHandler(body, true),
    //   200
    // );
    return Response.json(null, { status: 501 });
  },
  responses: {
    201: { description: 'Order fields updated successfully' },
    400: {
      description:
        'Invalid request params or order cannot be updated for the reason described in the error message',
      content: AaaApiErrorDTO,
    },
    403: {
      description: 'No access to selected club location',
    },
    501: { description: 'Method not implemented' },
  },
  handleErrors: (errorType, issues) => {
    return apiRouteErrorHandler(errorType, issues);
  },
});

export const AaaApiErrorDTO = z.object({
  errorMessage: z.string(),
  validationErrors: ValidationErrorDTO.array().optional(),
});

Providing description and content for 400 status will not work, because library handles Bad Requests differently, ignoring content and description:

export function addBadRequest(queryParams?: unknown, requestBody?: unknown) {
  if (!queryParams && !requestBody) return undefined;
  return { description: "Bad Request" } as ResponseObject;
}

https://github.com/omermecitoglu/next-openapi-route-handler/blob/main/src/core/responses.ts

In case of 400, my endpoint returns an explanation why request failed. I would want to have my Error schema in OpenAPI json docs as well, but I can't due to library design. Why was it designed like that?
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions