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

Stop Calling JSON GetTypeInfo with the runtime type #47859

Merged
merged 6 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Convert RequestDelegateWritesAsJsonResponseBody_WithJsonSerializerCon…
…text to shared test between RDF and RDG.
  • Loading branch information
eerhardt committed Apr 25, 2023
commit 4f9e3de93344cafcc60487b38663af2c799ca1e5
36 changes: 0 additions & 36 deletions src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2033,47 +2033,11 @@ public async Task RequestDelegateWritesJsonTypeDiscriminatorToJsonResponseBody_W
Assert.Equal(nameof(JsonTodoChild), deserializedResponseBody["$type"]!.GetValue<string>());
}

public static IEnumerable<object[]> JsonContextActions
{
get
{
return ComplexResult.Concat(ChildResult);
}
}

[JsonSerializable(typeof(Todo))]
[JsonSerializable(typeof(TodoChild))]
private partial class TestJsonContext : JsonSerializerContext
{ }

[Theory]
[MemberData(nameof(JsonContextActions))]
public async Task RequestDelegateWritesAsJsonResponseBody_WithJsonSerializerContext(Delegate @delegate)
{
var httpContext = CreateHttpContext();
httpContext.RequestServices = new ServiceCollection()
.AddSingleton(LoggerFactory)
.ConfigureHttpJsonOptions(o => o.SerializerOptions.TypeInfoResolver = TestJsonContext.Default)
.BuildServiceProvider();

var responseBodyStream = new MemoryStream();
httpContext.Response.Body = responseBodyStream;

var rdfOptions = new RequestDelegateFactoryOptions() { ServiceProvider = httpContext.RequestServices };
var factoryResult = RequestDelegateFactory.Create(@delegate, rdfOptions);
var requestDelegate = factoryResult.RequestDelegate;

await requestDelegate(httpContext);

var deserializedResponseBody = JsonSerializer.Deserialize<Todo>(responseBodyStream.ToArray(), new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});

Assert.NotNull(deserializedResponseBody);
Assert.Equal("Write even more tests!", deserializedResponseBody!.Name);
}

[Fact]
public void CreateDelegateThrows_WhenGetJsonTypeInfoFail()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Primitives;
using System.Text;
using System.Text.Json;

namespace Microsoft.AspNetCore.Http.Generators.Tests;

Expand Down Expand Up @@ -250,6 +251,70 @@ public async Task MapAction_HandlesCompletedTaskReturn()
await VerifyResponseBodyAsync(httpContext, string.Empty);
}

public static IEnumerable<object[]> JsonContextActions
{
get
{
yield return new[] { "TestAction", """Todo TestAction() => new Todo { Name = "Write even more tests!" };""" };
yield return new[] { "TaskTestAction", """Task<Todo> TaskTestAction() => Task.FromResult(new Todo { Name = "Write even more tests!" });""" };
yield return new[] { "ValueTaskTestAction", """ValueTask<Todo> ValueTaskTestAction() => ValueTask.FromResult(new Todo { Name = "Write even more tests!" });""" };

yield return new[] { "StaticTestAction", """static Todo StaticTestAction() => new Todo { Name = "Write even more tests!" };""" };
yield return new[] { "StaticTaskTestAction", """static Task<Todo> StaticTaskTestAction() => Task.FromResult(new Todo { Name = "Write even more tests!" });""" };
yield return new[] { "StaticValueTaskTestAction", """static ValueTask<Todo> StaticValueTaskTestAction() => ValueTask.FromResult(new Todo { Name = "Write even more tests!" });""" };

yield return new[] { "TestAction", """Todo TestAction() => new JsonTodoChild { Name = "Write even more tests!", Child = "With type hierarchies!" };""" };

yield return new[] { "TaskTestAction", """Task<Todo> TaskTestAction() => Task.FromResult<Todo>(new JsonTodoChild { Name = "Write even more tests!", Child = "With type hierarchies!" });""" };
yield return new[] { "TaskTestActionAwaited", """
async Task<Todo> TaskTestActionAwaited()
{
await Task.Yield();
return new JsonTodoChild { Name = "Write even more tests!", Child = "With type hierarchies!" };
}
""" };

yield return new[] { "ValueTaskTestAction", """ValueTask<Todo> ValueTaskTestAction() => ValueTask.FromResult<Todo>(new JsonTodoChild { Name = "Write even more tests!", Child = "With type hierarchies!" });""" };
yield return new[] { "ValueTaskTestActionAwaited", """
async ValueTask<Todo> ValueTaskTestActionAwaited()
{
await Task.Yield();
return new JsonTodoChild { Name = "Write even more tests!", Child = "With type hierarchies!" };
}
""" };
}
}

[Theory]
[MemberData(nameof(JsonContextActions))]
public async Task RequestDelegateWritesAsJsonResponseBody_WithJsonSerializerContext(string delegateName, string delegateSource)
{
var source = $"""
app.MapGet("/test", {delegateName});

{delegateSource}
""";

var (_, compilation) = await RunGeneratorAsync(source);
var serviceProvider = CreateServiceProvider(serviceCollection =>
{
serviceCollection.ConfigureHttpJsonOptions(o => o.SerializerOptions.TypeInfoResolver = SharedTestJsonContext.Default);
});
var endpoint = GetEndpointFromCompilation(compilation, serviceProvider: serviceProvider);

var httpContext = CreateHttpContext(serviceProvider);

await endpoint.RequestDelegate(httpContext);

var deserializedResponseBody = JsonSerializer.Deserialize<Todo>(((MemoryStream)httpContext.Response.Body).ToArray(), new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});

Assert.NotNull(deserializedResponseBody);
Assert.Equal("Write even more tests!", deserializedResponseBody!.Name);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class JsonTodoChild : JsonTodo
public string? Child { get; set; }
}

[JsonSerializable(typeof(Todo))]
[JsonSerializable(typeof(IAsyncEnumerable<JsonTodo>))]
public partial class SharedTestJsonContext : JsonSerializerContext
{ }
Expand Down