Registry Mirror
Skip to content

Inconsistent internal field handling between JsonSourceGenerationModes #70928

Closed

Description

@nxtn

Description

JsonSourceGenerationMode.Metadata and JsonSourceGenerationMode.Serialization handle internal fields differently in serialization.

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Serialization;

internal sealed class Root
{
    internal string key = "value";
}

[JsonSourceGenerationOptions(IncludeFields = true, GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(Root))]
internal sealed partial class MetadataContext : JsonSerializerContext
{
}

[JsonSourceGenerationOptions(IncludeFields = true, GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(Root))]
internal sealed partial class SerializationContext : JsonSerializerContext
{
}

internal static class Program
{
    private static JsonSerializerOptions Options => new()
    {
        IncludeFields = true,
    };

    private static void Main()
    {
        var root = new Root();

        // {}
        System.Console.WriteLine(JsonSerializer.Serialize(root, Options));

        // {}
        System.Console.WriteLine(JsonSerializer.Serialize(root, new MetadataContext(Options).Root));

        // {"key":"value"}
        System.Console.WriteLine(JsonSerializer.Serialize(root, new SerializationContext(Options).Root));
    }
}

Expected behavior

The behaviors should be consistent between modes.

Actual behavior

JsonSourceGenerationMode.Metadata doesn't include internal fields.
JsonSourceGenerationMode.Serialization includes internal fields.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6 and .NET 7 Preview 5

Other information

No response

Activity

  1. ghost added
    untriaged
    on Jun 18, 2022
  2. added
    enhancement
    and removed
    untriaged
    on Jul 19, 2022
  3. added this to the 8.0.0 milestone on Jul 19, 2022
  4. added
    source-generator
    and removed
    enhancement
    on Jul 19, 2022
  5. eiriktsarpalis commented on Jul 19, 2022

    @eiriktsarpalis
    Member

    It looks like the metadata source generator does not include fields when IncludeFields is set to true.

  6. modified the milestones: 8.0.0, Future on Sep 28, 2022
  7. modified the milestones: Future, 8.0.0 on Jun 7, 2023
  8. eiriktsarpalis commented on Jun 15, 2023

    @eiriktsarpalis
    Member

    This was addressed by #87136. The reproducing code will now consistently produce {} for all configurations.

  9. ghost locked as resolved and limited conversation to collaborators on Jul 16, 2023
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.Jsonbugsource-generator

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions