Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
isteyak opened this issue Apr 10, 2024 · 8 comments

Comments

@isteyak
Copy link

isteyak commented Apr 10, 2024

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
Copy link

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
Copy link
Author

isteyak commented Apr 12, 2024

still finding way to fix it

@isteyak
Copy link
Author

isteyak commented Apr 12, 2024

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

@isteyak
Copy link
Author

isteyak commented Apr 12, 2024

@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
Copy link
Owner

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

Moved to NJsonSchema.Annotations

@isteyak
Copy link
Author

isteyak commented Apr 12, 2024

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
Copy link
Contributor

ffwdq commented May 6, 2024

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

@isteyak
Copy link
Author

isteyak commented May 7, 2024

@ffwdq
Please refer this #4852 (comment)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants