Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable reflection in source gen tests and fix AOT testing. #89105

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task WriteAsyncEnumerable_CancellationToken_IsPassedToAsyncEnumerat
using var cts = new CancellationTokenSource();

IAsyncEnumerable<int> value = CreateEnumerable();
await JsonSerializer.SerializeAsync(utf8Stream, value, cancellationToken: cts.Token);
await JsonSerializer.SerializeAsync(utf8Stream, value, Serializer.DefaultOptions, cancellationToken: cts.Token);
Assert.Equal("[1,2]", utf8Stream.AsString());

async IAsyncEnumerable<int> CreateEnumerable([EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -331,7 +331,7 @@ public async Task RegressionTest_DisposingEnumeratorOnPendingMoveNextAsyncOperat
// Regression test for https://github.com/dotnet/runtime/issues/57360
using var stream = new Utf8MemoryStream();
using var cts = new CancellationTokenSource(millisecondsDelay: 1000);
await Assert.ThrowsAsync<TaskCanceledException>(async () => await JsonSerializer.SerializeAsync(stream, GetNumbersAsync(), cancellationToken: cts.Token));
await Assert.ThrowsAsync<TaskCanceledException>(async () => await JsonSerializer.SerializeAsync(stream, GetNumbersAsync(), Serializer.DefaultOptions, cancellationToken: cts.Token));

static async IAsyncEnumerable<int> GetNumbersAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public async Task IgnoreCycles_OnExtensionData()
[Fact]
public async Task IgnoreCycles_DoesNotSupportPreserveSemantics()
{
if (StreamingSerializer is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a condition on the Fact we could do instead? That would describe the platforms/configuration this test isn't supported in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, it's an abstract test suite and StreamingSerializer is overridden by derived classes. I don't think it could be done with the existing ConditionalFact/ConditionalTheory infrastructure without substantial refactoring of the test suites.

{
return;
}

// Object
var node = new NodeWithExtensionData();
node.Next = node;
Expand All @@ -246,7 +251,7 @@ public async Task IgnoreCycles_DoesNotSupportPreserveSemantics()
Assert.True(node.Next.MyOverflow.ContainsKey("$ref"));

using var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
node = await JsonSerializer.DeserializeAsync<NodeWithExtensionData>(ms, s_optionsIgnoreCycles);
node = await StreamingSerializer.DeserializeWrapper<NodeWithExtensionData>(ms, s_optionsIgnoreCycles);
Assert.True(node.MyOverflow.ContainsKey("$id"));
Assert.True(node.Next.MyOverflow.ContainsKey("$ref"));

Expand All @@ -257,7 +262,7 @@ public async Task IgnoreCycles_DoesNotSupportPreserveSemantics()

await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<RecursiveDictionary>(json, s_optionsIgnoreCycles));
using var ms2 = new MemoryStream(Encoding.UTF8.GetBytes(json));
await Assert.ThrowsAsync<JsonException>(() => JsonSerializer.DeserializeAsync<RecursiveDictionary>(ms2, s_optionsIgnoreCycles).AsTask());
await Assert.ThrowsAsync<JsonException>(() => StreamingSerializer.DeserializeWrapper<RecursiveDictionary>(ms2, s_optionsIgnoreCycles));

// List
var list = new RecursiveList();
Expand All @@ -266,12 +271,17 @@ public async Task IgnoreCycles_DoesNotSupportPreserveSemantics()

await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<RecursiveList>(json, s_optionsIgnoreCycles));
using var ms3 = new MemoryStream(Encoding.UTF8.GetBytes(json));
await Assert.ThrowsAsync<JsonException>(() => JsonSerializer.DeserializeAsync<RecursiveList>(ms3, s_optionsIgnoreCycles).AsTask());
await Assert.ThrowsAsync<JsonException>(() => StreamingSerializer.DeserializeWrapper<RecursiveList>(ms3, s_optionsIgnoreCycles));
}

[Fact]
public async Task IgnoreCycles_DoesNotSupportPreserveSemantics_Polymorphic()
{
if (StreamingSerializer is null)
{
return;
}

// Object
var node = new NodeWithObjectProperty();
node.Next = node;
Expand All @@ -282,7 +292,7 @@ public async Task IgnoreCycles_DoesNotSupportPreserveSemantics_Polymorphic()
Assert.True(nodeAsJsonElement.GetProperty("$ref").GetString() == "1");

using var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
node = await JsonSerializer.DeserializeAsync<NodeWithObjectProperty>(ms, s_optionsIgnoreCycles);
node = await StreamingSerializer.DeserializeWrapper<NodeWithObjectProperty>(ms, s_optionsIgnoreCycles);
nodeAsJsonElement = Assert.IsType<JsonElement>(node.Next);
Assert.True(nodeAsJsonElement.GetProperty("$ref").GetString() == "1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ public RequiredKeywordTests(JsonSerializerWrapper serializer) : base(serializer)
[InlineData(false)]
public async Task ClassWithRequiredKeywordDeserialization(bool ignoreNullValues)
{
JsonSerializerOptions options = new()
{
IgnoreNullValues = ignoreNullValues
};
JsonSerializerOptions options = Serializer.CreateOptions(makeReadOnly: false);
options.IgnoreNullValues = ignoreNullValues;

AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo<PersonWithRequiredMembers>(options),
nameof(PersonWithRequiredMembers.FirstName),
Expand Down Expand Up @@ -84,10 +82,8 @@ public class PersonWithRequiredMembers
[InlineData(false)]
public async Task ClassWithRequiredKeywordAndSmallParametrizedCtorFailsDeserialization(bool ignoreNullValues)
{
JsonSerializerOptions options = new()
{
IgnoreNullValues = ignoreNullValues
};
JsonSerializerOptions options = Serializer.CreateOptions(makeReadOnly: false);
options.IgnoreNullValues = ignoreNullValues;

AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo<PersonWithRequiredMembersAndSmallParametrizedCtor>(options),
nameof(PersonWithRequiredMembersAndSmallParametrizedCtor.FirstName),
Expand Down Expand Up @@ -167,10 +163,8 @@ public PersonWithRequiredMembersAndSmallParametrizedCtor(string firstName, strin
[InlineData(false)]
public async Task ClassWithRequiredKeywordAndLargeParametrizedCtorFailsDeserialization(bool ignoreNullValues)
{
JsonSerializerOptions options = new()
{
IgnoreNullValues = ignoreNullValues
};
JsonSerializerOptions options = Serializer.CreateOptions(makeReadOnly: false);
options.IgnoreNullValues = ignoreNullValues;

AssertJsonTypeInfoHasRequiredProperties(GetTypeInfo<PersonWithRequiredMembersAndLargeParametrizedCtor>(options),
nameof(PersonWithRequiredMembersAndLargeParametrizedCtor.AProp),
Expand Down Expand Up @@ -539,7 +533,7 @@ public class ClassWithRequiredExtensionDataProperty
[Fact]
public async Task RequiredKeywordAndJsonRequiredCustomAttributeWorkCorrectlyTogether()
{
JsonSerializerOptions options = JsonSerializerOptions.Default;
JsonSerializerOptions options = Serializer.CreateOptions();
JsonTypeInfo typeInfo = GetTypeInfo<ClassWithRequiredKeywordAndJsonRequiredCustomAttribute>(options);
AssertJsonTypeInfoHasRequiredProperties(typeInfo,
nameof(ClassWithRequiredKeywordAndJsonRequiredCustomAttribute.SomeProperty));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task DeserializeUnsupportedType<T>(ValueWrapper<T> wrapper)
// Verify Nullable<> semantics. NSE is not thrown because the serializer handles null.
if (isNullableOfT)
{
Assert.Null(JsonSerializer.Deserialize<T>("null"));
Assert.Null(await Serializer.DeserializeWrapper<T>("null"));

json = $@"{{""Prop"":null}}";
ClassWithType<T> obj = await Serializer.DeserializeWrapper<ClassWithType<T>>(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ public virtual void RoundtripWithCustomConverterProperty_Class()
{
string json = JsonSerializer.Serialize(obj, DefaultContext.ClassWithCustomConverterProperty);
Assert.Equal(ExpectedJson, json);
}

obj = JsonSerializer.Deserialize<ClassWithCustomConverterProperty>(ExpectedJson);
Assert.Equal(42, obj.Property.Value);
obj = JsonSerializer.Deserialize<ClassWithCustomConverterProperty>(ExpectedJson, DefaultContext.ClassWithCustomConverterProperty);
Assert.Equal(42, obj.Property.Value);
}
}

[Fact]
Expand Down Expand Up @@ -332,10 +332,10 @@ public virtual void RoundtripWithCustomConverterProperty_Struct()
{
string json = JsonSerializer.Serialize(obj, DefaultContext.StructWithCustomConverterProperty);
Assert.Equal(ExpectedJson, json);
}

obj = JsonSerializer.Deserialize<StructWithCustomConverterProperty>(ExpectedJson);
Assert.Equal(42, obj.Property.Value);
obj = JsonSerializer.Deserialize<StructWithCustomConverterProperty>(ExpectedJson, DefaultContext.StructWithCustomConverterProperty);
Assert.Equal(42, obj.Property.Value);
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ internal partial class SerializationContext : JsonSerializerContext, ITestContex
[JsonSerializable(typeof(StructWithCustomConverterFactory), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(ClassWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(StructWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(ClassWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(StructWithCustomConverterProperty), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(ClassWithCustomConverterFactoryProperty), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(StructWithCustomConverterFactoryProperty), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(ClassWithCustomConverterNullableProperty), GenerationMode = JsonSourceGenerationMode.Serialization)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkMinimum)</TargetFrameworks>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<!-- Source gen tests target AOT so we disable reflection everywhere for consistency. -->
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
<!-- SYSLIB0020: JsonSerializerOptions.IgnoreNullValues is obsolete -->
<!-- SYSLIB0049: JsonSerializerOptions.AddContext is obsolete -->
<!-- SYSLIB1034: Suppress JsonStringEnumConverter use warnings -->
Expand Down