anatine / zod-plugins

Plugins and utilities for Zod
640 stars 89 forks source link

[zod-openapi] Null handling in NestJS #181

Closed erkstruwe closed 8 months ago

erkstruwe commented 8 months ago

Hi there and thanks for your awesome library!

179 changed null handling from OpenAPI 3.0 to OpenAPI 3.1 syntax (from nullable: true to type: [..., "null"].

According to OpenAPI 3.0 documentation at https://swagger.io/docs/specification/data-models/data-types/, the syntax now used is explicitly marked as "incorrect". Thus, schemas created in NestJS using zod-openapi and zod-nestjs (which uses OpenAPI 3.0) are no longer working with downstream tools such as openapi-generator (e. g. with typescript-axios).

How can we get nullable attributes to work again?

MatthieuJnon commented 8 months ago

We are having the same issue over here, this is a breaking change.

Brian-McBride commented 8 months ago

So, what do we do? Maintain two versions for each version of OpenAPI?

I'm completely open to suggestions. It sounds like NestJS isn't going to update their OpenAPI Spec anytime soon.

Maybe we create an option to specify what API spec you want to use and generate to that one?

erkstruwe commented 8 months ago

NestJS's @nestjs/swagger uses a hardcoded OpenAPI version 3.0 here.

Given the progress this library has made towards OpenAPI 3.1, I can imagine it makes sense to keep types according to OpenAPI 3.1 throughout the zod-openapi package (e. g. extendApi parameters) and to manually change this "back" to OpenAPI 3.0 in zod-nestjs. After all, the list of changes is rather brief.

From my point of view, SchemaHolderClass._OPENAPI_METADATA_FACTORY() would be a good place to pull that off by altering the generatedSchema.

Alternatively, we could add a version parameter to zod-openapi's generateSchema function and handle the differences there. However, this could make typing more difficult (e. g. how should a user choose which version to target when using extendApi?).

erkstruwe commented 8 months ago

@Brian-McBride I drafted a PR for the "nullable" and "exclusive limits" issues. Could you please let me know what you think about it? Thanks!

Tirke commented 8 months ago

@erkstruwe how do we opt-in into that? If I don't explicitly pin @anatine/zod-openapi to 2.2.1 I still have the issue.