Skip to content

STJ Source Generator bloats the assembly when used with records #58136

Closed

Description

@aromaa

Description

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.

Also it would be nice if there was a way to prevent generating metadata for non-public members.

Repo

internal static class Program
{
	private static void Main()
	{
		JsonSerializer.Serialize(new Test(), typeof(Test), JsonContext.Default);
	}
}

internal sealed record Test;

[JsonSerializable(typeof(Test))]
internal sealed partial class JsonContext : JsonSerializerContext
{
}

Activity

  1. ghost added
    untriaged
    on Aug 25, 2021
  2. eiriktsarpalis commented on Aug 26, 2021

    @eiriktsarpalis
    Member

    Hi @aromaa, what version of the sdk are you using to reproduce this. Using your reproduction on 6.0.100-rc.1.21415.3 I'm getting the following generated code:

    // <auto-generated/>
    #nullable enable
    
    namespace Repro
    {
    
        public static partial class Program
        {
    
            internal sealed partial class JsonContext
            {
                private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Repro.Program.MyRecord>? _MyRecord;
                public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Repro.Program.MyRecord> MyRecord
                {
                    get
                    {
                        if (_MyRecord == null)
                        {
                            global::System.Text.Json.Serialization.JsonConverter? customConverter;
                                if (Options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(typeof(global::Repro.Program.MyRecord))) != null)
                                {
                                    _MyRecord = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::Repro.Program.MyRecord>(Options, customConverter);
                                }
                                else
                                {
                                    global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<global::Repro.Program.MyRecord> objectInfo = new global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<global::Repro.Program.MyRecord>()
                                    {
                                        ObjectCreator = static () => new global::Repro.Program.MyRecord(),
                                        ObjectWithParameterizedConstructorCreator = null,
                                        PropertyMetadataInitializer = MyRecordPropInit,
                                        ConstructorParameterMetadataInitializer = null,
                                        NumberHandling = default,
                                        SerializeHandler = MyRecordSerialize
                                    };
                    
                                    _MyRecord = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo<global::Repro.Program.MyRecord>(Options, objectInfo);
                                }
                        }
                
                        return _MyRecord;
                    }
                }
                
                private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] MyRecordPropInit(global::System.Text.Json.Serialization.JsonSerializerContext context)
                {
                    global::Repro.Program.JsonContext jsonContext = (global::Repro.Program.JsonContext)context;
                    global::System.Text.Json.JsonSerializerOptions options = context.Options;
                
                    global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] properties = global::System.Array.Empty<global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo>();
                
                    return properties;
                }
                
                private static void MyRecordSerialize(global::System.Text.Json.Utf8JsonWriter writer, global::Repro.Program.MyRecord value)
                {
                    if (value == null)
                    {
                        writer.WriteNullValue();
                        return;
                    }
                
                    writer.WriteStartObject();
                
                    writer.WriteEndObject();
                }
            }
    }
    }

    which on quick inspection doesn't seem to include any properties.

  3. aromaa commented on Aug 26, 2021

    @aromaa
    ContributorAuthor

    This seems to be fixed on RC builds. I was using 6.0.0-preview.7.21377.19. Doesn't repro with 6.0.0-rc.2.21425.6.

  4. ghost locked as resolved and limited conversation to collaborators on Sep 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions