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

@ApiSchema not working as expected when inheriting #3160

Closed
2 of 4 tasks
arabold opened this issue Nov 11, 2024 · 1 comment
Closed
2 of 4 tasks

@ApiSchema not working as expected when inheriting #3160

arabold opened this issue Nov 11, 2024 · 1 comment

Comments

@arabold
Copy link
Contributor

arabold commented Nov 11, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Assume you define two DTOs that inherit from each other:

@ApiSchema({ name: 'BaseResponse' })
class BaseResponseDto {
}

@ApiSchema({ name: 'Response' })
class ResponseDto extends BaseResponse {
}

Then use this class in your controller:

@Controller()
class MyController() {
   @Get()
   @ApiOkResponse({ type: ResponseDto })
   async doSomething(): Promise<ResponseDto> {
     // ...
   }
}

In the resulting OpenAPI spec you would expect that the response schema for doSomething is Reponse, the specialized subclass ResponseDto. However, the OpenAPI spec uses BaseResponse instead, the parent class.

Minimum reproduction code

Example in the description above

Steps to reproduce

No response

Expected behavior

For inherited classes I would expect the schema name of the subclass to be used, not of the parent class.

The error lies in the following piece of code in lib/services/schema-object-factory.ts:

    const schemaName = customSchema[0].name;

It should be replaced by

    const schemaName = customSchema[customSchema.length - 1].name;

to use the schema name of the subclass.

Package version

8.0.5

NestJS version

10.4.7

Node.js version

20.11.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

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

2 participants