Address feedback from #87680. (#87695)
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Fri, 16 Jun 2023 22:30:41 +0000 (23:30 +0100)
committerGitHub <noreply@github.com>
Fri, 16 Jun 2023 22:30:41 +0000 (23:30 +0100)
* Address feedback from #87680.

* Update src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonSerializerWrapper.Reflection.cs

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
---------

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectConverterFactory.cs
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonSerializerWrapper.Reflection.cs

index f76fa20..e0b04f3 100644 (file)
@@ -40,18 +40,11 @@ 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;
 
-            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))
+            bool useDefaultConstructorInUnannotatedStructs = _useDefaultConstructorInUnannotatedStructs && !typeToConvert.IsKeyValuePair();
+            if (!typeToConvert.TryGetDeserializationConstructor(useDefaultConstructorInUnannotatedStructs, out ConstructorInfo? constructor))
             {
                 ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute<JsonConstructorAttribute>(typeToConvert);
             }
index 6a59bf1..d353197 100644 (file)
@@ -1,7 +1,9 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Collections.Generic;
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using System.Runtime.CompilerServices;
 using System.Text.Json.Nodes;
@@ -13,6 +15,13 @@ namespace System.Text.Json.Serialization.Tests
 {
     public abstract partial class JsonSerializerWrapper
     {
+        // Ensure that the reflection-based serializer testing abstraction roots KeyValuePair<,>
+        // which is required by many tests in the reflection test suite.
+        [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties, typeof(KeyValuePair<,>))]
+        protected JsonSerializerWrapper()
+        {
+        }
+
         public static JsonSerializerWrapper SpanSerializer { get; } = new SpanSerializerWrapper();
         public static JsonSerializerWrapper StringSerializer { get; } = new StringSerializerWrapper();
         public static StreamingJsonSerializerWrapper AsyncStreamSerializer { get; } = new AsyncStreamSerializerWrapper();