Merge branch 'bug/json-body-non-object-types' into 'release'
Handle 500 when JSON body doesn't make up an object data type. See merge request theappsmith/internal-tools-server!316
This commit is contained in:
commit
f4b4c630fe
|
|
@ -11,6 +11,7 @@ import com.appsmith.external.plugins.BasePlugin;
|
||||||
import com.appsmith.external.plugins.PluginExecutor;
|
import com.appsmith.external.plugins.PluginExecutor;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.bson.internal.Base64;
|
import org.bson.internal.Base64;
|
||||||
|
|
@ -191,15 +192,23 @@ public class RestApiPlugin extends BasePlugin {
|
||||||
if (iteration == MAX_REDIRECTS) {
|
if (iteration == MAX_REDIRECTS) {
|
||||||
return Mono.error(new AppsmithPluginException(
|
return Mono.error(new AppsmithPluginException(
|
||||||
AppsmithPluginError.PLUGIN_ERROR,
|
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) {
|
if (isJsonContentType) {
|
||||||
GsonBuilder gson = new GsonBuilder();
|
try {
|
||||||
requestBodyAsObject = gson.create().fromJson(requestBodyAsString, Map.class);
|
requestBodyAsObject = objectFromJson(requestBodyAsString);
|
||||||
} else {
|
} catch (JsonSyntaxException e) {
|
||||||
|
return Mono.error(new AppsmithPluginException(
|
||||||
|
AppsmithPluginError.PLUGIN_ERROR,
|
||||||
|
"Malformed JSON: " + e.getMessage()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requestBodyAsObject == null) {
|
||||||
requestBodyAsObject = requestBodyAsString;
|
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
|
@Override
|
||||||
public Mono<Object> datasourceCreate(DatasourceConfiguration datasourceConfiguration) {
|
public Mono<Object> datasourceCreate(DatasourceConfiguration datasourceConfiguration) {
|
||||||
return Mono.empty();
|
return Mono.empty();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user