Skip to content

[System.Text.Json] Regression: [JsonIgnore] Attribute not Applied to Ref Returns #59936

Closed

Description

@Sewer56

Description

Found a regression in System.Text.Json where using properties with ref returns cannot be ignored for serialization, leading to an unavoidable exception.

Regression was introduced in System.Text.Json 6.0.0-rc.1.21406.5, going by the available packages in the dotnet6 NuGet repo. This package was pushed on Friday, August 6th, 2021.

As I'm not familiar with how dotnet is exactly built, I do not know how to pinpoint the sdk build number to the exact commit causing the issue however; but the build version should hopefully be useful.

Minimal Reproduction:

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

var sampleText = new TypeWithRefString();
var json = JsonSerializer.Serialize(sampleText); // Exception Here
var deserialized = JsonSerializer.Deserialize<TypeWithRefString>(json); // And Here

internal class TypeWithRefString
{
    [JsonIgnore]
    public ref string NameRef => ref Name;
    public string Name = "How many Richard Landers does it take to write a blog entry? Just one, you rock!";
}

Debug in your own desired way.
This snippet can be pasted and ran over a blank .NET 6 console app.

Configuration

N/A

Regression?

Yes, this use case worked with any version of System.Text.Json <= 6.0.0-rc.1.21406.4;

Other information

Exception:

System.ArgumentException: 'The type 'System.String&' may not be used as a type argument.'
   at System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(RuntimeType type)
   at System.RuntimeType.SanityCheckGenericArguments(RuntimeType[] genericArguments, RuntimeType[] genericParamters)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at System.Text.Json.Serialization.Converters.ObjectConverterFactory.CreateConverter(Type typeToConvert, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(Type typeToConvert, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetConverterInternal(Type typeToConvert)
   at System.Text.Json.JsonSerializerOptions.DetermineConverter(Type parentClassType, Type runtimePropertyType, MemberInfo memberInfo)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.GetConverter(Type type, Type parentClassType, MemberInfo memberInfo, Type& runtimeType, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.AddProperty(MemberInfo memberInfo, Type memberType, Type parentClassType, Boolean isVirtual, Nullable`1 parentTypeNumberHandling, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.CacheMember(Type declaringType, Type memberType, MemberInfo memberInfo, Boolean isVirtual, Nullable`1 typeNumberHandling, Boolean& propertyOrderSpecified, Dictionary`2& ignoredMembers)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonConverter converter, Type runtimeType, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.<RootBuiltInConvertersAndTypeInfoCreator>g__CreateJsonTypeInfo|107_0(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetClassFromContextOrCreate(Type type)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClassForRootType(Type type)
   at System.Text.Json.JsonSerializer.GetTypeInfo(Type runtimeType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Write[TValue](TValue& value, Type runtimeType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
   at Program.<Main>$(String[] args) in C:\Users\sewer\Desktop\Projects\SystemTextJsonBug\SystemTextJsonBug\SystemTextJsonBug\Program.cs:line 12

Activity

  1. ghost added
    untriaged
    on Oct 4, 2021
  2. eiriktsarpalis commented on Oct 4, 2021

    @eiriktsarpalis
    Member

    Can repro -- I believe this is the same regression as the one reported in #58469 (comment).

    We made the call earlier not to revert the change, but it seems to me that this particular issue makes a more compelling case /cc @layomia

    Related to #59364.

  3. added this to the 6.0.0 milestone on Oct 4, 2021
  4. added and removed
    untriaged
    on Oct 4, 2021
  5. richlander commented on Oct 4, 2021

    @richlander
    Member

    Ha! I'll be writing the RC2 post this week.

  6. ghost added
    in-pr
    on Oct 5, 2021
  7. ghost removed
    in-pr
    on Oct 12, 2021
  8. layomia commented on Oct 12, 2021

    @layomia
    Contributor

    Re-opening so we can consider porting the fix to .NET 6.0.

  9. eiriktsarpalis commented on Oct 12, 2021

    @eiriktsarpalis
    Member

    Fixed by #60299

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions