-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Discourage usage of nullable on API types #8488
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
base: master
Are you sure you want to change the base?
Conversation
For example, a map field marked with `+nullable` would accept either `foo: null` or `foo: {}`. | ||
|
||
Usage of `+nullable` is discouraged as it introduces several issues: | ||
- It is not compatible with json merge patching. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(from the merge patch RFC: "Null values in the merge patch are given special meaning to indicate the removal of existing values in the target.")
|
||
Usage of `+nullable` is discouraged as it introduces several issues: | ||
- It is not compatible with json merge patching. | ||
- It is not compatible with generic protobuf. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- It is not compatible with generic protobuf. | |
- Explicit `null` values are not be persisted in proto serializations |
|
||
Usage of `+nullable` is discouraged as it introduces several issues: | ||
- It is not compatible with json merge patching. | ||
- It is not compatible with generic protobuf. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's also not compatible with server-side apply, but this should be verified (I have a vague memory of SSA using null values as a signal to remove a key, which would make a null value impossible to persist via SSA)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reached out to folks in sig-apimachinery for a response for this one, hoping they will have seen this and can give me a quick answer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at an openshift type that has Nullable, the generated applyconfiguration looks like
type ClusterResourceQuotaSelectorApplyConfiguration struct {
LabelSelector *metav1.LabelSelectorApplyConfiguration `json:"labels,omitempty"`
AnnotationSelector map[string]string `json:"annotations,omitempty"`
}
If I were to explicitly set a null
, Idon't think this could round trip through this type given the omitempty.
My understanding of the generated applyconfiguration types is that they are always pointer+omitempty (apart from maps/slices which are just omitempty), and omitempty
is not compatible with +nullable
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some conversation with Stefan in https://kubernetes.slack.com/archives/C0EG7JC6T/p1751462820154829?thread_ts=1750672509.687029&cid=C0EG7JC6T
Thanks for this clarification! |
709c963
to
b650843
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a couple typos, then lgtm
- It is not compatible with json merge patching. | ||
- From the [JSON Merge Patch RFC](https://tools.ietf.org/html/rfc7386#section-1): | ||
> Null values in the merge patch are given special meaning to indicate the removal of existing values in the target. | ||
- Explicit `null` values are not be persisted in proto serializations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Explicit `null` values are not be persisted in proto serializations. | |
- Explicit `null` values are not persisted in proto serializations. |
- A persisted `null` value would not round-trip through the applyconfiguration type | ||
encode/decode cycle. | ||
|
||
Avoid designing APIs that require the distinction bewteen unset and `null`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid designing APIs that require the distinction bewteen unset and `null`. | |
Avoid designing APIs that require the distinction between unset and `null`. |
(and squash down to a single commit) |
b650843
to
aae2996
Compare
/lgtm |
aae2996
to
33b481e
Compare
New changes are detected. LGTM label has been removed. |
Had to rebase to fix conflicts after my updates around optional/required serialization merged |
/approve |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: JoelSpeed, liggitt The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
From conversation around #8486, we realised that the usage of nullable is problematic and would like to discourage new usage of it.