Skip to content

Commit 0d5f5bb

Browse files
committed
Refactor OpenAPIReader
1 parent ddd12c4 commit 0d5f5bb

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

Diff for: impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/OpenAPIExecutor.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private interface TargetSupplier {
4949
@Override
5050
public void init(CallOpenAPI task, WorkflowDefinition definition) {
5151
OpenAPIArguments args = task.getWith();
52-
this.targetSupplier = getTargetSupplier(args.getDocument().getEndpoint(), definition.expressionFactory(), args.getOperationId());
52+
this.targetSupplier = getTargetSupplier(args, definition.expressionFactory());
5353
}
5454

5555
@Override
@@ -84,8 +84,6 @@ private static TargetSupplier getURISupplier(UriTemplate template, String operat
8484
throw new IllegalArgumentException("Invalid OpenAPI specification. There is no operation ID " + operationId);
8585
}
8686

87-
88-
8987
return (w, t, n) -> client.target(host);
9088
} else if (template.getLiteralUriTemplate() != null) {
9189
return (w, t, n) ->
@@ -112,7 +110,11 @@ public WebTarget apply(WorkflowContext workflow, TaskContext<?> task, JsonNode n
112110
}
113111

114112
private static TargetSupplier getTargetSupplier(
115-
Endpoint endpoint, ExpressionFactory expressionFactory, String operationId) {
113+
OpenAPIArguments args, ExpressionFactory expressionFactory) {
114+
115+
Endpoint endpoint = args.getDocument().getEndpoint();
116+
String operationId = args.getOperationId();
117+
116118
if (endpoint.getEndpointConfiguration() != null) {
117119
EndpointUri uri = endpoint.getEndpointConfiguration().getUri();
118120
if (uri.getLiteralEndpointURI() != null) {

Diff for: impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/OpenAPIReader.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
import com.fasterxml.jackson.databind.node.ArrayNode;
55

66
import java.util.Map;
7+
import java.util.Optional;
8+
import java.util.Set;
79

810
public class OpenAPIReader {
911

12+
private static final String HTTPS = "https";
13+
private static final String HTTP = "http";
14+
private static final String DEFAULT_SCHEME = HTTPS;
15+
private static final Set<String> ALLOWED_SCHEMES = Set.of(HTTPS, HTTP);
16+
1017
public static String getHost(JsonNode jsonNode) {
1118
JsonNode host = jsonNode.get("host");
1219
if (host == null) {
@@ -19,23 +26,21 @@ public static String getHost(JsonNode jsonNode) {
1926
private static String getScheme(JsonNode jsonNode) {
2027
ArrayNode array = jsonNode.withArrayProperty("schemes");
2128
if (array != null && !array.isEmpty()) {
22-
// TODO: should get the first scheme?
23-
return array.get(0).asText();
29+
String firstScheme = array.get(0).asText();
30+
return ALLOWED_SCHEMES.contains(firstScheme) ? firstScheme : DEFAULT_SCHEME;
2431
}
25-
// TODO: should the http be the default scheme?
26-
return "http";
32+
return DEFAULT_SCHEME;
2733
}
2834

29-
public static JsonNode readOperation(JsonNode jsonNode, String operationId) {
35+
public static Optional<JsonNode> readOperation(JsonNode jsonNode, String operationId) {
3036
JsonNode paths = jsonNode.get("paths");
3137
for (Map.Entry<String, JsonNode> entry : paths.properties()) {
3238
for (Map.Entry<String, JsonNode> httpMethod : entry.getValue().properties()) {
3339
if (httpMethod.getValue().get("operationId").asText().equals(operationId)) {
34-
return httpMethod.getValue();
40+
return Optional.ofNullable(httpMethod.getValue());
3541
}
3642
}
3743
}
38-
39-
return null;
44+
return Optional.empty();
4045
}
4146
}

0 commit comments

Comments
 (0)