zod-to-openapi icon indicating copy to clipboard operation
zod-to-openapi copied to clipboard

extendZodWithOpenApi does not work with import z from 'zod/v4'

Open centur opened this issue 4 months ago • 2 comments

Hi, I'm trying to migrate project from zod3 to zod4, and we're using zod-to-openapi. We have quite a lot of schemas from zod/v3 time so for migration we have to use import { z } from 'zod/v4'; So in our server.ts to extend zod with .openapi() we tried to use extension provided by the package.

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

Unfortunately it does not work, there is a type mismatch :

src/server.ts:3:22 - error TS2345: 
Argument of type 'typeof import("/Users/centur/projects/zod-openapi-repro/node_modules/.pnpm/[email protected]/node_modules/zod/v4/classic/external")' 
is not assignable to 
parameter of type 'typeof import("/Users/centur/projects/zod-openapi-repro/node_modules/.pnpm/[email protected]/node_modules/zod/v4/classic/external")'.

Type 'typeof import("/Users/centur/projects/zod-openapi-repro/node_modules/.pnpm/[email protected]/node_modules/zod/v4/classic/external")' 
is missing the following properties from 
type 'typeof import("/Users/centur/projects/zod-openapi-repro/node_modules/.pnpm/[email protected]/node_modules/zod/v4/classic/external")': 
ZodTypeAny, ZodSchema, Schema

3 extendZodWithOpenApi(z);

From my understanding with zod package ^4.0.0 these two statements - import z from 'zod' and import z from 'zod/v4' should be importing the same or compatible types.

Is it possible to add (until zod fixes exports) separate extension method that works with zod/v4 imports ?

For repro - this is my package.json and server.ts snippet above should be sufficient.

{
  "name": "zod-openapi-repro",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "npx tsc -p ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "packageManager": "[email protected]",
  "dependencies": {
    "@asteasolutions/zod-to-openapi": "^8.1.0",
    "zod": "^4.0.17"
  },
  "devDependencies": {
    "typescript": "5.8.3"
  }
}

centur avatar Aug 20 '25 04:08 centur

I have reproduced the same issue. Is there any solution?

Jinx-1120 avatar Sep 15 '25 18:09 Jinx-1120

I've ended up biting the bullet and updated our entire swagger definition to this package - https://github.com/samchungy/zod-openapi it's a simple update, but quite a lot of manual work to update every endpoint. But it works with zod model natively, no need to call .openapi(). I tried multiple scenarios even copying some segments of code from this repo. Does not work. Switched and never looked back.

centur avatar Sep 16 '25 12:09 centur