Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure reflection property metadata is instantiated lazily #76828

Merged
merged 2 commits into from
Oct 11, 2022
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
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()
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.Assert(Converter.ConverterStrategy == ConverterStrategy.Object);
Debug.Assert(!IsConfigured);
Debug.Assert(PropertyCache is null);

if (Kind != JsonTypeInfoKind.Object)
{
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
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>();
}
}
}