diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphqlTypeToJavaTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphqlTypeToJavaTypeMapper.java index 5c813273ed102aca291371c68f1ac7503210de9a..b44ba8494daf14e3d5b5294e3123254cb62ba655 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphqlTypeToJavaTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphqlTypeToJavaTypeMapper.java @@ -19,7 +19,7 @@ class GraphqlTypeToJavaTypeMapper { ParameterDefinition parameter = new ParameterDefinition(); parameter.setName(MapperUtils.capitalizeIfRestricted(fieldDef.getName())); parameter.setType(getJavaType(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName)); - parameter.setAnnotations(getAnnotations(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName)); + parameter.setAnnotations(getAnnotations(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName, false)); return parameter; } @@ -69,19 +69,29 @@ class GraphqlTypeToJavaTypeMapper { } static List getAnnotations(MappingConfig mappingConfig, Type type, String name, String parentTypeName) { + return getAnnotations(mappingConfig, type, name, parentTypeName, true); + } + + private static List getAnnotations(MappingConfig mappingConfig, Type type, String name, String parentTypeName, + boolean mandatory) { if (type instanceof TypeName) { - return getAnnotations(mappingConfig, ((TypeName) type).getName(), name, parentTypeName); + return getAnnotations(mappingConfig, ((TypeName) type).getName(), name, parentTypeName, mandatory); } else if (type instanceof ListType) { - return getAnnotations(mappingConfig, ((ListType) type).getType(), name, parentTypeName); + return getAnnotations(mappingConfig, ((ListType) type).getType(), name, parentTypeName, mandatory); } else if (type instanceof NonNullType) { - return getAnnotations(mappingConfig, ((NonNullType) type).getType(), name, parentTypeName); + return getAnnotations(mappingConfig, ((NonNullType) type).getType(), name, parentTypeName, true); } return null; } - private static List getAnnotations(MappingConfig mappingConfig, String graphlType, String name, String parentTypeName) { + private static List getAnnotations(MappingConfig mappingConfig, String graphlType, String name, String parentTypeName, boolean mandatory) { List annotations = new ArrayList<>(); - // Todo: https://github.com/kobylynskyi/graphql-java-codegen/issues/3 + if (mandatory) { + String modelValidationAnnotation = mappingConfig.getModelValidationAnnotation(); + if (modelValidationAnnotation != null && !modelValidationAnnotation.trim().isEmpty()) { + annotations.add(modelValidationAnnotation); + } + } Map customAnnotationsMapping = mappingConfig.getCustomAnnotationsMapping(); if (name != null && parentTypeName != null && customAnnotationsMapping.containsKey(parentTypeName + "." + name)) { annotations.add(customAnnotationsMapping.get(parentTypeName + "." + name)); diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/DefaultMappingConfigValues.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/DefaultMappingConfigValues.java new file mode 100644 index 0000000000000000000000000000000000000000..c840298abf997c93871ebe1865d1679d54dbe24e --- /dev/null +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/DefaultMappingConfigValues.java @@ -0,0 +1,7 @@ +package com.kobylynskyi.graphql.codegen.model; + +public class DefaultMappingConfigValues { + + public static final String DEFAULT_VALIDATION_ANNOTATION = "javax.validation.constraints.NotNull"; + public static final boolean DEFAULT_GENERATE_APIS = true; +} diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java index f77472c80b7c7dab7967cc5df1625742250954bb..6824f2a289b5713c24db48a5f7f2433e64fe3c30 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java @@ -21,12 +21,13 @@ public class MappingConfig { */ private Map customAnnotationsMapping = new HashMap<>(); - private boolean generateApis = true; + private boolean generateApis = DefaultMappingConfigValues.DEFAULT_GENERATE_APIS; private String packageName; private String apiPackageName; private String modelPackageName; private String modelNamePrefix; private String modelNameSuffix; + private String modelValidationAnnotation = DefaultMappingConfigValues.DEFAULT_VALIDATION_ANNOTATION; public void putCustomTypeMappingIfAbsent(String from, String to) { if (customTypesMapping == null) { diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/GraphqlCodegenGitHubTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/GraphqlCodegenGitHubTest.java index 50e34c2595a1fdf0c270237cc82922b9f38b6df0..388490354f6b927e51968cb04ef5571d4d7fff2e 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/GraphqlCodegenGitHubTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/GraphqlCodegenGitHubTest.java @@ -83,6 +83,18 @@ class GraphqlCodegenGitHubTest { getGeneratedFileContent(files, "GithubCommitTO.java")); } + @Test + void generate_NoValidationAnnotation() throws Exception { + mappingConfig.setModelValidationAnnotation(null); + + generator.generate(); + File commitFile = Arrays.stream(Objects.requireNonNull(outputJavaClassesDir.listFiles())) + .filter(file -> file.getName().equalsIgnoreCase("Commit.java")) + .findFirst().orElseThrow(FileNotFoundException::new); + assertEquals(Utils.getFileContent(new File("src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt").getPath()), + Utils.getFileContent(commitFile.getPath())); + } + private static String getGeneratedFileContent(File[] files, String fileName) throws IOException { File file = Arrays.stream(files).filter(f -> f.getName().equalsIgnoreCase(fileName)) .findFirst().orElseThrow(FileNotFoundException::new); diff --git a/src/test/resources/expected-classes/Commit.java.txt b/src/test/resources/expected-classes/Commit.java.txt index 88dc55174ede47a75a77491dcb38066582471475..12f46d57237a55851b0f522bb655ba887b7ac8ea 100644 --- a/src/test/resources/expected-classes/Commit.java.txt +++ b/src/test/resources/expected-classes/Commit.java.txt @@ -4,43 +4,73 @@ import java.util.*; public class Commit implements Closer, IssueTimelineItem, PullRequestTimelineItem, GitObject, Node, Subscribable, UniformResourceLocatable{ + @javax.validation.constraints.NotNull private String abbreviatedOid; + @javax.validation.constraints.NotNull private Integer additions; private PullRequestConnection associatedPullRequests; private GitActor author; + @javax.validation.constraints.NotNull private Boolean authoredByCommitter; + @javax.validation.constraints.NotNull private String authoredDate; + @javax.validation.constraints.NotNull private Blame blame; + @javax.validation.constraints.NotNull private Integer changedFiles; + @javax.validation.constraints.NotNull private CommitCommentConnection comments; + @javax.validation.constraints.NotNull private String commitResourcePath; + @javax.validation.constraints.NotNull private String commitUrl; + @javax.validation.constraints.NotNull private String committedDate; + @javax.validation.constraints.NotNull private Boolean committedViaWeb; private GitActor committer; + @javax.validation.constraints.NotNull private Integer deletions; private DeploymentConnection deployments; + @javax.validation.constraints.NotNull private CommitHistoryConnection history; + @javax.validation.constraints.NotNull private String id; + @javax.validation.constraints.NotNull private String message; + @javax.validation.constraints.NotNull private String messageBody; + @javax.validation.constraints.NotNull private String messageBodyHTML; + @javax.validation.constraints.NotNull private String messageHeadline; + @javax.validation.constraints.NotNull private String messageHeadlineHTML; + @javax.validation.constraints.NotNull private String oid; + @javax.validation.constraints.NotNull private CommitConnection parents; private String pushedDate; + @javax.validation.constraints.NotNull private Repository repository; + @javax.validation.constraints.NotNull private String resourcePath; private GitSignature signature; private Status status; + @javax.validation.constraints.NotNull private String tarballUrl; + @javax.validation.constraints.NotNull private Tree tree; + @javax.validation.constraints.NotNull private String treeResourcePath; + @javax.validation.constraints.NotNull private String treeUrl; + @javax.validation.constraints.NotNull private String url; + @javax.validation.constraints.NotNull private Boolean viewerCanSubscribe; private SubscriptionState viewerSubscription; + @javax.validation.constraints.NotNull private String zipballUrl; public Commit() { diff --git a/src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt b/src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt new file mode 100644 index 0000000000000000000000000000000000000000..88dc55174ede47a75a77491dcb38066582471475 --- /dev/null +++ b/src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt @@ -0,0 +1,315 @@ +package com.github.graphql; + +import java.util.*; + +public class Commit implements Closer, IssueTimelineItem, PullRequestTimelineItem, GitObject, Node, Subscribable, UniformResourceLocatable{ + + private String abbreviatedOid; + private Integer additions; + private PullRequestConnection associatedPullRequests; + private GitActor author; + private Boolean authoredByCommitter; + private String authoredDate; + private Blame blame; + private Integer changedFiles; + private CommitCommentConnection comments; + private String commitResourcePath; + private String commitUrl; + private String committedDate; + private Boolean committedViaWeb; + private GitActor committer; + private Integer deletions; + private DeploymentConnection deployments; + private CommitHistoryConnection history; + private String id; + private String message; + private String messageBody; + private String messageBodyHTML; + private String messageHeadline; + private String messageHeadlineHTML; + private String oid; + private CommitConnection parents; + private String pushedDate; + private Repository repository; + private String resourcePath; + private GitSignature signature; + private Status status; + private String tarballUrl; + private Tree tree; + private String treeResourcePath; + private String treeUrl; + private String url; + private Boolean viewerCanSubscribe; + private SubscriptionState viewerSubscription; + private String zipballUrl; + + public Commit() { + } + + public String getAbbreviatedOid() { + return abbreviatedOid; + } + public void setAbbreviatedOid(String abbreviatedOid) { + this.abbreviatedOid = abbreviatedOid; + } + + public Integer getAdditions() { + return additions; + } + public void setAdditions(Integer additions) { + this.additions = additions; + } + + public PullRequestConnection getAssociatedPullRequests() { + return associatedPullRequests; + } + public void setAssociatedPullRequests(PullRequestConnection associatedPullRequests) { + this.associatedPullRequests = associatedPullRequests; + } + + public GitActor getAuthor() { + return author; + } + public void setAuthor(GitActor author) { + this.author = author; + } + + public Boolean getAuthoredByCommitter() { + return authoredByCommitter; + } + public void setAuthoredByCommitter(Boolean authoredByCommitter) { + this.authoredByCommitter = authoredByCommitter; + } + + public String getAuthoredDate() { + return authoredDate; + } + public void setAuthoredDate(String authoredDate) { + this.authoredDate = authoredDate; + } + + public Blame getBlame() { + return blame; + } + public void setBlame(Blame blame) { + this.blame = blame; + } + + public Integer getChangedFiles() { + return changedFiles; + } + public void setChangedFiles(Integer changedFiles) { + this.changedFiles = changedFiles; + } + + public CommitCommentConnection getComments() { + return comments; + } + public void setComments(CommitCommentConnection comments) { + this.comments = comments; + } + + public String getCommitResourcePath() { + return commitResourcePath; + } + public void setCommitResourcePath(String commitResourcePath) { + this.commitResourcePath = commitResourcePath; + } + + public String getCommitUrl() { + return commitUrl; + } + public void setCommitUrl(String commitUrl) { + this.commitUrl = commitUrl; + } + + public String getCommittedDate() { + return committedDate; + } + public void setCommittedDate(String committedDate) { + this.committedDate = committedDate; + } + + public Boolean getCommittedViaWeb() { + return committedViaWeb; + } + public void setCommittedViaWeb(Boolean committedViaWeb) { + this.committedViaWeb = committedViaWeb; + } + + public GitActor getCommitter() { + return committer; + } + public void setCommitter(GitActor committer) { + this.committer = committer; + } + + public Integer getDeletions() { + return deletions; + } + public void setDeletions(Integer deletions) { + this.deletions = deletions; + } + + public DeploymentConnection getDeployments() { + return deployments; + } + public void setDeployments(DeploymentConnection deployments) { + this.deployments = deployments; + } + + public CommitHistoryConnection getHistory() { + return history; + } + public void setHistory(CommitHistoryConnection history) { + this.history = history; + } + + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + + public String getMessageBody() { + return messageBody; + } + public void setMessageBody(String messageBody) { + this.messageBody = messageBody; + } + + public String getMessageBodyHTML() { + return messageBodyHTML; + } + public void setMessageBodyHTML(String messageBodyHTML) { + this.messageBodyHTML = messageBodyHTML; + } + + public String getMessageHeadline() { + return messageHeadline; + } + public void setMessageHeadline(String messageHeadline) { + this.messageHeadline = messageHeadline; + } + + public String getMessageHeadlineHTML() { + return messageHeadlineHTML; + } + public void setMessageHeadlineHTML(String messageHeadlineHTML) { + this.messageHeadlineHTML = messageHeadlineHTML; + } + + public String getOid() { + return oid; + } + public void setOid(String oid) { + this.oid = oid; + } + + public CommitConnection getParents() { + return parents; + } + public void setParents(CommitConnection parents) { + this.parents = parents; + } + + public String getPushedDate() { + return pushedDate; + } + public void setPushedDate(String pushedDate) { + this.pushedDate = pushedDate; + } + + public Repository getRepository() { + return repository; + } + public void setRepository(Repository repository) { + this.repository = repository; + } + + public String getResourcePath() { + return resourcePath; + } + public void setResourcePath(String resourcePath) { + this.resourcePath = resourcePath; + } + + public GitSignature getSignature() { + return signature; + } + public void setSignature(GitSignature signature) { + this.signature = signature; + } + + public Status getStatus() { + return status; + } + public void setStatus(Status status) { + this.status = status; + } + + public String getTarballUrl() { + return tarballUrl; + } + public void setTarballUrl(String tarballUrl) { + this.tarballUrl = tarballUrl; + } + + public Tree getTree() { + return tree; + } + public void setTree(Tree tree) { + this.tree = tree; + } + + public String getTreeResourcePath() { + return treeResourcePath; + } + public void setTreeResourcePath(String treeResourcePath) { + this.treeResourcePath = treeResourcePath; + } + + public String getTreeUrl() { + return treeUrl; + } + public void setTreeUrl(String treeUrl) { + this.treeUrl = treeUrl; + } + + public String getUrl() { + return url; + } + public void setUrl(String url) { + this.url = url; + } + + public Boolean getViewerCanSubscribe() { + return viewerCanSubscribe; + } + public void setViewerCanSubscribe(Boolean viewerCanSubscribe) { + this.viewerCanSubscribe = viewerCanSubscribe; + } + + public SubscriptionState getViewerSubscription() { + return viewerSubscription; + } + public void setViewerSubscription(SubscriptionState viewerSubscription) { + this.viewerSubscription = viewerSubscription; + } + + public String getZipballUrl() { + return zipballUrl; + } + public void setZipballUrl(String zipballUrl) { + this.zipballUrl = zipballUrl; + } + +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/CreateEventMutation.java.txt b/src/test/resources/expected-classes/CreateEventMutation.java.txt index f8be1b6a3d72e96ff852b85262894a1257a9a126..cf710cb03506b3011516aa5a83360a2e92145855 100644 --- a/src/test/resources/expected-classes/CreateEventMutation.java.txt +++ b/src/test/resources/expected-classes/CreateEventMutation.java.txt @@ -4,6 +4,7 @@ import java.util.*; public interface CreateEventMutation { + @javax.validation.constraints.NotNull Event createEvent(String categoryId, String createdBy) throws Exception; } \ No newline at end of file diff --git a/src/test/resources/expected-classes/EventByIdQuery.java.txt b/src/test/resources/expected-classes/EventByIdQuery.java.txt index 5ac2edd5e246971e1c9774218cbf68d6ebe940a9..283f3a40cc0e751c93db959bc21b0036534695d0 100644 --- a/src/test/resources/expected-classes/EventByIdQuery.java.txt +++ b/src/test/resources/expected-classes/EventByIdQuery.java.txt @@ -4,6 +4,7 @@ import java.util.*; public interface EventByIdQuery { + @javax.validation.constraints.NotNull Event eventById(String id) throws Exception; } \ No newline at end of file diff --git a/src/test/resources/expected-classes/EventsByCategoryAndStatusQuery.java.txt b/src/test/resources/expected-classes/EventsByCategoryAndStatusQuery.java.txt index 9afce0f732c4be2c666c4df84b499082b17c9493..a7c5fa1d9b59a07d05e14a9c00a087af5920636d 100644 --- a/src/test/resources/expected-classes/EventsByCategoryAndStatusQuery.java.txt +++ b/src/test/resources/expected-classes/EventsByCategoryAndStatusQuery.java.txt @@ -4,6 +4,7 @@ import java.util.*; public interface EventsByCategoryAndStatusQuery { + @javax.validation.constraints.NotNull Collection eventsByCategoryAndStatus(String categoryId, EventStatus status) throws Exception; } \ No newline at end of file diff --git a/src/test/resources/expected-classes/GithubCommitTO.java.txt b/src/test/resources/expected-classes/GithubCommitTO.java.txt index c28b92297e2ccdb9e36a304de6c13c3514b21013..a2838df9c632214ec7db5f5eb201bc283c1eb195 100644 --- a/src/test/resources/expected-classes/GithubCommitTO.java.txt +++ b/src/test/resources/expected-classes/GithubCommitTO.java.txt @@ -4,43 +4,73 @@ import java.util.*; public class GithubCommitTO implements GithubCloserTO, GithubIssueTimelineItemTO, GithubPullRequestTimelineItemTO, GithubGitObjectTO, GithubNodeTO, GithubSubscribableTO, GithubUniformResourceLocatableTO{ + @javax.validation.constraints.NotNull private String abbreviatedOid; + @javax.validation.constraints.NotNull private Integer additions; private GithubPullRequestConnectionTO associatedPullRequests; private GithubGitActorTO author; + @javax.validation.constraints.NotNull private Boolean authoredByCommitter; + @javax.validation.constraints.NotNull private String authoredDate; + @javax.validation.constraints.NotNull private GithubBlameTO blame; + @javax.validation.constraints.NotNull private Integer changedFiles; + @javax.validation.constraints.NotNull private GithubCommitCommentConnectionTO comments; + @javax.validation.constraints.NotNull private String commitResourcePath; + @javax.validation.constraints.NotNull private String commitUrl; + @javax.validation.constraints.NotNull private String committedDate; + @javax.validation.constraints.NotNull private Boolean committedViaWeb; private GithubGitActorTO committer; + @javax.validation.constraints.NotNull private Integer deletions; private GithubDeploymentConnectionTO deployments; + @javax.validation.constraints.NotNull private GithubCommitHistoryConnectionTO history; + @javax.validation.constraints.NotNull private String id; + @javax.validation.constraints.NotNull private String message; + @javax.validation.constraints.NotNull private String messageBody; + @javax.validation.constraints.NotNull private String messageBodyHTML; + @javax.validation.constraints.NotNull private String messageHeadline; + @javax.validation.constraints.NotNull private String messageHeadlineHTML; + @javax.validation.constraints.NotNull private String oid; + @javax.validation.constraints.NotNull private GithubCommitConnectionTO parents; private String pushedDate; + @javax.validation.constraints.NotNull private GithubRepositoryTO repository; + @javax.validation.constraints.NotNull private String resourcePath; private GithubGitSignatureTO signature; private GithubStatusTO status; + @javax.validation.constraints.NotNull private String tarballUrl; + @javax.validation.constraints.NotNull private GithubTreeTO tree; + @javax.validation.constraints.NotNull private String treeResourcePath; + @javax.validation.constraints.NotNull private String treeUrl; + @javax.validation.constraints.NotNull private String url; + @javax.validation.constraints.NotNull private Boolean viewerCanSubscribe; private GithubSubscriptionStateTO viewerSubscription; + @javax.validation.constraints.NotNull private String zipballUrl; public GithubCommitTO() { diff --git a/src/test/resources/expected-classes/Mutation.java.txt b/src/test/resources/expected-classes/Mutation.java.txt index e0d1a1f24a992e9f1c26bd441b19b3c28c70e580..3a16b53aefd3e274b87222b9b3b1a58e7225ad69 100644 --- a/src/test/resources/expected-classes/Mutation.java.txt +++ b/src/test/resources/expected-classes/Mutation.java.txt @@ -4,6 +4,7 @@ import java.util.*; public interface Mutation { + @javax.validation.constraints.NotNull Event createEvent(String categoryId, String createdBy) throws Exception; } \ No newline at end of file diff --git a/src/test/resources/expected-classes/Query.java.txt b/src/test/resources/expected-classes/Query.java.txt index 73bb7a8f70372bdbab672f9ff328a3d65bc06827..dc5d929300c722bb51499164f35315b291320c46 100644 --- a/src/test/resources/expected-classes/Query.java.txt +++ b/src/test/resources/expected-classes/Query.java.txt @@ -4,10 +4,13 @@ import java.util.*; public interface Query { + @javax.validation.constraints.NotNull String version() throws Exception; + @javax.validation.constraints.NotNull Collection eventsByCategoryAndStatus(String categoryId, EventStatus status) throws Exception; + @javax.validation.constraints.NotNull Event eventById(String id) throws Exception; } \ No newline at end of file diff --git a/src/test/resources/expected-classes/VersionQuery.java.txt b/src/test/resources/expected-classes/VersionQuery.java.txt index 9fcfce16cdc3fe2c49342011a060d088e37c2496..7e5d61528a3132268e7f086634bfa07f7cd4a2ff 100644 --- a/src/test/resources/expected-classes/VersionQuery.java.txt +++ b/src/test/resources/expected-classes/VersionQuery.java.txt @@ -4,6 +4,7 @@ import java.util.*; public interface VersionQuery { + @javax.validation.constraints.NotNull String version() throws Exception; } \ No newline at end of file