adonis-autoswagger icon indicating copy to clipboard operation
adonis-autoswagger copied to clipboard

🚫 Readonly properties of the models are not considered in the @requestBody.

Open abdel7517 opened this issue 1 year ago • 5 comments

Hello, is it possible to include readonly properties for models? I have a class that I'm using as a requestBody, specified with the @requestBody decorator, but the readonly properties do not appear in Swagger-UI. Thank you!

abdel7517 avatar Sep 04 '24 15:09 abdel7517

Can you please post some code for a better understanding?

ad-on-is avatar Sep 04 '24 17:09 ad-on-is

This is the class i try to import : export default class CreateUserDto { constructor( readonly name: string, readonly firstName: string, readonly email: string, readonly companyId: UUID, readonly password: string, readonly phone: string ) {} }

And I try to use with @requestBody, in this controller :

`export default class AuthUserController { constructor(private authUserUseCase: AuthUserUseCase) {} /**

  • @handle
  • @requestBody <CreateUserDto>
  • @responseBody 201 - {message: User authenticated successfully} */ async handle(ctx: HttpContext): Promise { const userPayload = ctx.request.all() as AuthUserDto const resultOfAuthUserUseCase = await this.authUserUseCase.handle(userPayload) if (resultOfAuthUserUseCase.isError) return ctx.response .status(resultOfAuthUserUseCase.statusCode) .json({ message: resultOfAuthUserUseCase.message }) return ctx.response.status(200).json({ message: 'User authenticated successfully', token: resultOfAuthUserUseCase.getValue(), }) } }`

abdel7517 avatar Sep 05 '24 12:09 abdel7517

May I ask, why not use a model or validator instead, since this is the adonisJS-way of doing these things?

ad-on-is avatar Sep 05 '24 12:09 ad-on-is

Thank you for your response. I am using a hexagonal architecture, so I handle all business constraints, including payload validation, in the domain layer of the application (where all my business logic resides). I could manage this in the controller, but it would be redundant work. However, I have already analyzed your package and modified it locally to include the readonly properties. Is it possible to make a PR?

abdel7517 avatar Sep 05 '24 12:09 abdel7517

I assume that the add-on is very busy, so while waiting for a response, for those who have the same need as I do, here is a modified version of the package that accepts both readonly props and props with a default value (e.g., size: number = 30). This is because these types of variables are not handled in the current version. https://www.npmjs.com/package/auto-swagger-adonis-abd

abdel7517 avatar Sep 11 '24 10:09 abdel7517