Skip to content

Commit 97ae851

Browse files
committed
Load operationIds
1 parent 7853e8f commit 97ae851

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/main/java/com/apiflows/parser/OpenAPIWorkflowValidator.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class OpenAPIWorkflowValidator {
1212
private OpenAPIWorkflow openAPIWorkflow = null;
1313
Set<String> workflowIds = new HashSet<>();
1414
Map<String, Set<String>> stepIds = new HashMap<>();
15+
Set<String> operationIds = new HashSet<>();
1516
Set<Schema> components = new HashSet<>();
1617

1718
OpenAPIWorkflowValidator() {
@@ -27,8 +28,9 @@ public OpenAPIWorkflowValidatorResult validate() {
2728
throw new RuntimeException("OpenAPIWorkflow is not provided");
2829
}
2930

30-
loadWorkflowIds(this.openAPIWorkflow.getWorkflows());
31+
loadWorkflowIds(this.openAPIWorkflow);
3132
loadStepIds(this.openAPIWorkflow.getWorkflows());
33+
loadOperationIds(this.openAPIWorkflow);
3234

3335
OpenAPIWorkflowValidatorResult result = new OpenAPIWorkflowValidatorResult();
3436

@@ -440,6 +442,49 @@ List<String> loadStepIds(List<Workflow> workflows) {
440442
return errors;
441443
}
442444

445+
List<String> loadOperationIds(OpenAPIWorkflow openAPIWorkflow) {
446+
List<String> errors = new ArrayList<>();
447+
448+
boolean multipleOpenApiFiles = getNumOpenApiSourceDescriptions(openAPIWorkflow.getSourceDescriptions()) > 1 ? true : false;
449+
450+
for(Workflow workflow : openAPIWorkflow.getWorkflows()) {
451+
errors.addAll(validateStepsOperationIds(workflow.getSteps(), multipleOpenApiFiles));
452+
453+
for(Step step : workflow.getSteps()) {
454+
if(step.getOperationId() != null) {
455+
this.operationIds.add(step.getOperationId());
456+
}
457+
}
458+
}
459+
460+
return errors;
461+
}
462+
463+
public List<String> validateStepsOperationIds(List<Step> steps, boolean multipleOpenApiFiles) {
464+
List<String> errors = new ArrayList<>();
465+
466+
for(Step step : steps) {
467+
if(multipleOpenApiFiles) {
468+
// must use runtime expression to map applicable SourceDescription
469+
if(step.getOperationId() != null && !step.getOperationId().startsWith("$sourceDescriptions.")) {
470+
errors.add("Operation " + step.getOperationId() + " must be specified using a runtime expression (e.g., $sourceDescriptions.<name>.<operationId>)");
471+
}
472+
}
473+
}
474+
475+
return errors;
476+
}
477+
478+
// num of SourceDescriptions with type 'openapi'
479+
int getNumOpenApiSourceDescriptions(List<SourceDescription> sourceDescriptions) {
480+
return (int) sourceDescriptions.stream().filter(p -> p.isOpenApi()).count();
481+
}
482+
483+
// num of SourceDescriptions with type 'workflowsSpec'
484+
int getNumWorkflowsSpecSourceDescriptions(List<SourceDescription> sourceDescriptions) {
485+
return (int) sourceDescriptions.stream().filter(p -> p.isWorkflowsSpec()).count();
486+
}
487+
443488
boolean stepExists(String workflowId, String stepId) {
444489
return this.stepIds.get(workflowId) != null && this.stepIds.get(workflowId).contains(stepId);
445490
}

0 commit comments

Comments
 (0)