From a46ab0c0d9a37cce0d45cfb27e6d455cb1ede3c2 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Mon, 6 Jun 2022 20:41:47 +0200 Subject: [PATCH 1/2] Fix simple scenarios for combining contexts --- .../System.Text.Json/gen/JsonSourceGenerator.Emitter.cs | 6 +++--- .../System/Text/Json/Serialization/JsonSerializerContext.cs | 6 ++---- .../Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs | 4 ++-- .../JsonSerializerContextTests.cs | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index 209c255a5d146..d0f8b2e121df8 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -706,10 +706,10 @@ private string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe sb.Append($@" -private static {JsonPropertyInfoTypeRef}[] {propInitMethodName}({JsonSerializerContextTypeRef} context) +private {JsonPropertyInfoTypeRef}[] {propInitMethodName}({JsonSerializerContextTypeRef}? context) {{ - {contextTypeRef} {JsonContextVarName} = ({contextTypeRef})context; - {JsonSerializerOptionsTypeRef} options = context.Options; + {contextTypeRef} {JsonContextVarName} = ({contextTypeRef}?)context ?? this; + {JsonSerializerOptionsTypeRef} options = {JsonContextVarName}.Options; {JsonPropertyInfoTypeRef}[] {PropVarName} = {propertyArrayInstantiationValue}; "); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs index cc1da1f048f9b..c6e4553b5e2bd 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs @@ -102,10 +102,8 @@ protected JsonSerializerContext(JsonSerializerOptions? options, bool bindOptions { options.TypeInfoResolver = this; } - else - { - _options = options; - } + + _options = options; } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs index 3e39ac31fd598..f86c091c00f48 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/SourceGenJsonTypeInfoOfT.cs @@ -102,7 +102,7 @@ internal override JsonParameterInfoValues[] GetParameterInfoValues() { JsonSerializerContext? context = Options.SerializerContext; JsonParameterInfoValues[] array; - if (context == null || CtorParamInitFunc == null || (array = CtorParamInitFunc()) == null) + if (CtorParamInitFunc == null || (array = CtorParamInitFunc()) == null) { ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeCtorParams(context, Type); return null!; @@ -120,7 +120,7 @@ internal void AddPropertiesUsingSourceGenInfo() JsonSerializerContext? context = Options.SerializerContext; JsonPropertyInfo[] array; - if (context == null || PropInitFunc == null || (array = PropInitFunc(context)) == null) + if (PropInitFunc == null || (array = PropInitFunc(context!)) == null) { if (typeof(T) == typeof(object)) { diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs index da18a517a8493..a47eb0e91ea79 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs @@ -106,7 +106,7 @@ public static void CombiningContexts_Serialization(T value, string expectedJs public static IEnumerable GetCombiningContextsData() { - yield return WrapArgs(new JsonMessage { Message = "Hi" }, """{ "Message" : { "Hi" } }"""); + yield return WrapArgs(new JsonMessage { Message = "Hi" }, """{ "Message" : "Hi", "Length" : 2 }"""); yield return WrapArgs(new Person("John", "Doe"), """{ "FirstName" : "John", "LastName" : "Doe" }"""); static object[] WrapArgs(T value, string expectedJson) => new object[] { value, expectedJson }; } From cc661caea4edb92edfbf981e034d7422fb5eafb8 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 7 Jun 2022 11:18:14 +0200 Subject: [PATCH 2/2] feedback --- .../Text/Json/Serialization/JsonSerializerContext.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs index c6e4553b5e2bd..6d87444a463c2 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerContext.cs @@ -101,9 +101,12 @@ protected JsonSerializerContext(JsonSerializerOptions? options, bool bindOptions if (bindOptionsToContext) { options.TypeInfoResolver = this; + Debug.Assert(_options == options, "options.TypeInfoResolver setter did not assign options"); + } + else + { + _options = options; } - - _options = options; } }