Skip to content

Commit ea5f959

Browse files
committed
bugfix: comments as descriptions
this is a small bugfix for enum field descriptions and a change so that an empty line in an comment separate a comment from a description (for the IDL) also: empty lines are not skipped, but are valid comments
1 parent d8bfa00 commit ea5f959

4 files changed

Lines changed: 148 additions & 43 deletions

File tree

‎src/main/java/graphql/parser/GraphqlAntlrToLanguage.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,11 @@ private List<Comment> getCommentOnChannel(List<Token> refChannel) {
822822
// we strip the leading hash # character but we don't trim because we don't
823823
// know the "comment markup". Maybe its space sensitive, maybe its not. So
824824
// consumers can decide that
825-
text = (text == null) ? "" : text;
826-
text = text.replaceFirst("^#", "");
827-
if (text.length() > 0) {
828-
comments.add(new Comment(text, new SourceLocation(refTok.getLine(), refTok.getCharPositionInLine())));
825+
if (text == null) {
826+
continue;
829827
}
828+
text = text.replaceFirst("^#", "");
829+
comments.add(new Comment(text, new SourceLocation(refTok.getLine(), refTok.getCharPositionInLine())));
830830
}
831831
return comments;
832832
}

‎src/main/java/graphql/schema/GraphQLEnumType.java‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package graphql.schema;
22

33

4+
import graphql.AssertException;
45
import graphql.language.EnumTypeDefinition;
56
import graphql.language.EnumValue;
6-
import graphql.AssertException;
77

88
import java.util.ArrayList;
99
import java.util.LinkedHashMap;
@@ -40,32 +40,9 @@ public Object parseLiteral(Object input) {
4040
}
4141
};
4242

43-
private Object getValueByName(Object value) {
44-
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value.toString());
45-
if (enumValueDefinition != null) return enumValueDefinition.getValue();
46-
return null;
47-
}
48-
49-
private Object getNameByValue(Object value) {
50-
if (value == null) {
51-
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
52-
if (valueDefinition.getValue() == null) return valueDefinition.getName();
53-
}
54-
} else {
55-
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
56-
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
57-
}
58-
}
59-
return null;
60-
}
61-
62-
public List<GraphQLEnumValueDefinition> getValues() {
63-
return new ArrayList<>(valueDefinitionMap.values());
64-
}
65-
6643

6744
public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values) {
68-
this(name,description,values,null);
45+
this(name, description, values, null);
6946
}
7047

7148
public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values, EnumTypeDefinition definition) {
@@ -76,6 +53,14 @@ public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDef
7653
buildMap(values);
7754
}
7855

56+
public List<GraphQLEnumValueDefinition> getValues() {
57+
return new ArrayList<>(valueDefinitionMap.values());
58+
}
59+
60+
public GraphQLEnumValueDefinition getValue(String name) {
61+
return valueDefinitionMap.get(name);
62+
}
63+
7964
private void buildMap(List<GraphQLEnumValueDefinition> values) {
8065
for (GraphQLEnumValueDefinition valueDefinition : values) {
8166
String name = valueDefinition.getName();
@@ -85,6 +70,26 @@ private void buildMap(List<GraphQLEnumValueDefinition> values) {
8570
}
8671
}
8772

73+
private Object getValueByName(Object value) {
74+
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value.toString());
75+
if (enumValueDefinition != null) return enumValueDefinition.getValue();
76+
return null;
77+
}
78+
79+
private Object getNameByValue(Object value) {
80+
if (value == null) {
81+
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
82+
if (valueDefinition.getValue() == null) return valueDefinition.getName();
83+
}
84+
} else {
85+
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
86+
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
87+
}
88+
}
89+
return null;
90+
}
91+
92+
8893
public String getName() {
8994
return name;
9095
}

‎src/main/java/graphql/schema/idl/SchemaGenerator.java‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@
4545
import graphql.schema.idl.errors.NotAnOutputTypeError;
4646
import graphql.schema.idl.errors.SchemaProblem;
4747

48+
import java.util.ArrayList;
4849
import java.util.Collections;
4950
import java.util.HashMap;
5051
import java.util.LinkedHashMap;
5152
import java.util.List;
5253
import java.util.Map;
5354
import java.util.Optional;
5455
import java.util.Stack;
56+
import java.util.stream.Collectors;
5557

5658
import static graphql.Assert.assertNotNull;
5759

@@ -366,7 +368,8 @@ private GraphQLEnumType buildEnumType(EnumTypeDefinition typeDefinition) {
366368
builder.name(typeDefinition.getName());
367369
builder.description(buildDescription(typeDefinition));
368370

369-
typeDefinition.getEnumValueDefinitions().forEach(evd -> builder.value(evd.getName()));
371+
typeDefinition.getEnumValueDefinitions().forEach(evd -> builder.value(evd.getName(), evd.getName(), buildDescription(evd)));
372+
// typeDefinition.getEnumValueDefinitions().forEach(evd -> builder.value(evd.getName()));
370373
return builder.build();
371374
}
372375

@@ -517,14 +520,17 @@ private TypeResolver getTypeResolverForInterface(BuildContext buildCtx, Interfac
517520

518521

519522
private String buildDescription(Node node) {
520-
StringBuilder sb = new StringBuilder();
521523
List<Comment> comments = node.getComments();
524+
List<String> lines = new ArrayList<>();
522525
for (int i = 0; i < comments.size(); i++) {
523-
if (i > 0) {
524-
sb.append("\n");
526+
String commentLine = comments.get(i).getContent().trim();
527+
if (commentLine.isEmpty()) {
528+
lines.clear();
529+
} else {
530+
lines.add(commentLine);
525531
}
526-
sb.append(comments.get(i).getContent().trim());
527532
}
528-
return sb.toString();
533+
if (lines.size() == 0) return null;
534+
return lines.stream().collect(Collectors.joining("\n"));
529535
}
530536
}

‎src/test/groovy/graphql/schema/idl/SchemaGeneratorTest.groovy‎

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ class SchemaGeneratorTest extends Specification {
289289
foobar.type instanceof GraphQLUnionType
290290
def types = ((GraphQLUnionType) foobar.type).getTypes();
291291
types.size() == 2
292-
types[0] instanceof GraphQLObjectType
293-
types[1] instanceof GraphQLObjectType
292+
types[0] instanceof GraphQLObjectType
293+
types[1] instanceof GraphQLObjectType
294294
types[0].name == "Foo"
295295
types[1].name == "Bar"
296296

@@ -328,8 +328,8 @@ class SchemaGeneratorTest extends Specification {
328328
foobar.type instanceof GraphQLUnionType
329329
def types = ((GraphQLUnionType) foobar.type).getTypes();
330330
types.size() == 2
331-
types[0] instanceof GraphQLObjectType
332-
types[1] instanceof GraphQLObjectType
331+
types[0] instanceof GraphQLObjectType
332+
types[1] instanceof GraphQLObjectType
333333
types[0].name == "Foo"
334334
types[1].name == "Bar"
335335

@@ -367,8 +367,8 @@ class SchemaGeneratorTest extends Specification {
367367
foobar.type instanceof GraphQLUnionType
368368
def types = ((GraphQLUnionType) foobar.type).getTypes();
369369
types.size() == 2
370-
types[0] instanceof GraphQLObjectType
371-
types[1] instanceof GraphQLObjectType
370+
types[0] instanceof GraphQLObjectType
371+
types[1] instanceof GraphQLObjectType
372372
types[0].name == "Foo"
373373
types[1].name == "Bar"
374374

@@ -406,8 +406,8 @@ class SchemaGeneratorTest extends Specification {
406406
foobar.type instanceof GraphQLUnionType
407407
def types = ((GraphQLUnionType) foobar.type).getTypes();
408408
types.size() == 2
409-
types[0] instanceof GraphQLObjectType
410-
types[1] instanceof GraphQLObjectType
409+
types[0] instanceof GraphQLObjectType
410+
types[1] instanceof GraphQLObjectType
411411
types[0].name == "Foo"
412412
types[1].name == "Bar"
413413

@@ -697,4 +697,98 @@ class SchemaGeneratorTest extends Specification {
697697
then:
698698
schema.getSubscriptionType().name == "Subscription"
699699
}
700+
701+
702+
def "comments are used as descriptions"() {
703+
given:
704+
def spec = """
705+
# description 1
706+
# description 2
707+
type Query {
708+
# description 3
709+
foo: String
710+
union: Union
711+
interface(input: Input): Interface
712+
enum: Enum
713+
}
714+
# description 4
715+
union Union = Query
716+
717+
# description 5
718+
interface Interface {
719+
# interface field
720+
foo: String
721+
}
722+
# description 6
723+
input Input {
724+
# input field
725+
foo: String
726+
}
727+
# description 7
728+
enum Enum {
729+
# enum value
730+
FOO
731+
}
732+
schema {
733+
query: Query
734+
}
735+
"""
736+
when:
737+
def wiring = RuntimeWiring.newRuntimeWiring()
738+
.type("Union", buildResolver())
739+
.type("Interface", buildResolver())
740+
.build()
741+
def schema = generateSchema(spec, wiring)
742+
743+
then:
744+
schema.getQueryType().description == "description 1\ndescription 2"
745+
schema.getQueryType().getFieldDefinition("foo").description == "description 3"
746+
((GraphQLUnionType) schema.getType("Union")).description == "description 4"
747+
748+
((GraphQLInterfaceType) schema.getType("Interface")).description == "description 5"
749+
((GraphQLInterfaceType) schema.getType("Interface")).getFieldDefinition("foo").description == "interface field"
750+
751+
((GraphQLInputObjectType) schema.getType("Input")).description == "description 6"
752+
((GraphQLInputObjectType) schema.getType("Input")).getFieldDefinition("foo").description == "input field"
753+
754+
((GraphQLEnumType) schema.getType("Enum")).description == "description 7"
755+
((GraphQLEnumType) schema.getType("Enum")).getValue("FOO").description == "enum value"
756+
}
757+
758+
def "comments are separated from descriptions with empty lines"() {
759+
given:
760+
def spec = """
761+
# should be ignored comment
762+
#
763+
# description 1
764+
# description 2
765+
type Query {
766+
# this should be ignored
767+
# and this
768+
#
769+
# and this after an empty line
770+
# and the last one that should be ignored
771+
#
772+
# description 3
773+
# description 4
774+
foo: String
775+
# ignored and with not description following
776+
#
777+
bar: String
778+
}
779+
schema {
780+
query: Query
781+
}
782+
"""
783+
when:
784+
def wiring = RuntimeWiring.newRuntimeWiring()
785+
.type("Union", buildResolver())
786+
.type("Interface", buildResolver())
787+
.build()
788+
def schema = generateSchema(spec, wiring)
789+
790+
then:
791+
schema.getQueryType().description == "description 1\ndescription 2"
792+
schema.getQueryType().getFieldDefinition("foo").description == "description 3\ndescription 4"
793+
}
700794
}

0 commit comments

Comments
 (0)