RicoSuter / NSwag

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

jsonignore attributes is not showing in latest nswag 14.0.7 version generated code #4852

Open isteyak opened 2 months ago

isteyak commented 2 months ago

I'musing openApiToCSharpClient (nswag 14.0.7 version) prior to that i was using (nswag 13.20.0 version) before my client code was showing jsonignore attributes like :

[System.Text.Json.Serialization.JsonPropertyName("href")] [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)] [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)] public System.Uri Href { get; set; }

Now its not showing

[System.Text.Json.Serialization.JsonPropertyName("href")] [System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)] public System.Uri Href { get; set; }

what do i need to do in-order to fix it?

franklixuefei commented 2 months ago

We are encountering the exact same issue. For us, we have this model class -

public class SystemData
{
    [JsonPropertyName("createdAt")]
    public DateTimeOffset CreatedAt { get; set; }
}

Prior to nswag v14.0.7 (we were using 13.8.2), we saw the below in the generated contract.g.cs, which is what we wanted:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class SystemData
{
    [System.Text.Json.Serialization.JsonPropertyName("createdAt")]
    [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]   
    public System.DateTimeOffset? CreatedAt { get; set; } = default!;
}

After we bumped up to v14.0.7, we see the below instead. Note that the JsonIgnore attribute is gone.

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class SystemData
{
    [System.Text.Json.Serialization.JsonPropertyName("createdAt")]
    public System.DateTimeOffset? CreatedAt { get; set; } = default!;
}

Could this be a regression?

isteyak commented 2 months ago

still finding way to fix it

isteyak commented 2 months ago

unfortunately jsonignore attributes for property has been removed from njsonschema 11.0.0 version.

isteyak commented 2 months ago

@franklixuefei I just came up with one of approach, just have a look. Its working for me

  1. Created a new folder, NSwagTemplates.
  2. Updated my NSwag config file to point to this new folder: "templateDirectory": "NSwagTemplates",
  3. Created the file Class.liquid in this folder with the contents copied from https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid Replaced lines 56-58 with the code {%- if UseSystemTextJson %} [System.Text.Json.Serialization.JsonPropertyName("{{ property.Name }}")] {%- if property.IsRequired -%} [System.Text.Json.Serialization.JsonIgnore(Condition = {{ "System.Text.Json.Serialization.JsonIgnoreCondition.Never" }})] {%- else -%} [System.Text.Json.Serialization.JsonIgnore(Condition = {{ "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault" }})] {%- endif -%}

4.Ran Nswag as normal - nswag run NSwagConfig.nswag

Took reference from [https://github.com/RicoSuter/NJsonSchema/issues/1564] image

RicoSuter commented 2 months ago

unfortunately jsonignore attributes for property has been removed from njsonschema 11.0.0 version.

Moved to NJsonSchema.Annotations

isteyak commented 2 months ago

unfortunately jsonignore attributes for property has been removed from njsonschema 11.0.0 version.

Moved to NJsonSchema.Annotations

I am not talking aboud JsonSchemaIgnoreAttribute. i am talking about System.Text.Json.Serialization.JsonIgnore in 13.20.0 version, when we select jsonlibrary = systemtextjson then for every property of generated code would have jsonignore attribute based on their required or non-required set-up

ffwdq commented 2 months ago

is there any update? i have the same problem where after update to 14.x the attribute isnt generated

isteyak commented 2 months ago

@ffwdq Please refer this https://github.com/RicoSuter/NSwag/issues/4852#issuecomment-2051817426

above one is work around using template, that worked for me