Skip to content

Commit 56c11f2

Browse files
committed
Merge in 'release/6.0' changes
2 parents 27eb94b + 7780d28 commit 56c11f2

File tree

9 files changed

+90
-50
lines changed

9 files changed

+90
-50
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ private sealed partial class Emitter
9898

9999
private readonly HashSet<string> _emittedPropertyFileNames = new();
100100

101+
private bool _generateGetConverterMethodForTypes = false;
102+
private bool _generateGetConverterMethodForProperties = false;
103+
101104
public Emitter(in JsonSourceGenerationContext sourceGenerationContext, SourceGenerationSpec generationSpec)
102105
{
103106
_sourceGenerationContext = sourceGenerationContext;
@@ -109,24 +112,20 @@ public void Emit()
109112
foreach (ContextGenerationSpec contextGenerationSpec in _generationSpec.ContextGenerationSpecList)
110113
{
111114
_currentContext = contextGenerationSpec;
112-
113-
bool generateGetConverterMethodForTypes = false;
114-
bool generateGetConverterMethodForProperties = false;
115+
_generateGetConverterMethodForTypes = false;
116+
_generateGetConverterMethodForProperties = false;
115117

116118
foreach (TypeGenerationSpec typeGenerationSpec in _currentContext.RootSerializableTypes)
117119
{
118120
GenerateTypeInfo(typeGenerationSpec);
119-
120-
generateGetConverterMethodForTypes |= typeGenerationSpec.HasTypeFactoryConverter;
121-
generateGetConverterMethodForProperties |= typeGenerationSpec.HasPropertyFactoryConverters;
122121
}
123122

124123
string contextName = _currentContext.ContextType.Name;
125124

126125
// Add root context implementation.
127126
AddSource(
128127
$"{contextName}.g.cs",
129-
GetRootJsonContextImplementation(generateGetConverterMethodForTypes, generateGetConverterMethodForProperties),
128+
GetRootJsonContextImplementation(),
130129
isRootContextDef: true);
131130

132131
// Add GetJsonTypeInfo override implementation.
@@ -302,6 +301,9 @@ private void GenerateTypeInfo(TypeGenerationSpec typeGenerationSpec)
302301
Location location = typeGenerationSpec.AttributeLocation ?? _currentContext.Location;
303302
_sourceGenerationContext.ReportDiagnostic(Diagnostic.Create(DuplicateTypeName, location, new string[] { typeGenerationSpec.TypeInfoPropertyName }));
304303
}
304+
305+
_generateGetConverterMethodForTypes |= typeGenerationSpec.HasTypeFactoryConverter;
306+
_generateGetConverterMethodForProperties |= typeGenerationSpec.HasPropertyFactoryConverters;
305307
}
306308

307309
private string GenerateForTypeWithKnownConverter(TypeGenerationSpec typeMetadata)
@@ -1145,9 +1147,7 @@ private string WrapWithCheckForCustomConverter(string source, string typeCompila
11451147
{IndentSource(source, numIndentations: 1)}
11461148
}}";
11471149

