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

@Transactional decorator breaking swagger documentation #149

Closed
nicobuzeta opened this issue May 6, 2024 · 2 comments · Fixed by #150
Closed

@Transactional decorator breaking swagger documentation #149

nicobuzeta opened this issue May 6, 2024 · 2 comments · Fixed by #150
Labels
bug Something isn't working

Comments

@nicobuzeta
Copy link
Contributor

nicobuzeta commented May 6, 2024

Hi, I'm having a problem with the @transactional decorator. When I add it to a controller method, the corresponding post on the swagger documentation loses its corresponding openapi.ApiResponse. I've looked at the compiled JS code and both decorators are being added correctly, so I'm not entirely sure where the problem could be coming from, but I'm not well versed in NestJS decorators. Do you have any idea what might be causing it?

For example:
These two endpoints:

  @Post()
  @UseGuards(AuthGuard)
  async create(@Body() dto: CreateNaturalPersonWithPersonWithLocationDto): Promise<GetNaturalPersonWithPersonWithLocationDto> {
    const naturalPerson = await this.personNaturalCreatorService.createWithPersonWithLocation({ dto });
    return this.personNaturalGetterService.getById({ id: naturalPerson.id });
  }

  @Post('/d')
  async createx(@Body() dto: CreateNaturalPersonWithPersonWithLocationDto): Promise<GetNaturalPersonWithPersonWithLocationDto> {
    const naturalPerson = await this.personNaturalCreatorService.createWithPersonWithLocation({ dto });
    return this.personNaturalGetterService.getById({ id: naturalPerson.id });
  }

  @Post('/s')
  @Transactional()
  async created(@Body() dto: CreateNaturalPersonWithPersonWithLocationDto): Promise<GetNaturalPersonWithPersonWithLocationDto> {
    const naturalPerson = await this.personNaturalCreatorService.createWithPersonWithLocation({ dto });
    return this.personNaturalGetterService.getById({ id: naturalPerson.id });
  }
}

The first two have the post schema on swagger, while the last doesn't. However, the compiled JS:

__decorate([
    (0, common_1.Post)(),
    (0, common_1.UseGuards)(auth_guard_1.AuthGuard),
    openapi.ApiResponse({ status: 201, type: require("./dto/getNaturalPerson.dto").GetNaturalPersonWithPersonWithLocationDto }),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [createNaturalPerson_dto_1.CreateNaturalPersonWithPersonWithLocationDto]),
    __metadata("design:returntype", Promise)
], PersonNaturalController.prototype, "create", null);
__decorate([
    (0, common_1.Post)('/d'),
    openapi.ApiResponse({ status: 201, type: require("./dto/getNaturalPerson.dto").GetNaturalPersonWithPersonWithLocationDto }),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [createNaturalPerson_dto_1.CreateNaturalPersonWithPersonWithLocationDto]),
    __metadata("design:returntype", Promise)
], PersonNaturalController.prototype, "createx", null);
__decorate([
    (0, common_1.Post)('/s'),
    (0, transactional_1.Transactional)(),
    openapi.ApiResponse({ status: 201, type: require("./dto/getNaturalPerson.dto").GetNaturalPersonWithPersonWithLocationDto }),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [createNaturalPerson_dto_1.CreateNaturalPersonWithPersonWithLocationDto]),
    __metadata("design:returntype", Promise)
], PersonNaturalController.prototype, "created", null);

Has the correct decorators from what I can see. What could the cause of the problem?

Edit:
On further inspection I found this issue on the swagger repo. I think this might be what's wrong with @transactional as it also overwrites the property.

@Papooch
Copy link
Owner

Papooch commented May 7, 2024

Good catch! I knew about this limitation but I hadn't figured out how to solve it before.

It's not really advisable to wrap entire controller methods in a transaction, so I didn't pay it much attention.

Thank you so much for the investigation and the PR, if it doesn't break anything, I'll merge it as soon as I can.

@Papooch Papooch added the bug Something isn't working label May 7, 2024
@Papooch
Copy link
Owner

Papooch commented May 22, 2024

Fix released in @nestjs-cls/transactional@2.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants