Add support for generating JSpecify annotations#868
Merged
Conversation
sjohnr
commented
Aug 22, 2025
...en-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/DataTypeGenerator.kt
Show resolved
Hide resolved
sjohnr
commented
Aug 22, 2025
...en-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/DataTypeGenerator.kt
Outdated
Show resolved
Hide resolved
bc49fd9 to
c6df4d3
Compare
Contributor
Author
|
Note: Updated PR with the following:
|
c6df4d3 to
81f4a4d
Compare
81f4a4d to
80bd7b3
Compare
Contributor
Author
|
Generated Example:
type User @key(fields: "id") {
id: ID!
username: String!
email: String!,
roles: [String]
}
@NullMarked
public class User {
private String id;
private String username;
private String email;
@Nullable
private List<@Nullable String> roles;
private User() {
}
public User(String id, String username, String email, @Nullable List<@Nullable String> roles) {
this.id = id;
this.username = username;
this.email = email;
this.roles = roles;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Nullable
public List<@Nullable String> getRoles() {
return roles;
}
public void setRoles(@Nullable List<@Nullable String> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "User{id='" + id + "', username='" + username + "', email='" + email + "', roles='" + roles + "'}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User that = (User) o;
return Objects.equals(id, that.id) &&
Objects.equals(username, that.username) &&
Objects.equals(email, that.email) &&
Objects.equals(roles, that.roles);
}
@Override
public int hashCode() {
return Objects.hash(id, username, email, roles);
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
@Nullable
private String id;
@Nullable
private String username;
@Nullable
private String email;
@Nullable
private List<@Nullable String> roles;
public User build() {
User result = new User();
result.id = Objects.requireNonNull(this.id, "id cannot be null");
result.username = Objects.requireNonNull(this.username, "username cannot be null");
result.email = Objects.requireNonNull(this.email, "email cannot be null");
result.roles = this.roles;
return result;
}
public Builder id(String id) {
this.id = id;
return this;
}
public Builder username(String username) {
this.username = username;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder roles(@Nullable List<@Nullable String> roles) {
this.roles = roles;
return this;
}
}
} |
iparadiso
approved these changes
Oct 21, 2025
Contributor
|
Even though jspecify is brought in transitively, we should also declare it as a recommended practice for clarity, protecting against dropped transitives, and version stability. |
Contributor
Author
I'm not sure. I think probably. I will look at a sample app that uses only the plugin and OSS dgs and see where it is in the dependency graph, and then update the docs with what I find. |
Contributor
Author
|
Note: Updated the PR with the following:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces JSpecify annotation support to DGS code generation, which allows nullability information to be preserved in generated Java types to provide null-safety at compile time.
Note that
graphql-javabrings in theorg.jspecify:jspecify:1.0.0dependency transitively.Key Changes
Configuration:
generateJSpecifyAnnotationsboolean flag toCodeGenConfig(defaults tofalse)generateJSpecifyAnnotationsJSpecify Integration:
JavaPoetUtils:jspecifyNonNullAnnotation()- generates@org.jspecify.annotations.NonNulljspecifyNullableAnnotation()- generates@org.jspecify.annotations.NullableAnnotation Coverage:
When
generateJSpecifyAnnotations = true, annotations are applied based on GraphQL schema nullability to the following locations:Listtype elements including nested nullability annotations which are generated on field definitions created byTypeUtilsConstructor Behavior:
privateinstead ofpublicto prevent instantiation without proper nullability validationThis could be addressed with a future enhancement to generate runtime validation in theDone ✅build()methodType System Integration:
TypeUtils.visitListType()to apply JSpecify annotations to list elements based on nested GraphQLNonNullTypeTesting:
Closes #866