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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2023 Red Hat Inc. and others.
* Copyright (c) 2019, 2024 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -15,6 +15,7 @@

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

import org.eclipse.lsp4j.CodeActionContext;
Expand All @@ -24,7 +25,7 @@
import org.eclipse.lsp4j.TextDocumentIdentifier;

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

/**
* Arguments utilities.
Expand All @@ -49,6 +50,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 +111,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 +147,34 @@ 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
*
* @param obj 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
*/
public static JsonObject getObjectAsJson(Map<String, Object> obj, String key) {
Object child = obj.get(key);
if (child != null && child instanceof Map<?, ?>) {
Gson gson = new Gson();
return (JsonObject) gson.toJsonTree(child);
}
return null;
* 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 data the object to get the child of
* @param key the key of the child
* @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 Object getValueFromDataParameter(Map<String, Object> data, String key) {

Object child = data.get(key);
if (child instanceof String) {
// 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();
JsonArray jsonArray = gson.toJsonTree(child).getAsJsonArray();
return jsonArray;
} else {
// Returns null if it is in any other format.
return null;
}

}
}
Loading