Skip to content

Commit 46aaa3b

Browse files
committed
fix: REST endpoint code vision hint is not resolving a public static
final and quarkus.rest.path parameter Fixes #1393 Signed-off-by: azerr <[email protected]>
1 parent e29fb4b commit 46aaa3b

File tree

6 files changed

+72
-22
lines changed

6 files changed

+72
-22
lines changed

projects/lsp4mp/projects/maven/hibernate-orm-resteasy-yaml/src/main/java/org/acme/hibernate/orm/FruitResource.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,9 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
7575
}
7676

7777
@DELETE
78+
@Path("{id}")
7879
@Transactional
79-
@Path(
80-
"{id}"
81-
)
82-
public
83-
Response
84-
delete(@PathParam Integer id) {
80+
public Response delete(@PathParam Integer id) {
8581
Fruit entity = entityManager.getReference(Fruit.class, id);
8682
if (entity == null) {
8783
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
@@ -90,6 +86,18 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
9086
return Response.status(204).build();
9187
}
9288

89+
private static final String PATH = "path_with_java_constant";
90+
91+
@GET
92+
@Path(PATH)
93+
public Fruit getSingle2(@PathParam Integer id) {
94+
Fruit entity = entityManager.find(Fruit.class, id);
95+
if (entity == null) {
96+
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
97+
}
98+
return entity;
99+
}
100+
93101
@Provider
94102
public static class ErrorMapper implements ExceptionMapper<Exception> {
95103

projects/lsp4mp/projects/maven/hibernate-orm-resteasy/src/main/java/org/acme/hibernate/orm/FruitResource.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,9 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
7575
}
7676

7777
@DELETE
78+
@Path("{id}")
7879
@Transactional
79-
@Path(
80-
"{id}"
81-
)
82-
public
83-
Response
84-
delete(@PathParam Integer id) {
80+
public Response delete(@PathParam Integer id) {
8581
Fruit entity = entityManager.getReference(Fruit.class, id);
8682
if (entity == null) {
8783
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
@@ -90,6 +86,18 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
9086
return Response.status(204).build();
9187
}
9288

89+
private static final String PATH = "path_with_java_constant";
90+
91+
@GET
92+
@Path(PATH)
93+
public Fruit getSingle2(@PathParam Integer id) {
94+
Fruit entity = entityManager.find(Fruit.class, id);
95+
if (entity == null) {
96+
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
97+
}
98+
return entity;
99+
}
100+
93101
@Provider
94102
public static class ErrorMapper implements ExceptionMapper<Exception> {
95103

src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/jaxrs/JaxRsContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public String getApplicationPath() {
111111
*/
112112
public void setApplicationPath(String applicationPath) {
113113
this.applicationPath = applicationPath;
114+
this.applicationPathLoaded = applicationPath != null;
114115
}
115116

116117
public static JaxRsContext getJaxRsContext(JavaCodeLensContext context) {

src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/utils/AnnotationUtils.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
package com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils;
1111

1212
import com.intellij.openapi.util.TextRange;
13-
import com.intellij.psi.PsiAnnotation;
14-
import com.intellij.psi.PsiAnnotationMemberValue;
15-
import com.intellij.psi.PsiAnnotationOwner;
16-
import com.intellij.psi.PsiElement;
17-
import com.intellij.psi.PsiFile;
18-
import com.intellij.psi.PsiModifierListOwner;
13+
import com.intellij.psi.*;
1914
import org.eclipse.lsp4j.Position;
2015
import org.eclipse.lsp4j.Range;
2116
import org.eclipse.lsp4j.util.Ranges;
2217
import org.jetbrains.annotations.Nullable;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
2320

2421
import java.util.ArrayList;
2522
import java.util.List;
@@ -34,6 +31,8 @@
3431
*/
3532
public class AnnotationUtils {
3633

34+
private static final Logger log = LoggerFactory.getLogger(AnnotationUtils.class);
35+
3736
/**
3837
* Returns checks if the <code>annotatable</code> parameter is annotated with the given annotation.
3938
*
@@ -173,9 +172,33 @@ public static boolean isMatchAnnotation(PsiAnnotation annotation, String annotat
173172
* @return the value of the given member name of the given annotation.
174173
*/
175174
public static String getAnnotationMemberValue(PsiAnnotation annotation, String memberName) {
176-
PsiAnnotationMemberValue member = getAnnotationMemberValueExpression(annotation, memberName);
177-
String value = member != null && member.getText() != null ? member.getText() : null;
178-
if (value != null && value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
175+
PsiElement member = getAnnotationMemberValueExpression(annotation, memberName);
176+
if (member == null) {
177+
return null;
178+
}
179+
if (member instanceof PsiReference reference) {
180+
// ex: @Path(MY_CONSTANTS) where MY_CONSTANTS is a Java field.
181+
member = reference.resolve();
182+
}
183+
if (member instanceof PsiField field) {
184+
// ex: private static final String MY_CONSTANTS = "foo";
185+
member = field.getInitializer();
186+
}
187+
if (member == null) {
188+
return null;
189+
}
190+
String value = null;
191+
if (member instanceof PsiLiteralExpression literalExpression) {
192+
// ex : @Path("foo") --> foo
193+
value = literalExpression.getText();
194+
} else {
195+
value = member.getText();
196+
}
197+
if (value == null) {
198+
return null;
199+
}
200+
// Remove double quote if needed.
201+
if (value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
179202
value = value.substring(1, value.length() - 1);
180203
}
181204
return value;

src/main/java/com/redhat/microprofile/psi/internal/quarkus/jaxrs/java/QuarkusJaxRsCodeLensParticipant.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class QuarkusJaxRsCodeLensParticipant implements IJavaCodeLensParticipant
3636
private static final String QUARKUS_HTTP_PORT = "quarkus.http.port";
3737
private static final String QUARKUS_DEV_HTTP_ROOT_PATH = "%dev.quarkus.http.root-path";
3838
private static final String QUARKUS_HTTP_ROOT_PATH = "quarkus.http.root-path";
39+
private static final String QUARKUS_REST_PATH = "quarkus.rest.path";
40+
private static final String QUARKUS_DEV_REST_PATH = "%dev.quarkus.rest.path";
3941

4042
@Override
4143
public void beginCodeLens(JavaCodeLensContext context, ProgressIndicator monitor) {
@@ -49,9 +51,16 @@ public void beginCodeLens(JavaCodeLensContext context, ProgressIndicator monitor
4951
JaxRsContext.getJaxRsContext(context).setServerPort(devServerPort);
5052

5153
// Retrieve HTTP root path from application.properties
54+
// quarkus.http.root-path
5255
String httpRootPath = mpProject.getProperty(QUARKUS_HTTP_ROOT_PATH);
5356
String devHttpRootPath = mpProject.getProperty(QUARKUS_DEV_HTTP_ROOT_PATH, httpRootPath);
5457
JaxRsContext.getJaxRsContext(context).setRootPath(devHttpRootPath);
58+
59+
// quarkus.rest.path
60+
// see https://quarkus.io/guides/rest#declaring-endpoints-uri-mapping
61+
String restPath = mpProject.getProperty(QUARKUS_REST_PATH);
62+
String devRestPath = mpProject.getProperty(QUARKUS_DEV_REST_PATH, restPath);
63+
JaxRsContext.getJaxRsContext(context).setApplicationPath(devRestPath);
5564
}
5665

5766
@Override

src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/jaxrs/java/JaxRsCodeLensTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ private static void assertCodeLenses(int port, String rootPath, MicroProfileJava
6666
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(38, 4, 4)), //
6767
cl("http://localhost:" + port + rootPath + "/fruits", "", r(48, 4, 4)), //
6868
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(60, 4, 4)), //
69-
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(81, 4, 4)));
69+
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(79, 4, 4)), //
70+
cl("http://localhost:" + port + rootPath + "/fruits/path_with_java_constant", "", r(92, 4, 4)));
7071
}
7172

7273
}

0 commit comments

Comments
 (0)