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] parameters mapped to schema + form explode #792

Closed
hffmnn opened this issue Oct 24, 2024 · 6 comments
Closed

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

hffmnn opened this issue Oct 24, 2024 · 6 comments

Comments

@hffmnn
Copy link

hffmnn commented Oct 24, 2024

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
Copy link
Member

yusukebe commented Nov 5, 2024

Hi @hffmnn

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

@takakikasuga
Copy link

takakikasuga commented Dec 26, 2024

@takakikasuga
Copy link

takakikasuga commented Dec 26, 2024

@hffmnn

you need to inject expode in params!

left is definition, right is generated open api schema!!

スクリーンショット 2024-12-26 10 13 21

@hffmnn
Copy link
Author

hffmnn commented Jan 7, 2025

@takakikasuga Thanks a lot for that info. Much appreciated!

@yusukebe
Copy link
Member

yusukebe commented Jan 8, 2025

@takakikasuga Woooow, great!

@hffmnn Can we close this issue?

@hffmnn
Copy link
Author

hffmnn commented Jan 8, 2025

@yusukebe Absolutely. Will do

@hffmnn hffmnn closed this as completed Jan 8, 2025
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

3 participants