From: Eirik Tsarpalis Date: Fri, 16 Jun 2023 12:50:21 +0000 (+0100) Subject: Attempt to fix browser-wasm test failures. (#87680) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~1587 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=76da696f3ffdd81506b09dfc440ee6f4e1001868;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Attempt to fix browser-wasm test failures. (#87680) * Attempt to fix browser-wasm test failures. * Address feedback. --- diff --git a/src/libraries/System.Text.Json/Common/ReflectionExtensions.cs b/src/libraries/System.Text.Json/Common/ReflectionExtensions.cs index 4a207fc..0fae60f 100644 --- a/src/libraries/System.Text.Json/Common/ReflectionExtensions.cs +++ b/src/libraries/System.Text.Json/Common/ReflectionExtensions.cs @@ -221,19 +221,8 @@ namespace System.Text.Json.Reflection 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)] diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectConverterFactory.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectConverterFactory.cs index 7ca6406..f76fa20 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectConverterFactory.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectConverterFactory.cs @@ -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; @@ -39,11 +40,18 @@ namespace System.Text.Json.Serialization.Converters 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(typeToConvert); }