Skip to content

Commit 62bb4f2

Browse files
Add .NET 7 source gen regression tests (#78857)
1 parent b3afffa commit 62bb4f2

16 files changed

+1254
-6
lines changed

src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net60RegressionTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
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;
5-
using System.Collections;
64
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Text;
95
using System.Text.Json.Serialization.Metadata;
10-
using System.Text.Json.Serialization.Tests;
116
using System.Text.Json.Tests.SourceGenRegressionTests.Net60;
12-
using System.Threading.Tasks;
137
using Xunit;
148
using HighLowTemps = System.Text.Json.Tests.SourceGenRegressionTests.Net60.HighLowTemps;
159
using WeatherForecastWithPOCOs = System.Text.Json.Tests.SourceGenRegressionTests.Net60.WeatherForecastWithPOCOs;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK.
5+
// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components.
6+
// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests.
7+
8+
// <auto-generated/>
9+
10+
#nullable enable annotations
11+
#nullable disable warnings
12+
13+
// Suppress warnings about [Obsolete] member usage in generated code.
14+
#pragma warning disable CS0618
15+
16+
namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70
17+
{
18+
public partial class Net70GeneratedContext
19+
{
20+
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter>? _ClassWithCustomConverter;
21+
/// <summary>
22+
/// Defines the source generated JSON serialization contract metadata for a given type.
23+
/// </summary>
24+
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter> ClassWithCustomConverter
25+
{
26+
get => _ClassWithCustomConverter ??= Create_ClassWithCustomConverter(Options, makeReadOnly: true);
27+
}
28+
29+
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter> Create_ClassWithCustomConverter(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
30+
{
31+
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter>? jsonTypeInfo = null;
32+
global::System.Text.Json.Serialization.JsonConverter? customConverter;
33+
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter))) != null)
34+
{
35+
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter>(options, customConverter);
36+
}
37+
else
38+
{
39+
global::System.Text.Json.Serialization.JsonConverter converter = new global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter.CustomConverter();
40+
global::System.Type typeToConvert = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter);
41+
if (!converter.CanConvert(typeToConvert))
42+
{
43+
throw new global::System.InvalidOperationException(string.Format("The converter '{0}' is not compatible with the type '{1}'.", converter.GetType(), typeToConvert));
44+
}
45+
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter> (options, converter);
46+
}
47+
48+
if (makeReadOnly)
49+
{
50+
jsonTypeInfo.MakeReadOnly();
51+
}
52+
53+
return jsonTypeInfo;
54+
}
55+
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK.
5+
// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components.
6+
// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests.
7+
8+
// <auto-generated/>
9+
10+
#nullable enable annotations
11+
#nullable disable warnings
12+
13+
// Suppress warnings about [Obsolete] member usage in generated code.
14+
#pragma warning disable CS0618
15+
16+
namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70
17+
{
18+
public partial class Net70GeneratedContext
19+
{
20+
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTimeOffset>? _DateTimeOffset;
21+
/// <summary>
22+
/// Defines the source generated JSON serialization contract metadata for a given type.
23+
/// </summary>
24+
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTimeOffset> DateTimeOffset
25+
{
26+
get => _DateTimeOffset ??= Create_DateTimeOffset(Options, makeReadOnly: true);
27+
}
28+
29+
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTimeOffset> Create_DateTimeOffset(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
30+
{
31+
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.DateTimeOffset>? jsonTypeInfo = null;
32+
global::System.Text.Json.Serialization.JsonConverter? customConverter;
33+
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.DateTimeOffset))) != null)
34+
{
35+
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.DateTimeOffset>(options, customConverter);
36+
}
37+
else
38+
{
39+
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.DateTimeOffset>(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.DateTimeOffsetConverter);
40+
}
41+
42+
if (makeReadOnly)
43+
{
44+
jsonTypeInfo.MakeReadOnly();
45+
}
46+
47+
return jsonTypeInfo;
48+
}
49+
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK.
5+
// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components.
6+
// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests.
7+
8+
// <auto-generated/>
9+
10+
#nullable enable annotations
11+
#nullable disable warnings
12+
13+
// Suppress warnings about [Obsolete] member usage in generated code.
14+
#pragma warning disable CS0618
15+
16+
namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70
17+
{
18+
public partial class Net70GeneratedContext
19+
{
20+
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>>? _DictionaryStringHighLowTemps;
21+
/// <summary>
22+
/// Defines the source generated JSON serialization contract metadata for a given type.
23+
/// </summary>
24+
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>> DictionaryStringHighLowTemps
25+
{
26+
get => _DictionaryStringHighLowTemps ??= Create_DictionaryStringHighLowTemps(Options, makeReadOnly: true);
27+
}
28+
29+
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>> Create_DictionaryStringHighLowTemps(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly)
30+
{
31+
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>>? jsonTypeInfo = null;
32+
global::System.Text.Json.Serialization.JsonConverter? customConverter;
33+
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>))) != null)
34+
{
35+
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>>(options, customConverter);
36+
}
37+
else
38+
{
39+
global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>> info = new global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>>()
40+
{
41+
ObjectCreator = () => new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>(),
42+
NumberHandling = default,
43+
SerializeHandler = DictionaryStringHighLowTempsSerializeHandler
44+
};
45+
46+
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateDictionaryInfo<global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>, global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>(options, info);
47+
48+
}
49+
50+
if (makeReadOnly)
51+
{
52+
jsonTypeInfo.MakeReadOnly();
53+
}
54+
55+
return jsonTypeInfo;
56+
}
57+
58+
59+
// Intentionally not a static method because we create a delegate to it. Invoking delegates to instance
60+
// methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk.
61+
private void DictionaryStringHighLowTempsSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>? value)
62+
{
63+
if (value == null)
64+
{
65+
writer.WriteNullValue();
66+
return;
67+
}
68+
69+
writer.WriteStartObject();
70+
71+
foreach (global::System.Collections.Generic.KeyValuePair<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps> pair in value)
72+
{
73+
writer.WritePropertyName(pair.Key);
74+
HighLowTempsSerializeHandler(writer, pair.Value!);
75+
}
76+
77+
writer.WriteEndObject();
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK.
5+
// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components.
6+
// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests.
7+
8+
// <auto-generated/>
9+
10+
#nullable enable annotations
11+
#nullable disable warnings
12+
13+
// Suppress warnings about [Obsolete] member usage in generated code.
14+
#pragma warning disable CS0618
15+
16+
namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70
17+
{
18+
public partial class Net70GeneratedContext: global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver
19+
{
20+
/// <inheritdoc/>
21+
public override global::System.Text.Json.Serialization.Metadata.JsonTypeInfo GetTypeInfo(global::System.Type type)
22+
{
23+
if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs))
24+
{
25+
return this.WeatherForecastWithPOCOs;
26+
}
27+
28+
if (type == typeof(global::System.DateTimeOffset))
29+
{
30+
return this.DateTimeOffset;
31+
}
32+
33+
if (type == typeof(global::System.Int32))
34+
{
35+
return this.Int32;
36+
}
37+
38+
if (type == typeof(global::System.String))
39+
{
40+
return this.String;
41+
}
42+
43+
if (type == typeof(global::System.Collections.Generic.List<global::System.DateTimeOffset>))
44+
{
45+
return this.ListDateTimeOffset;
46+
}
47+
48+
if (type == typeof(global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>))
49+
{
50+
return this.DictionaryStringHighLowTemps;
51+
}
52+
53+
if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps))
54+
{
55+
return this.HighLowTemps;
56+
}
57+
58+
if (type == typeof(global::System.String[]))
59+
{
60+
return this.StringArray;
61+
}
62+
63+
if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter))
64+
{
65+
return this.ClassWithCustomConverter;
66+
}
67+
68+
return null!;
69+
}
70+
71+
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.GetTypeInfo(global::System.Type type, global::System.Text.Json.JsonSerializerOptions options)
72+
{
73+
if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs))
74+
{
75+
return Create_WeatherForecastWithPOCOs(options, makeReadOnly: false);
76+
}
77+
78+
if (type == typeof(global::System.DateTimeOffset))
79+
{
80+
return Create_DateTimeOffset(options, makeReadOnly: false);
81+
}
82+
83+
if (type == typeof(global::System.Int32))
84+
{
85+
return Create_Int32(options, makeReadOnly: false);
86+
}
87+
88+
if (type == typeof(global::System.String))
89+
{
90+
return Create_String(options, makeReadOnly: false);
91+
}
92+
93+
if (type == typeof(global::System.Collections.Generic.List<global::System.DateTimeOffset>))
94+
{
95+
return Create_ListDateTimeOffset(options, makeReadOnly: false);
96+
}
97+
98+
if (type == typeof(global::System.Collections.Generic.Dictionary<global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>))
99+
{
100+
return Create_DictionaryStringHighLowTemps(options, makeReadOnly: false);
101+
}
102+
103+
if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps))
104+
{
105+
return Create_HighLowTemps(options, makeReadOnly: false);
106+
}
107+
108+
if (type == typeof(global::System.String[]))
109+
{
110+
return Create_StringArray(options, makeReadOnly: false);
111+
}
112+
113+
if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter))
114+
{
115+
return Create_ClassWithCustomConverter(options, makeReadOnly: false);
116+
}
117+
118+
return null;
119+
}
120+
121+
}
122+
}

0 commit comments

Comments
 (0)