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.

Response Type changes base on return type

See original GitHub issue

The generated code changes the response type base on the return type of the method. This might have some secret reason, but produces a very hard to find error when working with the generated service. In my case (see below) it cause the err.error property to be a string, instead of a javascript object (json) and made the whole error handling code fail silently, just because the return type in the asp.net core WebApi changed. Ofcorse changing the return type causes the response handling code to have to change, but it is unintuitive that the error handling code should change. What makes the issue worse, is that there are numerous points of failure for an issue like that: the asp.net api, swagger/open-api documentation generation, ng-openapi-gen configration files or even IIS settings. Took me a very long time to find the issue.

I think this behavior should be changed, but for my case having a way to set the default response type used for the whole api would have been sufficient as I only want to use json anyway. My current workaround is just to return the ‘0’ to get a json response …

Note: The actual namespace and Dto class name was changed in the following code per company policy.

[HttpPost("save", Name = nameof(Save))]
        public async Task Save(Dto dto)
        {
            var settings = dto.ToModels(tenant);
            await _userSettingsRepository.Update(settings);
        }

The code above produces

  save$Response(params?: {
    body?: Dto
  }): Observable<StrictHttpResponse<void>> {

    const rb = new RequestBuilder(this.rootUrl, DtoService.SavePath, 'post');
    if (params) {
      rb.body(params.body, 'application/*+json');
    }

    return this.http.request(rb.build({
      responseType: 'text',
      accept: '*/*'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return (r as HttpResponse<any>).clone({ body: undefined }) as StrictHttpResponse<void>;
      })
    );
  }

while the code

[HttpPost("save", Name = nameof(Save))]
        public async Task<int> Save(Dto dto)
        {
            var settings = dto.ToModels(tenant);
            await _userSettingsRepository.Update(settings);

            return 0;
        }

produces:

save$Response(params?: {
    body?: Dto
  }): Observable<StrictHttpResponse<number>> {

    const rb = new RequestBuilder(this.rootUrl, DtoService.SavePath, 'post');
    if (params) {
      rb.body(params.body, 'application/*+json');
    }

    return this.http.request(rb.build({
      responseType: 'json',
      accept: 'text/json'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return (r as HttpResponse<any>).clone({ body: parseFloat(String((r as HttpResponse<any>).body)) }) as StrictHttpResponse<number>;
      })
    );
  }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
luisfpgcommented, Jan 28, 2022

It is as expected. Unfortunately, there’s no way to specify distinct responseTypes for distinct HTTP status codes. The way we do a general error handling is like this: https://github.com/cyclosproject/cyclos4-ui/blob/0adeed887f8b397fa709d872a864df342df37f4c/src/app/core/error-handler.service.ts#L151 We just assume the error can be either a JSON object or a string.

0reactions
ThomasOrtizcommented, Jan 28, 2022

Thanks for responding so quick and giving me an example of what your other project does.

After looking around online I understand why it can’t be done because of an Angular HttpClient restriction which seems obvious now.

For anyone else who revists this issue here are Angular issues that are related:

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change functions return type based on the input ...
Returns TResult (or return type of transformResult callback, but I am not sure if this is possible) if the callback is provided.
Read more >
Response.type - Web APIs - MDN Web Docs
The type read-only property of the Response interface contains the type of the response. It can be one of the following:
Read more >
Azure API Management advanced policies | Microsoft Learn
Return response - Aborts pipeline execution and returns the ... Set status code - Changes the HTTP status code to the specified value....
Read more >
Customizing Queries - Redux Toolkit
transformResponse is called with the meta property returned from the baseQuery as its second argument, which can be used while determining the ...
Read more >
Use a mapping template to override an API's request and ...
Any type of request parameter, response header, or response status code may be overridden. Following are example uses for a mapping template override:....
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