Skip to content

Commit ef4c8d8

Browse files
committed
Bind operations with absolute and relative path
1 parent 6bc41c7 commit ef4c8d8

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/main/java/com/apiflows/parser/source/OperationBinder.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.apiflows.model.SourceDescription;
55
import com.apiflows.model.Step;
66
import com.apiflows.model.Workflow;
7+
import com.apiflows.parser.util.HttpUtil;
78
import io.swagger.v3.oas.models.OpenAPI;
89
import io.swagger.v3.oas.models.Operation;
910
import io.swagger.v3.oas.models.PathItem;
@@ -26,12 +27,18 @@ public class OperationBinder {
2627
* Binds workflow operations
2728
* @param openAPIWorkflow
2829
*/
29-
public void bind(OpenAPIWorkflow openAPIWorkflow) {
30+
public void bind(OpenAPIWorkflow openAPIWorkflow, String location) {
3031
List<Operation> operations = new ArrayList<>();
3132

3233
for(SourceDescription source : openAPIWorkflow.getSourceDescriptions()) {
33-
String filename = getRootFolder(openAPIWorkflow.getLocation()) + "/" + source.getUrl();
34-
operations.addAll(getOperations(filename));
34+
if(new HttpUtil().isUrl(source.getUrl())) {
35+
// absolute url
36+
operations.addAll(getOperations(source.getUrl()));
37+
} else {
38+
// relative path
39+
String filename = getRootFolder(location) + "/" + source.getUrl();
40+
operations.addAll(getOperations(filename));
41+
}
3542
}
3643

3744
for(Workflow workflow : openAPIWorkflow.getWorkflows()) {
@@ -52,15 +59,27 @@ List<Operation> getOperations(String openapi) {
5259
ParseOptions options = new ParseOptions();
5360
options.setResolve(true);
5461

55-
SwaggerParseResult parseResult = openApiParser.readLocation(openapi, null, options);
62+
SwaggerParseResult parseResult = null;
63+
64+
try {
65+
parseResult = openApiParser.readLocation(openapi, null, options);
66+
} catch (Exception e) {
67+
LOGGER.error("Cannot find or parse source description: " + openapi, e);
68+
throw new RuntimeException("Cannot find or parse source description: " + openapi);
69+
}
70+
71+
if(parseResult == null || parseResult.getOpenAPI() == null) {
72+
LOGGER.error("Cannot find or parse source description: " + openapi);
73+
throw new RuntimeException("Cannot parse source description: " + openapi);
74+
}
75+
5676
OpenAPI openAPI = parseResult.getOpenAPI();
5777

5878
for(PathItem pathItem : openAPI.getPaths().values()) {
5979
operations.addAll(pathItem.readOperations());
6080
}
6181

6282
return operations;
63-
6483
}
6584

6685
Operation findOperationById(String operationId, List<Operation> operations) {
@@ -123,7 +142,7 @@ String getRootFolder(String location) {
123142
} else {
124143
Path filePath = Paths.get(location);
125144

126-
return filePath.getParent().toString();
145+
return (filePath.getParent() != null ? filePath.getParent().toString() : null);
127146
}
128147
}
129148

src/test/java/com/apiflows/parser/source/OperationBinderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void bind() {
3939

4040
OpenAPIWorkflowParserResult result = new OpenAPIWorkflowParser().parse(WORKFLOWS_SPEC_FILE);
4141

42-
binder.bind(result.getOpenAPIWorkflow());
42+
binder.bind(result.getOpenAPIWorkflow(), WORKFLOWS_SPEC_FILE);
4343

4444
assertNotNull(result);
4545
Workflow workflowApplyCoupon = result.getOpenAPIWorkflow().getWorkflows().get(0);
@@ -62,7 +62,7 @@ void bindFromUrl() {
6262

6363
OpenAPIWorkflowParserResult result = new OpenAPIWorkflowParser().parse(WORKFLOWS_SPEC_FILE);
6464

65-
binder.bind(result.getOpenAPIWorkflow());
65+
binder.bind(result.getOpenAPIWorkflow(), WORKFLOWS_SPEC_FILE);
6666

6767
assertNotNull(result);
6868
Workflow workflowApplyCoupon = result.getOpenAPIWorkflow().getWorkflows().get(0);

0 commit comments

Comments
 (0)