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

Parameter is Undefined in the next Handler #15

Closed
hengmengsroin opened this issue Nov 11, 2024 · 6 comments · Fixed by #16
Closed

Parameter is Undefined in the next Handler #15

hengmengsroin opened this issue Nov 11, 2024 · 6 comments · Fixed by #16

Comments

@hengmengsroin
Copy link

Sample code:

route.get(
  "/:id",
  openApi({
    tags: ["meeting"],
    summary: "Get one meeting",
    request: {
      param: z.object({
        id: z.string(),
      }),
    },
    responses: {
      200: meetingZod,
    },
  }),
  async (c) => {
    let param = c.req.valid("param");
    console.log(param); // empty object {}
    let id = param.id;
    let res = await service.getOne(id);
    return c.json(res);
  }
);

param is undefined and the request cannot go through the zValidator too.

@paolostyle
Copy link
Owner

paolostyle commented Nov 11, 2024

I think I need more details. Assuming route is new Hono(), this test passes just fine:

  it('test', async () => {
    const route = new Hono();
    route.get(
      '/:id',
      openApi({
        tags: ['meeting'],
        summary: 'Get one meeting',
        request: {
          param: z.object({
            id: z.string(),
          }),
        },
        responses: {
          200: z.object({ id: z.string() }),
        },
      }),
      async (c) => {
        const param = c.req.valid('param');
        const id = param.id;
        return c.json({ id }, 200);
      },
    );

    const res = await route.request('/123');
    const data = await res.json();

    expect(data).toEqual({ id: '123' });
  });

How are you calling your API? What is route in your example?

@hengmengsroin
Copy link
Author

Yes, route = new Hono(). I found the issue now. it happens when I use other middleware before using openApi (cors, logger).
However, it won't happen when I use it with zodValidator. You can check the code below.

import { Hono } from "hono";
import { openApi } from "hono-zod-openapi";
import { assertEquals } from "jsr:@std/assert";
import { z } from "./deps.ts";
import { cors } from "hono/cors";

Deno.test({
  name: "test openApi",
  fn: async () => {
    const route = new Hono();
    route.use("*", cors()); // not working for openApi when use other middleware before openApi
    route.get(
      "/:id",
      openApi({
        tags: ["meeting"],
        summary: "Get one meeting",
        request: {
          param: z.object({
            id: z.string(),
          }),
        },
        responses: {
          200: z.object({ id: z.string() }),
        },
      }),
      (c) => {
        const param = c.req.valid("param");
        console.log(param);
        const id = param.id;
        return c.json({ id }, 200);
      }
    );
    route.use("*", cors()); // Works fine for openApi
    const res = await route.request("/123");
    const data = await res.json();
    assertEquals(data, { id: "123" });
  },
});

@paolostyle
Copy link
Owner

Okay, I see. I will look into it, this certainly should not happen.

@paolostyle
Copy link
Owner

This problem is related to every middleware that openApi is using internally. I created an issue in hono's repo here: honojs/hono#3657 as I can't see an easy way to fix it at the moment.

@hengmengsroin
Copy link
Author

I see. Thank you for your great work.

@paolostyle
Copy link
Owner

Please upgrade the library to 0.5.0 and upgrade Hono to v4.6.10. The fix is included there.

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

Successfully merging a pull request may close this issue.

2 participants