Enhance JSON deserialization to include service provider

Updated ReadAsJsonAsync method to accept an IServiceProvider parameter for improved JSON deserialization. Modified corresponding tests and helper classes to utilize the service provider for more flexible JSON serialization options.
This commit is contained in:
Sipke Schoorstra 2024-10-03 19:12:00 +02:00
parent 679a0515c8
commit 41e1062148
5 changed files with 7 additions and 6 deletions

View file

@ -1,14 +1,15 @@
using System.Text.Json;
using Elsa.Api.Client;
// ReSharper disable once CheckNamespace
namespace Elsa.Workflows.ComponentTests;
public static class HttpResponseMessageExtensions
{
public static async Task<T> ReadAsJsonAsync<T>(this HttpResponseMessage response, CancellationToken cancellationToken = default)
public static async Task<T> ReadAsJsonAsync<T>(this HttpResponseMessage response, IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
{
var json = await response.Content.ReadAsStringAsync(cancellationToken);
var options = RefitSettingsHelper.CreateJsonSerializerOptions();
var options = RefitSettingsHelper.CreateJsonSerializerOptions(serviceProvider);
return JsonSerializer.Deserialize<T>(json, options)!;
}
}

View file

@ -31,7 +31,7 @@ public class WorkflowServer(Infrastructure infrastructure, string url) : WebAppl
var client = CreateClient();
client.BaseAddress = new Uri(client.BaseAddress!, "/elsa/api");
client.Timeout = TimeSpan.FromMinutes(1);
return RestService.For<TClient>(client, CreateRefitSettings());
return RestService.For<TClient>(client, CreateRefitSettings(Services));
}
public HttpClient CreateHttpWorkflowClient()

View file

@ -11,7 +11,7 @@ public class HelloWorldTests(App app) : AppComponentTest(app)
{
var client = WorkflowServer.CreateApiClient<IExecuteWorkflowApi>();
using var response = await client.ExecuteAsync("1590068018aa4f0a");
var model = await response.ReadAsJsonAsync<Response>();
var model = await response.ReadAsJsonAsync<Response>(WorkflowServer.Services);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(WorkflowSubStatus.Finished, model.WorkflowState.SubStatus);
}

View file

@ -63,7 +63,7 @@ public class InputOutputLoggingTests(App app) : AppComponentTest(app)
{
var client = WorkflowServer.CreateApiClient<IExecuteWorkflowApi>();
using var response = await client.ExecuteAsync(workflowDefinitionId);
var model = await response.ReadAsJsonAsync<Response>();
var model = await response.ReadAsJsonAsync<Response>(WorkflowServer.Services);
return model.WorkflowState;
}

View file

@ -13,7 +13,7 @@ public class WorkflowCompletionTests(App app) : AppComponentTest(app)
{
var client = WorkflowServer.CreateApiClient<IExecuteWorkflowApi>();
using var response = await client.ExecuteAsync(workflowDefinitionId);
var model = await response.ReadAsJsonAsync<Response>();
var model = await response.ReadAsJsonAsync<Response>(WorkflowServer.Services);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(WorkflowSubStatus.Finished, model.WorkflowState.SubStatus);
}