|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.executors.openapi; |
| 17 | + |
| 18 | +import io.serverlessworkflow.api.types.ExternalResource; |
| 19 | +import io.serverlessworkflow.impl.TaskContext; |
| 20 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 21 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 22 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 23 | +import io.serverlessworkflow.impl.executors.CallableTask; |
| 24 | +import io.serverlessworkflow.impl.executors.http.HttpExecutor; |
| 25 | +import io.serverlessworkflow.impl.executors.http.HttpExecutorBuilder; |
| 26 | +import io.serverlessworkflow.impl.resources.ResourceLoaderUtils; |
| 27 | +import io.swagger.v3.oas.models.media.Schema; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Iterator; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | + |
| 36 | +class JacksonOpenAPIExecutor implements CallableTask { |
| 37 | + |
| 38 | + private final OpenAPIProcessor processor; |
| 39 | + private final ExternalResource resource; |
| 40 | + private final Map<String, Object> parameters; |
| 41 | + private final HttpExecutorBuilder builder; |
| 42 | + |
| 43 | + JacksonOpenAPIExecutor( |
| 44 | + OpenAPIProcessor processor, |
| 45 | + ExternalResource resource, |
| 46 | + Map<String, Object> parameters, |
| 47 | + HttpExecutorBuilder builder) { |
| 48 | + this.processor = processor; |
| 49 | + this.resource = resource; |
| 50 | + this.parameters = parameters; |
| 51 | + this.builder = builder; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public CompletableFuture<WorkflowModel> apply( |
| 56 | + WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { |
| 57 | + |
| 58 | + // In the same workflow, access to an already cached document |
| 59 | + final OperationDefinition operationDefinition = |
| 60 | + processor.parse( |
| 61 | + workflowContext |
| 62 | + .definition() |
| 63 | + .resourceLoader() |
| 64 | + .load( |
| 65 | + resource, |
| 66 | + ResourceLoaderUtils::readString, |
| 67 | + workflowContext, |
| 68 | + taskContext, |
| 69 | + input)); |
| 70 | + |
| 71 | + fillHttpBuilder(workflowContext.definition().application(), operationDefinition); |
| 72 | + // One executor per operation, even if the document is the same |
| 73 | + // Me may refactor this even further to reuse the same executor (since the base URI is the same, |
| 74 | + // but the path differs, although some use cases may require different client configurations for |
| 75 | + // different paths...) |
| 76 | + Collection<HttpExecutor> executors = |
| 77 | + operationDefinition.getServers().stream().map(s -> builder.build(s)).toList(); |
| 78 | + |
| 79 | + Iterator<HttpExecutor> iter = executors.iterator(); |
| 80 | + if (!iter.hasNext()) { |
| 81 | + throw new IllegalArgumentException( |
| 82 | + "List of servers is empty for schema " + resource.getName()); |
| 83 | + } |
| 84 | + CompletableFuture<WorkflowModel> future = |
| 85 | + iter.next().apply(workflowContext, taskContext, input); |
| 86 | + while (iter.hasNext()) { |
| 87 | + future.exceptionallyCompose(i -> iter.next().apply(workflowContext, taskContext, input)); |
| 88 | + } |
| 89 | + return future; |
| 90 | + } |
| 91 | + |
| 92 | + private void fillHttpBuilder(WorkflowApplication application, OperationDefinition operation) { |
| 93 | + Map<String, Object> headersMap = new HashMap<>(); |
| 94 | + Map<String, Object> queryMap = new HashMap<>(); |
| 95 | + Map<String, Object> pathParameters = new HashMap<>(); |
| 96 | + Set<String> missingParams = new HashSet<>(); |
| 97 | + |
| 98 | + Map<String, Object> bodyParameters = new HashMap<>(parameters); |
| 99 | + for (ParameterDefinition parameter : operation.getParameters()) { |
| 100 | + switch (parameter.getIn()) { |
| 101 | + case "header": |
| 102 | + param(parameter, bodyParameters, headersMap, missingParams); |
| 103 | + break; |
| 104 | + case "path": |
| 105 | + param(parameter, bodyParameters, pathParameters, missingParams); |
| 106 | + break; |
| 107 | + case "query": |
| 108 | + param(parameter, bodyParameters, queryMap, missingParams); |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (!missingParams.isEmpty()) { |
| 114 | + throw new IllegalArgumentException( |
| 115 | + "Missing required OpenAPI parameters for operation '" |
| 116 | + + (operation.getOperation().getOperationId() != null |
| 117 | + ? operation.getOperation().getOperationId() |
| 118 | + : "<unknown>" + "': ") |
| 119 | + + missingParams); |
| 120 | + } |
| 121 | + builder |
| 122 | + .withMethod(operation.getMethod()) |
| 123 | + .withPath(new OperationPathResolver(operation.getPath(), application, pathParameters)) |
| 124 | + .withBody(bodyParameters) |
| 125 | + .withQueryMap(queryMap) |
| 126 | + .withHeaders(headersMap); |
| 127 | + } |
| 128 | + |
| 129 | + private void param( |
| 130 | + ParameterDefinition parameter, |
| 131 | + Map<String, Object> origMap, |
| 132 | + Map<String, Object> collectorMap, |
| 133 | + Set<String> missingParams) { |
| 134 | + String name = parameter.getName(); |
| 135 | + if (origMap.containsKey(name)) { |
| 136 | + collectorMap.put(parameter.getName(), origMap.remove(name)); |
| 137 | + } else if (parameter.getRequired()) { |
| 138 | + Schema<?> schema = parameter.getSchema(); |
| 139 | + Object defaultValue = schema != null ? schema.getDefault() : null; |
| 140 | + if (defaultValue != null) { |
| 141 | + collectorMap.put(name, defaultValue); |
| 142 | + } else { |
| 143 | + missingParams.add(name); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments