RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.62k stars 1.23k forks source link

Upgrading NSwag to the latest version (v14.0.3) broke C# client generation for arrays/lists with nullable primitive types #4788

Closed michu94 closed 2 months ago

michu94 commented 5 months ago

Hello all!

Issue description:

I've just upgraded my NSwag libraries & client to the latest version v14.0.3 (from v13.19.0):

My C# dto class:

  public class TestClass
  {
      public IEnumerable<decimal?> Values { get; set; } = Enumerable.Empty<decimal?>();
  }

Before the upgrade, generated code looked like:

  [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.19.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
  public partial class GeneratedTestClass
  {
      [Newtonsoft.Json.JsonProperty("values", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
      public System.Collections.Generic.ICollection<decimal?> Values { get; set; }
  }

which is fine. As we can see in the output file our list is of type ICollection<decimal?>, exactly as expected.

But, after the upgrate to the versions listed at the top of this issue, generated code looks like that:

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.3.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
    public partial class GeneratedTestClass
    {
        [Newtonsoft.Json.JsonProperty("values", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public System.Collections.Generic.ICollection<decimal> Values { get; set; }
    }

As you can see, there is a discrepancy between what we have as our base class and the output from the generated class. We want to have nullable decimals in output list but NSwag generated ICollection<decimal> instead of ICollection<decimal?>. I haven't change anything but the libraries' versions. nswag.json and all the configuration stuff remained untouched.

Am I correct that this is misbehaving? Looks like a bug to me. I'm expecting that generated class will keep the underlying nullable primitive types?

I would highly appreciate every answer!

RicoSuter commented 4 months ago

Looks good to me:

image

michu94 commented 2 months ago

it works in the latest version