anatine / zod-plugins

Plugins and utilities for Zod
640 stars 89 forks source link

[zod-openapi] when return type is a function, the generated open API JSON shows it as {} #162

Open Siyer2 opened 1 year ago

Siyer2 commented 1 year ago

tl;dr when my schema has something like this in it:

image: z.function().returns(ImageSchema)

it displays as image: {} with no details on ImageSchema.

My implementation: I try to go from Zod to OpenAPI like this:

import { extendApi, generateSchema } from '@anatine/zod-openapi';
import { MySchema } from '../../typed/MySchema';

const openApiFile: any = {
  openapi: '3.0.0',
  info: {
    title: 'Schemas',
    version: '0.0.0',
  },
  components: {
    schemas: {},
  },
  paths: {},
};

// Generate the schema
const mySchemaOpenApi = generateSchema(extendApi(MySchema));
openApiFile.components.schemas.MySchema = mySchemaOpenApi;

// Add my path
const myPath = {...}
openApiFile.paths['/somepath'] = myPath;

// Push OpenAPI file
mkdirSync('data/', { recursive: true });
writeFileSync(`data/openapi.json`, JSON.stringify(openApiFile));

My schema looks like this:

export const MySchema = z.object({
  image: z.function().returns(ImageSchema)
});

But when I run this, wherever there is a z.function().returns(...), it simply returns {} rather than showing the ImageSchema. Is there anyway to fix this?