Open isteyak opened 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?
still finding way to fix it
unfortunately jsonignore attributes for property has been removed from njsonschema 11.0.0 version.
@franklixuefei I just came up with one of approach, just have a look. Its working for me
{%- 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]
unfortunately jsonignore attributes for property has been removed from njsonschema 11.0.0 version.
Moved to NJsonSchema.Annotations
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
is there any update? i have the same problem where after update to 14.x the attribute isnt generated
@ffwdq Please refer this https://github.com/RicoSuter/NSwag/issues/4852#issuecomment-2051817426
above one is work around using template, that worked for me
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?