diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaHelidonCommonCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaHelidonCommonCodegen.java index b2b3d1a6df13..5808e1c00693 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaHelidonCommonCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaHelidonCommonCodegen.java @@ -37,6 +37,7 @@ import java.util.Map; import java.util.Scanner; import java.util.Set; +import java.util.StringJoiner; import java.util.TreeMap; import java.util.concurrent.locks.ReentrantLock; import java.util.prefs.BackingStoreException; @@ -352,7 +353,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List op.path) .map(path -> path.charAt(0) != '/' ? "/" + path : path ) @@ -450,6 +451,46 @@ protected String rootJavaEEPackage() { return rootJavaEEPackage; } + static String commonPathPrefix(String[] paths) { + + if (paths.length == 0) { + return "/"; + } + + // Start out with the first path as the longest common prefix. The eventual longest common + // prefix can be no longer than the first path, so as we check other paths we simply + // revise the number of matching segments we have. + String[] commonSegments = stripAnyLeadingSlash(paths[0]).split("/"); + int commonSegmentsCount = commonSegments.length; + + // Examine the remaining paths. + for (int i = 1; i < paths.length; i++) { + String[] segments = stripAnyLeadingSlash(paths[i]).split("/"); + + // Check each segment of this next path against the common segments we have so far. + int segmentIndex = 0; + while (segmentIndex < Math.min(commonSegmentsCount, segments.length) + && commonSegments[segmentIndex].equals(segments[segmentIndex])) { + segmentIndex++; + } + commonSegmentsCount = segmentIndex; + if (commonSegmentsCount == 0) { + break; + } + } + StringJoiner commonPath = new StringJoiner("/", "/", ""); + commonPath.setEmptyValue("/"); + + for (int i = 0; i < commonSegmentsCount; i++) { + commonPath.add(commonSegments[i]); + } + return commonPath.toString(); + } + + private static String stripAnyLeadingSlash(String path) { + return path.startsWith("/") ? path.substring(1) : path; + } + /** * Prepares a map of predefined HTTP status code constants. *

@@ -716,9 +757,9 @@ static class VersionUtil { private static final String DEFAULT_VERSIONS = "\n" + " \n" + - " 2.6.5\n" + - " 3.2.7\n" + - " 4.0.9\n" + + " 2.6.10\n" + + " 3.2.11\n" + + " 4.1.4\n" + " \n" + ""; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/languages/HelidonCommonCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/languages/HelidonCommonCodegenTest.java index 750e722f2fe2..9d56d4cc913f 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/languages/HelidonCommonCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/languages/HelidonCommonCodegenTest.java @@ -54,4 +54,56 @@ void testVersionNotInDefaultListWithNoNetwork() { List.of("4.0.10", "3.2.1", "3.2.0", "2.0.4", "1.2.3", "1.2.2", "1.1.0"))) .isEqualTo("4.0.11-SNAPSHOT"); } + + @Test + void checkCommonPathWithPathParams() { + String[] paths = List.of("/users/{userId}/profile", + "/users/{userId}/problems", + "/users/{userEmail}", + "/users/{username}") + .toArray(new String[0]); + + String commonPrefix = JavaHelidonCommonCodegen.commonPathPrefix(paths); + assertThat(commonPrefix).isEqualTo("/users"); + } + + @Test + void checkCommonPathWithMultipleCommonLevels() { + String[] paths = List.of("/users/a/x", + "/users/a/y", + "/users/a/z") + .toArray(new String[0]); + + String commonPrefix = JavaHelidonCommonCodegen.commonPathPrefix(paths); + assertThat(commonPrefix).isEqualTo("/users/a"); + } + + @Test + void checkNoCommonSegments() { + String[] paths = List.of("/a/x", + "/b/y", + "/c") + .toArray(new String[0]); + + String commonPrefix = JavaHelidonCommonCodegen.commonPathPrefix(paths); + assertThat(commonPrefix).isEqualTo("/"); + } + + @Test + void checkSinglePathCommonSegments() { + String commonPrefix = JavaHelidonCommonCodegen.commonPathPrefix(new String[0]); + assertThat(commonPrefix).isEqualTo("/"); + } + + @Test + void checkMixedWithPathParam() { + String[] paths = List.of("/store/order/{order_id}", + "/store/inventory", + "/store/order/{order_id}", + "/store/order") + .toArray(new String[0]); + + String commonPrefix = JavaHelidonCommonCodegen.commonPathPrefix(paths); + assertThat(commonPrefix).isEqualTo("/store"); + } }