swagger icon indicating copy to clipboard operation
swagger copied to clipboard

oneOf always references plain Object

Open mbehm opened this issue 4 months ago • 4 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Current behavior

Using oneOf for @ApiProperty always results in ref to Object

export class Example {
  foo: string;
  bar: string;
}

export class Params {
  @ApiProperty({
    oneOf: [
      {$ref: getSchemaPath(Example)},
      {type: 'array', items: {$ref: getSchemaPath(Example)}}
    ],
  })
  example: Example | Example[]
}

Results in the generated swagger schema

schema:
  $ref: '#/components/schemas/Object'

Minimum reproduction code

https://gist.github.com/mbehm/965da4b9251118b288115aac160291e1

Steps to reproduce

No response

Expected behavior

Expect oneOf to correctly produce the following schema

schema:
  oneOf:
    - $ref: '#/components/schemas/Example'
    - type: array
      items:
        $ref: '#/components/schemas/Example'

Package version

11.2.0

NestJS version

11.0.1

Node.js version

24.3.0

In which operating systems have you tested?

  • [ ] macOS
  • [x] Windows
  • [ ] Linux

Other

No response

mbehm avatar Aug 27 '25 12:08 mbehm

As a workaround using a noop function for type eg. type: () => {} fixes the problem and produces the correct schema definition, but the way shown in the documentation seems to always result in a ref to Object so don't think that's intended.

mbehm avatar Aug 27 '25 19:08 mbehm

I’m interested in working on this issue. Is it okay to proceed before triage is complete?

olllayor avatar Sep 03 '25 13:09 olllayor

+1

zzswang avatar Sep 29 '25 00:09 zzswang

As a workaround using a noop function for type eg. type: () => {} fixes the problem and produces the correct schema definition, but the way shown in the documentation seems to always result in a ref to Object so don't think that's intended.

ApiProperty({
    type: () => {},
    oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
    description,
    required,
  })

it will throw error.

throw new Error(A circular dependency has been detected (property key: "${key}"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").)

zzswang avatar Sep 29 '25 02:09 zzswang