From 8ea8fb295e35d6a7311a57238c59d93999477cff Mon Sep 17 00:00:00 2001 From: Shrikant Kandula Date: Thu, 30 Apr 2020 13:32:24 +0000 Subject: [PATCH] Handle 500 when JSON body doesn't make up an object data type. --- .../com/external/plugins/RestApiPlugin.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java index 777458537a..5b6bcc70ba 100644 --- a/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java +++ b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java @@ -11,6 +11,7 @@ import com.appsmith.external.plugins.BasePlugin; import com.appsmith.external.plugins.PluginExecutor; import com.fasterxml.jackson.core.JsonProcessingException; import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.bson.internal.Base64; @@ -191,15 +192,23 @@ public class RestApiPlugin extends BasePlugin { if (iteration == MAX_REDIRECTS) { return Mono.error(new AppsmithPluginException( AppsmithPluginError.PLUGIN_ERROR, - "Exceeded the HTTO redirect limits of " + MAX_REDIRECTS + "Exceeded the HTTP redirect limits of " + MAX_REDIRECTS )); } - Object requestBodyAsObject; + Object requestBodyAsObject = null; if (isJsonContentType) { - GsonBuilder gson = new GsonBuilder(); - requestBodyAsObject = gson.create().fromJson(requestBodyAsString, Map.class); - } else { + try { + requestBodyAsObject = objectFromJson(requestBodyAsString); + } catch (JsonSyntaxException e) { + return Mono.error(new AppsmithPluginException( + AppsmithPluginError.PLUGIN_ERROR, + "Malformed JSON: " + e.getMessage() + )); + } + } + + if (requestBodyAsObject == null) { requestBodyAsObject = requestBodyAsString; } @@ -232,6 +241,31 @@ public class RestApiPlugin extends BasePlugin { }); } + /** + * Given a JSON string, we infer the top-level type of the object it represents and then parse it into that + * type. However, only `Map` and `List` top-levels are supported. Note that the map or list may contain + * anything, like booleans or number or even more maps or lists. It's only that the top-level type should be a + * map / list. + * @param jsonString A string that confirms to JSON syntax. Shouldn't be null. + * @return An object of type `Map`, `List`, if applicable, or `null`. + */ + private static Object objectFromJson(String jsonString) { + Class type; + String trimmed = jsonString.trim(); + + if (trimmed.startsWith("{")) { + type = Map.class; + } else if (trimmed.startsWith("[")) { + type = List.class; + } else { + // The JSON body is likely a literal boolean or number or string. For our purposes here, we don't have + // to parse this JSON. + return null; + } + + return new GsonBuilder().create().fromJson(jsonString, type); + } + @Override public Mono datasourceCreate(DatasourceConfiguration datasourceConfiguration) { return Mono.empty();