Skip to content

feat: pointer values allow null values when omitempty in json tag#1862

Closed
matthiasbruns wants to merge 11 commits into
open-component-model:mainfrom
matthiasbruns:feat/883_nullable_json_scheme
Closed

feat: pointer values allow null values when omitempty in json tag#1862
matthiasbruns wants to merge 11 commits into
open-component-model:mainfrom
matthiasbruns:feat/883_nullable_json_scheme

Conversation

@matthiasbruns

@matthiasbruns matthiasbruns commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

On-behalf-of: SAP matthias.bruns@sap.com

What this PR does / why we need it

This PR adds nullable support to pointer types in our schema generator.
This PR allows null values by checking for omitempty for pointer types during schema generation.

Which issue(s) this PR fixes

Contributes: #1832

Testing

Tested and used in #1846

@github-actions github-actions Bot added kind/feature new feature, enhancement, improvement, extension size/m Medium labels Feb 27, 2026
On-behalf-of: SAP <matthias.bruns@sap.com>
Signed-off-by: Matthias Bruns <git@matthiasbruns.com>
@matthiasbruns matthiasbruns force-pushed the feat/883_nullable_json_scheme branch from 1961771 to 9b283e9 Compare February 27, 2026 19:27
@matthiasbruns matthiasbruns marked this pull request as ready for review March 2, 2026 08:33
@matthiasbruns matthiasbruns requested a review from a team as a code owner March 2, 2026 08:33

@jakobmoellerdev jakobmoellerdev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general this is okay as impl, but im a bit afraid of the impact. wouldn't this mean that nullable needs to manually applied everywhere? TBH this is a very difficult thing for me because it would mean that pretty much most schemas would need to accept null. While functionally correct, Im afraid of implications on the schemas and the generators.

Wouldn't another choice be that we have the value return of null be equivalent to an empty value? As in, if a null value is discovered, that it gets inferred to be the zero value? for example, a field that is a []string, and gets set to null from an expression, could be [] as a value?

Happy to hear your thoughts

@matthiasbruns

Copy link
Copy Markdown
Contributor Author

In general this is okay as impl, but im a bit afraid of the impact. wouldn't this mean that nullable needs to manually applied everywhere? TBH this is a very difficult thing for me because it would mean that pretty much most schemas would need to accept null. While functionally correct, Im afraid of implications on the schemas and the generators.

Wouldn't another choice be that we have the value return of null be equivalent to an empty value? As in, if a null value is discovered, that it gets inferred to be the zero value? for example, a field that is a []string, and gets set to null from an expression, could be [] as a value?

Happy to hear your thoughts

  1. I wasnt sure if we would want to have this automatically applied, but thinking about this in the OCM context, I don't know what should break by automatically allowing null for pointer values
  2. I would not allow this by default, cause it could really introduce some behaviour we might not want. But, we could opt-in this by using optional here like:
type test struct {
  //+optional
  MyVal int `json:myVal`
 }
{
  "myVal":null
}

could result in
myVal=0

If there is no +optional, I would want the validation to fail.

WDYT @jakobmoellerdev

@jakobmoellerdev

Copy link
Copy Markdown
Member

I think optional is not a good choice because it is the equivalent of omitempty to stay consistent with kubebuilder syntax

@matthiasbruns

Copy link
Copy Markdown
Contributor Author

I think optional is not a good choice because it is the equivalent of omitempty to stay consistent with kubebuilder syntax

ok let me focus on the nullable pointer implementation first (without nullable)
let's discuss the value defaulting in a follow up

matthiasbruns and others added 2 commits March 2, 2026 12:36
On-behalf-of: SAP <matthias.bruns@sap.com>
Signed-off-by: Matthias Bruns <git@matthiasbruns.com>
@matthiasbruns matthiasbruns changed the title feat: add support for +nullable optin in json generator feat: pointer values allow null values by default Mar 2, 2026
…uns/open-component-model into feat/883_nullable_json_scheme

Signed-off-by: Matthias Bruns <git@matthiasbruns.com>
@matthiasbruns matthiasbruns force-pushed the feat/883_nullable_json_scheme branch from a3bd9fb to 299394d Compare March 2, 2026 12:52
@matthiasbruns

matthiasbruns commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

now all pointers support null by default

@github-actions github-actions Bot added the size/l Large label Mar 3, 2026
@matthiasbruns

matthiasbruns commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

I added omitempty (+optional before) back to the game. Based on @jakobmoellerdev comment, kubebuilder favors omitempty over the old +optional decorator https://book.kubebuilder.io/reference/markers

the reason:

  • we do not want to allow null for every pointer, e.g. Repository in Transform spec as well as input and output
  • I still think the impact of null-by-default for pointers is too wide and might introduce unwanted behaviour
  • we should actively decide, if we want to allow null for pointers

@matthiasbruns matthiasbruns force-pushed the feat/883_nullable_json_scheme branch from 365b445 to e735f18 Compare March 3, 2026 07:20
On-behalf-of: SAP <matthias.bruns@sap.com>
Signed-off-by: Matthias Bruns <git@matthiasbruns.com>
@matthiasbruns matthiasbruns force-pushed the feat/883_nullable_json_scheme branch from e735f18 to 317fc8e Compare March 3, 2026 07:26
@matthiasbruns matthiasbruns changed the title feat: pointer values allow null values by default feat: pointer values allow null values when omitempty in json tag Mar 3, 2026
@jakobmoellerdev

Copy link
Copy Markdown
Member

I think you missed the mark here. I was saying that omitempty is the equivalent of having no value and is also equivalent to +optional. Thus adding "nullable" to the behavior is not correct: just because a value is optional does not mean it is nullable.

From above my suggestion was

Wouldn't another choice be that we have the value return of null be equivalent to an empty value? As in, if a null value is discovered, that it gets inferred to be the zero value? for example, a field that is a []string, and gets set to null from an expression, could be [] as a value?

This is still not happening and you rely on adjusting the schema instead.

@matthiasbruns

Copy link
Copy Markdown
Contributor Author

We will postpone this, this needs more discussions - related PR kubernetes-sigs/kro#1003

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature new feature, enhancement, improvement, extension size/l Large size/m Medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants