Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zod-openapi outputting invalid OpenAPI specs when using 'nullable' #228

Open
sebpowell opened this issue Oct 3, 2024 · 4 comments
Open

Comments

@sebpowell
Copy link

sebpowell commented Oct 3, 2024

Hello,

I've just started using this library and have come across the following issue. Consider the following:

import { extendApi } from '@anatine/zod-openapi';
import { object, string, z } from 'zod';

export const GetItems = extendApi(
  z.object({
    skus: object({
      test: string().nullable(),
    }),
  }),
);

The Swagger UI looks like this:

Screenshot 2024-10-04 at 00 21 59

And the JSON / Yaml it generates looks like this:

schemas:
    GetItemsDto:
      type: object
      properties:
        items:
          type: object
          properties:
            test:
              type: 'null'
          required:
            - test
      required:
        - items

Not sure if this is a known bug, or whether I'm somehow doing something wrong?

In case it's relevant, I am using this within a Nest.js application, so the Swagger docs are being generated as per the documentation:

import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { ResponseInterceptor } from 'src/interceptors/response.interceptor';
import { ZodValidationPipe } from '@anatine/zod-nestjs';

import { patchNestjsSwagger } from '@anatine/zod-nestjs';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.useGlobalPipes(
    new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }),
  );

  app.useGlobalPipes(new ZodValidationPipe());

  app.useGlobalInterceptors(new ResponseInterceptor());

  const options = new DocumentBuilder()
    .setVersion('1.0')
    .addBearerAuth()
    .build();

  patchNestjsSwagger();

  const document = SwaggerModule.createDocument(app, options);

  SwaggerModule.setup('api', app, document);

  app.enableCors();

  // app.useLogger(app.get(Logger));

  await app.listen(3000);
}
bootstrap();

Thank you in advance.

@briangottfried
Copy link

We've gotten around this by doing .or(z.null()).

@Xayrulloh
Copy link

Same with me. Another way is .union([z.string(), z.null(), etc..]). But when you wanna edit that schema by calling it and to set that property to required it doesn't change anything it'd be still like string | null | etc

@benyap
Copy link

benyap commented Oct 18, 2024

We've reverted to @anatine/[email protected] as an alternative solution until this is fixed.

Changing all of our schemas which had .nullable() to .or(z.null()) wasn't an ideal workaround as it also changed the output OpenAPI spec.

v2.2.1 with .nullable()

{
  "field": {
    "type": "boolean",
    "nullable": true
  }
}

image

v2.2.6 after changing to .or(z.null())

{
  "field": {
    "oneOf": [
      {
        "type": "boolean"
      },
      {
        "type": "null"
      }
    ]
  }
}

image

@alex-statsig
Copy link

This is related to #211 and #221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants