Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ additionalProperties:
# performBeanValidation should default to "false" when not specified
useResponseEntity: "false"
useSpringBoot3: "true"
useTags: "true"

2 changes: 1 addition & 1 deletion bin/configs/spring-http-interface-reactive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ additionalProperties:
# useBeanValidation should default to "false" when not specified
# performBeanValidation should default to "false" when not specified
useSpringBoot3: "true"

useTags: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ additionalProperties:
performBeanValidation: "true"
useHttpServiceProxyFactoryInterfacesConfigurator: "true"
useSpringBoot3: "true"
useTags: "true"
1 change: 1 addition & 0 deletions bin/configs/spring-http-interface.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ additionalProperties:
# useBeanValidation should default to "false" when not specified
# performBeanValidation should default to "false" when not specified
useSpringBoot3: "true"
useTags: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
if (library.equals(SPRING_BOOT) && !useTags) {
if ((library.equals(SPRING_BOOT) || library.equals(SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY)) && !useTags) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
String basePath = resourcePath;
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
Expand All @@ -1081,6 +1081,10 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
} else {
co.subresourceOperation = !co.path.isEmpty();
}
if (SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY.equals(library)) {
super.addOperationToGroup(getUniquePathGroupName(basePath, operations), resourcePath, operation, co, operations);
return;
}
List<CodegenOperation> opList = operations.computeIfAbsent(basePath, k -> new ArrayList<>());
opList.add(co);
co.baseName = basePath;
Expand All @@ -1089,6 +1093,30 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
}
}

private String getUniquePathGroupName(String basePath, Map<String, List<CodegenOperation>> operations) {
String sanitizedBasePath = sanitizeName(basePath);
if (sanitizedBasePath.isEmpty()) {
sanitizedBasePath = "Path";
} else if (sanitizedBasePath.matches("^\\d.*")) {
sanitizedBasePath = "Class" + sanitizedBasePath;
}
String groupName = camelize(sanitizedBasePath, LOWERCASE_FIRST_LETTER);
String uniqueGroupName = groupName;
int suffix = 2;
while (operations.containsKey(uniqueGroupName)
&& !getFirstPathSegment(operations.get(uniqueGroupName).get(0).path).equals(basePath)) {
uniqueGroupName = groupName + suffix++;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
}
return uniqueGroupName;
}

private String getFirstPathSegment(String path) {
String basePath = path.startsWith("/") ? path.substring(1) : path;
int pos = basePath.indexOf("/");
basePath = pos > 0 ? basePath.substring(0, pos) : basePath;
return basePath.isEmpty() ? "default" : basePath;
}

