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-openapo openapi spec generation fails for routes imported from outside of #946

Open
platform-kit opened this issue Jan 29, 2025 · 2 comments
Labels

Comments

@platform-kit
Copy link

platform-kit commented Jan 29, 2025

Which middleware has the bug?

@hono/zod-openapi

What version of the middleware?

^0.18.3

What version of Hono are you using?

^0.2.1

What runtime/platform is your app running on? (with version if possible)

Node

What steps can reproduce the bug?

git clone https://github.com/platform-kit/hono-example repro

cd repro/min-reproduction/api && npm i && npm run dev;

Console should output something like:

02:30:47 0|app    | Mounting function # 1 of 3 /api/healthcheck
02:30:47 0|app    | /Users/user/htdocs/repro/min-reproduction/api/src/functions/healthcheck/healthcheck.ts
02:30:47 0|app    | Success.
02:30:47 0|app    | Mounting function # 2 of 3 /api/healthcheck
02:30:47 0|app    | /Users/user/htdocs/repro/min-reproduction/api/workspace/src/functions/healthcheck/healthcheck.ts
02:30:47 0|app    | Success.
02:30:47 0|app    | Mounting function # 3 of 3 /api/healthcheck
02:30:47 0|app    | /Users/user/htdocs/repro/min-reproduction/workspace/src/functions/healthcheck/healthcheck.ts
02:30:47 0|app    | Success.

What is the expected behavior?

I have split my app up into several child-apps so that I can load them programmatically.

The files in api/src/functions are correctly imported, routes are mounted/served, with working input validation from Zod.

They also show up in the Open API spec (at http://localhost:8000/api/openapi-spec.json) as well as in the Scalar GUI (http://localhost:8000/api/). As expected.

What is unexpected is that the files that are imported from paths api/workspace/src/functions and workspace/src/functions (sibling to /api/) DO NOT get registered in the Open API schema or in the Scalar GUI, yet the routes DO work, with Zod validation and everything.

http://localhost:8000/api/healthcheck1?text=world is the file from api/src/functions/healthcheck. It shows up in the schema / GUI, and shows correct Zod validation behavior.

http://localhost:8000/api/healthcheck2?text=world and http://localhost:8000/api/healthcheck3?text=world are the other two routes from the other import paths.

They function correctly, but do not show up in the schema.

I've also noticed that the 2nd one shows up when I run the code in my local terminal, but not when I run the code inside a devcontaniner. Not sure what the significance of that would be.

What do you see instead?

2 of 3 routes are missing from Open API schema.

Additional information

What gives? I am not sure how to debug this.

Is there some way of forcing the createRoute method from @hono/zod-openapi to output some kind of log about the success/failure of converting the Zod schema to openapi spec?

I'm at a loss as to how to move forward, since it is not working.

@yusukebe
Copy link
Member

yusukebe commented Feb 2, 2025

Hi @platform-kit

It's not a bug. The OpenAPIHono uses instanceof OpenAPIHono, and the module.default should always be true, but it sometimes will be false because maybe you use dynamic import (I don't know the actual cause).

for (const fn of functionsWithPath) {
  function setupRoute(module: any) {
    console.log('Mounting function # ' + current + ' of ' + total + '/api/' + fn.name + '\n' + fn.path + '\n\n')

    // should be true but it's not always true
    console.log(module.default instanceof OpenAPIHono)

   // ...

  await import(fn.path).then(async (module) => setupRoute(module)).catch((error) => console.log(error))
}

@yusukebe yusukebe added not bug and removed triage labels Feb 2, 2025
@yusukebe
Copy link
Member

yusukebe commented Feb 2, 2025

It might be possible to have the app contain a string or other data that can be used to identify whether or not it is an OpenAPI instead of using instanceof, but anyway, this kind of complex usage is not intended or supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants