Table of Contents

NBMsgPack110: Add DefaultValueAttribute when using non-default initializers

This diagnostic is emitted for a serialized field or property that has an initializer but no DefaultValueAttribute. The member type must support C# constant values that can be represented as attribute arguments, such as a primitive, string, or enum. Members of collection and other non-constant types are not reported.

The warning is particularly relevant when SerializeDefaultValues is set to a value other than its default, Always. Under such a policy, the serializer may omit the C# default value for the member type, while deserialization leaves the member at the different value assigned by its initializer. This can prevent values from round-tripping correctly.

Example violation

[GenerateShape]
public partial class MyType
{
    public int Age { get; set; } = 18;
}

Resolution

Apply DefaultValueAttribute with the initialized value so the serializer uses the intended default:

[GenerateShape]
public partial class MyType
{
    [System.ComponentModel.DefaultValue(18)]
    public int Age { get; set; } = 18;
}

The code fix is available when the initializer is a constant expression and adds DefaultValueAttribute using a globally qualified attribute name and that expression, including an explicit cast when needed so the attribute stores the member's exact type.