diff --git a/src/Camunda.Worker/Client/ExternalTaskClient.cs b/src/Camunda.Worker/Client/ExternalTaskClient.cs index 04af857..db9b627 100644 --- a/src/Camunda.Worker/Client/ExternalTaskClient.cs +++ b/src/Camunda.Worker/Client/ExternalTaskClient.cs @@ -34,6 +34,13 @@ public ExternalTaskClient(HttpClient httpClient) }); } + public ExternalTaskClient(HttpClient httpClient, Action configureJsonOptions) + : this(httpClient) + { + _jsonSerializerOptions = _jsonSerializerOptions + .Also(configureJsonOptions); + } + [ExcludeFromCodeCoverage] private static void ValidateHttpClient(HttpClient httpClient) { diff --git a/src/Camunda.Worker/Client/Serialization/JsonVariableJsonConverter.cs b/src/Camunda.Worker/Client/Serialization/JsonVariableJsonConverter.cs index edc1e9d..05db9f8 100644 --- a/src/Camunda.Worker/Client/Serialization/JsonVariableJsonConverter.cs +++ b/src/Camunda.Worker/Client/Serialization/JsonVariableJsonConverter.cs @@ -23,7 +23,7 @@ public class JsonVariableJsonConverter : JsonConverter 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(); } } diff --git a/src/Camunda.Worker/Client/ServiceCollectionExtensions.cs b/src/Camunda.Worker/Client/ServiceCollectionExtensions.cs index 0f32aa7..589d5ce 100644 --- a/src/Camunda.Worker/Client/ServiceCollectionExtensions.cs +++ b/src/Camunda.Worker/Client/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Net.Http; +using System.Text.Json; using Microsoft.Extensions.DependencyInjection; namespace Camunda.Worker.Client; @@ -8,11 +9,19 @@ public static class ServiceCollectionExtensions { public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services) { - return services.AddHttpClient(); + return services.AddHttpClient() + .AddTypedClient(httpClient => new ExternalTaskClient(httpClient)); } public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services, Action configureClient) { - return services.AddHttpClient(configureClient); + return services.AddHttpClient(configureClient) + .AddTypedClient(httpClient => new ExternalTaskClient(httpClient)); + } + + public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services, Action configureClient, Action configureJsonOptions) + { + return services.AddHttpClient(configureClient) + .AddTypedClient(httpClient => new ExternalTaskClient(httpClient, configureJsonOptions)); } } diff --git a/test/Camunda.Worker.Tests/Client/ExternalTaskClientTest.cs b/test/Camunda.Worker.Tests/Client/ExternalTaskClientTest.cs index 42b039a..ed18812 100644 --- a/test/Camunda.Worker.Tests/Client/ExternalTaskClientTest.cs +++ b/test/Camunda.Worker.Tests/Client/ExternalTaskClientTest.cs @@ -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; @@ -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() @@ -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 + { + ["TEST"] = JsonVariable.Create(entity) + } + }; + + await _client.CompleteAsync("testTask", request, CancellationToken.None); + + _handlerMock.VerifyNoOutstandingExpectation(); + } + [Fact] public async Task TestReportFailure() {