-
Notifications
You must be signed in to change notification settings - Fork 108
Add support for generating JSpecify annotations #868
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
Conversation
...en-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/DataTypeGenerator.kt
Show resolved
Hide resolved
...en-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/DataTypeGenerator.kt
Outdated
Show resolved
Hide resolved
bc49fd9 to
c6df4d3
Compare
|
Note: Updated PR with the following:
|
c6df4d3 to
81f4a4d
Compare
81f4a4d to
80bd7b3
Compare
|
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;
}
}
} |
|
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. |
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. |
|
Note: Updated the PR with the following:
|
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