-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
Differentiating explicit null from default values #680
Comments
@Frederick888 thanks for your feature request.
|
@kobylynskyi Thanks for your quick response! I'm not very familiar this project so please correct me if I'm wrong, but if we are going to track explicitly set fields in the (Am I actually talking about the same public static class Builder {
private String $alias;
private UpdatePersonInputTO input;
public Builder() {
}
public Builder alias(String alias) {
this.$alias = alias;
return this;
}
public Builder setInput(UpdatePersonInputTO input) {
this.input = input;
return this;
}
public UpdatePersonMutationRequest build() {
UpdatePersonMutationRequest obj = new UpdatePersonMutationRequest($alias);
obj.setInput(input);
return obj;
}
} And inspired by @robbertnoordzij's comment, what about using |
I tried out the @kobylynskyi What do you think? |
Seems plausible, I would only want it for nullable fields and as an option. |
@robbertnoordzij Thanks for the comment, that makes sense. Second try: Frederick888@c449cb2 A few questions:
|
By the way I just realised that this seems to be in GraphQL specs: graphql/graphql-spec#83 Related graphql-java PR: graphql-java/graphql-java#156 |
From the specification it is clear, however I do not see how they solved in graphql Java at this moment. I would expect an optional in such case for inputs and projection. Meaning: null not provided But the provided but empty in the spec is also null so that might be even more confusing. |
@robbertnoordzij You were right. The PR closed graphql-java/graphql-java#106 but only solved the issue partially (IIUC it omits null fields which haven't got default values from the final result, but provides no way to explicitly set null value). graphql-java/graphql-java#452 is the one that actually addressed this issue.
I agree. They were probably just trying to stick to JSON terminology though. |
@robbertnoordzij By the way, what were your thoughts on my second try? |
I am sorry, I totally missed the update on this issue. I am like you second approach with the Optional.ofNullable. However, I would suggest to this only for Nullable fields, since a non nullable field should always have a value and can only be null if it was not requested by the client. |
@robbertnoordzij Hmmm... which part are you referring to? In my second try it's not using For example, in the following generated model, import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequestSerializer;
import java.util.StringJoiner;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@javax.annotation.processing.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2021-05-26T17:12:05+1000"
)
@JsonInclude(Include.NON_NULL)
public class PersonTO implements java.io.Serializable, IClientTO {
private Optional<String> tenantId;
@javax.validation.constraints.NotNull
private String name;
public PersonTO() {
}
public PersonTO(String tenantId, String name) {
this.tenantId = Optional.ofNullable(tenantId);
this.name = name;
}
public PersonTO(Optional<String> tenantId, String name) {
this.tenantId = tenantId;
this.name = name;
}
public String getTenantId() {
return (tenantId == null || tenantId.isEmpty()) ? null : tenantId.get();
}
public void setTenantId(String tenantId) {
this.tenantId = Optional.ofNullable(tenantId);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", "{ ", " }");
if (tenantId != null && tenantId.isPresent()) {
joiner.add("tenantId: " + GraphQLRequestSerializer.getEntry(tenantId));
}
if (name != null) {
joiner.add("name: " + GraphQLRequestSerializer.getEntry(name));
}
return joiner.toString();
}
public static PersonTO.Builder builder() {
return new PersonTO.Builder();
}
public static class Builder {
private Optional<String> tenantId;
private String name;
public Builder() {
}
public Builder setTenantId(String tenantId) {
this.tenantId = Optional.ofNullable(tenantId);
return this;
}
public Builder setName(String name) {
this.name = name;
return this;
}
public PersonTO build() {
return new PersonTO(tenantId, name);
}
}
} And by the way, I've also reverted types like |
Shouldn't there be an additional method like the following to be able to know if value has been explicitly passed as null or was absent? public boolean isTenantIdDefined() {
return tenantId != null
} |
Is your feature request related to a problem? Please describe.
We'd like to use the generated models for a Merge Patch API.
It's similar to https://tools.ietf.org/html/rfc7396, where missing fields in the request payload are left untouched, and explicit
null
fields are unset.At the moment there doesn't seem to be a place to track the explicitly
null
'ed fields and by default they are missing from request payloads.Describe the solution you'd like
Add a magic map field (e.g.
Set<?> changedFields
) or a magic boolean field for each actual field (e.g. forsomeField
it can haveboolean isSomeFieldChanged
) to track changed fields via field mutators, and include these fields in serialisation result even if they are null.Describe alternatives you've considered
Copy out the generated models and manually do what I described above.
The text was updated successfully, but these errors were encountered: