Skip to content

Using nullable on ApiProperty causes $ref to be wrapped in allOf #2645

@MichaelMarner

Description

@MichaelMarner

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Using nullable with ApiProperty causes the referenced type to be wrapped in allOf in the resulting swagger file. I believe this is a mistake, and causes issues with generated code with strict typing (in our case, Dart).

The underlying problem is SchemaObjectFactory.createNotBuiltInTypeReference strips some keys from the property decorator, then checks if any are remaining. If there are, the type is wrapped in an allOf.

Minimum reproduction code

https://gist.github.com/MichaelMarner/1696ba2b7b97b8c8cb85289b191de222

Steps to reproduce

The following DTO

export class NestedTypeDto {
  @ApiProperty({
    type: string
  })
  value: string;
}

export class SampleDto {
  @ApiProperty({
    type: NestedTypeDto,
    required: true,
  })
  propertyWorksCorrectly: NestedTypeDto;
  
    @ApiProperty({
    type: NestedTypeDto,
    required: true,
    nullable: false,
  })
  wrappedInAllOf: NestedTypeDto;
}

Will result in the following generated Swagger:

          "propertyWorksCorrectly": {
            "$ref": "#/components/schemas/NestedTypeDto"
          },
          "wrappedInAllOf": {
            "nullable": false,
            "allOf": [{ "$ref": "#/components/schemas/NestedTypeDto" }]
          },

Expected behavior

The Swagger output in the example above should not wrap the $ref in allOf. eg:

          "propertyWorksCorrectly": {
            "$ref": "#/components/schemas/NestedTypeDto"
          },
          "wrappedInAllOf": {
            "nullable": false,
            "$ref": "#/components/schemas/NestedTypeDto"
          },

Package version

7.1.12

NestJS version

10.0.2

Node.js version

18.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

From the spec:

OpenAPI 3.0 does not have an explicit null type as in JSON Schema, but you can use nullable: true to specify that the value may be null.
The example above may be mapped to the nullable types int? in C# and java.lang.Integer in Java. In objects, a nullable property is not the same as an optional property, but some tools may choose to map an optional property to the null value.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions