From c1c785ee1de9b2ad5bb7c3141cdc07ef8df7d6b5 Mon Sep 17 00:00:00 2001 From: Jasmin Date: Tue, 30 Jan 2024 11:45:21 +0100 Subject: [PATCH] SIANXSVC-1204: Wrap response inside a result (#7) The older pattern is not longer supported by e5e and therefore this library needs to be fixed. Closes SIANXSVC-1204 --- .../Integration/E5ECommunicationServiceTests.cs | 2 +- .../TestHelpers/TestHostBuilder.cs | 8 +++++--- src/Anexia.E5E/Hosting/E5ECommunicationService.cs | 14 ++++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Anexia.E5E.Tests/Integration/E5ECommunicationServiceTests.cs b/src/Anexia.E5E.Tests/Integration/E5ECommunicationServiceTests.cs index 7174b71..ed7c306 100644 --- a/src/Anexia.E5E.Tests/Integration/E5ECommunicationServiceTests.cs +++ b/src/Anexia.E5E.Tests/Integration/E5ECommunicationServiceTests.cs @@ -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] diff --git a/src/Anexia.E5E.Tests/TestHelpers/TestHostBuilder.cs b/src/Anexia.E5E.Tests/TestHelpers/TestHostBuilder.cs index e939e93..a2926b3 100644 --- a/src/Anexia.E5E.Tests/TestHelpers/TestHostBuilder.cs +++ b/src/Anexia.E5E.Tests/TestHelpers/TestHostBuilder.cs @@ -119,10 +119,12 @@ private async Task ReadResponseAsync() var json = stdout .Replace(options.StdoutTerminationSequence, "") .Replace(options.DaemonExecutionTerminationSequence, ""); - var resp = JsonSerializer.Deserialize(json, E5EJsonSerializerOptions.Default); - // ReSharper disable once NullableWarningSuppressionIsUsed - return resp!; + var result = JsonSerializer.Deserialize(json, E5EJsonSerializerOptions.Default); + if (!result.TryGetProperty("result", out result)) + throw new InvalidOperationException("The response does not contain a 'result' property"); + + return result.Deserialize(E5EJsonSerializerOptions.Default)!; } public Task StartWithTestEntrypointAsync(Func handler) diff --git a/src/Anexia.E5E/Hosting/E5ECommunicationService.cs b/src/Anexia.E5E/Hosting/E5ECommunicationService.cs index ed8c84a..e966a12 100644 --- a/src/Anexia.E5E/Hosting/E5ECommunicationService.cs +++ b/src/Anexia.E5E/Hosting/E5ECommunicationService.cs @@ -1,3 +1,4 @@ +using System.Text; using System.Text.Json; using Anexia.E5E.Abstractions; @@ -141,14 +142,19 @@ private async Task 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(response, E5EJsonSerializerOptions.Default); + var json = JsonSerializer.Serialize(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) {