/
using_newtonsoft_for_serialization.cs
65 lines (52 loc) · 1.9 KB
/
using_newtonsoft_for_serialization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Alba;
using IntegrationTests;
using Marten;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Shouldly;
using Wolverine.Http.Tests.Bugs;
using Wolverine.Marten;
namespace Wolverine.Http.Tests;
public class using_newtonsoft_for_serialization
{
[Fact]
public async Task end_to_end()
{
#region sample_use_newtonsoft_for_http_serialization
var builder = WebApplication.CreateBuilder(Array.Empty<string>());
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddMarten(Servers.PostgresConnectionString)
.IntegrateWithWolverine();
builder.Host.UseWolverine(opts =>
{
opts.Discovery.IncludeAssembly(GetType().Assembly);
});
await using var host = await AlbaHost.For(builder, app =>
{
app.MapWolverineEndpoints(opts =>
{
// Opt into using Newtonsoft.Json for JSON serialization just with Wolverine.HTTP routes
// Configuring the JSON serialization is optional
opts.UseNewtonsoftJsonForSerialization(settings => settings.TypeNameHandling = TypeNameHandling.All);
});
});
#endregion
var result = await host.Scenario(x =>
{
x.Post.Json(new NumberRequest(3, 4)).ToUrl("/newtonsoft/numbers");
});
var text = result.ReadAsText();
text.ShouldBe("{\"$type\":\"Wolverine.Http.Tests.MathResponse, Wolverine.Http.Tests\",\"Sum\":7,\"Product\":12}");
}
}
public record NumberRequest(int X, int Y);
public record MathResponse(int Sum, int Product);
public static class MathEndpoint
{
[WolverinePost("/newtonsoft/numbers")]
public static MathResponse Post(NumberRequest request)
{
return new MathResponse(request.X + request.Y, request.X * request.Y);
}
}