Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not put validation annotaiton if return type is a nullable list having non-null values #959 #960

Merged
merged 1 commit into from
Feb 10, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.kobylynskyi.graphql.codegen.utils.Utils;
import graphql.language.Argument;
import graphql.language.Directive;
import graphql.language.InputValueDefinition;
import graphql.language.ListType;
import graphql.language.NamedNode;
import graphql.language.NonNullType;
Expand Down Expand Up @@ -39,9 +40,20 @@ public abstract class AnnotationsMapper {
public List<String> getAnnotations(MappingContext mappingContext, Type<?> type,
NamedNode<?> def, String parentTypeName, boolean mandatory) {
if (type instanceof ListType) {
return getAnnotations(mappingContext, ((ListType) type).getType(), def, parentTypeName, mandatory);
Type<?> subType = ((ListType) type).getType();
return getAnnotations(mappingContext, subType, def, parentTypeName, mandatory);
} else if (type instanceof NonNullType) {
return getAnnotations(mappingContext, ((NonNullType) type).getType(), def, parentTypeName, true);
Type<?> parentType = null;
if (def instanceof ExtendedFieldDefinition) {
parentType = ((ExtendedFieldDefinition) def).getType();
} else if (def instanceof InputValueDefinition) {
parentType = ((InputValueDefinition) def).getType();
}
// if parent is a list, then pass a mandatory flag as is (do not override it)
if (!(parentType instanceof ListType)) {
mandatory = true;
}
return getAnnotations(mappingContext, ((NonNullType) type).getType(), def, parentTypeName, mandatory);
} else if (type instanceof TypeName) {
return getAnnotations(mappingContext, ((TypeName) type).getName(), def.getName(), parentTypeName,
getDirectives(def), mandatory, def);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,20 @@ void generate_Directives() throws Exception {
getFileByName(files, "User.java"));
}

@Test
void generate_ModelValidationAnnotationForSubType() throws Exception {
new JavaGraphQLCodegen(singletonList(
"src/test/resources/schemas/nullable-list-type-with-nullable-sub-types.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/annotation/" +
"NullableListReturnTypeWithMandatoryElementsQueryResolver.java.txt"),
getFileByName(files, "NullableListReturnTypeWithMandatoryElementsQueryResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/annotation/TypeWithNullableListType.java.txt"),
getFileByName(files, "TypeWithNullableListType.java"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface CommitResolver {
@javax.validation.constraints.NotNull
CommitCommentConnection comments(Commit commit, String after, String before, Integer first, Integer last, graphql.schema.DataFetchingEnvironment env) throws Exception;

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;
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;

@javax.validation.constraints.NotNull
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;
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/expected-classes/ProfileOwner.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public interface ProfileOwner {
String getName();

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

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

int getPinnedItemsRemaining();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.kobylynskyi.graphql.test1;


/**
* NonNull validation annotation should not be present here
*/
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface NullableListReturnTypeWithMandatoryElementsQueryResolver {

/**
* NonNull validation annotation should not be present here
*/
java.util.List<TypeWithNullableListType> nullableListReturnTypeWithMandatoryElements() throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.kobylynskyi.graphql.test1;


@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public class TypeWithNullableListType implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private java.util.List<Type2> nullableListFieldWithMandatoryElements;

public TypeWithNullableListType() {
}

public TypeWithNullableListType(java.util.List<Type2> nullableListFieldWithMandatoryElements) {
this.nullableListFieldWithMandatoryElements = nullableListFieldWithMandatoryElements;
}

/**
* NonNull validation annotation should not be present here
*/
public java.util.List<Type2> getNullableListFieldWithMandatoryElements() {
return nullableListFieldWithMandatoryElements;
}
/**
* NonNull validation annotation should not be present here
*/
public void setNullableListFieldWithMandatoryElements(java.util.List<Type2> nullableListFieldWithMandatoryElements) {
this.nullableListFieldWithMandatoryElements = nullableListFieldWithMandatoryElements;
}



public static TypeWithNullableListType.Builder builder() {
return new TypeWithNullableListType.Builder();
}

public static class Builder {

private java.util.List<Type2> nullableListFieldWithMandatoryElements;

public Builder() {
}

/**
* NonNull validation annotation should not be present here
*/
public Builder setNullableListFieldWithMandatoryElements(java.util.List<Type2> nullableListFieldWithMandatoryElements) {
this.nullableListFieldWithMandatoryElements = nullableListFieldWithMandatoryElements;
return this;
}


public TypeWithNullableListType build() {
return new TypeWithNullableListType(nullableListFieldWithMandatoryElements);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package com.kobylynskyi.graphql.test1;
public interface EventsQueryResolver {

@Deprecated
@javax.validation.constraints.NotNull
java.util.List<Event> events() throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package com.kobylynskyi.graphql.test1;
public interface QueryResolver {

@Deprecated
@javax.validation.constraints.NotNull
java.util.List<Event> events() throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
)
public interface AssetsQueryResolver {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class EventInput implements java.io.Serializable {

@javax.validation.constraints.NotNull
private Status status;
@javax.validation.constraints.NotNull
private java.util.List<AssetInput> assets;

public EventInput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public interface EventResolver {
/**
* Optional list of assets
*/
@javax.validation.constraints.NotNull
java.util.List<Asset> assets(Event event) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
)
public interface EventsQueryResolver {

@javax.validation.constraints.NotNull
java.util.List<Event> events() throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
)
public interface QueryResolver {

@javax.validation.constraints.NotNull
java.util.List<Event> events() throws Exception;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
)
public interface AssetsQueryResolver {

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

}
1 change: 0 additions & 1 deletion src/test/resources/expected-classes/extend/Event.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class Event implements java.io.Serializable, PinnableItem, Node {
private Status status;
@javax.validation.constraints.NotNull
private String createdDateTime;
@javax.validation.constraints.NotNull
private java.util.List<Asset> assets;
@javax.validation.constraints.NotNull
private String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class EventInput implements java.io.Serializable {

@javax.validation.constraints.NotNull
private Status status;
@javax.validation.constraints.NotNull
private java.util.List<AssetInput> assets;

public EventInput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
)
public interface EventsQueryResolver {

@javax.validation.constraints.NotNull
java.util.List<Event> events() throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
)
public interface QueryResolver {

@javax.validation.constraints.NotNull
java.util.List<Event> events() throws Exception;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface QueryResolver {
java.util.List<License> licenses() throws Exception;

@javax.validation.constraints.NotNull
java.util.List<MarketplaceCategory> marketplaceCategories(Boolean excludeEmpty, Boolean excludeSubcategories, @javax.validation.constraints.NotNull java.util.List<String> includeCategories) throws Exception;
java.util.List<MarketplaceCategory> marketplaceCategories(Boolean excludeEmpty, Boolean excludeSubcategories, java.util.List<String> includeCategories) throws Exception;

java.util.Optional<MarketplaceCategory> marketplaceCategory(@javax.validation.constraints.NotNull String slug, Boolean useTopicAliases) throws Exception;

Expand Down Expand Up @@ -53,7 +53,7 @@ public interface QueryResolver {
java.util.Optional<SecurityAdvisory> securityAdvisory(@javax.validation.constraints.NotNull String ghsaId) throws Exception;

@javax.validation.constraints.NotNull
SecurityVulnerabilityConnection securityVulnerabilities(String after, String before, SecurityAdvisoryEcosystem ecosystem, Integer first, Integer last, SecurityVulnerabilityOrder orderBy, String Package, @javax.validation.constraints.NotNull java.util.List<SecurityAdvisorySeverity> severities) throws Exception;
SecurityVulnerabilityConnection securityVulnerabilities(String after, String before, SecurityAdvisoryEcosystem ecosystem, Integer first, Integer last, SecurityVulnerabilityOrder orderBy, String Package, java.util.List<SecurityAdvisorySeverity> severities) throws Exception;

java.util.Optional<SponsorsListing> sponsorsListing(@javax.validation.constraints.NotNull String slug) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package com.github.graphql
trait EventsQueryResolver {

@deprecated(message = "No longer supported")
@javax.validation.constraints.NotNull
@throws[Exception]
def events(): scala.Seq[Event]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package com.github.graphql
trait QueryResolver {

@deprecated(message = "No longer supported")
@javax.validation.constraints.NotNull
@throws[Exception]
def events(): scala.Seq[Event]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
)
trait AssetsQueryResolver {

@javax.validation.constraints.NotNull
@throws[Exception]
def assets(): scala.Seq[Asset]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ case class Event(
status: Status,
@javax.validation.constraints.NotNull
createdDateTime: String,
@javax.validation.constraints.NotNull
assets: scala.Seq[Asset],
@javax.validation.constraints.NotNull
override val id: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Status._
case class EventInput(
@javax.validation.constraints.NotNull
status: Status,
@javax.validation.constraints.NotNull
assets: scala.Seq[AssetInput]
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
)
trait EventsQueryResolver {

@javax.validation.constraints.NotNull
@throws[Exception]
def events(): scala.Seq[Event]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
)
trait QueryResolver {

@javax.validation.constraints.NotNull
@throws[Exception]
def events(): scala.Seq[Event]

@javax.validation.constraints.NotNull
@throws[Exception]
def assets(): scala.Seq[Asset]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
)
trait QueryResolver {

@javax.validation.constraints.NotNull
@throws[Exception]
def events(): scala.Seq[Event]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Query {
# NonNull validation annotation should not be present here
nullableListReturnTypeWithMandatoryElements: [TypeWithNullableListType!]
}

type TypeWithNullableListType {
# NonNull validation annotation should not be present here
nullableListFieldWithMandatoryElements: [Type2!]
}

type Type2 {
id: ID!
}