Skip to content

Commit 3147492

Browse files
committed
graphql variables type judgment
1 parent 5e09800 commit 3147492

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

server/openblocks-plugins/graphqlPlugin/src/main/java/com/openblocks/plugin/graphql/GraphQLExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import static com.openblocks.sdk.plugin.restapi.auth.RestApiAuthType.DIGEST_AUTH;
1212
import static com.openblocks.sdk.util.JsonUtils.readTree;
1313
import static com.openblocks.sdk.util.JsonUtils.toJsonThrows;
14-
import static com.openblocks.sdk.util.MustacheHelper.renderMustacheForObjectValue;
1514
import static com.openblocks.sdk.util.MustacheHelper.renderMustacheString;
1615
import static com.openblocks.sdk.util.StreamUtils.collectList;
1716
import static com.openblocks.sdk.util.StreamUtils.distinctByKey;
@@ -74,6 +73,7 @@
7473
import com.openblocks.sdk.plugin.restapi.auth.RestApiAuthType;
7574
import com.openblocks.sdk.query.QueryVisitorContext;
7675
import com.openblocks.sdk.util.JsonUtils;
76+
import com.openblocks.sdk.util.MustacheHelper;
7777
import com.openblocks.sdk.webclient.WebClients;
7878

7979
import lombok.Builder;
@@ -139,7 +139,7 @@ public GraphQLQueryExecutionContext buildQueryExecutionContext(GraphQLDatasource
139139
List<Property> updatedQueryBodyParams = renderMustacheValueInProperties(queryBodyParams, requestParams);
140140
var updatedVariables = JsonUtils.createObjectNode();
141141
queryConfig.getVariables().forEach(property -> updatedVariables.set(property.getKey(),
142-
JsonUtils.valueToTree(renderMustacheForObjectValue(property.getValue(), requestParams))));
142+
MustacheHelper.renderMustacheJson(property.getValue(), requestParams)));
143143
String normalizedUrl = buildUrl(urlDomain, updatedQueryPath, requestParams);
144144
Map<String, String> allHeaders = buildHeaders(datasourceHeaders, updatedQueryHeaders);
145145
String contentType = parseContentType(allHeaders).toLowerCase();

server/openblocks-sdk/src/main/java/com/openblocks/sdk/plugin/sheet/changeset/SheetKeyValuePairChangeSet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.openblocks.sdk.plugin.sheet.changeset;
22

33
import static com.openblocks.sdk.exception.PluginCommonError.INVALID_GUI_SETTINGS;
4-
import static com.openblocks.sdk.util.MustacheHelper.renderMustacheForObjectValue;
4+
import static com.openblocks.sdk.util.MustacheHelper.renderMustacheJson;
55
import static java.util.Collections.emptyMap;
66

77
import java.util.ArrayList;
@@ -14,6 +14,7 @@
1414
import org.apache.commons.lang3.tuple.Pair;
1515

1616
import com.openblocks.sdk.exception.PluginException;
17+
import com.openblocks.sdk.util.JsonUtils;
1718

1819
import lombok.extern.slf4j.Slf4j;
1920

@@ -46,7 +47,7 @@ public SheetKeyValuePairChangeSet(Object comp) {
4647
public SheetChangeSetRow render(Map<String, Object> requestMap) {
4748
List<SheetChangeSetItem> result = new ArrayList<>();
4849
for (String column : map.keySet()) {
49-
Object renderedValue = renderMustacheForObjectValue(map.get(column), requestMap);
50+
Object renderedValue = JsonUtils.jsonNodeToObject(renderMustacheJson(map.get(column), requestMap));
5051
result.add(new SheetChangeSetItem(column, renderedValue));
5152
}
5253
return new SheetChangeSetRow(result);

server/openblocks-sdk/src/main/java/com/openblocks/sdk/util/MustacheHelper.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,6 @@ public static String renderMustacheString(String template, Map<String, ?> paramM
298298
return renderMustacheTokens(tokenize, paramMap);
299299
}
300300

301-
public static Object renderMustacheForObjectValue(String template, Map<String, ?> paramMap) {
302-
if (isBlank(template)) {
303-
return template;
304-
}
305-
List<String> tokenize = tokenize(template.trim());
306-
if (tokenize.size() == 1) {
307-
String token = tokenize.get(0);
308-
if (isMustacheToken(token)) {
309-
String key = removeCurlyBraces(token);
310-
if (paramMap.containsKey(key)) {
311-
return paramMap.get(key);
312-
}
313-
}
314-
}
315-
return renderMustacheString(template, paramMap);
316-
}
317-
318301
@SuppressWarnings("DuplicatedCode")
319302
public static String renderMustacheStringWithoutRemoveSurroundedPar(String template, Map<String, ?> paramMap) {
320303
if (isBlank(template)) {

server/openblocks-sdk/src/test/java/com/openblocks/sdk/helpers/MustacheHelperTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.assertj.core.api.Assertions.assertThat;
88

99
import java.util.Arrays;
10+
import java.util.Collection;
1011
import java.util.HashMap;
1112
import java.util.List;
1213
import java.util.Map;
@@ -17,6 +18,7 @@
1718

1819
import com.google.common.collect.ImmutableList;
1920
import com.google.common.collect.Lists;
21+
import com.openblocks.sdk.util.JsonUtils;
2022
import com.openblocks.sdk.util.MustacheHelper;
2123

2224
@SuppressWarnings(
@@ -443,4 +445,30 @@ public void testRenderMustacheJsonString() {
443445
String s = MustacheHelper.renderMustacheJsonString(json, map);
444446
System.out.println(s);
445447
}
448+
449+
@Test
450+
public void typeJudgment() {
451+
Object number = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("1", Map.of()));
452+
Assert.assertTrue(number instanceof Integer);
453+
Object b = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("true", Map.of()));
454+
Assert.assertTrue(b instanceof Boolean);
455+
Object array = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("['abc', 1]", Map.of()));
456+
Assert.assertTrue(array instanceof Collection<?>);
457+
Object array2 = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("[abc, cde]", Map.of()));
458+
Assert.assertTrue(array2 instanceof Collection<?>);
459+
Object o = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("{'a': 1, 'b': 'xx'}", Map.of()));
460+
Assert.assertTrue(o instanceof Map<?, ?>);
461+
Object o2 = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("{a:1, b:2}", Map.of()));
462+
Assert.assertTrue(o2 instanceof Map<?, ?>);
463+
Object o3 = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("{{3}}3", Map.of("3", 3)));
464+
Assert.assertEquals(o3, "33");
465+
Object o4 = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("{{3}}3", Map.of("3", "3")));
466+
Assert.assertEquals(o4, "33");
467+
Object o5 = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("{{t}}rue", Map.of("t", "t")));
468+
Assert.assertEquals(o5, "true");
469+
Object o6 = JsonUtils.jsonNodeToObject(MustacheHelper.renderMustacheJson("[1, {{3}}3]", Map.of("3", 3)));
470+
Object[] objects = ((Collection<?>) o6).toArray();
471+
Assert.assertEquals(objects[0], 1);
472+
Assert.assertEquals(objects[1], "33");
473+
}
446474
}

0 commit comments

Comments
 (0)