Skip to content

System.Text.Json.JsonSerializer.Serialize, when JsonIgnoreAttribute and "required" used together, throw exception #78443

@chsword

Description

@chsword

Description

When JsonIgnoreAttribute and C# required used together, throw exception

    public void TestRequired()
    {
        var model = new TestModel
        {
            Str1 = "a",
            Str2 = "b"
        };
        var json = System.Text.Json.JsonSerializer.Serialize(model);
        Console.WriteLine(json);
    }

    public class TestModel
    {
        [System.Text.Json.Serialization.JsonIgnore]
        public required  string Str1 { get; set; }
        [JsonPropertyName("s_str2")]
        public required string Str2 { get; set; }
    }

Configuration

.NET 7
Windows

Regression?

Other information

System.InvalidOperationException: JsonPropertyInfo 'Str1' defined in type 'LingHuo.SDK.Tests.Tencent.TencentFaceIdH5Test+TestModel' is marked required but does not specify a setter.
    at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_JsonPropertyRequiredAndNotDeserializable(JsonPropertyInfo jsonPropertyInfo)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo.Configure()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.InitializePropertyCache()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.Configure()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<EnsureConfigured>g__ConfigureLocked|143_0()
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Boolean resolveIfMutable)
   at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions options, Type inputType)
   at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
   at 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions