diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/generators/common/DocCommenTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/generators/common/DocCommenTests.java index e8dedb1ba..193be15b1 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/generators/common/DocCommenTests.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/generators/common/DocCommenTests.java @@ -1,15 +1,28 @@ package io.ballerina.openapi.generators.common; +import io.ballerina.openapi.core.generators.common.GeneratorUtils; +import org.testng.Assert; import org.testng.annotations.Test; public class DocCommenTests { @Test() public void testForFunctionReplaceContentWithinBrackets() { - // need ot improve regex to capture abc.json -// Assert.assertEquals("admin/api/ZZZ/customers/XXX/abc.json", + // todo : need ot improve regex to capture `.` in string ex: abc.json +// Assert.assertEquals("admin/api/ZZZ/customers/XXX/ZZZ", // GeneratorUtils.replaceContentWithinBrackets("admin/api/2021-10/customers/{customer_id}/abc.json", "XXX")); -// Assert.assertEquals("admin/api/ZZZ/customers/XXX/abc.json", -// GeneratorUtils.replaceContentWithinBrackets( +// Assert.assertEquals("admin/api/ZZZ/customers/XXX/ZZZ", +// GeneratorUtils.replaceContentWithinBrackets( // "admin/api/'2021\\-10/customers/[string customer_id]/abc\\.json", "XXX")); + + Assert.assertEquals("/user/ssh_signing_keys/XXX", GeneratorUtils.replaceContentWithinBrackets( + "/user/ssh_signing_keys/{ssh_signing_key_id}", "XXX")); + Assert.assertEquals("/user/gpg_keys/XXX", GeneratorUtils.replaceContentWithinBrackets( + "/user/gpg_keys/{gpg_key_id}", "XXX")); + + Assert.assertEquals(GeneratorUtils.replaceContentWithinBrackets("user/gpg_keys/[int gpg_key_id]", + "XXX"), "user/gpg_keys/XXX"); + Assert.assertEquals(GeneratorUtils.replaceContentWithinBrackets( + "user/ssh_signing_keys/[int ssh_signing_key_id]", "XXX"), + "user/ssh_signing_keys/XXX"); } } diff --git a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/GeneratorUtils.java b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/GeneratorUtils.java index 6a8c87cca..44bf83443 100644 --- a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/GeneratorUtils.java +++ b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/GeneratorUtils.java @@ -994,19 +994,26 @@ public static String replaceContentWithinBrackets(String input, String replaceme input = sb.toString(); if (!input.startsWith(".")) { //Pattern to ignore especial characters - //todo: need to improve the regex pattern to capture abc.json - Pattern pattern2 = Pattern.compile("/(?=[^/]*[^a-zA-Z0-9/])(.*?)(?=/)"); + Pattern pattern2 = Pattern.compile("/(?=[^/]*[^a-zA-Z0-9_/])(.*?)(?=/)"); Matcher matcher2 = pattern2.matcher(input); StringBuffer sb2 = new StringBuffer(); while (matcher2.find()) { - matcher2.appendReplacement(sb2, "/" + "ZZZ"); + matcher2.appendReplacement(sb2, "/ZZZ"); } matcher2.appendTail(sb2); + //todo: need to improve the regex pattern to capture abc.json +// input = sb2.toString(); +// Pattern pattern3 = Pattern.compile("\\..*?[^/]*"); +// Matcher matcher3 = pattern3.matcher(input); +// StringBuffer sb3 = new StringBuffer(); +// while (matcher3.find()) { +// matcher3.appendReplacement(sb3, "/ZZZ"); +// } +// matcher3.appendTail(sb3); return sb2.toString(); } else { return input; } - } public static TypeDescriptorNode generateStatusCodeTypeInclusionRecord(String code, ApiResponse response,