honojs / middleware

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

Typesafe enum query param in RPC client and @hono/zod-openapi #831

Closed 0machi closed 1 week ago

0machi commented 1 week ago

What version of Hono are you using?

  "dependencies": {
    "@hono/zod-openapi": "^0.17.1",
    "hono": "^4.6.10"
  },
  "devDependencies": {
    "@cloudflare/workers-types": "^4.20241022.0",
    "wrangler": "^3.83.0"
  }

What runtime/platform is your app running on?

Wrangler

What steps can reproduce the issue?

When combining @hono/zod-openapi and hono/client as in the sample code below, the type of query parameters inferred by the RPC client becomes string | string[] instead of "foo" | "bar".

Is this an issue with @hono/zod-openapi, as in https://github.com/honojs/hono/issues/2481, or is it a specification?

import { OpenAPIHono, createRoute, z } from '@hono/zod-openapi'
import { hc, InferRequestType } from 'hono/client'

const route = createRoute({
  method: 'get',
  path: '/',
  request: {
    query: z.object({
      msg: z.enum(['foo', 'bar']),
    }),
  },
  responses: {
    200: {
      content: {
        'application/json': {
          schema: z.object({
            msg: z.string(),
          }),
        },
      },
      description: 'test',
    }
  },
})

const app = new OpenAPIHono().openapi(route, (c) => {
  const { msg } = c.req.valid('query')
  return c.json({ msg })
})

type AppType = typeof app
const client = hc<AppType>('http://localhost:8787')
const $get = client.index.$get
type AppGetType = InferRequestType<typeof $get>

export default app

image

yusukebe commented 1 week ago

Hi @0machi

Thank you for raising this issue. It may be a bug or an unimplemented thing. I'll investigate it later!