question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

complex type in API [FromBody] signature incorrectly handled in generated TypeScript as string

See original GitHub issue

Hi, I’m using:

  • Swashbuckle package (version 5.6.0) in WebApi 2 .NET project to generate json representation of API -
  • nswag npm package (version 11.11.3) with swagger2tsclient command to generate TS based on API json representation

nswag swagger2tsclient /Input:swagger.json /Output:src/api.ts /ClassName:{controller}Service /WrapDtoExceptions:true /TypeScriptVersion:2.0 /Template:Angular DateTimeType:Date /GenerateClientClasses:true /GenerateClientInterfaces:true /ImportRequiredTypes:true /BaseUrlTokenName:API_BASE_URL /HttpClass:HttpClient /injectionTokenType:InjectionToken

WebApi controller:

 public async Task<IHttpActionResult> Patch(int id, [FromBody] ComplexType complexType)
 {
       return this.StatusCode(HttpStatusCode.NoContent);
 }

  public class ComplexType 
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }

swagger json for this request params is:

"parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Id",
            "required": true,
            "type": "integer",
            "format": "int32"
          },
          {
            "name": "complexType",
            "in": "body",
            "description": "ComplexType model",
            "required": true,
            "schema": { "$ref": "#/definitions/ComplexType" }
          }
        ],

the problem is that generated typescript code expects complex type as string which isn’t right:

patch(id: number, complexType: string): Observable<any> {

generated code also includes TS class definition for ComplexType but as you can see, string type is used instead of ComplexType in TS function parameter

Am I missing something in my setup? do I need to apply some attribute/command switch to ensure generated typescript is using complex type as parameter instead of string?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RicoSutercommented, May 16, 2018

Yep, but it’s just a temporary patch until this is fixed…

1reaction
RicoSutercommented, May 16, 2018

If I switch the consumes/produces, it works:

    "consumes": [
      "application/json",
      "application/xml"
    ],
    "produces": [
      "application/json",
      "application/xml"
    ],

have to check whether we have to fix this in the generator or if this is expected (probably not 😃)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass multiple complex objects to a post/put Web API method
You simply need to adjust the signature to accept just one complex object from the body, the JObject , let's call it stuff...
Read more >
Accepting Raw Request Body Content with ASP.NET Web API
This parameter signature does not work with any of these posted values: Raw Content Buffer Data (entire buffer); A JSON string with application/ ......
Read more >
Passing multiple POST parameters to Web API Controller ...
Here's how Web API handles parameters and how you can optionally manage multiple parameters to API Controller methods.
Read more >
Oops, I Did It Again...I Made A Rust Web API And It Was ...
We just need to provide a string name, like Todos::new("Task") , and this type will generate a new unique ID and set it...
Read more >
Angular 5/ASP.NET Core: The Best of Both Worlds
In this article, Toptal Freelance Angular Developer Pablo Albella teaches us how to create the best architecture for both these worlds. authors are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found