cloudflare / cloudflare/chanfana

schema5.openapi is not a function

Open
#167 0 comments 4 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
766
Forks
70
Avg merge
25m
Merged PRs (30d)
4

Description

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.
(file:///.../node_modules/.pnpm/@asteasolutions+zod-to-openapi@7.1.1_zod@3.23.8/node_modules/@asteasolutions/zod-to-openapi/dist/index.mjs:995:54)
at Array.map ()
at OpenAPIGenerator.generateInlineParameters
(file:///.../node_modules/.pnpm/@asteasolutions+zod-to-openapi@7.1.1_zod@3.23.8/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

```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";
```

Contributor guide

Open the contributing guide

Research direction

Start with the Converter example in the issue, then compare package.json and wrangler.toml with the official Cloudflare OpenAPI templates. Reproduce the schema using z.object and z.string().describe, and confirm whether direct Zod schemas work without extendZodWithOpenApi or whether the workaround needs documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.