Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #416 - Quick-fix fails due to getData() is null #536

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.eclipse.lsp4j.CodeActionContext;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4jakarta.jdt.internal.beanvalidation.RemoveDynamicConstraintAnnotationQuickFix;
turkeylurkey marked this conversation as resolved.
Show resolved Hide resolved

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
* Arguments utilities.
Expand All @@ -49,6 +53,8 @@ public class ArgumentUtils {
private static final String LINE_PROPERTY = "line";
private static final String URI_PROPERTY = "uri";

private static final Logger LOGGER = Logger.getLogger(ArgumentUtils.class.getName());

public static Map<String, Object> getFirst(List<Object> arguments) {
return arguments.isEmpty() ? null : (Map<String, Object>) arguments.get(0);
}
Expand Down Expand Up @@ -108,15 +114,20 @@ public static CodeActionContext getCodeActionContext(Map<String, Object> obj, St
}
List<Map<String, Object>> diagnosticsObj = (List<Map<String, Object>>) contextObj.get(DIAGNOSTICS_PROPERTY);
List<Diagnostic> diagnostics = diagnosticsObj.stream().map(diagnosticObj -> {
LOGGER.info("Received diagnostic data" + diagnosticObj.toString());
Diagnostic diagnostic = new Diagnostic();
diagnostic.setRange(getRange(diagnosticObj, RANGE_PROPERTY));
diagnostic.setCode(getString(diagnosticObj, CODE_PROPERTY));
diagnostic.setMessage(getString(diagnosticObj, MESSAGE_PROPERTY));
diagnostic.setSource(getString(diagnosticObj, SOURCE_PROPERTY));
// In Eclipse IDE (LSP client), the data is JsonObject, and in JDT-LS (ex :
// vscode as LSP client) the data is a Map, we
// convert the Map to a JsonObject to be consistent with any LSP clients.
diagnostic.setData(getObjectAsJson(diagnosticObj, DATA_PROPERTY));
// vscode as LSP client) the data is a Map.

// In Vscode we are sending data in two different formats either as a string or
// as an array of strings. eg: data = “AssertTrue” or data =[“ApplicationScoped", "RequestScoped”]
// if it is a string -> set data as an Object.
// if ite is an array of strings - > set data as an JsonArray
diagnostic.setData(getValueFromDataParameter(diagnosticObj, DATA_PROPERTY));
return diagnostic;
}).collect(Collectors.toList());
List<String> only = null;
Expand All @@ -139,20 +150,32 @@ public static Map<String, Object> getObject(Map<String, Object> obj, String key)
}

/**
* Returns the child as a JsonObject if it exists and is an object, and null
* otherwise
* Returns the child as a JSON array if the data parameter contains an array of strings; otherwise,
* returns it as an object if present, or null if not.
*
* @param obj the object to get the child of
*
* @param data the object to get the child of
* @param key the key of the child
* @return the child as a JsonObject if it exists and is an object, and null
* otherwise
* @return Returns the child as a JSON array if the data parameter contains an array of strings; otherwise,
* returns it as an object if present, or null if not.
*/
public static JsonObject getObjectAsJson(Map<String, Object> obj, String key) {
Object child = obj.get(key);
if (child != null && child instanceof Map<?, ?>) {
public static Object getValueFromDataParameter(Map<String, Object> data, String key) {

Object child = data.get(key);
if (child != null && child instanceof String) {
turkeylurkey marked this conversation as resolved.
Show resolved Hide resolved
// if the value in the 'data' is a string, we string the object.
turkeylurkey marked this conversation as resolved.
Show resolved Hide resolved
// eg: data = “AssertTrue”
return child;
} else if (child instanceof List<?>) {
// if the value in the 'data' is an array, we will convert it to an JsonArray.
// eg: data =[“ApplicationScoped", "RequestScoped”]
Gson gson = new Gson();
return (JsonObject) gson.toJsonTree(child);
JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray();
return jsonArray;
} else {
//Returns the object if it exists and is an object, and null otherwise
return getObject(data, key);
turkeylurkey marked this conversation as resolved.
Show resolved Hide resolved
}
return null;

}
}
Loading