Skip to content

Commit

Permalink
Support configurable JsonSerializerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
anserg256 committed Nov 8, 2023
1 parent 47ab500 commit 73727b8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/Camunda.Worker/Client/ExternalTaskClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public ExternalTaskClient(HttpClient httpClient)
});
}

public ExternalTaskClient(HttpClient httpClient, Action<JsonSerializerOptions> configureJsonOptions)
: this(httpClient)
{
_jsonSerializerOptions = _jsonSerializerOptions
.Also(configureJsonOptions);
}

[ExcludeFromCodeCoverage]
private static void ValidateHttpClient(HttpClient httpClient)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class JsonVariableJsonConverter : JsonConverter<JsonVariable>
public override void Write(Utf8JsonWriter writer, JsonVariable value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString("value", value.Value.ToJsonString());
writer.WriteString("value", value.Value.ToJsonString(options));
writer.WriteEndObject();
}
}
13 changes: 11 additions & 2 deletions src/Camunda.Worker/Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net.Http;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;

namespace Camunda.Worker.Client;
Expand All @@ -8,11 +9,19 @@ public static class ServiceCollectionExtensions
{
public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services)
{
return services.AddHttpClient<IExternalTaskClient, ExternalTaskClient>();
return services.AddHttpClient<IExternalTaskClient>()
.AddTypedClient<IExternalTaskClient>(httpClient => new ExternalTaskClient(httpClient));
}

public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services, Action<HttpClient> configureClient)
{
return services.AddHttpClient<IExternalTaskClient, ExternalTaskClient>(configureClient);
return services.AddHttpClient<IExternalTaskClient>(configureClient)
.AddTypedClient<IExternalTaskClient>(httpClient => new ExternalTaskClient(httpClient));
}

public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services, Action<HttpClient> configureClient, Action<JsonSerializerOptions> configureJsonOptions)
{
return services.AddHttpClient<IExternalTaskClient>(configureClient)
.AddTypedClient<IExternalTaskClient>(httpClient => new ExternalTaskClient(httpClient, configureJsonOptions));
}
}
50 changes: 43 additions & 7 deletions test/Camunda.Worker.Tests/Client/ExternalTaskClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading;
using System.Threading.Tasks;
using Camunda.Worker.Variables;
Expand All @@ -14,16 +15,17 @@ namespace Camunda.Worker.Client;
public class ExternalTaskClientTest : IDisposable
{
private readonly MockHttpMessageHandler _handlerMock = new();
private readonly ExternalTaskClient _client;
private ExternalTaskClient _client;
private HttpClient _httpClient;

public ExternalTaskClientTest()
{
_client = new ExternalTaskClient(
new HttpClient(_handlerMock)
{
BaseAddress = new Uri("http://test/api")
}
);
_httpClient = new HttpClient(_handlerMock)
{
BaseAddress = new Uri("http://test/api")
};

_client = new ExternalTaskClient(_httpClient);
}

public void Dispose()
Expand Down Expand Up @@ -94,6 +96,40 @@ public async Task TestComplete()
_handlerMock.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task TestCompleteWithCustomJsonOptions()
{
_client = new ExternalTaskClient(_httpClient, opt =>
{
opt.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
});

var entity = new
{
Description = "The \"Спутник-1\" satellite"
};

var requestContent = @"{""workerId"":""testWorker"",""variables"":{""TEST"":{""value"":" +
@"""{\""Description\"":\""The \\\""Спутник-1\\\"" satellite\""}""," +
@"""type"":""Json""}},""localVariables"":null}";

_handlerMock.Expect(HttpMethod.Post, "http://test/api/external-task/testTask/complete")
.WithContent(requestContent)
.Respond(HttpStatusCode.NoContent);

var request = new CompleteRequest("testWorker")
{
Variables = new Dictionary<string, VariableBase>
{
["TEST"] = JsonVariable.Create(entity)
}
};

await _client.CompleteAsync("testTask", request, CancellationToken.None);

_handlerMock.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task TestReportFailure()
{
Expand Down

0 comments on commit 73727b8

Please sign in to comment.