Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that all node IDs are unique #191

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ dependencies {
configurations.all {
resolutionStrategy {
force("com.google.guava:guava:32.1.3-jre") // CVE for 31.1
force("org.eclipse.platform:org.eclipse.core.runtime:3.29.0") // CVE for 3.26.100
force("org.eclipse.platform:org.eclipse.core.runtime:3.29.0") // CVE for < 3.29.0
force("com.fasterxml.jackson.core:jackson-core:2.16.0") // Dependency Jar Hell
}
}
}
Expand Down Expand Up @@ -239,7 +240,7 @@ diffCoverageReport {
file.withOutputStream { out ->
exec {
ignoreExitValue true
commandLine 'git', 'diff', '--no-color', '--minimal', diffBase, '|| true'
commandLine 'git', 'diff', '--no-color', '--minimal', diffBase
standardOutput = out
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -158,14 +157,20 @@ private TimeValue parseTimeout(WorkflowNode node) {

private static List<WorkflowNode> topologicalSort(List<WorkflowNode> workflowNodes, List<WorkflowEdge> workflowEdges) {
// Basic validation
Set<String> nodeIds = workflowNodes.stream().map(n -> n.id()).collect(Collectors.toSet());
Map<String, WorkflowNode> nodeMap = new HashMap<>();
for (WorkflowNode node : workflowNodes) {
if (nodeMap.containsKey(node.id())) {
throw new FlowFrameworkException("Duplicate node id " + node.id() + ".", RestStatus.BAD_REQUEST);
}
nodeMap.put(node.id(), node);
}
for (WorkflowEdge edge : workflowEdges) {
String source = edge.source();
if (!nodeIds.contains(source)) {
if (!nodeMap.containsKey(source)) {
throw new FlowFrameworkException("Edge source " + source + " does not correspond to a node.", RestStatus.BAD_REQUEST);
}
String dest = edge.destination();
if (!nodeIds.contains(dest)) {
if (!nodeMap.containsKey(dest)) {
throw new FlowFrameworkException("Edge destination " + dest + " does not correspond to a node.", RestStatus.BAD_REQUEST);
}
if (source.equals(dest)) {
Expand All @@ -176,7 +181,6 @@ private static List<WorkflowNode> topologicalSort(List<WorkflowNode> workflowNod
// Build predecessor and successor maps
Map<WorkflowNode, Set<WorkflowEdge>> predecessorEdges = new HashMap<>();
Map<WorkflowNode, Set<WorkflowEdge>> successorEdges = new HashMap<>();
Map<String, WorkflowNode> nodeMap = workflowNodes.stream().collect(Collectors.toMap(WorkflowNode::id, Function.identity()));
for (WorkflowEdge edge : workflowEdges) {
WorkflowNode source = nodeMap.get(edge.source());
WorkflowNode dest = nodeMap.get(edge.destination());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ public void testExceptions() throws IOException {
);
assertEquals("Workflow step type [unimplemented_step] is not implemented.", ex.getMessage());
assertEquals(RestStatus.NOT_IMPLEMENTED, ((FlowFrameworkException) ex).getRestStatus());

ex = assertThrows(FlowFrameworkException.class, () -> parse(workflow(List.of(node("A"), node("A")), Collections.emptyList())));
assertEquals("Duplicate node id A.", ex.getMessage());
assertEquals(RestStatus.BAD_REQUEST, ((FlowFrameworkException) ex).getRestStatus());
}

public void testSuccessfulGraphValidation() throws Exception {
Expand Down
Loading