Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/main/java/graphql/GraphqlErrorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;
import java.util.Objects;

import static graphql.collect.ImmutableKit.map;
import static graphql.collect.ImmutableKit.mapAndDropNulls;

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

public static Object locations(List<SourceLocation> locations) {
return map(locations, GraphqlErrorHelper::location);
return mapAndDropNulls(locations, GraphqlErrorHelper::location);
}

/**
* Positive integers starting from 1 required for error locations,
* from the spec <a href="https://spec.graphql.org/draft/#sec-Errors.Error-Result-Format">...</a>
*/
public static Object location(SourceLocation location) {
Map<String, Integer> map = new LinkedHashMap<>();
map.put("line", location.getLine());
map.put("column", location.getColumn());
return map;
int line = location.getLine();
int column = location.getColumn();
if (line < 1 || column < 1) {
return null;
}
return Map.of("line", line, "column", column);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny change, this becomes an immutable map

}

public static int hashCode(GraphQLError dis) {
Expand Down
28 changes: 25 additions & 3 deletions src/test/groovy/graphql/GraphQLErrorTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GraphQLErrorTest extends Specification {
.description("Test ValidationError")
.build() |
[
locations: [[line: 666, column: 999], [line: 333, column: 0]],
locations: [[line: 666, column: 999], [line: 333, column: 1]],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do these numbers chance - what caused that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past, our tests had "0" being used as a valid column location. This PR bans anything less than 1 for line and column number, which made these old tests fail. So I adjusted these old tests to use a valid location number

Then added a new test with invalid numbers to check the new filtering behaviour

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The source locations for these old tests are generated by a method lower down in this file

List<SourceLocation> mkLocations()

message : "Test ValidationError",
extensions:[classification:"ValidationError"],
]
Expand All @@ -44,7 +44,7 @@ class GraphQLErrorTest extends Specification {

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

}

def "toSpecification filters out error locations with line and column not starting at 1, as required in spec"() {
// See specification wording: https://spec.graphql.org/draft/#sec-Errors.Error-Result-Format

given:
def error = ValidationError.newValidationError()
.validationErrorType(ValidationErrorType.UnknownType)
.sourceLocations([mkLocation(-1, -1), mkLocation(333, 1)])
.description("Test ValidationError")
.build()

def expectedMap = [
locations: [
[line: 333, column: 1]
],
message: "Test ValidationError",
extensions: [classification:"ValidationError"]
]

expect:
error.toSpecification() == expectedMap
}

class CustomException extends RuntimeException implements GraphQLError {
private LinkedHashMap<String, String> map

Expand Down Expand Up @@ -110,7 +132,7 @@ class GraphQLErrorTest extends Specification {
}

List<SourceLocation> mkLocations() {
return [mkLocation(666, 999), mkLocation(333, 0)]
return [mkLocation(666, 999), mkLocation(333, 1)]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix old tests to be compliant

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I see

}

SourceLocation mkLocation(int line, int column) {
Expand Down
Loading