Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configurable JsonSerializerOptions #67

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
12 changes: 10 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,18 @@ public static class ServiceCollectionExtensions
{
public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services)
{
return services.AddHttpClient<IExternalTaskClient, ExternalTaskClient>();
return services.AddHttpClient<IExternalTaskClient, ExternalTaskClient>(
httpClient => new ExternalTaskClient(httpClient));
}

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

public static IHttpClientBuilder AddExternalTaskClient(this IServiceCollection services, Action<HttpClient> configureClient, Action<JsonSerializerOptions> configureJsonOptions)
{
return services.AddHttpClient<IExternalTaskClient, ExternalTaskClient>(
httpClient => new ExternalTaskClient(httpClient, configureJsonOptions)).ConfigureHttpClient(configureClient);
}
}
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
Loading