hono-openapi: [BUG] Failed to determine a type for ZodEffects - transform

Hi, found an issue when calling the openAPISpecs function if one route uses a schema that has a .transform().

Here’s the error that shows up when I startup my application:

error: Uncaught (in promise) Error: Failed to determine a type for ZodEffects - transform at createSchema. Please change the 'effectType' to 'input', wrap it in a ZodPipeline or assign it a manual 'type'.
    at createManualOutputTransformSchema (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/components.chunk.mjs:186:11)
    at createTransformSchema (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/components.chunk.mjs:160:15)
    at createSchemaSwitch (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/components.chunk.mjs:1198:12)
    at createNewSchema (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/components.chunk.mjs:1248:18)
    at createSchemaOrRef (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/components.chunk.mjs:1344:10)
    at createSchemaObject (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/components.chunk.mjs:1348:18)
    at createSchema (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/components.chunk.mjs:1353:18)
    at createSchema (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/zod-openapi/3.3.0/dist/index.mjs:41:18)
    at Object.builder (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/hono-openapi/0.1.4/zod.esm.js:1:164)
    at resolver (file:///C:/Users/kingbri/AppData/Local/deno/npm/registry.npmjs.org/hono-openapi/0.1.4/zod.esm.js:1:331)

To make this error not occur, remove the .transform() call or comment out the /openapi route. I’ve included a test server below. Change Deno to whatever runtime you use.

import { Hono } from "hono";
import { describeRoute, openAPISpecs } from "hono-openapi";
import { resolver, validator as zValidator } from "hono-openapi/zod";
import { z } from "zod";
import { apiReference } from "@scalar/hono-api-reference";

const app = new Hono();

const testSchema = z.object({
    input: z.string(),
    aliased_input: z.string().optional(),
    dynamic_input: z.string().optional(),
})
.transform((obj) => {
    return {
        ...obj,
        aliased_input: obj.aliased_input ?? obj.dynamic_input ?? "Hello!"
    }
})

const extendedSchema = z.object({
    extended_input: z.string()
}).and(testSchema);


const responseSchema = z.object({
    finish: z.string()
})

app.post(
    "/",
    describeRoute({
        description: "Says hello",
        responses: {
            200: {
                description: "Success",
                content: {
                    "application/json": {
                        schema: resolver(responseSchema)
                    }
                }
            }
        }
    }),
    zValidator("json", extendedSchema),
    (c) => {
        console.log(c.req.valid('json'));
        return c.json(responseSchema.parse({ finish: "Hello!" }))
    }
)

app.get(
    "/openapi",
    openAPISpecs(app)
);

app.get(
    "/reference",
    apiReference({
        spec: {
            url: "/openapi",
        },
    }),
);

Deno.serve(app.fetch)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Just released a new version hono-openapi@0.1.5, which seems to resolve the issue at my end. @samchungy Thanks for pointing it out.

Hmmm does that get passed to the createSchema options?

Yes, Over here - main/packages/core/src/openapi.ts#L58

I think you’re explicitly mapping the options here though right?

https://github.com/rhinobase/hono-openapi/blob/37147e7a403395c871fff746eb254bc434d48a8b/packages/core/src/zod.ts#L16-L20

Will there need to be a new version of zod-openapi for this change to take effect?

It’ll be in the next version yep, when I decide to release.

Ah I see, it’s still technically correct, but rather they’re flattening it further. I can add that.