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

Custom type is not retained as type, but rewritten as allOf: - $ref: ... breaking nullable: true spec in @ApiProperty #2948

Open
2 of 4 tasks
gregoryorton-ws opened this issue May 13, 2024 · 5 comments

Comments

@gregoryorton-ws
Copy link

gregoryorton-ws commented May 13, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Custom type is not retained as type, but rewritten as allOf: - $ref: ... breaking nullable: true spec in @ApiProperty

In @ApiProperty when using something like:

@ApiProperty({ nullable: true, type: MyCustomDto })
readonly myVar: MyCustomDto | null;
...

the rendered openapi.yaml is like:

myVar:
  nullable: true
  allOf:
     - $ref: "#/components/schemas/MyCustomDto"

tools like Redoc.ly then start to throw errors:

image

Minimum reproduction code

https://nothing.com

Steps to reproduce

see above

Expected behavior

myVar:
  type: object
  properties:
    id:
      type: integer

Package version

7.3.0

NestJS version

10.3.3

Node.js version

20

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

maybe related: #2645
maybe related: #2646

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@gregoryorton-ws
Copy link
Author

gregoryorton-ws commented May 14, 2024

Sure, but I don't know what the solution would be here -

what should the value of type be? Would we need to put the custom DTO into a schemas and then reference it?

@gregoryorton-ws
Copy link
Author

One solution looks like:

myVar:
  nullable: true
  allOf:
    - $ref: "#/components/schemas/MyCustomDto"
    - type: object

I will test with redoc.ly to see if it's acceptable. If it is, then I think we can just add this type to the output for the custom type.

@gregoryorton-ws
Copy link
Author

gregoryorton-ws commented May 14, 2024

The only think redoc.ly will accept is:

myVar:
  nullable: true
  type: object
  allOf:
    - $ref: "#/components/schemas/MyCustomDto"

so it seems like an easy fix

@clintonb
Copy link
Contributor

Here is the source of our woes:

const keysToRemove = ['type', 'isArray', 'required'];
const validMetadataObject = omit(metadata, keysToRemove);
const extraMetadataKeys = Object.keys(validMetadataObject);
if (extraMetadataKeys.length > 0) {
return {
name: metadata.name || key,
required: metadata.required,
...validMetadataObject,
allOf: [{ $ref }]
} as SchemaObjectMetadata;
}

Extra keys that are not in the set of ['type', 'isArray', 'required'] will result in an allOf instead of a direct $ref. In this case (and #2645), the key is nullable. In my case, it's name.

The list of supported keys needs to be expanded, or removed altogether. I'm not sure which one as I haven't read the OpenAPI spec to any level of detail.

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

No branches or pull requests

3 participants