chanfana icon indicating copy to clipboard operation
chanfana copied to clipboard

schema5.openapi is not a function

Open svengau opened this issue 6 months ago • 0 comments

I've got an error when trying to directly use zod schema, rather than types proposed by the lib (Num, Str, ..).

I've found 2 workarounds, so this issue is low priority (you can even close it if you want), but at least it's documented in case someone has the same issue.

Also, it works on the official openAPI cloudflare templates. I've tried to find the diff between this project and mine, but impossible to fix it on my project.

✘ [ERROR] TypeError: schema5.openapi is not a function
      at null.<anonymous>
  (file:///.../node_modules/.pnpm/@[email protected][email protected]/node_modules/@asteasolutions/zod-to-openapi/dist/index.mjs:995:54)
      at Array.map (<anonymous>)
      at OpenAPIGenerator.generateInlineParameters
  (file:///.../node_modules/.pnpm/@[email protected][email protected]/node_modules/@asteasolutions/zod-to-openapi/dist/index.mjs:970:58)
      at enhanceMissingParametersError.location

my code:

export class Converter extends OpenAPIRoute {
  schema = {
    summary: "Convert a document",
    request: {
      query: z.object({
        // url: Str({ description: "Url of the document to convert" }), <-- this works ! :)
        url: z.string().describe("url of the document to convert"), <-- this doesn't work ! :(
      }),
...

Environment

Node v20.10.0

package.json

"@cloudflare/itty-router-openapi": "^1.0.1",
"chanfana": "^2.0.2",
"hono": "^4.5.5",
"wrangler": "^3.72.0"

wrangler.toml

compatibility_date = "2024-08-15"
#compatibility_flags = ["nodejs_compat"]
node_compat = true

Workaround 1

Simply add the following before your zod schema

import { z } from "zod";
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
extendZodWithOpenApi(z);

Workaround 2

Use type provided by chanfana.

import { Bool, Enumeration, ..., Str } from "chanfana";

svengau avatar Aug 16 '24 14:08 svengau