-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAPIWorkflowValidator.java
More file actions
597 lines (458 loc) · 21.5 KB
/
OpenAPIWorkflowValidator.java
File metadata and controls
597 lines (458 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package com.apiflows.parser;
import com.apiflows.model.*;
import com.fasterxml.jackson.core.JsonPointer;
import io.swagger.v3.oas.models.media.Schema;
import java.util.*;
import java.util.regex.Pattern;
public class OpenAPIWorkflowValidator {
private OpenAPIWorkflow openAPIWorkflow = null;
Set<String> workflowIds = new HashSet<>();
Map<String, Set<String>> stepIds = new HashMap<>();
Set<String> operationIds = new HashSet<>();
Set<String> componentIds = new HashSet<>();
Components components = null;
OpenAPIWorkflowValidator() {
}
public OpenAPIWorkflowValidator(OpenAPIWorkflow openAPIWorkflow) {
this.openAPIWorkflow = openAPIWorkflow;
}
public OpenAPIWorkflowValidatorResult validate() {
if(this.openAPIWorkflow == null) {
throw new RuntimeException("OpenAPIWorkflow is not provided");
}
loadWorkflowIds(this.openAPIWorkflow);
loadStepIds(this.openAPIWorkflow.getWorkflows());
loadOperationIds(this.openAPIWorkflow);
loadComponents(this.openAPIWorkflow.getComponents());
OpenAPIWorkflowValidatorResult result = new OpenAPIWorkflowValidatorResult();
if (openAPIWorkflow.getArazzo() == null || openAPIWorkflow.getArazzo().isEmpty()) {
result.addError("'arazzo' is undefined");
}
// Info
result.addErrors(validateInfo(openAPIWorkflow.getInfo()));
// SourceDescriptions
result.addErrors(validateSourceDescriptions(openAPIWorkflow.getSourceDescriptions()));
// Workflows
if (openAPIWorkflow.getWorkflows() == null || openAPIWorkflow.getWorkflows().isEmpty()) {
result.addError("'Workflows' is undefined");
}
if (openAPIWorkflow.getWorkflows() != null) {
for (Workflow workflow : openAPIWorkflow.getWorkflows()) {
int i = 0;
result.addErrors(validateWorkflow(workflow, i));
for (Step step : workflow.getSteps()) {
result.addErrors(validateStep(step, workflow.getWorkflowId()));
}
}
}
// Components
result.addErrors(validateComponents(openAPIWorkflow.getComponents()));
if(!result.getErrors().isEmpty()) {
result.setValid(false);
}
return result;
}
List<String> validateInfo(Info info) {
List<String> errors = new ArrayList<>();
if (info == null) {
errors.add("'Info' is undefined");
}
if (info != null && (info.getTitle() == null || info.getTitle().isEmpty())) {
errors.add("'Info title' is undefined");
}
if (info != null && (info.getVersion() == null || info.getVersion().isEmpty())) {
errors.add("'Info version' is undefined");
}
return errors;
}
List<String> validateSourceDescriptions(List<SourceDescription> sourceDescriptions) {
List<String> SUPPORTED_TYPES = Arrays.asList("openapi", "arazzo");
List<String> errors = new ArrayList<>();
if (sourceDescriptions == null) {
errors.add("'SourceDescriptions' is undefined");
}
if (sourceDescriptions != null) {
int i = 0;
for (SourceDescription sourceDescription : sourceDescriptions) {
if (sourceDescription.getName() == null || sourceDescription.getName().isEmpty()) {
errors.add("'SourceDescription[" + i + "] name' is undefined");
}
if (sourceDescription.getUrl() == null || sourceDescription.getUrl().isEmpty()) {
errors.add("'SourceDescription[" + i + "] url' is undefined");
}
if (sourceDescription.getType() != null) {
if(!SUPPORTED_TYPES.contains(sourceDescription.getType())) {
errors.add("'SourceDescription[" + i + "] type' is invalid");
}
}
i++;
}
if(i == 0) {
errors.add("'SourceDescriptions' is empty");
}
}
return errors;
}
List<String> validateWorkflow(Workflow workflow, int index ){
List<String> errors = new ArrayList<>();
if (workflow.getWorkflowId() == null || workflow.getWorkflowId().isEmpty()) {
errors.add("'Workflow[" + index + "] workflowId' is undefined");
}
if (workflow.getWorkflowId() != null && !isValidWorkflowId(workflow.getWorkflowId())) {
errors.add("WorkflowId " + workflow.getWorkflowId() + " format is invalid (should match regex " + getWorkflowIdRegularExpression() + ")");
}
if (workflow.getSteps() == null || workflow.getSteps().isEmpty()) {
errors.add("'Workflow " + workflow.getWorkflowId() + "' no Steps are undefined");
}
for (String key : workflow.getOutputs().keySet()) {
if(!isValidOutputsKey(key)) {
errors.add("Workflow[" + workflow.getWorkflowId() + "] Outputs key is invalid (should match regex " + getOutputsKeyRegularExpression() + ")");
}
}
return errors;
}
List<String> validateStep(Step step, String workflowId ) {
List<String> errors = new ArrayList<>();
String stepId = step.getStepId();
if (stepId == null || stepId.isEmpty()) {
errors.add("'Workflow[" + workflowId + "] stepId' is undefined");
}
if (stepId != null && !isValidStepId(stepId)) {
errors.add("'Step " + stepId + " is invalid (should match regex " + getStepIdRegularExpression() + ")");
}
int numAssignedValues = (step.getOperationId() != null ? 1 : 0) +
(step.getWorkflowId() != null ? 1 : 0) +
(step.getOperationPath() != null ? 1 : 0);
if (numAssignedValues != 1) {
if(stepId != null) {
errors.add("'Step " + stepId + " should provide only one of the following: [operationId, operationPath, workflowId]");
} else {
errors.add("'Workflow[" + workflowId + "]' should provide only one of the following: [operationId, operationPath, workflowId]");
}
}
if(step.getParameters() != null) {
for(Parameter parameter : step.getParameters()) {
if(isRuntimeExpression(parameter.getReference())) {
// reference a reusable object
errors.addAll(validateReusableParameter(parameter, workflowId, null));
} else {
// parameter
errors.addAll(validateParameter(parameter, workflowId, null));
if(step.getWorkflowId() == null) {
// when the step in context is NOT a workflowId the parameter IN must be defined
if(!isRuntimeExpression(parameter.getName()) && parameter.getIn() == null) {
errors.add("'Workflow[" + workflowId + "]' parameter IN must be defined");
}
}
}
}
}
if(step.getDependsOn() != null) {
if(!stepExists(workflowId, step.getDependsOn())) {
errors.add("'Step " + stepId + " 'dependsOn' is invalid (no such a step exists)");
}
if(step.getDependsOn().equals(stepId)) {
errors.add("'Step " + stepId + " 'dependsOn' is invalid (same value as stepId)");
}
}
for(Criterion criterion : step.getSuccessCriteria()) {
errors.addAll(validateCriterion(criterion, stepId));
}
for(SuccessAction successAction: step.getOnSuccess()) {
errors.addAll(validateSuccessAction(workflowId, stepId, successAction));
}
for(FailureAction failureAction : step.getOnFailure()) {
errors.addAll(validateFailureAction(workflowId, stepId, failureAction));
}
return errors;
}
List<String> validateParameter(Parameter parameter, String workflowId, String componentName) {
List<String> SUPPORTED_VALUES = Arrays.asList("path", "query", "header", "cookie");
String source;
if (workflowId != null) {
source = "Workflow[" + workflowId + "]";
} else {
source = "Component[" + componentName + "]";
}
List<String> errors = new ArrayList<>();
// Parameter object
String name = parameter.getName();
if (name == null) {
errors.add(source + " parameter has no name");
}
if (parameter.getIn() != null) {
if (!SUPPORTED_VALUES.contains(parameter.getIn())) {
if (name != null) {
errors.add(source + "parameter " + name + " in (" + parameter.getIn() + ") is invalid");
} else {
errors.add(source + " parameter in (" + parameter.getIn() + ") is invalid");
}
}
}
if (parameter.getValue() == null) {
if (name != null) {
errors.add(source + " parameter " + name + " has no value");
} else {
errors.add(source + " parameter has no value");
}
}
if(isRuntimeExpression(parameter.getName())) {
errors.add(source + " parameter " + name + " is a Reusable Parameter object");
}
return errors;
}
List<String> validateReusableParameter(Parameter parameter, String workflowId, String componentName ) {
String source;
if(workflowId != null) {
source = "Workflow[" + workflowId + "]";
} else {
source = "Component[" + componentName + "]";
}
// reference to reusable object
String reference = parameter.getReference();
// normalize reference
String key = reference.replace("$components.parameters.", "");
List<String> errors = new ArrayList<>();
// check reusable parameter exists in Components
if(!this.components.getParameters().containsKey(key)) {
errors.add(source + " parameter '" + reference + "' not found");
}
return errors;
}
List<String> validateSuccessAction(String workflowId, String stepId, SuccessAction successAction) {
List<String> SUPPORTED_VALUES = Arrays.asList("end", "goto");
List<String> errors = new ArrayList<>();
if (successAction.getType() == null) {
errors.add("Step " + stepId + " SuccessAction has no type");
}
if (successAction.getType() != null) {
if (!SUPPORTED_VALUES.contains(successAction.getType())) {
errors.add("Step " + stepId + " SuccessAction type (" + successAction.getType() + ") is invalid");
}
}
if("goto".equals(successAction.getType())) {
errors.addAll(validateGotoTarget(stepId, "SuccessAction", successAction.getStepId(), successAction.getWorkflowId()));
if(successAction.getStepId() != null) {
if (!stepExists(workflowId, successAction.getStepId())) {
errors.add("Step " + stepId + " SuccessAction stepId is invalid (no such a step exists)");
}
}
if(successAction.getWorkflowId() != null) {
if(!workflowExists(successAction.getWorkflowId())) {
errors.add("Step " + stepId + " SuccessAction workflowId is invalid (no such a workflow exists)");
}
}
}
return errors;
}
List<String> validateFailureAction(String workflowId, String stepId, FailureAction failureAction) {
List<String> SUPPORTED_VALUES = Arrays.asList("end", "retry", "goto");
List<String> errors = new ArrayList<>();
if (failureAction.getType() == null) {
errors.add("Step " + stepId + " FailureAction has no type");
}
if (failureAction.getType() != null) {
if (!SUPPORTED_VALUES.contains(failureAction.getType())) {
errors.add("Step " + stepId + " FailureAction type (" + failureAction.getType() + ") is invalid");
}
}
if("goto".equals(failureAction.getType())) {
errors.addAll(validateGotoTarget(stepId, "FailureAction", failureAction.getStepId(), failureAction.getWorkflowId()));
}
if(failureAction.getRetryAfter() != null && failureAction.getRetryAfter() < 0) {
errors.add("Step " + stepId + " FailureAction retryAfter must be non-negative");
}
if(failureAction.getRetryLimit() != null && failureAction.getRetryLimit() < 0) {
errors.add("Step " + stepId + " FailureAction retryLimit must be non-negative");
}
if(failureAction.getStepId() != null && failureAction.getType() != null
&& (failureAction.getType().equals("goto") || failureAction.getType().equals("retry"))) {
// when type `goto` or `retry` stepId must exist (if provided)
if (!stepExists(workflowId, failureAction.getStepId())) {
errors.add("Step " + stepId + " FailureAction stepId is invalid (no such a step exists)");
}
}
return errors;
}
List<String> validateCriterion(Criterion criterion, String stepId) {
List<String> SUPPORTED_VALUES = Arrays.asList("simple", "regex", "jsonpath", "xpath");
List<String> errors = new ArrayList<>();
if(criterion.getCondition() == null) {
errors.add("Step " + stepId + " has no condition");
}
if (criterion.getType() != null && !SUPPORTED_VALUES.contains(criterion.getType())) {
errors.add("Step " + stepId + " SuccessCriteria type (" + criterion.getType() + ") is invalid");
}
if (criterion.getType() != null && criterion.getContext() == null) {
errors.add("Step " + stepId + " SuccessCriteria type is specified but context is not provided");
}
return errors;
}
List<String> validateComponents(Components components) {
List<String> errors = new ArrayList<>();
if(components != null) {
if (components.getParameters() != null) {
for(String key : components.getParameters().keySet()) {
if(!isValidComponentKey(key)) {
errors.add("'Component parameter name " + key + " is invalid (should match regex " + getComponentKeyRegularExpression() + ")");
}
}
components.getParameters().entrySet().stream()
.forEach(entry -> errors.addAll(validateParameter(entry.getValue(), null, entry.getKey())));
}
if (components.getInputs() != null) {
for(String key : components.getInputs().keySet()) {
if(!isValidComponentKey(key)) {
errors.add("'Component input " + key + " is invalid (should match regex " + getComponentKeyRegularExpression() + ")");
}
}
}
}
return errors;
}
boolean isValidWorkflowId(String workflowId) {
return Pattern.matches(getWorkflowIdRegularExpression(), workflowId);
}
boolean isValidStepId(String stepId) {
return Pattern.matches(getStepIdRegularExpression(), stepId);
}
boolean isValidComponentKey(String key) {
return Pattern.matches(getComponentKeyRegularExpression(), key);
}
String getStepIdRegularExpression() {
return "[A-Za-z0-9_\\-]+";
}
String getWorkflowIdRegularExpression() {
return "[A-Za-z0-9_\\-]+";
}
String getComponentKeyRegularExpression() {
return "^[a-zA-Z0-9\\.\\-_]+$";
}
boolean isValidOutputsKey(String key) {
return Pattern.matches(getOutputsKeyRegularExpression(), key);
}
String getOutputsKeyRegularExpression() {
return "^[a-zA-Z0-9\\.\\-_]+$";
}
List<String> loadWorkflowIds(OpenAPIWorkflow openAPIWorkflow) {
List<String> errors = new ArrayList<>();
boolean multipleSpecs = getNumArazzoTypeSourceDescriptions(openAPIWorkflow.getSourceDescriptions()) > 1 ? true : false;
if(openAPIWorkflow.getWorkflows() != null) {
validateWorkflowIdsUniqueness(openAPIWorkflow.getWorkflows());
for (Workflow workflow : openAPIWorkflow.getWorkflows()) {
errors.addAll(validateStepsWorkflowIds(workflow.getSteps(), multipleSpecs));
}
for (Workflow workflow : openAPIWorkflow.getWorkflows()) {
this.workflowIds.add(workflow.getWorkflowId());
}
}
return errors;
}
List<String> loadStepIds(List<Workflow> workflows) {
List<String> errors = new ArrayList<>();
if(workflows != null) {
for(Workflow workflow : workflows) {
Set<String> steps = this.stepIds.get(workflow.getWorkflowId());
if(steps == null) {
steps = new HashSet<>();
}
for(Step step : workflow.getSteps()) {
if(!steps.add(step.getStepId())) {
// id already exists
errors.add("StepId is not unique: " + step.getStepId());
}
}
this.stepIds.put(workflow.getWorkflowId(), steps);
}
}
return errors;
}
List<String> loadOperationIds(OpenAPIWorkflow openAPIWorkflow) {
List<String> errors = new ArrayList<>();
boolean multipleOpenApiFiles = getNumOpenApiSourceDescriptions(openAPIWorkflow.getSourceDescriptions()) > 1 ? true : false;
for(Workflow workflow : openAPIWorkflow.getWorkflows()) {
errors.addAll(validateStepsOperationIds(workflow.getSteps(), multipleOpenApiFiles));
for(Step step : workflow.getSteps()) {
if(step.getOperationId() != null) {
this.operationIds.add(step.getOperationId());
}
}
}
return errors;
}
void loadComponents(Components components) {
this.components = components;
}
public List<String> validateStepsOperationIds(List<Step> steps, boolean multipleOpenApiFiles) {
List<String> errors = new ArrayList<>();
for(Step step : steps) {
if(multipleOpenApiFiles) {
// must use runtime expression to map applicable SourceDescription
if(step.getOperationId() != null && !step.getOperationId().startsWith("$sourceDescriptions.")) {
errors.add("Operation " + step.getOperationId() + " must be specified using a runtime expression (e.g., $sourceDescriptions.<name>.<operationId>)");
}
}
}
return errors;
}
// num of SourceDescriptions with type 'openapi'
int getNumOpenApiSourceDescriptions(List<SourceDescription> sourceDescriptions) {
return (int) sourceDescriptions.stream().filter(p -> p.isOpenApi()).count();
}
// num of SourceDescriptions with type 'arazzo'
int getNumArazzoTypeSourceDescriptions(List<SourceDescription> sourceDescriptions) {
return (int) sourceDescriptions.stream().filter(p -> p.isArazzo()).count();
}
boolean stepExists(String workflowId, String stepId) {
return this.stepIds.get(workflowId) != null && this.stepIds.get(workflowId).contains(stepId);
}
boolean workflowExists(String workflowId) {
return this.workflowIds.stream().anyMatch(p -> p.contains(workflowId));
}
List<String> validateWorkflowIdsUniqueness(List<Workflow> workflows) {
List<String> errors = new ArrayList<>();
Set<String> ids = new HashSet<>();
for(Workflow workflow : workflows) {
if(!ids.add(workflow.getWorkflowId())) {
// id already exists
errors.add("WorkflowId is not unique: " + workflow.getWorkflowId());
}
}
return errors;
}
List<String> validateStepsWorkflowIds(List<Step> steps, boolean multipleArazzoTypeFiles) {
List<String> errors = new ArrayList<>();
for(Step step : steps) {
if(multipleArazzoTypeFiles) {
// must use runtime expression to map applicable SourceDescription
if(step.getWorkflowId() != null && !step.getWorkflowId().startsWith("$sourceDescriptions.")) {
errors.add("Operation " + step.getWorkflowId() + " must be specified using a runtime expression (e.g., $sourceDescriptions.<name>.<workflowId>)");
}
}
}
return errors;
}
public boolean isValidJsonPointer(String jsonPointerString) {
boolean ret;
try {
JsonPointer jsonPointer = JsonPointer.compile(jsonPointerString);
ret = true;
} catch (IllegalArgumentException e) {
ret = false;
}
return ret;
}
boolean isRuntimeExpression(String name) {
return name != null && name.startsWith("$");
}
private List<String> validateGotoTarget(String stepId, String actionLabel, String targetStepId, String targetWorkflowId) {
List<String> errors = new ArrayList<>();
if (targetWorkflowId == null && targetStepId == null) {
errors.add("Step " + stepId + " " + actionLabel + " must define either workflowId or stepId");
}
if (targetWorkflowId != null && targetStepId != null) {
errors.add("Step " + stepId + " " + actionLabel + " cannot define both workflowId and stepId");
}
return errors;
}
}