Skip to content

Commit

Permalink
SIANXSVC-1204: Wrap response inside a result (#7)
Browse files Browse the repository at this point in the history
The older pattern is not longer supported by e5e and therefore this
library needs to be fixed.

Closes SIANXSVC-1204
  • Loading branch information
nachtjasmin authored Jan 30, 2024
1 parent 1d955c9 commit c1c785e
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ public async Task ShouldHaveCorrectStdoutFormatting()
{
await Host.StartWithTestEntrypointAsync(_ => E5EResponse.From("response"));
var (stdout, _) = await Host.WriteOnceAsync(builder => builder.WithData("request"));
Assert.Equal(@"+++{""data"":""response"",""type"":""text""}---", stdout);
Assert.Equal(@"+++{""result"":{""data"":""response"",""type"":""text""}}---", stdout);
}

[Fact]
8 changes: 5 additions & 3 deletions src/Anexia.E5E.Tests/TestHelpers/TestHostBuilder.cs
Original file line number Diff line number Diff line change
@@ -119,10 +119,12 @@ private async Task<E5EResponse> ReadResponseAsync()
var json = stdout
.Replace(options.StdoutTerminationSequence, "")
.Replace(options.DaemonExecutionTerminationSequence, "");
var resp = JsonSerializer.Deserialize<E5EResponse>(json, E5EJsonSerializerOptions.Default);

// ReSharper disable once NullableWarningSuppressionIsUsed
return resp!;
var result = JsonSerializer.Deserialize<JsonElement>(json, E5EJsonSerializerOptions.Default);
if (!result.TryGetProperty("result", out result))
throw new InvalidOperationException("The response does not contain a 'result' property");

return result.Deserialize<E5EResponse>(E5EJsonSerializerOptions.Default)!;
}

public Task StartWithTestEntrypointAsync(Func<E5ERequest, E5EResponse> handler)
14 changes: 10 additions & 4 deletions src/Anexia.E5E/Hosting/E5ECommunicationService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using System.Text.Json;

using Anexia.E5E.Abstractions;
@@ -141,14 +142,19 @@ private async Task<string> ExecuteFunctionAsync(IE5EFunctionHandler handler, E5E
try
{
_logger.ReceivedResponse(response);
string json = "";
#if NET8_0_OR_GREATER
json = JsonSerializer.Serialize(response, E5ESerializationContext.Default.E5EResponse);
var json = JsonSerializer.Serialize(response, E5ESerializationContext.Default.E5EResponse);
#else
json = JsonSerializer.Serialize<E5EResponse>(response, E5EJsonSerializerOptions.Default);
var json = JsonSerializer.Serialize<E5EResponse>(response, E5EJsonSerializerOptions.Default);
#endif

return _options.StdoutTerminationSequence + json;
// Although we could use a dedicated response class for serialization, this would create another overhead
// which is easily replaced with this string concatenation.
return new StringBuilder().Append(_options.StdoutTerminationSequence)
.Append("{\"result\":")
.Append(json)
.Append('}')
.ToString();
}
catch (Exception e)
{

0 comments on commit c1c785e

Please sign in to comment.