diff --git a/element-template-generator/openapi-parser/pom.xml b/element-template-generator/openapi-parser/pom.xml index fa2161c35e..2f0c581ba4 100644 --- a/element-template-generator/openapi-parser/pom.xml +++ b/element-template-generator/openapi-parser/pom.xml @@ -29,6 +29,10 @@ swagger-parser ${version.swagger-parser} + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + org.junit.jupiter diff --git a/element-template-generator/openapi-parser/src/main/java/io/camunda/connector/generator/openapi/OpenApiGenerationSource.java b/element-template-generator/openapi-parser/src/main/java/io/camunda/connector/generator/openapi/OpenApiGenerationSource.java index 44225b7374..532e942b0f 100644 --- a/element-template-generator/openapi-parser/src/main/java/io/camunda/connector/generator/openapi/OpenApiGenerationSource.java +++ b/element-template-generator/openapi-parser/src/main/java/io/camunda/connector/generator/openapi/OpenApiGenerationSource.java @@ -17,6 +17,7 @@ package io.camunda.connector.generator.openapi; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.parser.OpenAPIV3Parser; import java.io.IOException; @@ -53,7 +54,7 @@ private static OpenAPI fetchOpenApi(List cliParams) { var openApiPathOrContent = cliParams.get(0); var openApiParser = new OpenAPIV3Parser(); try { - if (isValidJSON(openApiPathOrContent)) { + if (isValidJSON(openApiPathOrContent) || isValidYAML(openApiPathOrContent)) { return openApiParser.readContents(openApiPathOrContent).getOpenAPI(); } return openApiParser.read(openApiPathOrContent); @@ -76,6 +77,16 @@ public static boolean isValidJSON(String jsonInString) { } } + public static boolean isValidYAML(String yamlString) { + try { + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + mapper.readTree(yamlString); + return yamlString.contains("\n"); + } catch (IOException e) { + return false; + } + } + private static Set extractOperationIds(List cliParams) { var cliParamsWithoutOptions = cliParams.stream().filter(param -> !param.startsWith("--")).toList(); diff --git a/element-template-generator/openapi-parser/src/test/java/io/camunda/connector/generator/openapi/ExampleTest.java b/element-template-generator/openapi-parser/src/test/java/io/camunda/connector/generator/openapi/ExampleTest.java index 2d542ce018..8c09d0d246 100644 --- a/element-template-generator/openapi-parser/src/test/java/io/camunda/connector/generator/openapi/ExampleTest.java +++ b/element-template-generator/openapi-parser/src/test/java/io/camunda/connector/generator/openapi/ExampleTest.java @@ -68,6 +68,27 @@ void generateFromRawJsonContent() throws IOException { } } + @Test + void generateFromRawYamlContent() { + // given + try (var openApiYamlContent = new FileInputStream("src/test/resources/example.yaml")) { + byte[] b = new byte[openApiYamlContent.available()]; + if (openApiYamlContent.read(b) == -1) { + throw new RuntimeException("Failed to read yaml file!"); + } + var source = new OpenApiGenerationSource(List.of(new String(b))); + var generator = new OpenApiOutboundTemplateGenerator(); + + // when + var templates = generator.generate(source); + + // then + System.out.println(mapper.writeValueAsString(templates)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + @Test void scan() { var parser = new OpenAPIV3Parser(); @@ -96,4 +117,22 @@ void scanFromRawJsonContent() { throw new RuntimeException(e); } } + + @Test + void scanFromRawYamlContent() { + try (var openApiYamlContent = new FileInputStream("src/test/resources/example.yaml")) { + byte[] b = new byte[openApiYamlContent.available()]; + if (openApiYamlContent.read(b) == -1) { + throw new RuntimeException("Failed to read yaml file!"); + } + var source = new OpenApiGenerationSource(List.of(new String(b))); + var generator = new OpenApiOutboundTemplateGenerator(); + + var scanResult = generator.scan(source); + + System.out.println(scanResult); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/element-template-generator/openapi-parser/src/test/resources/example.yaml b/element-template-generator/openapi-parser/src/test/resources/example.yaml new file mode 100644 index 0000000000..9a21f91230 --- /dev/null +++ b/element-template-generator/openapi-parser/src/test/resources/example.yaml @@ -0,0 +1,24 @@ +openapi: 3.0.0 +info: + title: Sample API + description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML. + version: 0.1.9 +servers: + - url: http://api.example.com/v1 + description: Optional server description, e.g. Main (production) server + - url: http://staging-api.example.com + description: Optional server description, e.g. Internal staging server for testing +paths: + /users: + get: + summary: Returns a list of users. + description: Optional extended description in CommonMark or HTML. + responses: + '200': # status code + description: A JSON array of user names + content: + application/json: + schema: + type: array + items: + type: string diff --git a/element-template-generator/postman-collections-parser/pom.xml b/element-template-generator/postman-collections-parser/pom.xml index 99448871ac..4729903dd5 100644 --- a/element-template-generator/postman-collections-parser/pom.xml +++ b/element-template-generator/postman-collections-parser/pom.xml @@ -19,6 +19,10 @@ io.camunda.connector element-template-generator-http-dsl + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + org.junit.jupiter diff --git a/element-template-generator/postman-collections-parser/src/main/java/io/camunda/connector/generator/postman/PostmanCollectionsGenerationSource.java b/element-template-generator/postman-collections-parser/src/main/java/io/camunda/connector/generator/postman/PostmanCollectionsGenerationSource.java index 3d2050ca6e..277de82a9c 100644 --- a/element-template-generator/postman-collections-parser/src/main/java/io/camunda/connector/generator/postman/PostmanCollectionsGenerationSource.java +++ b/element-template-generator/postman-collections-parser/src/main/java/io/camunda/connector/generator/postman/PostmanCollectionsGenerationSource.java @@ -17,6 +17,8 @@ package io.camunda.connector.generator.postman; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.camunda.connector.generator.postman.model.PostmanCollectionV210; import io.camunda.connector.generator.postman.utils.ObjectMapperProvider; import java.io.File; @@ -59,6 +61,10 @@ private static PostmanCollectionV210 fetchPostmanCollection(List cliPara throw new IllegalArgumentException( "Couldn't parse Postman Collection to v.2.1.0 standard", e); } + } else if (isValidYAML(pathOrContent)) { + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + mapper.findAndRegisterModules(); + return mapper.readValue(pathOrContent, JsonNode.class); } final File postmanCollectionsFileJson; if (pathOrContent.startsWith("http")) { // Network @@ -121,6 +127,16 @@ public static boolean isValidJSON(String jsonInString) { } } + public static boolean isValidYAML(String yamlString) { + try { + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + mapper.readTree(yamlString); + return yamlString.contains("\n"); + } catch (IOException e) { + return false; + } + } + // 0th element is Postman Collections file; the 1st..Nth - operations private static Set extractOperationIds(List cliParams) { if (cliParams.size() == 1) { diff --git a/element-template-generator/postman-collections-parser/src/test/java/io/camunda/connector/generator/postman/PostmanCollectionsGeneratorDryRunExampleTest.java b/element-template-generator/postman-collections-parser/src/test/java/io/camunda/connector/generator/postman/PostmanCollectionsGeneratorDryRunExampleTest.java index f648746cc5..bf627bec86 100644 --- a/element-template-generator/postman-collections-parser/src/test/java/io/camunda/connector/generator/postman/PostmanCollectionsGeneratorDryRunExampleTest.java +++ b/element-template-generator/postman-collections-parser/src/test/java/io/camunda/connector/generator/postman/PostmanCollectionsGeneratorDryRunExampleTest.java @@ -65,38 +65,58 @@ void scan(List args) { } private static Stream commandLineArguments() { + String postmanCollectionsJsonContent = getJsonContent(); + String postmanCollectionsYamlContent = getYamlContent(); + + return Stream.of( + // Test case 1: generate all methods + Arguments.of(List.of("src/test/resources/operate-api-saas-bearer.json")), + + // Test case 2: generate specific methods + Arguments.of( + List.of( + "src/test/resources/postman-books.json", + "/1. Sending requests & inspecting responses/books", + "/1. Sending requests & inspecting responses/book", + "/1. Sending requests & inspecting responses/add book")), + + // Test case 3: fetch from internet + Arguments.of( + List.of( + "https://raw.githubusercontent.com/camunda-community-hub/camunda-8-api-postman-collection/main/Operate%20Public%20API%20-%20SaaS.postman_collection.json")), + + // Test case 4: from internet with operations + Arguments.of( + List.of( + "https://raw.githubusercontent.com/camunda-community-hub/camunda-8-api-postman-collection/main/Operate%20Public%20API%20-%20SaaS.postman_collection.json", + "/Process instances/Search for process instances", + "/Process instances/Get process instance by key")), + + // Test case 5: from raw json content + Arguments.of(List.of(postmanCollectionsJsonContent)), + + // Test case 6: from raw yaml content + Arguments.of(List.of(postmanCollectionsYamlContent))); + } + + private static String getJsonContent() { try (var openApiJsonContent = new FileInputStream("src/test/resources/operate-api-saas-bearer.json")) { - String postmanCollectionsJsonContent = - ObjectMapperProvider.getInstance() - .readValue(openApiJsonContent, JsonNode.class) - .toString(); - return Stream.of( - // Test case 1: generate all methods - Arguments.of(List.of("src/test/resources/operate-api-saas-bearer.json")), - - // Test case 2: generate specific methods - Arguments.of( - List.of( - "src/test/resources/postman-books.json", - "/1. Sending requests & inspecting responses/books", - "/1. Sending requests & inspecting responses/book", - "/1. Sending requests & inspecting responses/add book")), - - // Test case 3: fetch from internet - Arguments.of( - List.of( - "https://raw.githubusercontent.com/camunda-community-hub/camunda-8-api-postman-collection/main/Operate%20Public%20API%20-%20SaaS.postman_collection.json")), - - // Test case 4: from internet with operations - Arguments.of( - List.of( - "https://raw.githubusercontent.com/camunda-community-hub/camunda-8-api-postman-collection/main/Operate%20Public%20API%20-%20SaaS.postman_collection.json", - "/Process instances/Search for process instances", - "/Process instances/Get process instance by key")), - - // Test case 5: from raw content - Arguments.of(List.of(postmanCollectionsJsonContent))); + return ObjectMapperProvider.getInstance() + .readValue(openApiJsonContent, JsonNode.class) + .toString(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static String getYamlContent() { + try (var openApiYamlContent = new FileInputStream("src/test/resources/example.yaml")) { + byte[] b = new byte[openApiYamlContent.available()]; + if (openApiYamlContent.read(b) == -1) { + throw new RuntimeException("Failed to read yaml file!"); + } + return new String(b); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/element-template-generator/postman-collections-parser/src/test/resources/example.yaml b/element-template-generator/postman-collections-parser/src/test/resources/example.yaml new file mode 100644 index 0000000000..bdb17f2f0e --- /dev/null +++ b/element-template-generator/postman-collections-parser/src/test/resources/example.yaml @@ -0,0 +1,25 @@ +info: + _postman_id: 431257b5-df1f-4a84-863a-46d2aa3ae731 + name: TEST + schema: https://schema.getpostman.com/json/collection/v2.1.0/collection.json + _exporter_id: '25378986' +item: + - name: v1 + item: + - name: New Request + request: + method: GET + header: [] + response: [] + - name: v2 + item: + - name: New Request + request: + method: GET + header: [] + response: [] + - name: test + request: + method: GET + header: [] + response: []