honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
473 stars 169 forks source link

[zod-openapi] parameters mapped to schema + form explode #792

Open hffmnn opened 4 weeks ago

hffmnn commented 4 weeks ago

Note: Because openapi-zod is a library standing on the shoulders of other libraries, I don't have an idea if this is the correct repository to ask.

I am currently migrating an existing API with an OpenAPI spec (that users use to automatically generate clients) to hono. What I would need (without breaking changes for automatically generated clients), would be this:

- in: query
          name: sort
          schema:
            $ref: "#/components/schemas/sort"
          style: form
          explode: true

I have the following call to createRoute

export const list = createRoute({
  path: "/profile",
  method: "get",
  summary: "Get a list of profiles for a given space.",
  request: {
    query: SortSchema,,
  },
});

and the following schema:

import { z } from "@hono/zod-openapi";

export const SortSchema = z.object({
  field: z.string().default("submittedAt").openapi({
    description: "The field to sort by. Default is 'submittedAt'.",
  }),
  order: z.enum(["ASC", "DESC"]).default("ASC").openapi({
    description: "The sort order. Can be either 'ASC' or 'DESC'. Default is 'ASC'.",
  }),
}).openapi("sort");

When generating some OpenAPI, it generates this as parameters (yaml for better readability):

...
parameters:
        - schema:
            type: string
            default: submittedAt
            description: The field to sort by. Default is 'submittedAt'.
          required: false
          name: field
          in: query
        - schema:
            type: string
            enum:
              - ASC
              - DESC
            default: ASC
            description: The sort order. Can be either 'ASC' or 'DESC'. Default is 'ASC'.
          required: false
          name: order
          in: query

First question: Shouldn't it put the schemas under a shared component (as it does with responses). This is what I would have expected:

- in: query
          name: sort
          schema:
            $ref: "#/components/schemas/sort"

So far I am without any luck to achieve this.

Second question: Where would it be possible to add the style and explode properties?

yusukebe commented 2 weeks ago

Hi @hffmnn

I think this is probably the Zod to OpenAPI-side matter which is used in the Zod OpenAPI. Can you check it?