controller-tools icon indicating copy to clipboard operation
controller-tools copied to clipboard

Should `omitzero` be used to default fields to optional when they have no explicit marker?

Open alvaroaleman opened this issue 3 months ago • 1 comments

Pretty much title, by default we mark fields as optional if they have omitempty set and no explicit marker exists. It would be consistent to do the same for omitzero.

The counterargument on the other hand is that any sort of defaulting is a bit magic. I am not completely sure what is best here but have a slight tendency to make it consistent, so opening this for discussion.

alvaroaleman avatar Nov 18 '25 13:11 alvaroaleman

Defaulting to optional on omitempty is a historic mistake and not one I'm keen to repeat. Latest API guidelines in upstream kube do not differentiate omitempty for optional and required fields, all fields should have omitempty (or omitzero on non-pointer structs)

This defaulting behaviour has over time caused issues as people have accidentally made fields optional that were meant to be required (embedding upstream types that didn't follow the earlier rules).

We are making an effort to make optional and required an explicit choice in the community (see enabling Kube-api-linter in both K/K and new kubebuilder projects) so adding more defaulting seems like a step in the wrong direction

IMO I would rather we start emitting a warning to folks who haven't got an optional/required marker rather than enabling this behaviour further

JoelSpeed avatar Nov 18 '25 13:11 JoelSpeed