Guard against errors when getting element String value

This commit is contained in:
nsingh
2018-06-05 15:39:05 -07:00
parent 82974b9f1d
commit 1125337db8
4 changed files with 15 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.ide.vscode.commons.util.Assert;
import org.springframework.ide.vscode.commons.util.CollectorUtil;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.ExternalProcess;
import org.springframework.ide.vscode.commons.util.GsonUtil;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.StringUtil;
import org.springframework.ide.vscode.commons.util.text.LanguageId;
@@ -143,7 +144,7 @@ public abstract class BoshCommandBasedModelProvider<T> implements DynamicModelPr
protected Collection<String> getNames(JSONCursor _cursor, YamlTraversal path) {
return path.traverseAmbiguously(_cursor)
.flatMap((cursor) -> {
String text = cursor.target.getAsString();
String text = GsonUtil.getAsString(cursor.target);
if (StringUtil.hasText(text)) {
return Stream.of(text);
} else {

View File

@@ -15,6 +15,7 @@ import java.util.List;
import org.springframework.ide.vscode.bosh.BoshCliConfig;
import org.springframework.ide.vscode.commons.util.CollectorUtil;
import org.springframework.ide.vscode.commons.util.GsonUtil;
import org.springframework.ide.vscode.commons.yaml.path.YamlPath;
import org.springframework.ide.vscode.commons.yaml.path.YamlTraversal;
import org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext;
@@ -56,7 +57,7 @@ public class BoshCommandReleasesProvider extends BoshCommandBasedModelProvider<
private String getStringProperty(JSONCursor c, String prop) {
c = YamlPath.EMPTY.thenValAt(prop).traverse(c);
if (c!=null) {
return c.target.getAsString();
return GsonUtil.getAsString(c.target);
}
return null;
}

View File

@@ -15,6 +15,7 @@ import java.util.Collection;
import org.springframework.ide.vscode.bosh.BoshCliConfig;
import org.springframework.ide.vscode.commons.util.CollectorUtil;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.GsonUtil;
import org.springframework.ide.vscode.commons.yaml.path.YamlPath;
import org.springframework.ide.vscode.commons.yaml.path.YamlTraversal;
import org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext;
@@ -64,7 +65,7 @@ public class BoshCommandStemcellsProvider extends BoshCommandBasedModelProvider<
private String getStringProperty(JSONCursor c, String prop) {
c = YamlPath.EMPTY.thenValAt(prop).traverse(c);
if (c!=null) {
return c.target.getAsString();
return GsonUtil.getAsString(c.target);
}
return null;
}

View File

@@ -52,4 +52,13 @@ public class GsonUtil {
return Stream.empty();
}
}
public static String getAsString(JsonElement target) {
// Need to check if it is a primitive before get as string, otherwise
// exceptions can be thrown. See the JsonElement javadoc
if (target != null && target.isJsonPrimitive()) {
return target.getAsString();
}
return null;
}
}