Skip to content

Commit

Permalink
Enhance JSON deserialization to include service provider
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sfmskywalker committed Oct 3, 2024
1 parent 679a051 commit 41e1062
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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)!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public TClient CreateApiClient<TClient>()
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task HelloWorldWorkflow_ShouldReturnOk()
{
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private async Task<WorkflowState> ExecuteWorkflowAsync(string workflowDefinition
{
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task Workflow_ShouldComplete(string workflowDefinitionId)
{
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);
}
Expand Down

0 comments on commit 41e1062

Please sign in to comment.