Skip to content

Commit 4900539

Browse files
authored
Do not put validation annotaiton if return type is a nullable list having non-null values #959 (#960)
1 parent 68a0803 commit 4900539

28 files changed

+127
-30
lines changed

src/main/java/com/kobylynskyi/graphql/codegen/mapper/AnnotationsMapper.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.kobylynskyi.graphql.codegen.utils.Utils;
77
import graphql.language.Argument;
88
import graphql.language.Directive;
9+
import graphql.language.InputValueDefinition;
910
import graphql.language.ListType;
1011
import graphql.language.NamedNode;
1112
import graphql.language.NonNullType;
@@ -39,9 +40,20 @@ public abstract class AnnotationsMapper {
3940
public List<String> getAnnotations(MappingContext mappingContext, Type<?> type,
4041
NamedNode<?> def, String parentTypeName, boolean mandatory) {
4142
if (type instanceof ListType) {
42-
return getAnnotations(mappingContext, ((ListType) type).getType(), def, parentTypeName, mandatory);
43+
Type<?> subType = ((ListType) type).getType();
44+
return getAnnotations(mappingContext, subType, def, parentTypeName, mandatory);
4345
} else if (type instanceof NonNullType) {
44-
return getAnnotations(mappingContext, ((NonNullType) type).getType(), def, parentTypeName, true);
46+
Type<?> parentType = null;
47+
if (def instanceof ExtendedFieldDefinition) {
48+
parentType = ((ExtendedFieldDefinition) def).getType();
49+
} else if (def instanceof InputValueDefinition) {
50+
parentType = ((InputValueDefinition) def).getType();
51+
}
52+
// if parent is a list, then pass a mandatory flag as is (do not override it)
53+
if (!(parentType instanceof ListType)) {
54+
mandatory = true;
55+
}
56+
return getAnnotations(mappingContext, ((NonNullType) type).getType(), def, parentTypeName, mandatory);
4557
} else if (type instanceof TypeName) {
4658
return getAnnotations(mappingContext, ((TypeName) type).getName(), def.getName(), parentTypeName,
4759
getDirectives(def), mandatory, def);

src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenAnnotationsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,20 @@ void generate_Directives() throws Exception {
266266
getFileByName(files, "User.java"));
267267
}
268268

269+
@Test
270+
void generate_ModelValidationAnnotationForSubType() throws Exception {
271+
new JavaGraphQLCodegen(singletonList(
272+
"src/test/resources/schemas/nullable-list-type-with-nullable-sub-types.graphqls"),
273+
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
274+
275+
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
276+
assertSameTrimmedContent(
277+
new File("src/test/resources/expected-classes/annotation/" +
278+
"NullableListReturnTypeWithMandatoryElementsQueryResolver.java.txt"),
279+
getFileByName(files, "NullableListReturnTypeWithMandatoryElementsQueryResolver.java"));
280+
assertSameTrimmedContent(
281+
new File("src/test/resources/expected-classes/annotation/TypeWithNullableListType.java.txt"),
282+
getFileByName(files, "TypeWithNullableListType.java"));
283+
}
284+
269285
}

src/test/resources/expected-classes/CommitResolver.java.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface CommitResolver {
1919
@javax.validation.constraints.NotNull
2020
CommitCommentConnection comments(Commit commit, String after, String before, Integer first, Integer last, graphql.schema.DataFetchingEnvironment env) throws Exception;
2121

22-
DeploymentConnection deployments(Commit commit, String after, String before, @javax.validation.constraints.NotNull java.util.List<String> environments, Integer first, Integer last, DeploymentOrder orderBy, graphql.schema.DataFetchingEnvironment env) throws Exception;
22+
DeploymentConnection deployments(Commit commit, String after, String before, java.util.List<String> environments, Integer first, Integer last, DeploymentOrder orderBy, graphql.schema.DataFetchingEnvironment env) throws Exception;
2323

2424
@javax.validation.constraints.NotNull
2525
CommitHistoryConnection history(Commit commit, String after, CommitAuthor author, String before, Integer first, Integer last, String path, String since, String until, graphql.schema.DataFetchingEnvironment env) throws Exception;

src/test/resources/expected-classes/ProfileOwner.java.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public interface ProfileOwner {
2525
String getName();
2626

2727
@javax.validation.constraints.NotNull
28-
PinnableItemConnection getPinnableItems(String after, String before, Integer first, Integer last, @javax.validation.constraints.NotNull java.util.List<PinnableItemType> types);
28+
PinnableItemConnection getPinnableItems(String after, String before, Integer first, Integer last, java.util.List<PinnableItemType> types);
2929

3030
@javax.validation.constraints.NotNull
31-
PinnableItemConnection getPinnedItems(String after, String before, Integer first, Integer last, @javax.validation.constraints.NotNull java.util.List<PinnableItemType> types);
31+
PinnableItemConnection getPinnedItems(String after, String before, Integer first, Integer last, java.util.List<PinnableItemType> types);
3232

3333
int getPinnedItemsRemaining();
3434

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
4+
/**
5+
* NonNull validation annotation should not be present here
6+
*/
7+
@javax.annotation.Generated(
8+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
9+
date = "2020-12-31T23:59:59-0500"
10+
)
11+
public interface NullableListReturnTypeWithMandatoryElementsQueryResolver {
12+
13+
/**
14+
* NonNull validation annotation should not be present here
15+
*/
16+
java.util.List<TypeWithNullableListType> nullableListReturnTypeWithMandatoryElements() throws Exception;
17+
18+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
4+
@javax.annotation.Generated(
5+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
6+
date = "2020-12-31T23:59:59-0500"
7+
)
8+
public class TypeWithNullableListType implements java.io.Serializable {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
private java.util.List<Type2> nullableListFieldWithMandatoryElements;
13+
14+
public TypeWithNullableListType() {
15+
}
16+
17+
public TypeWithNullableListType(java.util.List<Type2> nullableListFieldWithMandatoryElements) {
18+
this.nullableListFieldWithMandatoryElements = nullableListFieldWithMandatoryElements;
19+
}
20+
21+
/**
22+
* NonNull validation annotation should not be present here
23+
*/
24+
public java.util.List<Type2> getNullableListFieldWithMandatoryElements() {
25+
return nullableListFieldWithMandatoryElements;
26+
}
27+
/**
28+
* NonNull validation annotation should not be present here
29+
*/
30+
public void setNullableListFieldWithMandatoryElements(java.util.List<Type2> nullableListFieldWithMandatoryElements) {
31+
this.nullableListFieldWithMandatoryElements = nullableListFieldWithMandatoryElements;
32+
}
33+
34+
35+
36+
public static TypeWithNullableListType.Builder builder() {
37+
return new TypeWithNullableListType.Builder();
38+
}
39+
40+
public static class Builder {
41+
42+
private java.util.List<Type2> nullableListFieldWithMandatoryElements;
43+
44+
public Builder() {
45+
}
46+
47+
/**
48+
* NonNull validation annotation should not be present here
49+
*/
50+
public Builder setNullableListFieldWithMandatoryElements(java.util.List<Type2> nullableListFieldWithMandatoryElements) {
51+
this.nullableListFieldWithMandatoryElements = nullableListFieldWithMandatoryElements;
52+
return this;
53+
}
54+
55+
56+
public TypeWithNullableListType build() {
57+
return new TypeWithNullableListType(nullableListFieldWithMandatoryElements);
58+
}
59+
60+
}
61+
}

src/test/resources/expected-classes/deprecated/EventsQueryResolver.java.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package com.kobylynskyi.graphql.test1;
88
public interface EventsQueryResolver {
99

1010
@Deprecated
11-
@javax.validation.constraints.NotNull
1211
java.util.List<Event> events() throws Exception;
1312

1413
}

src/test/resources/expected-classes/deprecated/QueryResolver.java.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package com.kobylynskyi.graphql.test1;
88
public interface QueryResolver {
99

1010
@Deprecated
11-
@javax.validation.constraints.NotNull
1211
java.util.List<Event> events() throws Exception;
1312

1413
}

src/test/resources/expected-classes/extend-with-resolvers/AssetsQueryResolver.java.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
)
55
public interface AssetsQueryResolver {
66

7-
@javax.validation.constraints.NotNull
87
java.util.List<Asset> assets() throws Exception;
98

109
}

src/test/resources/expected-classes/extend-with-resolvers/EventInput.java.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class EventInput implements java.io.Serializable {
88

99
@javax.validation.constraints.NotNull
1010
private Status status;
11-
@javax.validation.constraints.NotNull
1211
private java.util.List<AssetInput> assets;
1312

1413
public EventInput() {

0 commit comments

Comments
 (0)