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

Nullable Enum not generated as nullable #1889

Closed
2 of 4 tasks
jbdemonte opened this issue Apr 13, 2022 · 10 comments
Closed
2 of 4 tasks

Nullable Enum not generated as nullable #1889

jbdemonte opened this issue Apr 13, 2022 · 10 comments

Comments

@jbdemonte
Copy link

jbdemonte commented Apr 13, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Using the following annotation:

export class ClientResponseDto {
  @Expose()
  @ApiProperty({ enum: PaymentTermsEnum, enumName: 'PaymentTermsEnum', nullable: true })
  paymentTerms!: PaymentTermsEnum | null;

}

openapi-generator 5.4 generates:

export interface GetClientByIdResponseDto { 
    paymentTerms: PaymentTermsEnum;

I need to use the following workaround to get it nullable:

export class ClientResponseDto {
  @Expose()
  @ApiProperty({ oneOf: [{ type: 'enum', $ref: 'PaymentTermsEnum', nullable: true }] })
  paymentTerms!: PaymentTermsEnum | null;
}

=>

export interface GetClientByIdResponseDto {
    paymentTerms: PaymentTermsEnum | null;
}

Minimum reproduction code

https://github.com/jbdemonte/issue-nestjs-openapi-generator-nullable-enum

Steps to reproduce

  1. npm install
  2. npm run generate:angular-client

Expected behavior

the exported value type should be value!: FooBarEnum | null;

Package version

5.2.1

NestJS version

8.4.4

Node.js version

16.13.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

Let's track this here #2051

@milo526
Copy link

milo526 commented Oct 31, 2023

Can we re-open this issue?

The problem is still present in the latest version of this library, and the linked merge request is closed.

@kamilmysliwiec
Copy link
Member

This should be fixed in newer versions of the library. Could you create a separate issue and provide a minimum reproduction repository?

@mothershipper
Copy link
Contributor

mothershipper commented Dec 18, 2023

We're also experiencing this issue on the latest version of the library.

The problem is that when you name the enum, Nest is extracting the enum into a shared type (with$ref), but leaving the nullable: true on the property rather than the shared type:

objects:
  Enum:
    type: string
    values:
      - One
      - Two
  MyApiObj:
    properties:
       someField:
         $ref: objects/Enum
         nullable: true

OpenAPI treats $ref as a replace operation rather than merge, so nullable: true is lost.

If you don't supply a name to the ApiProperty decorator, Nest doesn't extract the enum definition into a separate type, declaring it inline so nullable: true is preserved. While this works, it also means code-generators generate worse names for the enum.

It looks like Nest added support for the nest compiler to generate unnamed nullable enum types as described above, but not named types. Though, if the named enum is used in difference contexts (one nullable, one not) you really can't extract to a shared named type, unless using OpenAPI 3.1.x semantics for nullable properties, i.e.

objects:
  Enum:
    type: string
    values:
      - One
      - Two
  MyApiObj:
    properties:
       someField:
         oneOf:
           - $ref: objects/Enum
           - type: 'null'

@jsangilve
Copy link

jsangilve commented Apr 26, 2024

Spent today more than one hour dealing with the issue perfectly described by @mothershipper 🔝 : when the enumName property is used, it does not work as expected and the nullable: true is ignored in the generated TS.

@jessecoleman
Copy link

Would be nice to get a fix for this, I also just ran into the same behavior.

@wrheinheimer
Copy link

wrheinheimer commented Aug 22, 2024

This seems to be fixed by adding the following lines to SchemaObjectFactory.createEnumSchemaType:

const wrappedRef = metadata.nullable ? { allOf: [{ $ref }] } : { $ref };
const refHost = metadata.isArray ? { items: wrappedRef } : wrappedRef;

@MrCode1997
Copy link

MrCode1997 commented Oct 9, 2024

Hi, i still have the issue, could you fix it?
At the moment this problem is blocking our pipelines

@BenStirrup
Copy link

Any update ?

@kamilmysliwiec
Copy link
Member

It should be fixed in the latest version. If you run into any issues, please create a new issue with a reproduction repository

@nestjs nestjs locked and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants