未验证 提交 768eaa24 编写于 作者: B Bogdan Kobylynskyi 提交者: GitHub

GraphQL serialization of java.util.Map #668 (#669)

上级 2cb4ae93
......@@ -145,7 +145,7 @@ public class GraphQLRequestSerializer {
/**
* Serialize object to a string
*
* @param input can be any object or collection of objects.
* @param input can be any object or collection/map of objects.
* @param useObjectMapper if true, then use Jackson's ObjectMapper to convert from object to string,
* otherwise use toString
* @return serialized object
......@@ -154,19 +154,16 @@ public class GraphQLRequestSerializer {
public static String getEntry(Object input, boolean useObjectMapper) {
if (input == null) {
return null;
}
if (input instanceof Collection<?>) {
StringJoiner joiner = new StringJoiner(", ", "[ ", " ]");
for (Object collectionEntry : (Collection<?>) input) {
joiner.add(getEntry(collectionEntry, useObjectMapper));
}
return joiner.toString();
}
if (useObjectMapper) {
} else if (useObjectMapper) {
return objectMapperWriteValueAsString(input);
}
if (input instanceof Enum<?>) {
return input.toString();
} else if (input instanceof Collection<?>) {
return serializeCollection((Collection<?>) input, useObjectMapper);
} else if (input instanceof Map<?, ?>) {
return serializeMap((Map<?, ?>) input, useObjectMapper);
} else if (input instanceof Map.Entry<?, ?>) {
return serializeMapEntry((Map.Entry<?, ?>) input, useObjectMapper);
} else if (input instanceof Enum<?>) {
return serializeEnum((Enum<?>) input);
} else if (input instanceof String) {
return escapeJsonString(input.toString());
} else if (input.getClass().getName().equals("scala.Some")) { // TODO: move to Scala Serializer
......@@ -180,13 +177,29 @@ public class GraphQLRequestSerializer {
}
}
public static String serializeCollection(Collection<?> input, boolean useObjectMapper) {
StringJoiner joiner = new StringJoiner(", ", "[ ", " ]");
for (Object entry : input) {
joiner.add(getEntry(entry, useObjectMapper));
}
return joiner.toString();
}
public static String objectMapperWriteValueAsString(Object input) {
try {
return OBJECT_MAPPER.writeValueAsString(input);
} catch (JsonProcessingException e) {
throw new UnableToBuildJsonQueryException(e);
public static String serializeMap(Map<?, ?> input, boolean useObjectMapper) {
StringJoiner joiner = new StringJoiner(", ", "{ ", " }");
for (Map.Entry<?, ?> entry : input.entrySet()) {
joiner.add(getEntry(entry, useObjectMapper));
}
return joiner.toString();
}
public static String serializeMapEntry(Map.Entry<?, ?> input, boolean useObjectMapper) {
// no need to quote String key
return input.getKey() + ": " + getEntry(input.getValue(), useObjectMapper);
}
public static String serializeEnum(Enum<?> input) {
return input.toString();
}
/**
......@@ -231,4 +244,12 @@ public class GraphQLRequestSerializer {
return sb.toString();
}
public static String objectMapperWriteValueAsString(Object input) {
try {
return OBJECT_MAPPER.writeValueAsString(input);
} catch (JsonProcessingException e) {
throw new UnableToBuildJsonQueryException(e);
}
}
}
......@@ -26,6 +26,8 @@ import org.junit.jupiter.params.provider.MethodSource;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
......@@ -422,6 +424,7 @@ class GraphQLRequestSerializerTest {
.setContextId("something")
.setIds(null)
.setTranslated(false)
.setEntries(null)
.build();
GraphQLRequest graphQLRequest = new GraphQLRequest(request,
new EventResponseProjection()
......@@ -433,6 +436,32 @@ class GraphQLRequestSerializerTest {
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
@ParameterizedTest(name = "{0}")
@MethodSource("provideAllSerializers")
void serialize_requestWithMap(String name, Function<GraphQLRequest, String> serializer,
Function<String, String> expectedQueryDecorator) {
Map<Object, Object> entries = new HashMap<>();
entries.put("entryInt", 1);
entries.put("entryString", "2");
entries.put("entryFloat", 3.3);
entries.put("entryNull", null);
EventsByIdsQueryRequest request = new EventsByIdsQueryRequest.Builder()
.setContextId("something")
.setIds(null)
.setTranslated(false)
.setEntries(entries)
.build();
GraphQLRequest graphQLRequest = new GraphQLRequest(request,
new EventResponseProjection()
.id()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query eventsByIds { " +
"eventsByIds(contextId: \"something\", translated: false, entries: " +
"{ entryNull: null, entryInt: 1, entryFloat: 3.3, entryString: \"2\" }){ id } }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
@ParameterizedTest(name = "{0}")
@MethodSource("provideAllSerializers")
void serialize_AllInputsNull(String name, Function<GraphQLRequest, String> serializer,
......
......@@ -37,6 +37,10 @@ public class EventsByIdsQueryRequest implements GraphQLOperationRequest {
this.input.put("translated", translated);
}
public void setEntries(Map<Object, Object> entries) {
this.input.put("entries", entries);
}
@Override
public GraphQLOperation getOperationType() {
return OPERATION_TYPE;
......@@ -72,6 +76,7 @@ public class EventsByIdsQueryRequest implements GraphQLOperationRequest {
private String contextId;
private Collection<String> ids;
private Boolean translated;
private Map<Object, Object> entries;
public Builder() {
}
......@@ -91,12 +96,18 @@ public class EventsByIdsQueryRequest implements GraphQLOperationRequest {
return this;
}
public Builder setEntries(Map<Object, Object> entries) {
this.entries = entries;
return this;
}
public EventsByIdsQueryRequest build() {
EventsByIdsQueryRequest obj = new EventsByIdsQueryRequest();
obj.setContextId(contextId);
obj.setIds(ids);
obj.setTranslated(translated);
obj.setEntries(entries);
return obj;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册