Skip to content

Commit

Permalink
Attempt to fix browser-wasm test failures. (#87680)
Browse files Browse the repository at this point in the history
* Attempt to fix browser-wasm test failures.

* Address feedback.
  • Loading branch information
eiriktsarpalis committed Jun 16, 2023
1 parent 7bcf80a commit 76da696
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
15 changes: 2 additions & 13 deletions src/libraries/System.Text.Json/Common/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,8 @@ public static bool IsVirtual(this PropertyInfo propertyInfo)
return propertyInfo.GetMethod?.IsVirtual == true || propertyInfo.SetMethod?.IsVirtual == true;
}

public static bool IsKeyValuePair(this Type type, Type? keyValuePairType = null)
{
if (!type.IsGenericType)
{
return false;
}

// Work around not being able to use typeof(KeyValuePair<,>) directly during compile-time src gen type analysis.
keyValuePairType ??= typeof(KeyValuePair<,>);

Type generic = type.GetGenericTypeDefinition();
return generic == keyValuePairType;
}
public static bool IsKeyValuePair(this Type type)
=> type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);

public static bool TryGetDeserializationConstructor(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
Expand Down Expand Up @@ -39,11 +40,18 @@ public override bool CanConvert(Type typeToConvert)
Justification = "The ctor is marked RequiresUnreferencedCode.")]
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
ConstructorInfo? constructor;
JsonConverter converter;
Type converterType;

bool useDefaultCtorInUnannotatedStructs = _useDefaultConstructorInUnannotatedStructs && !typeToConvert.IsKeyValuePair();
if (!typeToConvert.TryGetDeserializationConstructor(useDefaultCtorInUnannotatedStructs, out ConstructorInfo? constructor))
if (typeToConvert.IsKeyValuePair())
{
// browser-wasm compat -- ensure the linker doesn't trim away constructor parameter names from KVP.
Type[] genericArguments = typeToConvert.GetGenericArguments();
Type keyValuePairType = typeof(KeyValuePair<,>).MakeGenericType(genericArguments);
constructor = keyValuePairType.GetConstructor(genericArguments);
}
else if (!typeToConvert.TryGetDeserializationConstructor(_useDefaultConstructorInUnannotatedStructs, out constructor))
{
ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute<JsonConstructorAttribute>(typeToConvert);
}
Expand Down

0 comments on commit 76da696

Please sign in to comment.