NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

NSwag studio code generation unwraps the class properties and puts them in as parameters rather than the object itself.

Open patel-jeel92 opened this issue 6 years ago • 11 comments

WebAPI targets .NET Core 2.1

I have an api call which takes in my custom object as a parameter. When I generate the client code using NSwag Studio, it unwraps all the properties of the custom object and puts those as parameters rather than the object itself. As a result my client looks like this:

System.Threading.Tasks.Task AddNewPerson(string name, int age, sting zodiacSign);

instead of

System.Threading.Tasks.Task AddNewPerson(Person person);

patel-jeel92 avatar Jul 16 '18 14:07 patel-jeel92

Same problem, used [FromBody] to fix de issue.

smariussorin avatar Sep 03 '18 10:09 smariussorin

[FromBody] won't work with GET. This is still an issue and should be a setting.

Kordonme avatar Oct 16 '18 10:10 Kordonme

Any update on this? I'm having the same issue as well. I don't want to [FromBody] all my APIs, as it'd make more sense to get the inital issue fixed. I'm guessing this has something to do with the CodeGen.nswag configuration?

Caraballo114 avatar Jan 02 '19 19:01 Caraballo114

Cant this be solved with a custom asp.net core api convention?

RicoSuter avatar Jan 09 '19 11:01 RicoSuter

immo the issue must be solved during json generation step, but I'm not really into openapi specs so maybe this is not standard to achieve. I would be nice from this controller

[HttpGet]
[ProducesResponseType(typeof(Contact), 200]
public IActionResult Get([FromQuery]ContactQuery query) { ... }

(which produces this output json)...

{
  "v1/contacts": {
    "get": {
      ...
      "parameters" : [
        <each ContactQuery field>
      ]
    }
  }
}

...to have a generated client with the ContactQuery model instead of single split parameters

rrr3da avatar Apr 03 '19 08:04 rrr3da

Hi! I have the same problem: the complex parameters decorated as "[FromQuery]" are translated with a list of primitive type properties.

Are there news about the resolution of this issue?

packe100 avatar Aug 22 '19 07:08 packe100

The problem is that this cannot be described with openapi/swagger

RicoSuter avatar Aug 22 '19 19:08 RicoSuter

Hi, thank you for your answer.

That being the case (i.e. no support from openapi), I would say that what I'm trying to do - FromQuery argument automation - is not a common/orthodox practice. Is there probably a better solution?

packe100 avatar Aug 24 '19 15:08 packe100

Any progress on this?

madhavpatel6 avatar Mar 25 '21 04:03 madhavpatel6

Has someone found a work-around for this? It gets pretty annoying when you have GET endpoint with 20~ search parameters

DavisZ94 avatar May 03 '22 07:05 DavisZ94

Would love a solotion for this problem. Any news?

CedricBaetens avatar Aug 17 '22 14:08 CedricBaetens

Related issues/duplicates: https://github.com/RicoSuter/NSwag/issues/1193 https://github.com/RicoSuter/NSwag/issues/3301 https://github.com/RicoSuter/NSwag/issues/3486

magicxor avatar Dec 14 '22 17:12 magicxor

There is a software swagger-typescript-api that creates more convenient signatures for complex DTOs in GET requests:

image

Maybe we could use it as a reference to implement a similar feature in NSwag

magicxor avatar Dec 14 '22 17:12 magicxor

The option to generate DTO classes for input/output parameters works but is not used inside the C# client. It still use flattened parameters.

adampaquette avatar Apr 11 '23 14:04 adampaquette

I was able to solve this using a modified version of the solution here: https://tkit.dev/2020/03/10/complex-get-query-param-objects-in-swashbuckle/

brian-kane521 avatar Dec 07 '23 16:12 brian-kane521

The link in my previous comment causes correct code generation and the Swagger webpage behaves as expected, however NSwagStudio does not generate the correct code.

brian-kane521 avatar Dec 08 '23 15:12 brian-kane521

@brian-kane521 can you show us your modified version of the solution?

eduardheller89 avatar Mar 06 '24 16:03 eduardheller89

@eduardheller89 I ended up reverting my c# back to standard and instead using a custom liquid template for NSwagStudio to generate typescript API calls using classes/objects instead of each individual parameter.

For a controller with a signature like this:

[HttpGet]
public async Task<ActionResult<ResponseType>> GetSomething([FromQuery] RequestType request)
{ // code here }

NSwagStudio will generate the following in the typescript client:

export class getSomethingParametersDto {
    param1: number[];
    param2: boolean;
    // etc
}

getSomethingDto(body: getSomethingParametersDto ): Promise<ResponseType> {

        const content_ = Object.keys(body as any).map((key) => {
            return encodeURIComponent(key) + '=' + encodeURIComponent((body as any)[key]);
        }).join('&')

        let url_ = this.baseUrl + "/api/Something?" + content_;

        let options_: RequestInit = {
            method: "GET",
            headers: {
                "Accept": "text/plain"
            }
        };

        return this.transformOptions(options_).then(transformedOptions_ => {
            return this.http.fetch(url_, transformedOptions_);
        }).then((_response: Response) => {
            return this.transformResult(url_, _response, (_response: Response) => this.processGetSomething(_response));
        });
    }

Essentially, the generated code will translate from an object to individual query parameters. This has no effect on the generated swagger JSON or c# behavior, so while it solved the usability problem I was hoping to solve, it might not be the best approach if you're concerned about 3rd party API consumers etc.

Here's the liquid template I wrote: FetchClient.zip

brian-kane521 avatar Mar 06 '24 19:03 brian-kane521

@brian-kane521 thanks for the quicks response and solution. But I am afraid I am using third party api consumer, so I need the query parameters as a ref inside parameters groups as schema. Do you still have the IOperationFilter extensions in nswag which produces the code mentioned in the article? I tried but failed to convert it to nswag.

eduardheller89 avatar Mar 07 '24 09:03 eduardheller89

@eduardheller89 Are you having issues with the code as-is from the article? Looking back at my git history, I only made minor modifications to conform with .NET 6/C# 10. I can post the code here but it is functionally the same, so if you're having other issues it probably will not resolve them.

brian-kane521 avatar Mar 07 '24 15:03 brian-kane521