Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.

Home Page:https://papooch.github.io/nestjs-cls/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@Transactional decorator breaking swagger documentation

nicobuzeta opened this issue · comments

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.

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.