Skip to content

Commit 867fbaf

Browse files
authored
Merge pull request #352 from serverlessworkflow/fix-open-api-processor
Fixed the OpenApiFunctionProcessor, which was failling unexpectedly when returned contentless responses
2 parents 74d2971 + d9ecd97 commit 867fbaf

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/apps/Synapse.Worker/Services/Processors/OpenApiFunctionProcessor.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,8 @@ protected override async Task ProcessAsync(CancellationToken cancellationToken)
287287
foreach (string server in this.Servers)
288288
{
289289
var requestUri = $"{server}{this.Path}";
290-
if (requestUri.StartsWith("//"))
291-
requestUri = $"https:{requestUri}";
292-
if (!string.IsNullOrWhiteSpace(this.QueryString))
293-
requestUri += $"?{this.QueryString}";
290+
if (requestUri.StartsWith("//"))requestUri = $"https:{requestUri}";
291+
if (!string.IsNullOrWhiteSpace(this.QueryString)) requestUri += $"?{this.QueryString}";
294292
using var request = new HttpRequestMessage(this.HttpMethod, requestUri);
295293
foreach (var header in this.Headers)
296294
{
@@ -303,34 +301,29 @@ protected override async Task ProcessAsync(CancellationToken cancellationToken)
303301
request.Content = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json); //todo: support other media types?
304302
}
305303
using var response = await this.HttpClient.SendAsync(request, cancellationToken);
306-
if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
307-
continue;
304+
if (response.StatusCode == HttpStatusCode.ServiceUnavailable)continue;
308305
var rawContent = await response.Content.ReadAsByteArrayAsync(cancellationToken)!;
309306
var contentString = null as string;
310-
if (rawContent != null)
311-
contentString = Encoding.UTF8.GetString(rawContent);
307+
if (rawContent != null) contentString = Encoding.UTF8.GetString(rawContent);
312308
if (!response.IsSuccessStatusCode)
313309
{
314-
this.Logger.LogInformation("Failed to execute the Open API operation '{operationId}' at '{uri}'. The remote server responded with a non-success status code '{statusCode}'.", this.Operation.OperationId, response.RequestMessage!.RequestUri, response.StatusCode);
310+
this.Logger.LogError("Failed to execute the Open API operation '{operationId}' at '{uri}'. The remote server responded with a non-success status code '{statusCode}'.", this.Operation.OperationId, response.RequestMessage!.RequestUri, response.StatusCode);
315311
this.Logger.LogDebug("Response content:\r\n{responseContent}", contentString ?? "None");
316312
response.EnsureSuccessStatusCode(contentString);
317313
}
318-
if (rawContent != null)
314+
if (rawContent != null && rawContent.Length > 0)
319315
{
320316
var mediaType = response.Content?.Headers.ContentType?.MediaType;
321-
var serializer = this.SerializerProvider.GetSerializersFor(mediaType).FirstOrDefault();
322-
if (serializer == null)
323-
throw new NotSupportedException($"Failed to find a serializer for the specified media type '{mediaType}'");
317+
if(string.IsNullOrWhiteSpace(mediaType)) this.Logger.LogWarning("Failed to determine the response's content type. Assuming {json}", MediaTypeNames.Application.Json);
318+
var serializer = this.SerializerProvider.GetSerializersFor(mediaType).FirstOrDefault() ?? throw new NotSupportedException($"Failed to find a serializer for the specified media type '{mediaType}'");
324319
using var stream = new MemoryStream(rawContent!);
325320
output = (await serializer.DeserializeAsync<JToken>(stream, cancellationToken)).ToObject();
326321
}
327322
success = true;
328323
break;
329324
}
330-
if (output == null)
331-
output = new();
332-
if (!success)
333-
throw new HttpRequestException($"Failed to execute the operation activity '{this.Operation.OperationId}' (id: '{this.Activity.Id}'): No service available", null, HttpStatusCode.ServiceUnavailable);
325+
output ??= new();
326+
if (!success)throw new HttpRequestException($"Failed to execute the operation activity '{this.Operation.OperationId}' (id: '{this.Activity.Id}'): No service available", null, HttpStatusCode.ServiceUnavailable);
334327
await this.OnNextAsync(new V1WorkflowActivityCompletedIntegrationEvent(this.Activity.Id, output), cancellationToken);
335328
await this.OnCompletedAsync(cancellationToken);
336329
}

0 commit comments

Comments
 (0)