/**
* Whether the current library supports the {@code x-spring-paginated} extension (i.e. a
* Spring Data {@code Pageable} parameter). Supported for the {@code spring-boot} server and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,9 @@ protected void applyJackson3Package() {
}

private boolean supportLibraryUseTags() {
return SPRING_BOOT.equals(library) || SPRING_CLOUD_LIBRARY.equals(library);
return SPRING_BOOT.equals(library)
|| SPRING_CLOUD_LIBRARY.equals(library)
|| SPRING_HTTP_INTERFACE.equals(library);
}

/**
Expand Down Expand Up @@ -928,6 +930,10 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
} else {
co.subresourceOperation = !co.path.isEmpty();
}
if (SPRING_HTTP_INTERFACE.equals(library)) {
super.addOperationToGroup(getUniquePathGroupName(basePath, operations), resourcePath, operation, co, operations);
return;
}
final List<CodegenOperation> opList = operations.computeIfAbsent(basePath, k -> new ArrayList<>());
opList.add(co);
co.baseName = basePath;
Expand All @@ -937,6 +943,30 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera

}

private String getUniquePathGroupName(String basePath, Map<String, List<CodegenOperation>> operations) {
String sanitizedBasePath = sanitizeName(basePath);
if (sanitizedBasePath.isEmpty()) {
sanitizedBasePath = "Path";
} else if (sanitizedBasePath.matches("^\\d.*")) {
sanitizedBasePath = "Class" + sanitizedBasePath;
}
String groupName = camelize(sanitizedBasePath, LOWERCASE_FIRST_LETTER);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
String uniqueGroupName = groupName;
int suffix = 2;
while (operations.containsKey(uniqueGroupName)
&& !getFirstPathSegment(operations.get(uniqueGroupName).get(0).path).equals(basePath)) {
uniqueGroupName = groupName + suffix++;
}
return uniqueGroupName;
}

private String getFirstPathSegment(String path) {
String basePath = path.startsWith("/") ? path.substring(1) : path;
int pos = basePath.indexOf("/");
basePath = pos > 0 ? basePath.substring(0, pos) : basePath;
return basePath.isEmpty() ? "default" : basePath;
}

@Override
public void preprocessOpenAPI(OpenAPI openAPI) {
super.preprocessOpenAPI(openAPI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,158 @@ public void shouldNotUseTagsForClassname() throws IOException {
assertThat(notExisting).isNull();
}

@Test
public void useTags_false_groupsByFirstPathSegment_springHttpInterface() {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_HTTP_INTERFACE);
codegen.additionalProperties().put(USE_TAGS, "false");
codegen.processOpts();

CodegenOperation co = new CodegenOperation();
co.operationId = "findByStatus";
co.path = "/pet/findByStatus";
Map<String, List<CodegenOperation>> groups = new HashMap<>();

codegen.addOperationToGroup("Pet", "/pet/findByStatus", new Operation(), co, groups);

assertTrue(groups.containsKey("pet"));
assertEquals(co.baseName, "pet");
}

@Test
public void useTags_true_groupsByTag_springHttpInterface() {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_HTTP_INTERFACE);
codegen.additionalProperties().put(USE_TAGS, "true");
codegen.processOpts();

CodegenOperation co = new CodegenOperation();
co.operationId = "findByStatus";
Map<String, List<CodegenOperation>> groups = new HashMap<>();

codegen.addOperationToGroup("Pet", "/pet/findByStatus", new Operation(), co, groups);

assertTrue(groups.containsKey("Pet"));
}

@Test
public void useTags_false_groupsByFirstPathSegment_sanitizesInvalidIdentifierChars_springHttpInterface() {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_HTTP_INTERFACE);
codegen.additionalProperties().put(USE_TAGS, "false");
codegen.processOpts();

CodegenOperation co = new CodegenOperation();
co.operationId = "dummy";
co.path = "/another-fake/dummy";
Map<String, List<CodegenOperation>> groups = new HashMap<>();

codegen.addOperationToGroup("$another-fake?", "/another-fake/dummy", new Operation(), co, groups);

// the first path segment "another-fake" must be sanitized into a valid Java identifier
// (no hyphen) instead of being used as-is, which previously produced e.g.
// "AnotherFakeApi another-fakeHttpProxy()" - invalid Java syntax.
assertTrue(groups.containsKey("anotherFake"));
assertEquals(co.baseName, "anotherFake");
}

@Test
public void useTags_false_pathGroupsRemainDistinctAndOperationIdsUnique_springHttpInterface() {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_HTTP_INTERFACE);
codegen.additionalProperties().put(USE_TAGS, "false");
codegen.processOpts();

CodegenOperation first = new CodegenOperation();
first.operationId = "dummy";
first.path = "/another-fake/dummy";
CodegenOperation duplicate = new CodegenOperation();
duplicate.operationId = "dummy";
duplicate.path = "/another-fake/other";
CodegenOperation colliding = new CodegenOperation();
colliding.operationId = "dummy";
colliding.path = "/another_fake/dummy";
Map<String, List<CodegenOperation>> groups = new HashMap<>();

codegen.addOperationToGroup("First", "/another-fake/dummy", new Operation(), first, groups);
codegen.addOperationToGroup("Second", "/another-fake/other", new Operation(), duplicate, groups);
codegen.addOperationToGroup("Third", "/another_fake/dummy", new Operation(), colliding, groups);

assertTrue(groups.containsKey("anotherFake"));
assertTrue(groups.containsKey("anotherFake2"));
assertEquals(duplicate.operationId, "dummy_0");
assertEquals(colliding.baseName, "anotherFake2");
}

@Test
public void useTags_false_groupsRootOperationsAndPrefixesDigitLeadingPath_springHttpInterface() {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_HTTP_INTERFACE);
codegen.additionalProperties().put(USE_TAGS, "false");
codegen.processOpts();

CodegenOperation rootGet = new CodegenOperation();
rootGet.operationId = "getRoot";
rootGet.path = "/";
CodegenOperation rootPost = new CodegenOperation();
rootPost.operationId = "postRoot";
rootPost.path = "/";
CodegenOperation digitLeading = new CodegenOperation();
digitLeading.operationId = "getPets";
digitLeading.path = "/123/pets";
Map<String, List<CodegenOperation>> groups = new HashMap<>();

codegen.addOperationToGroup("Root", "/", new Operation(), rootGet, groups);
codegen.addOperationToGroup("Root", "/", new Operation(), rootPost, groups);
codegen.addOperationToGroup("Pets", "/123/pets", new Operation(), digitLeading, groups);

assertEquals(groups.get("default").size(), 2);
assertTrue(groups.containsKey("class123"));
assertEquals(digitLeading.baseName, "class123");
assertEquals(codegen.toApiName(digitLeading.baseName), "Class123Api");
}

@Test
public void useTags_false_pathGroupsWithEmptySanitizedNamesRemainDistinct_springHttpInterface() {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_HTTP_INTERFACE);
codegen.additionalProperties().put(USE_TAGS, "false");
codegen.processOpts();

CodegenOperation first = new CodegenOperation();
first.operationId = "first";
first.path = "/@/first";
CodegenOperation second = new CodegenOperation();
second.operationId = "second";
second.path = "/!/second";
Map<String, List<CodegenOperation>> groups = new HashMap<>();

codegen.addOperationToGroup("First", "/@/first", new Operation(), first, groups);
codegen.addOperationToGroup("Second", "/!/second", new Operation(), second, groups);

assertTrue(groups.containsKey("path"));
assertTrue(groups.containsKey("path2"));
assertEquals(codegen.toApiName(second.baseName), "Path2Api");
}

@Test
public void useTags_false_preservesRawPathGroupName_springCloud() {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_CLOUD_LIBRARY);
codegen.additionalProperties().put(USE_TAGS, "false");
codegen.processOpts();

CodegenOperation co = new CodegenOperation();
co.operationId = "dummy";
co.path = "/another-fake/dummy";
Map<String, List<CodegenOperation>> groups = new HashMap<>();

codegen.addOperationToGroup("AnotherFake", "/another-fake/dummy", new Operation(), co, groups);

assertTrue(groups.containsKey("another-fake"));
assertEquals(co.baseName, "another-fake");
}

@Test
public void shouldAddValidAnnotationIntoCollectionWhenBeanValidationIsEnabled_issue14723() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
Loading
Loading