Skip to content

Commit bc388c4

Browse files
LarsKrogJensenbbakerman
authored andcommitted
ParsedDocumentEntry's fields are made final and added some guards to prevent null inputs.
Added unit test for ParsedDocumentEntry as well.
1 parent 3c6e9cb commit bc388c4

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/main/java/graphql/execution/preparsed/PreparsedDocumentEntry.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@
55

66
import java.util.Collections;
77
import java.util.List;
8+
import java.util.Objects;
89

910
/**
1011
* An instance of a preparsed doucument entry represents the result of a query parse and validation, like
1112
* an either implementation it contains either the correct result in th document property or the errors.
1213
*/
1314
public class PreparsedDocumentEntry {
14-
private Document document;
15-
private List<? extends GraphQLError> errors;
15+
private final Document document;
16+
private final List<? extends GraphQLError> errors;
1617

1718
public PreparsedDocumentEntry(Document document) {
19+
Objects.requireNonNull(document);
1820
this.document = document;
21+
this.errors = null;
1922
}
2023

2124
public PreparsedDocumentEntry(List<? extends GraphQLError> errors) {
25+
Objects.requireNonNull(errors);
26+
this.document = null;
2227
this.errors = errors;
2328
}
2429

2530
public PreparsedDocumentEntry(GraphQLError error) {
26-
this.errors = Collections.singletonList(error);
31+
this(Collections.singletonList(Objects.requireNonNull(error)));
2732
}
2833

2934
public Document getDocument() {
@@ -33,8 +38,7 @@ public Document getDocument() {
3338
public List<? extends GraphQLError> getErrors() {
3439
return errors;
3540
}
36-
37-
41+
3842
public boolean hasErrors() {
3943
return errors != null && !errors.isEmpty();
4044
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package graphql.execution.preparsed
2+
3+
import graphql.GraphQLError
4+
import graphql.InvalidSyntaxError
5+
import graphql.language.Document
6+
import graphql.language.SourceLocation
7+
import graphql.validation.ValidationError
8+
import graphql.validation.ValidationErrorType
9+
import spock.lang.Specification
10+
11+
class PreparsedDocumentEntryTest extends Specification {
12+
def "Ensure a non-null document returns"() {
13+
given:
14+
def document = new Document()
15+
16+
when:
17+
def docEntry = new PreparsedDocumentEntry(document)
18+
19+
then:
20+
docEntry.document == document
21+
docEntry.errors == null
22+
}
23+
24+
def "Ensure a null document throws NPE"() {
25+
when:
26+
new PreparsedDocumentEntry((Document) null)
27+
28+
then:
29+
thrown(NullPointerException)
30+
}
31+
32+
def "Ensure a non-null errors returns"() {
33+
given:
34+
def errors = [new InvalidSyntaxError(new SourceLocation(0, 0)),
35+
new ValidationError(ValidationErrorType.InvalidSyntax)]
36+
37+
when:
38+
def docEntry = new PreparsedDocumentEntry(errors)
39+
40+
then:
41+
docEntry.document == null
42+
docEntry.errors == errors
43+
}
44+
45+
def "Ensure a null errors throws NPE"() {
46+
when:
47+
new PreparsedDocumentEntry((List<GraphQLError>) null)
48+
49+
then:
50+
thrown(NullPointerException)
51+
}
52+
53+
def "Ensure a null error throws NPE"() {
54+
when:
55+
new PreparsedDocumentEntry((GraphQLError) null)
56+
57+
then:
58+
thrown(NullPointerException)
59+
}
60+
61+
62+
}

0 commit comments

Comments
 (0)