Skip to content

Commit

Permalink
fix: REST endpoint code vision hint is not resolving a public static
Browse files Browse the repository at this point in the history
final and quarkus.rest.path parameter

Fixes #1393

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Oct 10, 2024
1 parent e29fb4b commit 46aaa3b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
}

@DELETE
@Path("{id}")
@Transactional
@Path(
"{id}"
)
public
Response
delete(@PathParam Integer id) {
public Response delete(@PathParam Integer id) {
Fruit entity = entityManager.getReference(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
Expand All @@ -90,6 +86,18 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
return Response.status(204).build();
}

private static final String PATH = "path_with_java_constant";

@GET
@Path(PATH)
public Fruit getSingle2(@PathParam Integer id) {
Fruit entity = entityManager.find(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
}
return entity;
}

@Provider
public static class ErrorMapper implements ExceptionMapper<Exception> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
}

@DELETE
@Path("{id}")
@Transactional
@Path(
"{id}"
)
public
Response
delete(@PathParam Integer id) {
public Response delete(@PathParam Integer id) {
Fruit entity = entityManager.getReference(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
Expand All @@ -90,6 +86,18 @@ public Fruit update(@PathParam Integer id, Fruit fruit) {
return Response.status(204).build();
}

private static final String PATH = "path_with_java_constant";

@GET
@Path(PATH)
public Fruit getSingle2(@PathParam Integer id) {
Fruit entity = entityManager.find(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
}
return entity;
}

@Provider
public static class ErrorMapper implements ExceptionMapper<Exception> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public String getApplicationPath() {
*/
public void setApplicationPath(String applicationPath) {
this.applicationPath = applicationPath;
this.applicationPathLoaded = applicationPath != null;
}

public static JaxRsContext getJaxRsContext(JavaCodeLensContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
package com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiAnnotationOwner;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.*;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.util.Ranges;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -34,6 +31,8 @@
*/
public class AnnotationUtils {

private static final Logger log = LoggerFactory.getLogger(AnnotationUtils.class);

/**
* Returns checks if the <code>annotatable</code> parameter is annotated with the given annotation.
*
Expand Down Expand Up @@ -173,9 +172,33 @@ public static boolean isMatchAnnotation(PsiAnnotation annotation, String annotat
* @return the value of the given member name of the given annotation.
*/
public static String getAnnotationMemberValue(PsiAnnotation annotation, String memberName) {
PsiAnnotationMemberValue member = getAnnotationMemberValueExpression(annotation, memberName);
String value = member != null && member.getText() != null ? member.getText() : null;
if (value != null && value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
PsiElement member = getAnnotationMemberValueExpression(annotation, memberName);
if (member == null) {
return null;
}
if (member instanceof PsiReference reference) {
// ex: @Path(MY_CONSTANTS) where MY_CONSTANTS is a Java field.
member = reference.resolve();
}
if (member instanceof PsiField field) {
// ex: private static final String MY_CONSTANTS = "foo";
member = field.getInitializer();
}
if (member == null) {
return null;
}
String value = null;
if (member instanceof PsiLiteralExpression literalExpression) {
// ex : @Path("foo") --> foo
value = literalExpression.getText();
} else {
value = member.getText();
}
if (value == null) {
return null;
}
// Remove double quote if needed.
if (value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
value = value.substring(1, value.length() - 1);
}
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class QuarkusJaxRsCodeLensParticipant implements IJavaCodeLensParticipant
private static final String QUARKUS_HTTP_PORT = "quarkus.http.port";
private static final String QUARKUS_DEV_HTTP_ROOT_PATH = "%dev.quarkus.http.root-path";
private static final String QUARKUS_HTTP_ROOT_PATH = "quarkus.http.root-path";
private static final String QUARKUS_REST_PATH = "quarkus.rest.path";
private static final String QUARKUS_DEV_REST_PATH = "%dev.quarkus.rest.path";

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

// Retrieve HTTP root path from application.properties
// quarkus.http.root-path
String httpRootPath = mpProject.getProperty(QUARKUS_HTTP_ROOT_PATH);
String devHttpRootPath = mpProject.getProperty(QUARKUS_DEV_HTTP_ROOT_PATH, httpRootPath);
JaxRsContext.getJaxRsContext(context).setRootPath(devHttpRootPath);

// quarkus.rest.path
// see https://quarkus.io/guides/rest#declaring-endpoints-uri-mapping
String restPath = mpProject.getProperty(QUARKUS_REST_PATH);
String devRestPath = mpProject.getProperty(QUARKUS_DEV_REST_PATH, restPath);
JaxRsContext.getJaxRsContext(context).setApplicationPath(devRestPath);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ private static void assertCodeLenses(int port, String rootPath, MicroProfileJava
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(38, 4, 4)), //
cl("http://localhost:" + port + rootPath + "/fruits", "", r(48, 4, 4)), //
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(60, 4, 4)), //
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(81, 4, 4)));
cl("http://localhost:" + port + rootPath + "/fruits/{id}", "", r(79, 4, 4)), //
cl("http://localhost:" + port + rootPath + "/fruits/path_with_java_constant", "", r(92, 4, 4)));
}

}

0 comments on commit 46aaa3b

Please sign in to comment.