Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@

package com.predic8.membrane.core.lang;

import com.fasterxml.jackson.databind.*;
import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.interceptor.Interceptor.*;
import com.predic8.membrane.core.lang.groovy.*;
import com.predic8.membrane.core.openapi.serviceproxy.*;
import com.predic8.membrane.core.openapi.util.*;
import com.predic8.membrane.core.router.*;
import com.predic8.membrane.core.util.text.*;
import org.slf4j.*;

import java.util.*;
import java.util.function.*;

import static com.predic8.membrane.core.interceptor.Interceptor.Flow.*;
import static com.predic8.membrane.core.openapi.util.UriTemplateMatcher.*;
import static com.predic8.membrane.core.util.FileUtil.*;
import static com.predic8.membrane.core.util.URLParamUtil.*;
import static java.util.Collections.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.HeaderMap;
import com.predic8.membrane.core.http.LazyCookieMap;
import com.predic8.membrane.core.interceptor.Interceptor.Flow;
import com.predic8.membrane.core.lang.groovy.GroovyBuiltInFunctions;
import com.predic8.membrane.core.lang.groovy.PathParametersMap;
import com.predic8.membrane.core.openapi.serviceproxy.APIProxy;
import com.predic8.membrane.core.openapi.util.PathDoesNotMatchException;
import com.predic8.membrane.core.router.Router;
import com.predic8.membrane.core.util.text.SerializationFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static com.predic8.membrane.core.interceptor.Interceptor.Flow.REQUEST;
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.RESPONSE;
import static com.predic8.membrane.core.openapi.util.UriTemplateMatcher.matchTemplate;
import static com.predic8.membrane.core.util.FileUtil.readInputStream;
import static com.predic8.membrane.core.util.URLParamUtil.getParams;
import static java.util.Collections.emptyMap;

public class ScriptingUtils {

Expand Down Expand Up @@ -92,9 +96,9 @@ public static Map<String, Object> createParameterBindings(Router router, Exchang
if (includeJsonObject) {
try {
log.debug("Parsing body as JSON for scripting plugins");
params.put("json", om.readValue(readInputStream(msg.getBodyAsStreamDecoded()), Map.class));
params.put("json", om.readValue(readInputStream(msg.getBodyAsStreamDecoded()), Object.class));
} catch (Exception e) {
log.warn("Can't parse body as JSON", e);
log.info("Can't parse body as JSON: {}", e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.predic8.membrane.core.lang;

import com.predic8.membrane.core.router.DefaultRouter;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import static com.predic8.membrane.core.http.Request.get;
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.REQUEST;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class ScriptingUtilsTest {

@Nested
class json {

@Test
void map() throws URISyntaxException {
assertInstanceOf(Map.class, createBinding("""
{"foo":1}""").get("json"));
}

@Test
void array() throws URISyntaxException {
assertInstanceOf(List.class, createBinding("[1,2,3]").get("json"));
}

@Test
void string() throws URISyntaxException {
assertInstanceOf(String.class, createBinding("""
"foo"
""").get("json"));
}

@Test
void number() throws URISyntaxException {
assertInstanceOf(Integer.class, createBinding("7").get("json"));
}

private static @NotNull Map<String, Object> createBinding(String json) throws URISyntaxException {
var bindings = ScriptingUtils.createParameterBindings(new DefaultRouter(), get("/foo").json(json).buildExchange(), REQUEST, true, null);
assertNotNull(bindings);
return bindings;
}
}

}
Loading