Skip to content

System.Text.Json is adding EqualityContract properties metadata for records #77675

@Sergio0694

Description

@Sergio0694

Description

Possible regression from #58136, cc. @eiriktsarpalis.

"Records contains a property "EqualityContract" of type Type. This leads to the generator generating serializers for many of the reflection related types as for example Type, ConstructorInfo, MethodInfo, Assembly, Module, etc."

Reproduction Steps

Paste the following code:

public sealed record Test;

[JsonSerializable(typeof(Test))]
public sealed partial class MyTestContext : JsonSerializerContext
{
}

Expected behavior

The generated code for Test should not include the "EqualityContract" property.

Actual behavior

// <auto-generated/>

#nullable enable annotations
#nullable disable warnings

// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618

namespace MyTestProject
{
    public sealed partial class MyTestContext
    {
        private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::MyTestProject.Test>? _Test;
        /// <summary>
        /// Defines the source generated JSON serialization contract metadata for a given type.
        /// </summary>
        public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::MyTestProject.Test> Test
        {
            get => _Test ??= Create_Test(Options);
        }
        
        // Intentionally not a static method because we create a delegate to it. Invoking delegates to instance
        // methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk.
        private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::MyTestProject.Test> Create_Test(global::System.Text.Json.JsonSerializerOptions options)
        {
            global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::MyTestProject.Test>? jsonTypeInfo = null;
            global::System.Text.Json.Serialization.JsonConverter? customConverter;
            if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::MyTestProject.Test))) != null)
            {
                jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::MyTestProject.Test>(options, customConverter);
            }
            else
            {
                global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<global::MyTestProject.Test> objectInfo = new global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<global::MyTestProject.Test>()
                {
                    ObjectCreator = static () => new global::MyTestProject.Test(),
                    ObjectWithParameterizedConstructorCreator = null,
                    PropertyMetadataInitializer = _ => TestPropInit(options),
                    ConstructorParameterMetadataInitializer = null,
                    NumberHandling = default,
                    SerializeHandler = TestSerializeHandler
                };
        
                jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo<global::MyTestProject.Test>(options, objectInfo);
            }
        
            return jsonTypeInfo;
        }
        
        private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] TestPropInit(global::System.Text.Json.JsonSerializerOptions options)
        {
            global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[1];
        
            global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Type> info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Type>()
            {
                IsProperty = true,
                IsPublic = false,
                IsVirtual = false,
                DeclaringType = typeof(global::MyTestProject.Test),
                Converter = null,
                Getter = null,
                Setter = null,
                IgnoreCondition = null,
                HasJsonInclude = false,
                IsExtensionData = false,
                NumberHandling = default,
                PropertyName = "EqualityContract",
                JsonPropertyName = null
            };
        
            global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo0 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.Type>(options, info0);
            properties[0] = propertyInfo0;
        
            return properties;
        }
        
        private void TestSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::MyTestProject.Test? value)
        {
            if (value == null)
            {
                writer.WriteNullValue();
                return;
            }
        
            writer.WriteStartObject();
        
            writer.WriteEndObject();
        }
    }
}

Regression?

Seems like it might be the case, yes. I'm using 7.0.0-rc.2.22472.3.

Known Workarounds

None.

Configuration

.NET Standard 2.0 class library, using System.Text.Json 7.0.0-rc.2.22472.3.

Metadata

Metadata

Labels

area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionspartner-impactThis issue impacts a partner who needs to be kept updated

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions