Skip to content

Commit acdd2a1

Browse files
committed
Polishing contribution
Closes gh-849
1 parent 47aa333 commit acdd2a1

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

spring-graphql/src/main/java/org/springframework/graphql/client/ResponseMapGraphQlResponse.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
package org.springframework.graphql.client;
1818

19-
import java.math.BigInteger;
2019
import java.util.Collections;
2120
import java.util.List;
2221
import java.util.Map;
23-
import java.util.Objects;
2422
import java.util.stream.Collectors;
2523

2624
import graphql.ErrorClassification;
@@ -125,29 +123,28 @@ private static final class MapResponseError implements ResponseError {
125123
MapResponseError(Map<String, Object> errorMap) {
126124
Assert.notNull(errorMap, "'errorMap' is required");
127125
this.errorMap = errorMap;
128-
this.locations = initLocations(errorMap);
126+
this.locations = initSourceLocations(errorMap);
129127
this.path = initPath(errorMap);
130128
}
131129

132130
@SuppressWarnings("unchecked")
133-
private static List<SourceLocation> initLocations(Map<String, Object> errorMap) {
134-
return ((List<Map<String, Object>>) errorMap.getOrDefault("locations", Collections.emptyList())).stream()
135-
.map(map -> new SourceLocation(
136-
objectAsInt(map.get("line")),
137-
objectAsInt(map.get("column")),
138-
Objects.toString(map.get("sourceName"))
139-
))
131+
private static List<SourceLocation> initSourceLocations(Map<String, Object> errorMap) {
132+
List<Map<String, Object>> locations = (List<Map<String, Object>>) errorMap.get("locations");
133+
if (locations == null) {
134+
return Collections.emptyList();
135+
}
136+
return locations.stream()
137+
.map(m -> new SourceLocation(getInt(m, "line"), getInt(m, "column"), (String) m.get("sourceName")))
140138
.collect(Collectors.toList());
141139
}
142140

143-
private static int objectAsInt(Object value) {
144-
145-
if (value instanceof BigInteger bigInteger) {
146-
return bigInteger.intValue();
147-
} else if (value instanceof Number number) {
141+
private static int getInt(Map<String, Object> map, String key) {
142+
if (map.get(key) instanceof Number number) {
148143
return number.intValue();
149-
} else {
150-
return -1;
144+
}
145+
else {
146+
throw new IllegalArgumentException(
147+
"Expected integer value: " + ObjectUtils.nullSafeClassName(map.get(key)));
151148
}
152149
}
153150

@@ -162,6 +159,7 @@ private static String initPath(Map<String, Object> errorMap) {
162159
(s, s2) -> null);
163160
}
164161

162+
165163
@Override
166164
@Nullable
167165
public String getMessage() {

spring-graphql/src/test/java/org/springframework/graphql/client/DefaultGraphQlClientResponseTests.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,19 @@ void fieldErrors() {
138138
assertThat(errors.get(2).getPath()).isEqualTo("me.friends[0].name");
139139
}
140140

141-
@Test
141+
@Test // gh-849
142142
void errorWithBigIntegerNumbers() throws IOException {
143143

144144
String path = "me.friends";
145+
GraphQLError error = createError("/me", "fail-me", new SourceLocation(100, 100));
145146

146-
GraphQLError error0 = createError("/me", "fail-me", new SourceLocation(100, 100));
147-
148-
ObjectMapper objectMapper = new ObjectMapper().enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
149-
String error0string = objectMapper.writeValueAsString(error0);
150-
Map<?, ?> error0Map = objectMapper.readValue(error0string, Map.class);
151-
152-
List<?> list = List.of(error0Map);
147+
ObjectMapper mapper = new ObjectMapper().enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
148+
Map<?, ?> errorMap = mapper.readValue(mapper.writeValueAsString(error), Map.class);
153149

154-
ClientGraphQlResponse response = createResponse(Collections.singletonMap("errors", list));
150+
ClientGraphQlResponse response = createResponse(Collections.singletonMap("errors", List.of(errorMap)));
155151
ClientResponseField field = response.field(path);
156152

157153
List<ResponseError> errors = field.getErrors();
158-
159154
assertThat(errors).hasSize(1);
160155
assertThat(errors.get(0).getPath()).isEqualTo("me");
161156
assertThat(errors.get(0).getLocations().get(0).getLine()).isEqualTo(100);

0 commit comments

Comments
 (0)