Skip to content

[release/6.0] Make delegates unsupported by JsonSerializer #63514

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

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -92,6 +92,7 @@ private sealed class Parser
private readonly Type? _jsonValueType;

// Unsupported types
private readonly Type _delegateType;
private readonly Type _typeType;
private readonly Type _serializationInfoType;
private readonly Type _intPtrType;
Expand Down Expand Up @@ -219,6 +220,7 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
_jsonValueType = _metadataLoadContext.Resolve(JsonValueFullName);

// Unsupported types.
_delegateType = _metadataLoadContext.Resolve(SpecialType.System_Delegate);
_typeType = _metadataLoadContext.Resolve(typeof(Type));
_serializationInfoType = _metadataLoadContext.Resolve(typeof(Runtime.Serialization.SerializationInfo));
_intPtrType = _metadataLoadContext.Resolve(typeof(IntPtr));
Expand Down Expand Up @@ -901,7 +903,8 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
}
}
else if (_knownUnsupportedTypes.Contains(type) ||
ImplementsIAsyncEnumerableInterface(type))
ImplementsIAsyncEnumerableInterface(type) ||
_delegateType.IsAssignableFrom(type))
{
classType = ClassType.KnownUnsupportedType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public override bool CanConvert(Type type)
type == typeof(SerializationInfo) ||
type == typeof(IntPtr) ||
type == typeof(UIntPtr) ||
// Exlude delegates.
typeof(Delegate).IsAssignableFrom(type) ||
// DateOnly/TimeOnly support to be added in future releases;
// guard against invalid object-based serializations for now.
// cf. https://github.com/dotnet/runtime/issues/53539
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2817,5 +2817,37 @@ public class TypeWith_IgnoredPropWith_BadConverter
public class BadConverter
{
}

[Fact]
public async Task TestClassWithIgnoredCallbacks()
{
Assert.Equal("{}", await JsonSerializerWrapperForString.SerializeWrapper(new ClassWithIgnoredCallbacks()));
var obj = await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithIgnoredCallbacks>(@"{""Func"":"""",""Action"":""""}");
Assert.False(obj.Func(""));
Assert.Null(obj.Action);
}

[Fact]
public async Task TestClassWithCallbacks()
{
await Assert.ThrowsAsync<NotSupportedException>(async () => await JsonSerializerWrapperForString.SerializeWrapper(new ClassWithCallbacks()));
await Assert.ThrowsAsync<NotSupportedException>(async () => await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithCallbacks>(@"{""Func"":{},""Action"":{}"));
}

public class ClassWithIgnoredCallbacks
{
[JsonIgnore]
public Func<string, bool> Func { get; set; } = (val) => false;

[JsonIgnore]
public Action<bool> Action { get; set; }
}

public class ClassWithCallbacks
{
public Func<string, bool> Func { get; set; }

public Action<bool> Action { get; set; } = (val) => Console.WriteLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ public override async Task HonorJsonPropertyName_PrivateSetter()
[JsonSerializable(typeof(TypeWith_IgnoredRefStringProp))]
[JsonSerializable(typeof(TypeWith_PropWith_BadConverter))]
[JsonSerializable(typeof(TypeWith_IgnoredPropWith_BadConverter))]
[JsonSerializable(typeof(ClassWithIgnoredCallbacks))]
[JsonSerializable(typeof(ClassWithCallbacks))]
internal sealed partial class PropertyVisibilityTestsContext_Metadata : JsonSerializerContext
{
}
Expand Down Expand Up @@ -439,6 +441,8 @@ public override async Task JsonIgnoreCondition_WhenWritingNull_OnValueType_Fail_
[JsonSerializable(typeof(TypeWith_IgnoredRefStringProp))]
[JsonSerializable(typeof(TypeWith_PropWith_BadConverter))]
[JsonSerializable(typeof(TypeWith_IgnoredPropWith_BadConverter))]
[JsonSerializable(typeof(ClassWithIgnoredCallbacks))]
[JsonSerializable(typeof(ClassWithCallbacks))]
internal sealed partial class PropertyVisibilityTestsContext_Default : JsonSerializerContext
{
}
Expand Down