Attempt to fix browser-wasm test failures. (#87680)
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Fri, 16 Jun 2023 12:50:21 +0000 (13:50 +0100)
committerGitHub <noreply@github.com>
Fri, 16 Jun 2023 12:50:21 +0000 (13:50 +0100)
* Attempt to fix browser-wasm test failures.

* Address feedback.

src/libraries/System.Text.Json/Common/ReflectionExtensions.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectConverterFactory.cs

index 4a207fc..0fae60f 100644 (file)
@@ -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)]
index 7ca6406..f76fa20 100644 (file)
@@ -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<JsonConstructorAttribute>(typeToConvert);
             }