Polishing contribution
Closes gh-849
This commit is contained in:
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.graphql.client;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import graphql.ErrorClassification;
|
||||
@@ -125,29 +123,28 @@ class ResponseMapGraphQlResponse extends AbstractGraphQlResponse {
|
||||
MapResponseError(Map<String, Object> errorMap) {
|
||||
Assert.notNull(errorMap, "'errorMap' is required");
|
||||
this.errorMap = errorMap;
|
||||
this.locations = initLocations(errorMap);
|
||||
this.locations = initSourceLocations(errorMap);
|
||||
this.path = initPath(errorMap);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static List<SourceLocation> initLocations(Map<String, Object> errorMap) {
|
||||
return ((List<Map<String, Object>>) errorMap.getOrDefault("locations", Collections.emptyList())).stream()
|
||||
.map(map -> new SourceLocation(
|
||||
objectAsInt(map.get("line")),
|
||||
objectAsInt(map.get("column")),
|
||||
Objects.toString(map.get("sourceName"))
|
||||
))
|
||||
private static List<SourceLocation> initSourceLocations(Map<String, Object> errorMap) {
|
||||
List<Map<String, Object>> locations = (List<Map<String, Object>>) errorMap.get("locations");
|
||||
if (locations == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return locations.stream()
|
||||
.map(m -> new SourceLocation(getInt(m, "line"), getInt(m, "column"), (String) m.get("sourceName")))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static int objectAsInt(Object value) {
|
||||
|
||||
if (value instanceof BigInteger bigInteger) {
|
||||
return bigInteger.intValue();
|
||||
} else if (value instanceof Number number) {
|
||||
private static int getInt(Map<String, Object> map, String key) {
|
||||
if (map.get(key) instanceof Number number) {
|
||||
return number.intValue();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
"Expected integer value: " + ObjectUtils.nullSafeClassName(map.get(key)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,6 +159,7 @@ class ResponseMapGraphQlResponse extends AbstractGraphQlResponse {
|
||||
(s, s2) -> null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getMessage() {
|
||||
|
||||
@@ -138,24 +138,19 @@ public class DefaultGraphQlClientResponseTests {
|
||||
assertThat(errors.get(2).getPath()).isEqualTo("me.friends[0].name");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // gh-849
|
||||
void errorWithBigIntegerNumbers() throws IOException {
|
||||
|
||||
String path = "me.friends";
|
||||
GraphQLError error = createError("/me", "fail-me", new SourceLocation(100, 100));
|
||||
|
||||
GraphQLError error0 = createError("/me", "fail-me", new SourceLocation(100, 100));
|
||||
ObjectMapper mapper = new ObjectMapper().enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
|
||||
Map<?, ?> errorMap = mapper.readValue(mapper.writeValueAsString(error), Map.class);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper().enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
|
||||
String error0string = objectMapper.writeValueAsString(error0);
|
||||
Map<?, ?> error0Map = objectMapper.readValue(error0string, Map.class);
|
||||
|
||||
List<?> list = List.of(error0Map);
|
||||
|
||||
ClientGraphQlResponse response = createResponse(Collections.singletonMap("errors", list));
|
||||
ClientGraphQlResponse response = createResponse(Collections.singletonMap("errors", List.of(errorMap)));
|
||||
ClientResponseField field = response.field(path);
|
||||
|
||||
List<ResponseError> errors = field.getErrors();
|
||||
|
||||
assertThat(errors).hasSize(1);
|
||||
assertThat(errors.get(0).getPath()).isEqualTo("me");
|
||||
assertThat(errors.get(0).getLocations().get(0).getLine()).isEqualTo(100);
|
||||
|
||||
Reference in New Issue
Block a user