diff --git a/spring-graphql/src/main/java/org/springframework/graphql/client/ResponseMapGraphQlResponse.java b/spring-graphql/src/main/java/org/springframework/graphql/client/ResponseMapGraphQlResponse.java index d8f08951..22f82976 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/client/ResponseMapGraphQlResponse.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/client/ResponseMapGraphQlResponse.java @@ -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 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 initLocations(Map errorMap) { - return ((List>) 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 initSourceLocations(Map errorMap) { + List> locations = (List>) 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 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() { diff --git a/spring-graphql/src/test/java/org/springframework/graphql/client/DefaultGraphQlClientResponseTests.java b/spring-graphql/src/test/java/org/springframework/graphql/client/DefaultGraphQlClientResponseTests.java index 666198ca..c39be5a3 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/client/DefaultGraphQlClientResponseTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/client/DefaultGraphQlClientResponseTests.java @@ -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 errors = field.getErrors(); - assertThat(errors).hasSize(1); assertThat(errors.get(0).getPath()).isEqualTo("me"); assertThat(errors.get(0).getLocations().get(0).getLine()).isEqualTo(100);