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

Why NSwag doesn't generate model from request? #3486

Closed
DragonDara opened this issue May 27, 2021 · 11 comments
Closed

Why NSwag doesn't generate model from request? #3486

DragonDara opened this issue May 27, 2021 · 11 comments

Comments

@DragonDara
Copy link

DragonDara commented May 27, 2021

Hi, there!

I have REST API on .NET Core 3.1 and we use NSwag to generate client code(typescript).

It works well except for one thing. Nswag doesn't generate a model in the request query.

/// <summary>
/// Get SNT report by filters
/// </summary>
[RoleAuthorize(RoleTypes = new RoleType[] { RoleType.SntOperator, RoleType.SntReadOnly, RoleType.TCOWarehouse })]
[HttpGet("get-snt-report")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[SwaggerResponse(typeof(FileStreamResult))]
public async Task<IActionResult> GetSntReport([FromQuery] SntListFilter filter, CancellationToken cancellationToken)
{
    var stream = await _sender.Send(new GetSntReportQuery(filter), cancellationToken);
    return File(stream, "text/plain", "SNT-report.txt");
}

After generating, I get the following typescript code:

getSntReport(dateFrom?: Date | null | undefined, dateTo?: Date | null | undefined, type?: SntType | null | undefined, status?: SntStatus | null | undefined, borderCrossingDateFrom?: Date | null | undefined, borderCrossingDateTo?: Date | null | undefined, lastUpdateDateFrom?: Date | null | undefined, lastUpdateDateTo?: Date | null | undefined, sellerTin?: string | null | undefined, customerTin?: string | null | undefined, number?: string | null | undefined, registrationNumber?: string | null | undefined, declarationNumberForSnt?: string | null | undefined, carModel?: string | null | undefined, carStateNumber?: string | null | undefined): Observable<FileResponse>

I searched the Internet for answers, but I failed.
Does nswag support such functionality? If yes, what should I use?

Regards,
Darkhan

@ViRuSTriNiTy
Copy link

You should use the generic IActionResult<> interface to specify the result type. You can also specify the return type by attributing the action like [ProducesResponseType(typeof(MyReturnType), ...)].

@DragonDara
Copy link
Author

You should use the generic IActionResult<> interface to specify the result type. You can also specify the return type by attributing the action like [ProducesResponseType(typeof(MyReturnType), ...)].

No, You got me wrong. I retrieved successfully return type in generated code . My issue is to generate a model that in request as the param of the controller method.
изображение
SntListFilter model isn't generated by Nswag. So I struggle to resolve it.

@ViRuSTriNiTy
Copy link

ViRuSTriNiTy commented May 27, 2021

Ok, so it seems you have the same issue here as I already reported in #3301, see the comments why it is that way.

Simply put, there is not solution right now and you have to live with simple parameters. I did try to change the NSwag sources to generate a model, I remember it being pretty hard effort but then I got distracted and could never finish the change and provide a proof of concept (if that is even possible).

@DragonDara
Copy link
Author

Ok, so it seems you have the same issue here as I already reported in #3301, see the comments why it is that way.

Simply put, there is not solution right now and you have to live with simple parameters. I did try to change the NSwag sources to generate a model, I remember it being pretty hard effort but then I got distracted and could never finish the change and provide a proof of concept (if that is even possible).

Thanks for truly reply) Is it possible to tell Nswag to generate a particular model?

@ViRuSTriNiTy
Copy link

ViRuSTriNiTy commented May 28, 2021

Is it possible to tell Nswag to generate a particular model?

No as NSwag flattens the class properties (in your case the propertiers of SntListFilter) to a simple parameter list. @RicoSuter mentioned the underlying reason for this

#3301 (comment)

I think the main "problem" is that OpenAPI/Swagger has no notion to describe that directly

As I already wrote, I tried to find a solution for it but I could not finish my solution so that it can be merged.

@DragonDara
Copy link
Author

Thanks a lot for helping me

@alpha-nero1
Copy link

Hi There @DragonDara after playing around with the same issue, I found that using [FromBody] instead of [FromQuery] makes nswag generate the request dto for you instead of the annoying endless params.

So for instance create(title: string, description: string...) will be turned into:
create(request: Command)

Hope this helps if anyone else runs into this issue,
Thanks

@DragonDara
Copy link
Author

DragonDara commented Mar 13, 2023 via email

@ramax495
Copy link

Hi There @DragonDara after playing around with the same issue, I found that using [FromBody] instead of [FromQuery] makes nswag generate the request dto for you instead of the annoying endless params.

So for instance create(title: string, description: string...) will be turned into: create(request: Command)

Hope this helps if anyone else runs into this issue, Thanks

It really generates DTO-s, but GET-methods on backend has been broken and error is thrown - "A non-empty request body is required".

@goors
Copy link

goors commented May 27, 2024

This can not be done for Get, it will brake any Dto that you have into params ... sucks...
I will try this #3301 (comment)

@Mkrager
Copy link

Mkrager commented Dec 20, 2024

Does anyone know the solution?

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

6 participants