Skip to content

Commit 030ddf5

Browse files
committed
take BasePathAwareController annotation from spring data rest into account when indexing controllers
1 parent 9646c1f commit 030ddf5

File tree

9 files changed

+124
-27
lines changed

9 files changed

+124
-27
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/Annotations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Annotations {
5050
public static final String DATA_JPA_QUERY = "org.springframework.data.jpa.repository.Query";
5151
public static final String DATA_JPA_NATIVE_QUERY = "org.springframework.data.jpa.repository.NativeQuery";
5252
public static final String DATA_MONGODB_QUERY = "org.springframework.data.mongodb.repository.Query";
53-
53+
public static final String DATA_REST_BASE_PATH_AWARE_CONTROLLER = "org.springframework.data.rest.webmvc.BasePathAwareController";
5454

5555
public static final String AUTOWIRED = "org.springframework.beans.factory.annotation.Autowired";
5656
public static final String QUALIFIER = "org.springframework.beans.factory.annotation.Qualifier";

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/requestmapping/RequestMappingIndexer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ public class RequestMappingIndexer {
5858
public static void indexRequestMappings(Bean controller, TypeDeclaration type, ITypeBinding annotationType, SpringIndexerJavaContext context, TextDocument doc) {
5959
AnnotationHierarchies annotationHierarchies = AnnotationHierarchies.get(type);
6060

61-
boolean isController = annotationHierarchies.isAnnotatedWith(annotationType, Annotations.CONTROLLER);
61+
boolean isWebController = annotationHierarchies.isAnnotatedWith(annotationType, Annotations.CONTROLLER);
62+
boolean isDataRestWebController = annotationHierarchies.isAnnotatedWith(annotationType, Annotations.DATA_REST_BASE_PATH_AWARE_CONTROLLER);
6263
boolean isFeignClient = annotationHierarchies.isAnnotatedWith(annotationType, Annotations.FEIGN_CLIENT);
6364

64-
if (isController || isFeignClient) {
65+
if (isWebController || isDataRestWebController || isFeignClient) {
6566
MethodDeclaration[] methods = type.getMethods();
6667
if (methods == null) {
6768
return;

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/utils/SpringIndexerJava.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class SpringIndexerJava implements SpringIndexer {
9494

9595
// whenever the implementation of the indexer changes in a way that the stored data in the cache is no longer valid,
9696
// we need to change the generation - this will result in a re-indexing due to no up-to-date cache data being found
97-
private static final String GENERATION = "GEN-27";
97+
private static final String GENERATION = "GEN-28";
9898
private static final String INDEX_FILES_TASK_ID = "index-java-source-files-task-";
9999

100100
private static final String SYMBOL_KEY = "symbols";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Broadcom
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Broadcom - initial API and implementation
10+
*******************************************************************************/
11+
package org.springframework.ide.vscode.boot.java.data.test;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
15+
import java.io.File;
16+
import java.util.List;
17+
import java.util.concurrent.CompletableFuture;
18+
import java.util.concurrent.TimeUnit;
19+
20+
import org.eclipse.lsp4j.TextDocumentIdentifier;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Import;
26+
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
27+
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
28+
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
29+
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
30+
import org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingIndexElement;
31+
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
32+
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
33+
import org.springframework.ide.vscode.commons.protocol.spring.DocumentElement;
34+
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
35+
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
36+
import org.springframework.test.context.junit.jupiter.SpringExtension;
37+
38+
/**
39+
* @author Martin Lippert
40+
*/
41+
@ExtendWith(SpringExtension.class)
42+
@BootLanguageServerTest
43+
@Import(SymbolProviderTestConf.class)
44+
public class DataRestWebControllerIndexElementsTest {
45+
46+
@Autowired private BootLanguageServerHarness harness;
47+
@Autowired private JavaProjectFinder projectFinder;
48+
@Autowired private SpringSymbolIndex indexer;
49+
@Autowired private SpringMetamodelIndex springIndex;
50+
51+
private File directory;
52+
53+
@BeforeEach
54+
public void setup() throws Exception {
55+
harness.intialize(null);
56+
57+
directory = new File(ProjectsHarness.class.getResource("/test-projects/test-spring-data-symbols/").toURI());
58+
String projectDir = directory.toURI().toString();
59+
60+
// trigger project creation
61+
projectFinder.find(new TextDocumentIdentifier(projectDir)).get();
62+
63+
CompletableFuture<Void> initProject = indexer.waitOperation();
64+
initProject.get(5, TimeUnit.SECONDS);
65+
}
66+
67+
@Test
68+
void testSimpleRepositoryElements() throws Exception {
69+
String docUri = directory.toPath().resolve("src/main/java/org/test/web/DataRestController.java").toUri().toString();
70+
71+
DocumentElement document = springIndex.getDocument(docUri);
72+
List<Bean> children = SpringMetamodelIndex.getNodesOfType(Bean.class, List.of(document));
73+
Bean dataRestControllerElement = children.get(0);
74+
assertEquals("dataRestController", dataRestControllerElement.getName());
75+
assertEquals(1, children.size());
76+
77+
List<RequestMappingIndexElement> mappingNodes = SpringMetamodelIndex.getNodesOfType(RequestMappingIndexElement.class, List.of(dataRestControllerElement));
78+
assertEquals(1, mappingNodes.size());
79+
80+
assertEquals("/something", mappingNodes.get(0).getPath());
81+
}
82+
83+
84+
}

headless-services/spring-boot-language-server/src/test/resources/test-projects/test-spring-data-symbols/pom.xml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="https://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56

67
<groupId>com.example</groupId>
78
<artifactId>test-spring-data-symbols</artifactId>
89
<version>0.0.1-SNAPSHOT</version>
910
<packaging>jar</packaging>
10-
11+
1112
<parent>
1213
<groupId>org.springframework.boot</groupId>
1314
<artifactId>spring-boot-starter-parent</artifactId>
14-
<version>1.5.10.RELEASE</version>
15-
<relativePath/> <!-- lookup parent from repository -->
15+
<version>3.5.0</version>
16+
<relativePath /> <!-- lookup parent from repository -->
1617
</parent>
1718

1819
<properties>
1920
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2021
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21-
<java.version>1.8</java.version>
22+
<java.version>17</java.version>
2223
</properties>
2324

2425
<dependencies>
2526
<dependency>
2627
<groupId>org.springframework.boot</groupId>
2728
<artifactId>spring-boot-starter</artifactId>
2829
</dependency>
29-
30+
3031
<dependency>
3132
<groupId>org.springframework.boot</groupId>
3233
<artifactId>spring-boot-starter-actuator</artifactId>
@@ -35,6 +36,12 @@
3536
<groupId>org.springframework.boot</groupId>
3637
<artifactId>spring-boot-starter-data-jpa</artifactId>
3738
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-rest</artifactId>
43+
</dependency>
44+
3845
</dependencies>
3946

4047
<build>
@@ -43,15 +50,6 @@
4350
<groupId>org.springframework.boot</groupId>
4451
<artifactId>spring-boot-maven-plugin</artifactId>
4552
</plugin>
46-
<plugin>
47-
<groupId>org.apache.maven.plugins</groupId>
48-
<artifactId>maven-javadoc-plugin</artifactId>
49-
<version>3.2.0</version>
50-
<configuration>
51-
<source>8</source>
52-
<detectJavaApiLink>false</detectJavaApiLink>
53-
</configuration>
54-
</plugin>
5553
</plugins>
5654
</build>
5755

headless-services/spring-boot-language-server/src/test/resources/test-projects/test-spring-data-symbols/src/main/java/org/test/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public CommandLineRunner demo(CustomerRepository repository) {
3737
log.info("");
3838

3939
// fetch an individual customer by ID
40-
Customer customer = repository.findOne(1L);
40+
Customer customer = repository.findById(1L).get();
4141
log.info("Customer found with findOne(1L):");
4242
log.info("--------------------------------");
4343
log.info(customer.toString());

headless-services/spring-boot-language-server/src/test/resources/test-projects/test-spring-data-symbols/src/main/java/org/test/Customer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// tag::sample[]
22
package org.test;
33

4-
import javax.persistence.Entity;
5-
import javax.persistence.GeneratedValue;
6-
import javax.persistence.GenerationType;
7-
import javax.persistence.Id;
8-
import javax.persistence.ManyToOne;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.GenerationType;
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.ManyToOne;
99

1010
import org.test.model.Employee;
1111
import org.test.model.Person;

headless-services/spring-boot-language-server/src/test/resources/test-projects/test-spring-data-symbols/src/main/java/org/test/model/Employee.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// tag::sample[]
22
package org.test.model;
33

4-
import javax.persistence.Entity;
5-
import javax.persistence.Id;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.Id;
66

77
@Entity
88
public class Employee extends Person {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.test.web;
2+
3+
import org.springframework.data.rest.webmvc.BasePathAwareController;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@BasePathAwareController
7+
public class DataRestController {
8+
9+
@GetMapping("something")
10+
public String saySomething() {
11+
return "hello";
12+
}
13+
14+
}

0 commit comments

Comments
 (0)