NSwag: The ignore condition 'JsonIgnoreCondition.WhenWritingNull' is not valid on value-type member

Hello,

Since version 13.16.0 of Nswag, the following attribute is added to all properties of the generated models when generating the C# clients. [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]

This causes the following errors on value type properties. The ignore condition 'JsonIgnoreCondition.WhenWritingNull' is not valid on value-type member 'LanguageId' on type 'User'. Consider using 'JsonIgnoreCondition.WhenWritingDefault'.

Is there a way to disable the generation of this attribute?

Kind regards, Jorsi

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 33
  • Comments: 23 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Any update on this? The latest package version has had a show stopping issue for almost 3 months now.

we are also experiencing this issue, however rolling back to 13.15.10 has resolved it for now.

How can i opt-out of this bevaviour or force my values?

Before these changes there was no attribute what so ever (so the default behaviour of STJ i assume). Then it was briefly System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull. And now its different again. This is kinda breaking…

This breaks alot of my connectors, assumptions and tests. Often a called backend service is not that easily to change.

From my current understanding of the code base its now either “WhenWritingDefault” or “Never” depending of the property if “required” or not. And nothing else?

The broken stuff is also subtle. Took me some time to find out the an INT 0 was not send because the whole property was disregarded because it was not required.

It looks like rolling back to 13.15.10 resolves this issue for the time being.

Same here.

@RicoSuter - Thanks for the fix!! I just hit this issue with NSwag.MSBuild. Could we get an updated version of that package published to Nuget?

I used this code to circumvent the problem at runtime.

internal partial class UserClient // NSwag generated client
{
    partial void UpdateJsonSerializerSettings(System.Text.Json.JsonSerializerOptions settings)
    {
        settings.TypeInfoResolver = new DefaultJsonTypeInfoResolver
        {
            Modifiers = { JsonHelper.AlwaysSerializeValueTypeProperties }
        };
    }
}

file sealed class JsonHelper
{
    private static readonly Func<object, object?, bool> AlwaysSerialize = (_, __) => true;

    /// <summary>
    /// NSwag generates JsonIgnoreCondition.WhenWritingDefault for value type properties (enum, int, bool, etc.) which is wrong.
    /// </summary>
    public static void AlwaysSerializeValueTypeProperties(JsonTypeInfo ti)
    {
        // https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/custom-contracts

        foreach (var prop in ti.Properties)
        {
            if (prop.PropertyType.IsValueType && prop.AttributeProvider is not null)
            {
                prop.ShouldSerialize = AlwaysSerialize;
            }
        }
    }
}

Same with v13.16.1. Following solution resolved the issue :

It looks like rolling back to 13.15.10 resolves this issue for the time being.