|
| 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 com.fasterxml.jackson.databind.JsonNode; |
| 19 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +public class OpenAPI { |
| 28 | + |
| 29 | + public enum SwaggerVersion { |
| 30 | + SWAGGER_V2, |
| 31 | + OPENAPI_V3 |
| 32 | + } |
| 33 | + |
| 34 | + private final JsonNode root; |
| 35 | + private final boolean isSwaggerV2; |
| 36 | + |
| 37 | + public OpenAPI(JsonNode root) { |
| 38 | + this.root = Objects.requireNonNull(root, "root cannot be null"); |
| 39 | + this.isSwaggerV2 = isSwaggerV2(); |
| 40 | + this.validatePaths(); |
| 41 | + this.moveRequestInBodyToRequestBody(); |
| 42 | + } |
| 43 | + |
| 44 | + private void moveRequestInBodyToRequestBody() { |
| 45 | + if (!isSwaggerV2) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + JsonNode pathsNode = root.get("paths"); |
| 50 | + if (pathsNode == null || !pathsNode.isObject()) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + Iterator<String> pathNames = pathsNode.fieldNames(); |
| 55 | + pathNames.forEachRemaining( |
| 56 | + pathName -> { |
| 57 | + JsonNode pathNode = pathsNode.get(pathName); |
| 58 | + if (pathNode == null || !pathNode.isObject()) { |
| 59 | + return; |
| 60 | + } |
| 61 | + Iterator<String> methodNames = pathNode.fieldNames(); |
| 62 | + methodNames.forEachRemaining( |
| 63 | + methodName -> { |
| 64 | + JsonNode operationNode = pathNode.get(methodName); |
| 65 | + if (operationNode != null && operationNode.isObject()) { |
| 66 | + processOperationNode((ObjectNode) operationNode); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + private void processOperationNode(ObjectNode operationNode) { |
| 73 | + JsonNode parametersNode = operationNode.get("parameters"); |
| 74 | + if (parametersNode == null || !parametersNode.isArray()) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + ArrayNode originalParameters = (ArrayNode) parametersNode; |
| 79 | + ArrayNode filteredParameters = originalParameters.arrayNode(); |
| 80 | + boolean requestBodyCreated = false; |
| 81 | + |
| 82 | + for (JsonNode parameterNode : originalParameters) { |
| 83 | + if (parameterNode == null || !parameterNode.isObject()) { |
| 84 | + filteredParameters.add(parameterNode); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + JsonNode inNode = parameterNode.get("in"); |
| 89 | + if (inNode != null && "body".equals(inNode.asText())) { |
| 90 | + if (!requestBodyCreated) { |
| 91 | + ObjectNode requestBodyNode = operationNode.putObject("requestBody"); |
| 92 | + ObjectNode contentNode = requestBodyNode.putObject("content"); |
| 93 | + ObjectNode mediaTypeNode = contentNode.putObject("application/json"); |
| 94 | + ObjectNode schemaNode = mediaTypeNode.putObject("schema"); |
| 95 | + JsonNode schemaFromParam = parameterNode.get("schema"); |
| 96 | + if (schemaFromParam != null && schemaFromParam.isObject()) { |
| 97 | + schemaNode.setAll((ObjectNode) schemaFromParam); |
| 98 | + } |
| 99 | + requestBodyCreated = true; |
| 100 | + } |
| 101 | + } else { |
| 102 | + filteredParameters.add(parameterNode); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + operationNode.set("parameters", filteredParameters); |
| 107 | + } |
| 108 | + |
| 109 | + private void validatePaths() { |
| 110 | + if (!root.has("paths")) { |
| 111 | + throw new IllegalArgumentException("OpenAPI document must contain 'paths' field"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private boolean isSwaggerV2() { |
| 116 | + JsonNode swaggerNode = root.get("swagger"); |
| 117 | + return swaggerNode != null && swaggerNode.asText().startsWith("2.0"); |
| 118 | + } |
| 119 | + |
| 120 | + public PathItemInfo findOperationById(String operationId) { |
| 121 | + JsonNode paths = root.get("paths"); |
| 122 | + |
| 123 | + Set<Map.Entry<String, JsonNode>> properties = paths.properties(); |
| 124 | + |
| 125 | + for (Map.Entry<String, JsonNode> path : properties) { |
| 126 | + JsonNode pathNode = path.getValue(); |
| 127 | + Set<Map.Entry<String, JsonNode>> methods = pathNode.properties(); |
| 128 | + for (Map.Entry<String, JsonNode> method : methods) { |
| 129 | + JsonNode operationNode = method.getValue().get("operationId"); |
| 130 | + if (operationNode != null && operationNode.asText().equals(operationId)) { |
| 131 | + return new PathItemInfo(path.getKey(), path.getValue(), method.getKey()); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + throw new IllegalArgumentException("Operation with ID " + operationId + " not found"); |
| 136 | + } |
| 137 | + |
| 138 | + public List<String> getServers() { |
| 139 | + |
| 140 | + if (isSwaggerV2) { |
| 141 | + if (root.has("host")) { |
| 142 | + String host = root.get("host").asText(); |
| 143 | + String basePath = root.has("basePath") ? root.get("basePath").asText() : ""; |
| 144 | + String scheme = "http"; |
| 145 | + if (root.has("schemes") |
| 146 | + && root.get("schemes").isArray() |
| 147 | + && !root.get("schemes").isEmpty()) { |
| 148 | + scheme = root.get("schemes").get(0).asText(); |
| 149 | + } |
| 150 | + return List.of(scheme + "://" + host + basePath); |
| 151 | + } else { |
| 152 | + return List.of(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return root.has("servers") ? List.of(root.get("servers").findPath("url").asText()) : List.of(); |
| 157 | + } |
| 158 | + |
| 159 | + public SwaggerVersion getSwaggerVersion() { |
| 160 | + return isSwaggerV2 ? SwaggerVersion.SWAGGER_V2 : SwaggerVersion.OPENAPI_V3; |
| 161 | + } |
| 162 | + |
| 163 | + public JsonNode resolveSchema(String ref) { |
| 164 | + if (!ref.startsWith("#/")) { |
| 165 | + throw new IllegalArgumentException("Only local references are supported"); |
| 166 | + } |
| 167 | + String[] parts = ref.substring(2).split("/"); |
| 168 | + JsonNode currentNode = root; |
| 169 | + for (String part : parts) { |
| 170 | + currentNode = currentNode.get(part); |
| 171 | + if (currentNode == null) { |
| 172 | + throw new IllegalArgumentException("Reference " + ref + " could not be resolved"); |
| 173 | + } |
| 174 | + } |
| 175 | + return currentNode; |
| 176 | + } |
| 177 | + |
| 178 | + public record PathItemInfo(String path, JsonNode operation, String method) {} |
| 179 | +} |
0 commit comments