|
|
@@ -3,6 +3,7 @@
|
|
|
|
|
|
using System.Text.Json;
|
|
|
using System.Text.Json.Serialization;
|
|
|
+using Microsoft.AspNetCore.Http.Json;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
@@ -24,16 +25,19 @@ public partial class HttpResultsHelperTests
|
|
|
Name = "Write even more tests!",
|
|
|
};
|
|
|
var responseBodyStream = new MemoryStream();
|
|
|
- var httpContext = CreateHttpContext(responseBodyStream, useJsonContext);
|
|
|
+ var httpContext = CreateHttpContext(responseBodyStream);
|
|
|
+ var serializerOptions = new JsonOptions().SerializerOptions;
|
|
|
+
|
|
|
+ if (useJsonContext)
|
|
|
+ {
|
|
|
+ serializerOptions.AddContext<TestJsonContext>();
|
|
|
+ }
|
|
|
|
|
|
// Act
|
|
|
- await HttpResultsHelper.WriteResultAsJsonAsync(httpContext, NullLogger.Instance, value);
|
|
|
+ await HttpResultsHelper.WriteResultAsJsonAsync(httpContext, NullLogger.Instance, value, jsonSerializerOptions: serializerOptions);
|
|
|
|
|
|
// Assert
|
|
|
- var body = JsonSerializer.Deserialize<TodoStruct>(responseBodyStream.ToArray(), new JsonSerializerOptions
|
|
|
- {
|
|
|
- PropertyNameCaseInsensitive = true
|
|
|
- });
|
|
|
+ var body = JsonSerializer.Deserialize<TodoStruct>(responseBodyStream.ToArray(), serializerOptions);
|
|
|
|
|
|
Assert.Equal("Write even more tests!", body!.Name);
|
|
|
Assert.True(body!.IsComplete);
|
|
|
@@ -52,16 +56,19 @@ public partial class HttpResultsHelperTests
|
|
|
Name = "Write even more tests!",
|
|
|
};
|
|
|
var responseBodyStream = new MemoryStream();
|
|
|
- var httpContext = CreateHttpContext(responseBodyStream, useJsonContext);
|
|
|
+ var httpContext = CreateHttpContext(responseBodyStream);
|
|
|
+ var serializerOptions = new JsonOptions().SerializerOptions;
|
|
|
+
|
|
|
+ if (useJsonContext)
|
|
|
+ {
|
|
|
+ serializerOptions.AddContext<TestJsonContext>();
|
|
|
+ }
|
|
|
|
|
|
// Act
|
|
|
- await HttpResultsHelper.WriteResultAsJsonAsync(httpContext, NullLogger.Instance, value);
|
|
|
+ await HttpResultsHelper.WriteResultAsJsonAsync(httpContext, NullLogger.Instance, value, jsonSerializerOptions: serializerOptions);
|
|
|
|
|
|
// Assert
|
|
|
- var body = JsonSerializer.Deserialize<Todo>(responseBodyStream.ToArray(), new JsonSerializerOptions
|
|
|
- {
|
|
|
- PropertyNameCaseInsensitive = true
|
|
|
- });
|
|
|
+ var body = JsonSerializer.Deserialize<Todo>(responseBodyStream.ToArray(), serializerOptions);
|
|
|
|
|
|
Assert.NotNull(body);
|
|
|
Assert.Equal("Write even more tests!", body!.Name);
|
|
|
@@ -82,16 +89,87 @@ public partial class HttpResultsHelperTests
|
|
|
Child = "With type hierarchies!"
|
|
|
};
|
|
|
var responseBodyStream = new MemoryStream();
|
|
|
- var httpContext = CreateHttpContext(responseBodyStream, useJsonContext);
|
|
|
+ var httpContext = CreateHttpContext(responseBodyStream);
|
|
|
+ var serializerOptions = new JsonOptions().SerializerOptions;
|
|
|
+
|
|
|
+ if (useJsonContext)
|
|
|
+ {
|
|
|
+ serializerOptions.AddContext<TestJsonContext>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Act
|
|
|
+ await HttpResultsHelper.WriteResultAsJsonAsync(httpContext, NullLogger.Instance, value, jsonSerializerOptions: serializerOptions);
|
|
|
+
|
|
|
+ // Assert
|
|
|
+ var body = JsonSerializer.Deserialize<TodoChild>(responseBodyStream.ToArray(), serializerOptions);
|
|
|
+
|
|
|
+ Assert.NotNull(body);
|
|
|
+ Assert.Equal("Write even more tests!", body!.Name);
|
|
|
+ Assert.True(body!.IsComplete);
|
|
|
+ Assert.Equal("With type hierarchies!", body!.Child);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Theory]
|
|
|
+ [InlineData(true)]
|
|
|
+ [InlineData(false)]
|
|
|
+ public async Task WriteResultAsJsonAsync_Works_UsingBaseType_ForChildTypes(bool useJsonContext)
|
|
|
+ {
|
|
|
+ // Arrange
|
|
|
+ var value = new TodoChild()
|
|
|
+ {
|
|
|
+ Id = 1,
|
|
|
+ IsComplete = true,
|
|
|
+ Name = "Write even more tests!",
|
|
|
+ Child = "With type hierarchies!"
|
|
|
+ };
|
|
|
+ var responseBodyStream = new MemoryStream();
|
|
|
+ var httpContext = CreateHttpContext(responseBodyStream);
|
|
|
+ var serializerOptions = new JsonOptions().SerializerOptions;
|
|
|
+
|
|
|
+ if (useJsonContext)
|
|
|
+ {
|
|
|
+ serializerOptions.AddContext<TestJsonContext>();
|
|
|
+ }
|
|
|
|
|
|
// Act
|
|
|
- await HttpResultsHelper.WriteResultAsJsonAsync(httpContext, NullLogger.Instance, value);
|
|
|
+ await HttpResultsHelper.WriteResultAsJsonAsync<Todo>(httpContext, NullLogger.Instance, value, jsonSerializerOptions: serializerOptions);
|
|
|
|
|
|
// Assert
|
|
|
- var body = JsonSerializer.Deserialize<TodoChild>(responseBodyStream.ToArray(), new JsonSerializerOptions
|
|
|
+ var body = JsonSerializer.Deserialize<TodoChild>(responseBodyStream.ToArray(), serializerOptions);
|
|
|
+
|
|
|
+ Assert.NotNull(body);
|
|
|
+ Assert.Equal("Write even more tests!", body!.Name);
|
|
|
+ Assert.True(body!.IsComplete);
|
|
|
+ Assert.Equal("With type hierarchies!", body!.Child);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Theory]
|
|
|
+ [InlineData(true)]
|
|
|
+ [InlineData(false)]
|
|
|
+ public async Task WriteResultAsJsonAsync_Works_UsingBaseType_ForChildTypes_WithJsonPolymorphism(bool useJsonContext)
|
|
|
+ {
|
|
|
+ // Arrange
|
|
|
+ var value = new TodoJsonChild()
|
|
|
+ {
|
|
|
+ Id = 1,
|
|
|
+ IsComplete = true,
|
|
|
+ Name = "Write even more tests!",
|
|
|
+ Child = "With type hierarchies!"
|
|
|
+ };
|
|
|
+ var responseBodyStream = new MemoryStream();
|
|
|
+ var httpContext = CreateHttpContext(responseBodyStream);
|
|
|
+ var serializerOptions = new JsonOptions().SerializerOptions;
|
|
|
+
|
|
|
+ if (useJsonContext)
|
|
|
{
|
|
|
- PropertyNameCaseInsensitive = true
|
|
|
- });
|
|
|
+ serializerOptions.AddContext<TestJsonContext>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Act
|
|
|
+ await HttpResultsHelper.WriteResultAsJsonAsync<JsonTodo>(httpContext, NullLogger.Instance, value, jsonSerializerOptions: serializerOptions);
|
|
|
+
|
|
|
+ // Assert
|
|
|
+ var body = JsonSerializer.Deserialize<TodoJsonChild>(responseBodyStream.ToArray(), serializerOptions);
|
|
|
|
|
|
Assert.NotNull(body);
|
|
|
Assert.Equal("Write even more tests!", body!.Name);
|
|
|
@@ -99,31 +177,26 @@ public partial class HttpResultsHelperTests
|
|
|
Assert.Equal("With type hierarchies!", body!.Child);
|
|
|
}
|
|
|
|
|
|
- private static DefaultHttpContext CreateHttpContext(Stream stream, bool useJsonContext = false)
|
|
|
+ private static DefaultHttpContext CreateHttpContext(Stream stream)
|
|
|
=> new()
|
|
|
{
|
|
|
- RequestServices = CreateServices(useJsonContext),
|
|
|
+ RequestServices = CreateServices(),
|
|
|
Response =
|
|
|
{
|
|
|
Body = stream,
|
|
|
},
|
|
|
};
|
|
|
|
|
|
- private static IServiceProvider CreateServices(bool useJsonContext = false)
|
|
|
+ private static IServiceProvider CreateServices()
|
|
|
{
|
|
|
var services = new ServiceCollection();
|
|
|
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
|
|
|
-
|
|
|
- if (useJsonContext)
|
|
|
- {
|
|
|
- services.ConfigureHttpJsonOptions(o => o.SerializerOptions.TypeInfoResolver = TestJsonContext.Default);
|
|
|
- }
|
|
|
-
|
|
|
return services.BuildServiceProvider();
|
|
|
}
|
|
|
|
|
|
[JsonSerializable(typeof(Todo))]
|
|
|
[JsonSerializable(typeof(TodoChild))]
|
|
|
+ [JsonSerializable(typeof(JsonTodo))]
|
|
|
[JsonSerializable(typeof(TodoStruct))]
|
|
|
private partial class TestJsonContext : JsonSerializerContext
|
|
|
{ }
|
|
|
@@ -150,4 +223,14 @@ public partial class HttpResultsHelperTests
|
|
|
{
|
|
|
public string Child { get; set; }
|
|
|
}
|
|
|
+
|
|
|
+ [JsonDerivedType(typeof(TodoJsonChild))]
|
|
|
+ private class JsonTodo : Todo
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ private class TodoJsonChild : JsonTodo
|
|
|
+ {
|
|
|
+ public string Child { get; set; }
|
|
|
+ }
|
|
|
}
|