Skip to content

Commit 91e6efa

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents d84eb74 + 1598445 commit 91e6efa

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/main/java/graphql/GraphqlErrorHelper.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Map;
88
import java.util.Objects;
99

10-
import static graphql.collect.ImmutableKit.map;
10+
import static graphql.collect.ImmutableKit.mapAndDropNulls;
1111

1212
/**
1313
* This little helper allows GraphQlErrors to implement
@@ -51,14 +51,20 @@ public static Map<String, Object> toSpecification(GraphQLError error) {
5151
}
5252

5353
public static Object locations(List<SourceLocation> locations) {
54-
return map(locations, GraphqlErrorHelper::location);
54+
return mapAndDropNulls(locations, GraphqlErrorHelper::location);
5555
}
5656

57+
/**
58+
* Positive integers starting from 1 required for error locations,
59+
* from the spec <a href="https://spec.graphql.org/draft/#sec-Errors.Error-Result-Format">...</a>
60+
*/
5761
public static Object location(SourceLocation location) {
58-
Map<String, Integer> map = new LinkedHashMap<>();
59-
map.put("line", location.getLine());
60-
map.put("column", location.getColumn());
61-
return map;
62+
int line = location.getLine();
63+
int column = location.getColumn();
64+
if (line < 1 || column < 1) {
65+
return null;
66+
}
67+
return Map.of("line", line, "column", column);
6268
}
6369

6470
public static int hashCode(GraphQLError dis) {

src/test/groovy/graphql/GraphQLErrorTest.groovy

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class GraphQLErrorTest extends Specification {
3131
.description("Test ValidationError")
3232
.build() |
3333
[
34-
locations: [[line: 666, column: 999], [line: 333, column: 0]],
34+
locations: [[line: 666, column: 999], [line: 333, column: 1]],
3535
message : "Test ValidationError",
3636
extensions:[classification:"ValidationError"],
3737
]
@@ -44,7 +44,7 @@ class GraphQLErrorTest extends Specification {
4444

4545
new InvalidSyntaxError(mkLocations(), "Not good syntax m'kay") |
4646
[
47-
locations: [[line: 666, column: 999], [line: 333, column: 0]],
47+
locations: [[line: 666, column: 999], [line: 333, column: 1]],
4848
message : "Not good syntax m'kay",
4949
extensions:[classification:"InvalidSyntax"],
5050
]
@@ -72,6 +72,28 @@ class GraphQLErrorTest extends Specification {
7272

7373
}
7474

75+
def "toSpecification filters out error locations with line and column not starting at 1, as required in spec"() {
76+
// See specification wording: https://spec.graphql.org/draft/#sec-Errors.Error-Result-Format
77+
78+
given:
79+
def error = ValidationError.newValidationError()
80+
.validationErrorType(ValidationErrorType.UnknownType)
81+
.sourceLocations([mkLocation(-1, -1), mkLocation(333, 1)])
82+
.description("Test ValidationError")
83+
.build()
84+
85+
def expectedMap = [
86+
locations: [
87+
[line: 333, column: 1]
88+
],
89+
message: "Test ValidationError",
90+
extensions: [classification:"ValidationError"]
91+
]
92+
93+
expect:
94+
error.toSpecification() == expectedMap
95+
}
96+
7597
class CustomException extends RuntimeException implements GraphQLError {
7698
private LinkedHashMap<String, String> map
7799

@@ -110,7 +132,7 @@ class GraphQLErrorTest extends Specification {
110132
}
111133

112134
List<SourceLocation> mkLocations() {
113-
return [mkLocation(666, 999), mkLocation(333, 0)]
135+
return [mkLocation(666, 999), mkLocation(333, 1)]
114136
}
115137

116138
SourceLocation mkLocation(int line, int column) {

0 commit comments

Comments
 (0)