1148-
private string GetRootJsonContextImplementation(
1149-
bool generateGetConverterMethodForTypes,
1150-
bool generateGetConverterMethodForProperties)
1150+
private string GetRootJsonContextImplementation()
11511151
{
11521152
string contextTypeRef = _currentContext.ContextTypeRef;
11531153
string contextTypeName = _currentContext.ContextType.Name;
@@ -1171,12 +1171,12 @@ private string GetRootJsonContextImplementation(
11711171
11721172
{GetFetchLogicForRuntimeSpecifiedCustomConverter()}");
11731173

1174-
if (generateGetConverterMethodForProperties)
1174+
if (_generateGetConverterMethodForProperties)
11751175
{
11761176
sb.Append(GetFetchLogicForGetCustomConverter_PropertiesWithFactories());
11771177
}
11781178

1179-
if (generateGetConverterMethodForProperties || generateGetConverterMethodForTypes)
1179+
if (_generateGetConverterMethodForProperties || _generateGetConverterMethodForTypes)
11801180
{
11811181
sb.Append(GetFetchLogicForGetCustomConverter_TypesWithFactories());
11821182
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public interface ITestContext
4242
public JsonTypeInfo<StructWithCustomConverterFactory> StructWithCustomConverterFactory { get; }
4343
public JsonTypeInfo<ClassWithCustomConverterProperty> ClassWithCustomConverterProperty { get; }
4444
public JsonTypeInfo<StructWithCustomConverterProperty> StructWithCustomConverterProperty { get; }
45-
public JsonTypeInfo<ClassWithCustomConverterPropertyFactory> ClassWithCustomConverterPropertyFactory { get; }
46-
public JsonTypeInfo<StructWithCustomConverterPropertyFactory> StructWithCustomConverterPropertyFactory { get; }
45+
public JsonTypeInfo<ClassWithCustomConverterFactoryProperty> ClassWithCustomConverterFactoryProperty { get; }
46+
public JsonTypeInfo<StructWithCustomConverterFactoryProperty> StructWithCustomConverterFactoryProperty { get; }
4747
public JsonTypeInfo<ClassWithBadCustomConverter> ClassWithBadCustomConverter { get; }
4848
public JsonTypeInfo<StructWithBadCustomConverter> StructWithBadCustomConverter { get; }
4949
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.Reflection;
56
using System.Text.Json.Serialization;
67
using Microsoft.DotNet.RemoteExecutor;
@@ -89,5 +90,44 @@ internal record Person(string FirstName, string LastName);
8990
internal partial class PersonJsonContext : JsonSerializerContext
9091
{
9192
}
93+
94+
// Regression test for https://github.com/dotnet/runtime/issues/62079
95+
[Fact]
96+
public static void SupportsPropertiesWithCustomConverterFactory()
97+
{
98+
var value = new ClassWithCustomConverterFactoryProperty { MyEnum = Serialization.Tests.SampleEnum.MinZero };
99+
string json = JsonSerializer.Serialize(value, SingleClassWithCustomConverterFactoryPropertyContext.Default.ClassWithCustomConverterFactoryProperty);
100+
Assert.Equal(@"{""MyEnum"":""MinZero""}", json);
101+
}
102+
103+
public class ParentClass
104+
{
105+
public ClassWithCustomConverterFactoryProperty? Child { get; set; }
106+
}
107+
108+
[JsonSerializable(typeof(ParentClass))]
109+
internal partial class SingleClassWithCustomConverterFactoryPropertyContext : JsonSerializerContext
110+
{
111+
}
112+
113+
// Regression test for https://github.com/dotnet/runtime/issues/61860
114+
[Fact]
115+
public static void SupportsGenericParameterWithCustomConverterFactory()
116+
{
117+
var value = new List<TestEnum> { TestEnum.Cee };
118+
string json = JsonSerializer.Serialize(value, GenericParameterWithCustomConverterFactoryContext.Default.ListTestEnum);
119+
Assert.Equal(@"[""Cee""]", json);
120+
}
121+
122+
[JsonConverter(typeof(JsonStringEnumConverter))]
123+
public enum TestEnum
124+
{
125+
Aye, Bee, Cee
126+
}
127+
128+
[JsonSerializable(typeof(List<TestEnum>))]
129+
internal partial class GenericParameterWithCustomConverterFactoryContext : JsonSerializerContext
130+
{
131+
}
92132
}
93133
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace System.Text.Json.SourceGeneration.Tests
3636
[JsonSerializable(typeof(StructWithCustomConverterFactory))]
3737
[JsonSerializable(typeof(ClassWithCustomConverterProperty))]
3838
[JsonSerializable(typeof(StructWithCustomConverterProperty))]
39-
[JsonSerializable(typeof(ClassWithCustomConverterPropertyFactory))]
40-
[JsonSerializable(typeof(StructWithCustomConverterPropertyFactory))]
39+
[JsonSerializable(typeof(ClassWithCustomConverterFactoryProperty))]
40+
[JsonSerializable(typeof(StructWithCustomConverterFactoryProperty))]
4141
[JsonSerializable(typeof(ClassWithBadCustomConverter))]
4242
[JsonSerializable(typeof(StructWithBadCustomConverter))]
4343
internal partial class MetadataAndSerializationContext : JsonSerializerContext, ITestContext
@@ -81,8 +81,8 @@ public override void EnsureFastPathGeneratedAsExpected()
8181
Assert.NotNull(MetadataAndSerializationContext.Default.StructWithCustomConverterFactory);
8282
Assert.NotNull(MetadataAndSerializationContext.Default.ClassWithCustomConverterProperty);
8383
Assert.NotNull(MetadataAndSerializationContext.Default.StructWithCustomConverterProperty);
84-
Assert.NotNull(MetadataAndSerializationContext.Default.ClassWithCustomConverterPropertyFactory);
85-
Assert.NotNull(MetadataAndSerializationContext.Default.StructWithCustomConverterPropertyFactory);
84+
Assert.NotNull(MetadataAndSerializationContext.Default.ClassWithCustomConverterFactoryProperty);
85+
Assert.NotNull(MetadataAndSerializationContext.Default.StructWithCustomConverterFactoryProperty);
8686
Assert.Throws<InvalidOperationException>(() => MetadataAndSerializationContext.Default.ClassWithBadCustomConverter);
8787
Assert.Throws<InvalidOperationException>(() => MetadataAndSerializationContext.Default.StructWithBadCustomConverter);
8888
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ namespace System.Text.Json.SourceGeneration.Tests
3535
[JsonSerializable(typeof(StructWithCustomConverterFactory), GenerationMode = JsonSourceGenerationMode.Metadata)]
3636
[JsonSerializable(typeof(ClassWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Metadata)]
3737
[JsonSerializable(typeof(StructWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Metadata)]
38-
[JsonSerializable(typeof(ClassWithCustomConverterPropertyFactory), GenerationMode = JsonSourceGenerationMode.Metadata)]
39-
[JsonSerializable(typeof(StructWithCustomConverterPropertyFactory), GenerationMode = JsonSourceGenerationMode.Metadata)]
38+
[JsonSerializable(typeof(ClassWithCustomConverterFactoryProperty), GenerationMode = JsonSourceGenerationMode.Metadata)]
39+
[JsonSerializable(typeof(StructWithCustomConverterFactoryProperty), GenerationMode = JsonSourceGenerationMode.Metadata)]
4040
[JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata)]
4141
[JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata)]
4242
internal partial class MetadataWithPerTypeAttributeContext : JsonSerializerContext, ITestContext
@@ -79,8 +79,8 @@ public override void EnsureFastPathGeneratedAsExpected()
7979
Assert.Null(MetadataWithPerTypeAttributeContext.Default.StructWithCustomConverterFactory.SerializeHandler);
8080
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ClassWithCustomConverterProperty.SerializeHandler);
8181
Assert.Null(MetadataWithPerTypeAttributeContext.Default.StructWithCustomConverterProperty.SerializeHandler);
82-
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ClassWithCustomConverterPropertyFactory.SerializeHandler);
83-
Assert.Null(MetadataWithPerTypeAttributeContext.Default.StructWithCustomConverterPropertyFactory.SerializeHandler);
82+
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ClassWithCustomConverterFactoryProperty.SerializeHandler);
83+
Assert.Null(MetadataWithPerTypeAttributeContext.Default.StructWithCustomConverterFactoryProperty.SerializeHandler);
8484
Assert.Throws<InvalidOperationException>(() => MetadataWithPerTypeAttributeContext.Default.ClassWithBadCustomConverter.SerializeHandler);
8585
Assert.Throws<InvalidOperationException>(() => MetadataWithPerTypeAttributeContext.Default.StructWithBadCustomConverter.SerializeHandler);
8686
}
@@ -116,8 +116,8 @@ public override void EnsureFastPathGeneratedAsExpected()
116116
[JsonSerializable(typeof(StructWithCustomConverterFactory))]
117117
[JsonSerializable(typeof(ClassWithCustomConverterProperty))]
118118
[JsonSerializable(typeof(StructWithCustomConverterProperty))]
119-
[JsonSerializable(typeof(ClassWithCustomConverterPropertyFactory))]
120-
[JsonSerializable(typeof(StructWithCustomConverterPropertyFactory))]
119+
[JsonSerializable(typeof(ClassWithCustomConverterFactoryProperty))]
120+
[JsonSerializable(typeof(StructWithCustomConverterFactoryProperty))]
121121
[JsonSerializable(typeof(ClassWithBadCustomConverter))]
122122
[JsonSerializable(typeof(StructWithBadCustomConverter))]
123123
internal partial class MetadataContext : JsonSerializerContext, ITestContext
@@ -183,8 +183,8 @@ public override void EnsureFastPathGeneratedAsExpected()
183183
Assert.Null(MetadataContext.Default.StructWithCustomConverterFactory.SerializeHandler);
184184
Assert.Null(MetadataContext.Default.ClassWithCustomConverterProperty.SerializeHandler);
185185
Assert.Null(MetadataContext.Default.StructWithCustomConverterProperty.SerializeHandler);
186-
Assert.Null(MetadataContext.Default.ClassWithCustomConverterPropertyFactory.SerializeHandler);
187-
Assert.Null(MetadataContext.Default.StructWithCustomConverterPropertyFactory.SerializeHandler);
186+
Assert.Null(MetadataContext.Default.ClassWithCustomConverterFactoryProperty.SerializeHandler);
187+
Assert.Null(MetadataContext.Default.StructWithCustomConverterFactoryProperty.SerializeHandler);
188188
Assert.Throws<InvalidOperationException>(() => MetadataContext.Default.ClassWithBadCustomConverter.SerializeHandler);
189189
Assert.Throws<InvalidOperationException>(() => MetadataContext.Default.StructWithBadCustomConverter.SerializeHandler);
190190
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace System.Text.Json.SourceGeneration.Tests
3636
[JsonSerializable(typeof(StructWithCustomConverterFactory), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
3737
[JsonSerializable(typeof(ClassWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
3838
[JsonSerializable(typeof(StructWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
39-
[JsonSerializable(typeof(ClassWithCustomConverterPropertyFactory), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
40-
[JsonSerializable(typeof(StructWithCustomConverterPropertyFactory), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
39+
[JsonSerializable(typeof(ClassWithCustomConverterFactoryProperty), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
40+
[JsonSerializable(typeof(StructWithCustomConverterFactoryProperty), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
4141
[JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
4242
[JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)]
4343
internal partial class MixedModeContext : JsonSerializerContext, ITestContext
@@ -81,8 +81,8 @@ public override void EnsureFastPathGeneratedAsExpected()
8181
Assert.Null(MixedModeContext.Default.StructWithCustomConverterFactory.SerializeHandler);
8282
Assert.Null(MixedModeContext.Default.ClassWithCustomConverterProperty.SerializeHandler);
8383
Assert.Null(MixedModeContext.Default.StructWithCustomConverterProperty.SerializeHandler);
84-
Assert.Null(MixedModeContext.Default.ClassWithCustomConverterPropertyFactory.SerializeHandler);
85-
Assert.Null(MixedModeContext.Default.StructWithCustomConverterPropertyFactory.SerializeHandler);
84+
Assert.Null(MixedModeContext.Default.ClassWithCustomConverterFactoryProperty.SerializeHandler);
85+
Assert.Null(MixedModeContext.Default.StructWithCustomConverterFactoryProperty.SerializeHandler);
8686
Assert.Throws<InvalidOperationException>(() => MixedModeContext.Default.ClassWithBadCustomConverter.SerializeHandler);
8787
Assert.Throws<InvalidOperationException>(() => MixedModeContext.Default.StructWithBadCustomConverter.SerializeHandler);
8888
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,28 +246,28 @@ public virtual void RoundTripWithCustomPropertyConverterFactory_Class()
246246
{
247247
const string Json = "{\"MyEnum\":\"One\"}";
248248

249-
ClassWithCustomConverterPropertyFactory obj = new()
249+
ClassWithCustomConverterFactoryProperty obj = new()
250250
{
251251
MyEnum = SampleEnum.One
252252
};
253253

254254
if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization)
255255
{
256-
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.ClassWithCustomConverterPropertyFactory));
256+
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.ClassWithCustomConverterFactoryProperty));
257257
}
258258
else
259259
{
260-
string json = JsonSerializer.Serialize(obj, DefaultContext.ClassWithCustomConverterPropertyFactory);
260+
string json = JsonSerializer.Serialize(obj, DefaultContext.ClassWithCustomConverterFactoryProperty);
261261
Assert.Equal(Json, json);
262262
}
263263

264264
if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization)
265265
{
266-
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.ClassWithCustomConverterPropertyFactory));
266+
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.ClassWithCustomConverterFactoryProperty));
267267
}
268268
else
269269
{
270-
obj = JsonSerializer.Deserialize(Json, DefaultContext.ClassWithCustomConverterPropertyFactory);
270+
obj = JsonSerializer.Deserialize(Json, DefaultContext.ClassWithCustomConverterFactoryProperty);
271271
Assert.Equal(SampleEnum.One, obj.MyEnum);
272272
}
273273
}
@@ -277,28 +277,28 @@ public virtual void RoundTripWithCustomPropertyConverterFactory_Struct()
277277
{
278278
const string Json = "{\"MyEnum\":\"One\"}";
279279

280-
StructWithCustomConverterPropertyFactory obj = new()
280+
StructWithCustomConverterFactoryProperty obj = new()
281281
{
282282
MyEnum = SampleEnum.One
283283
};
284284

285285
if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization)
286286
{
287-
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.StructWithCustomConverterPropertyFactory));
287+
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.StructWithCustomConverterFactoryProperty));
288288
}
289289
else
290290
{
291-
string json = JsonSerializer.Serialize(obj, DefaultContext.StructWithCustomConverterPropertyFactory);
291+
string json = JsonSerializer.Serialize(obj, DefaultContext.StructWithCustomConverterFactoryProperty);
292292
Assert.Equal(Json, json);
293293
}
294294

295295
if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization)
296296
{
297-
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.StructWithCustomConverterPropertyFactory));
297+
Assert.Throws<InvalidOperationException>(() => JsonSerializer.Serialize(obj, DefaultContext.StructWithCustomConverterFactoryProperty));
298298
}
299299
else
300300
{
301-
obj = JsonSerializer.Deserialize(Json, DefaultContext.StructWithCustomConverterPropertyFactory);
301+
obj = JsonSerializer.Deserialize(Json, DefaultContext.StructWithCustomConverterFactoryProperty);
302302
Assert.Equal(SampleEnum.One, obj.MyEnum);
303303
}
304304
}

0 commit comments

Comments
 (0)