Nswag: 13.2.0.0 generating non-working C# client with ChangeType as a culprit

Created on 27 Dec 2019  路  12Comments  路  Source: RicoSuter/NSwag

Upgrading from 13.1.6.0 to 13.2.0.0 creates client, that compiles, but does not work (Can't convert from System.String).

Problem is when client is generated, it tries to serialize json to types with ChangeType:

var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
var result_ = (System.Collections.Generic.ICollection<string>)System.Convert.ChangeType(responseData_, typeof(System.Collections.Generic.ICollection<string>));

Old version does this correctly:

var objectResponse_ = await ReadObjectResponseAsync<System.Collections.Generic.ICollection<string>>(response_, headers_).ConfigureAwait(false);
return objectResponse_.Object;

Here is the bug repro for this issue: https://github.com/Enegia/NSwagGeneratorChangeTypeBugRepro

bug

Most helpful comment

Have the same issue here. Just upgrades and the latest upgrade causes this problem

All 12 comments

Have the same issue here. Just upgrades and the latest upgrade causes this problem

I was able to work around this by adding a [Produces("application/json")] to the top of my controller definition. e.g.

    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    public class MyController : ControllerBase

@scberr thanks, that fixed problem for us too.

We have tons of controllers, so we end up using global filter (startup.cs):

services.AddControllers(options =>
{
    options.Filters.Add(new ProducesAttribute("application/json"));
});

Strange, i think we need to look into this regression

this also broke my code. Don't have the ability to change controller endpoints. Rolled back 13.1.6.0

Must be a regression of this PR:
https://github.com/RicoSuter/NSwag/pull/2587

@Jehoel

Erk!

I'm away from my desktop where my proper NSwag repo is - I'll be able to look at this after this coming weekend.

I think i only minimized the problems but when the type is text/plain then there is still a problem in some cases. Can someone create a fix PR?

Is this still a problem?

@RicoSuter - in this issue ( https://github.com/RicoSuter/NSwag/issues/2384 ) the user @tmort93 (and 3 other +1s) are asking for an ETA on my already-merged patch.

Was this page helpful?
0 / 5 - 0 ratings