Ensure that the generated JsonSerializerContext.Default static property returns a...
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Thu, 25 May 2023 14:38:16 +0000 (15:38 +0100)
committerGitHub <noreply@github.com>
Thu, 25 May 2023 14:38:16 +0000 (15:38 +0100)
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs

index e4e8e5a..e329d01 100644 (file)
@@ -22,7 +22,6 @@ namespace System.Text.Json.SourceGeneration
             private const string CreateValueInfoMethodName = "CreateValueInfo";
             private const string CtorParamInitMethodNameSuffix = "CtorParamInit";
             private const string DefaultOptionsStaticVarName = "s_defaultOptions";
-            private const string DefaultContextBackingStaticVarName = "s_defaultContext";
             private const string OriginatingResolverPropertyName = "OriginatingResolver";
             private const string InfoVarName = "info";
             private const string NumberHandlingPropName = "NumberHandling";
@@ -1026,12 +1025,10 @@ namespace System.Text.Json.SourceGeneration
                 writer.WriteLine();
 
                 writer.WriteLine($$"""
-                    private static {{contextTypeRef}}? {{DefaultContextBackingStaticVarName}};
-
                     /// <summary>
                     /// The default <see cref="{{JsonSerializerContextTypeRef}}"/> associated with a default <see cref="{{JsonSerializerOptionsTypeRef}}"/> instance.
                     /// </summary>
-                    public static {{contextTypeRef}} Default => {{DefaultContextBackingStaticVarName}} ??= new {{contextTypeRef}}(new {{JsonSerializerOptionsTypeRef}}({{DefaultOptionsStaticVarName}}));
+                    public static {{contextTypeRef}} Default { get; } = new {{contextTypeRef}}(new {{JsonSerializerOptionsTypeRef}}({{DefaultOptionsStaticVarName}}));
 
                     /// <summary>
                     /// The source-generated options associated with this context.
index 2af24e5..a61ba2b 100644 (file)
@@ -171,6 +171,22 @@ namespace System.Text.Json.SourceGeneration.Tests
                 }).Dispose();
         }
 
+        [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
+        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+        public static void JsonSerializerContext_GeneratedDefault_IsSingleton()
+        {
+            RemoteExecutor.Invoke(
+                static () =>
+                {
+                    const int Count = 30;
+                    var contexts = new MetadataContext[Count];
+                    Parallel.For(0, Count, i => contexts[i] = MetadataContext.Default);
+
+                    Assert.All(contexts, ctx => Assert.Same(MetadataContext.Default, ctx));
+
+                }).Dispose();
+        }
+
         [Fact]
         public static void SupportsReservedLanguageKeywordsAsProperties()
         {