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

SIANXSVC-1204: Wrap response inside a result #7

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
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 5 additions & 3 deletions src/Anexia.E5E.Tests/TestHelpers/TestHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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;
Expand Down Expand Up @@ -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)
{
Expand Down