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

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

Open
abdel7517 opened this issue Sep 4, 2024 · 5 comments

Comments

@abdel7517
Copy link

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!

@ad-on-is
Copy link
Owner

ad-on-is commented Sep 4, 2024

Can you please post some code for a better understanding?

@abdel7517
Copy link
Author

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
  • @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(),
    })
    }
    }`

@ad-on-is
Copy link
Owner

ad-on-is commented Sep 5, 2024

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

@abdel7517
Copy link
Author

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
Copy link
Author

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

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

No branches or pull requests

2 participants