Skip to content

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

@Sewer56

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions