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 010af4f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
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

0 comments on commit 010af4f

Please sign in to comment.