Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ensure reflection property metadata is instantiated lazily.
  • Loading branch information
eiriktsarpalis committed Oct 10, 2022
commit 9dc70c823cd954b466921d45aadb039da10e29a0
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions o
PopulatePolymorphismMetadata();
MapInterfaceTypesToCallbacks();

if (converter.ConverterStrategy == ConverterStrategy.Object)
{
AddPropertiesAndParametersUsingReflection();
}

Func<object>? createObject = JsonSerializerOptions.MemberAccessorStrategy.CreateConstructor(typeof(T));
SetCreateObjectIfCompatible(createObject);
CreateObjectForExtensionDataProperty = createObject;
Expand All @@ -37,11 +32,17 @@ internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions o
converter.ConfigureJsonTypeInfoUsingReflection(this, options);
}

[RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)]
private void AddPropertiesAndParametersUsingReflection()
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:RequiresUnreferencedCode",
Justification = "The ctor is marked RequiresUnreferencedCode.")]
internal override void LateAddProperties()
{
Debug.Assert(Converter.ConverterStrategy == ConverterStrategy.Object);
Debug.Assert(!IsConfigured);
Debug.Assert(PropertyCache is null);

if (Kind != JsonTypeInfoKind.Object)
{
return;
}

const BindingFlags BindingFlags =
BindingFlags.Instance |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1700,5 +1700,69 @@ public string? Value
set => _value = value ?? "NULL";
}
}

[Fact]
public static void TypeWithIgnoredUnsupportedType_ShouldBeSupported()
{
// Regression test for https://github.com/dotnet/runtime/issues/76807

// Sanity check -- metadata resolution for the unsupported type is failing
Assert.Throws<InvalidOperationException>(() => JsonSerializerOptions.Default.GetTypeInfo(typeof(UnsupportedType)));

// Serialization works as expected
string json = JsonSerializer.Serialize(new PocoWithIgnoredUnsupportedType());
JsonTestHelper.AssertJsonEqual("{}", json);

// Metadata is reported as expected
JsonTypeInfo jti = JsonSerializerOptions.Default.GetTypeInfo(typeof(PocoWithIgnoredUnsupportedType));
Assert.Equal(1, jti.Properties.Count);
JsonPropertyInfo propertyInfo = jti.Properties[0];
Assert.Null(propertyInfo.Get);
Assert.Null(propertyInfo.Set);
}

public class PocoWithIgnoredUnsupportedType
{
[JsonIgnore]
public UnsupportedType UnsuportedProperty { get; set; }
}

[Fact]
public static void TypeWithUnIgnoredUnsupportedType_CanModifyUnsupportedType()
{
var options = new JsonSerializerOptions
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers =
{
static jti =>
{
if (jti.Type == typeof(PocoWithUnIgnoredUnsupportedType))
{
Assert.Equal(1, jti.Properties.Count);
JsonPropertyInfo propertyInfo = jti.Properties[0];
Assert.Equal(typeof(UnsupportedType), propertyInfo.PropertyType);

jti.Properties.Clear();
}
}
}
}
};

string json = JsonSerializer.Serialize(new PocoWithUnIgnoredUnsupportedType(), options);
JsonTestHelper.AssertJsonEqual("{}", json);
}

public class PocoWithUnIgnoredUnsupportedType
{
public UnsupportedType UnsuportedProperty { get; set; }
}

public class UnsupportedType
{
public ReadOnlySpan<byte> Span => Array.Empty<byte>();
}
}
}