Skip to content

Commit 0648ecb

Browse files
authored
Merge pull request #437 from serverlessworkflow/fix-evaluate-openapi-operation
Fixed the `OpenApiCallExecutor` to evaluate the `operationId` parameter when its value is a runtime expressions
2 parents e0ecff9 + 2ecc326 commit 0648ecb

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/runner/Synapse.Runner/Services/Executors/OpenApiCallExecutor.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ protected override async Task DoInitializeAsync(CancellationToken cancellationTo
124124
}
125125
using var responseStream = await response.Content!.ReadAsStreamAsync(cancellationToken)!;
126126
this.Document = new OpenApiStreamReader().Read(responseStream, out var diagnostic);
127+
var operationId = this.OpenApi.OperationId;
128+
if (operationId.IsRuntimeExpression()) operationId = await this.Task.Workflow.Expressions.EvaluateAsync<string>(operationId, this.Task.Input, this.GetExpressionEvaluationArguments(), cancellationToken).ConfigureAwait(false);
127129
var operation = this.Document.Paths
128130
.SelectMany(p => p.Value.Operations)
129-
.FirstOrDefault(o => o.Value.OperationId == this.OpenApi.OperationId);
130-
if (operation.Value == null) throw new NullReferenceException($"Failed to find an operation with id '{this.OpenApi.OperationId}' in OpenAPI document at '{uri}'");
131+
.FirstOrDefault(o => o.Value.OperationId == operationId);
132+
if (operation.Value == null) throw new NullReferenceException($"Failed to find an operation with id '{operationId}' in OpenAPI document at '{uri}'");
131133
this.HttpMethod = operation.Key.ToHttpMethod();
132134
this.Operation = operation.Value;
133135
this.Servers = this.Document.Servers.Select(s => s.Url).ToList();
@@ -141,23 +143,23 @@ protected override async Task DoInitializeAsync(CancellationToken cancellationTo
141143
foreach (var param in parameters.Where(p => p.In == ParameterLocation.Cookie))
142144
{
143145
if (this.Parameters.TryGetValue(param.Name, out var value, StringComparison.OrdinalIgnoreCase) && value != null) this.Cookies.Add(param.Name, value.ToString()!);
144-
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{this.OpenApi.OperationId}'");
146+
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{operationId}'");
145147
}
146148
foreach (var param in parameters.Where(p => p.In == ParameterLocation.Header))
147149
{
148150
if (this.Parameters.TryGetValue(param.Name, out var value, StringComparison.OrdinalIgnoreCase) && value != null) this.Headers.Add(param.Name, value.ToString()!);
149-
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{this.OpenApi.OperationId}'");
151+
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{operationId}'");
150152
}
151153
foreach (var param in parameters.Where(p => p.In == ParameterLocation.Path))
152154
{
153155
if (this.Parameters.TryGetValue(param.Name, out var value, StringComparison.OrdinalIgnoreCase) && value != null) this.Path = this.Path.Replace($"{{{param.Name}}}", value.ToString());
154-
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{this.OpenApi.OperationId}'");
156+
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{operationId}'");
155157
}
156158
var queryParameters = new Dictionary<string, string>();
157159
foreach (var param in parameters.Where(p => p.In == ParameterLocation.Query))
158160
{
159161
if (this.Parameters.TryGetValue(param.Name, out var value, StringComparison.OrdinalIgnoreCase) && value != null) queryParameters.Add(param.Name, value.ToString()!);
160-
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{this.OpenApi.OperationId}'");
162+
else if (param.Required) throw new NullReferenceException($"Failed to find the definition of the required parameter '{param.Name}' in the OpenAPI operation with id '{operationId}'");
161163
}
162164
this.Query = string.Join("&", queryParameters.Select(kvp => $"{kvp.Key}={kvp.Value}"));
163165
if (operation.Value.RequestBody != null)
@@ -177,7 +179,7 @@ protected override async Task DoInitializeAsync(CancellationToken cancellationTo
177179
this.Body = this.Parameters;
178180
}
179181
}
180-
if (this.Body == null && operation.Value.RequestBody.Required) throw new NullReferenceException($"Failed to determine the required body parameter for the OpenAPI operation with id '{this.OpenApi.OperationId}'");
182+
if (this.Body == null && operation.Value.RequestBody.Required) throw new NullReferenceException($"Failed to determine the required body parameter for the OpenAPI operation with id '{operationId}'");
181183
}
182184
}
183185

@@ -281,7 +283,7 @@ protected override async Task DoExecuteAsync(CancellationToken cancellationToken
281283
break;
282284
}
283285
output ??= new();
284-
if (!success) throw new HttpRequestException($"Failed to execute the Open APU operation with id '{this.Operation.OperationId}': No service available", null, HttpStatusCode.ServiceUnavailable);
286+
if (!success) throw new HttpRequestException($"Failed to execute the Open API operation with id '{this.Operation.OperationId}': No service available", null, HttpStatusCode.ServiceUnavailable);
285287
await this.SetResultAsync(output, this.Task.Definition.Then, cancellationToken).ConfigureAwait(false);
286288
}
287289

0 commit comments

Comments
 (0)