function-sdk-go icon indicating copy to clipboard operation
function-sdk-go copied to clipboard

JSON marshalling not working with the `inline` and `omitempty` options

Open TheBigLee opened this issue 1 year ago • 1 comments

What happened?

When you use a specific field in a resource that has the inline and the omitempty field specified, the json marshaling will fail with the error:

json: cannot marshal Go value of type v1beta1.DataKeySelector: Go struct field
      NamespacedName cannot have any options other than `inline` or `unknown` specified'

This error is for example triggered when using the set field of a release using provider-helm

How can we reproduce it?

  • Have a v1beta1.Release from provider-helm
  • Have a SetVal object in the release like this:
v1beta1.Release{
  Spec: v1beta1.ReleaseSpec{
    ForProvider: v1beta1.ReleaseParameters{
      ValuesSpec: v1beta1.ValuesSpec{
        Set: []v1beta1.SetVal{
          {
            Name: "password",
            ValueFrom: &v1beta1.ValueFromSource{
              SecretKeyRef: &v1beta1.DataKeySelector{
                NamespacedName: v1beta1.NamespacedName{
                  Namespace: "foo",
                  Name: "bar",
                },
                Key: "password",
              },
            },
          },
        },
      },
    },
  },
}

Why does this fail? The NamespacedName field in provider-helm has the following json fields: ,inline,omitempty (see: https://github.com/crossplane-contrib/provider-helm/blob/main/apis/release/v1beta1/types.go#L50) However, this is currently not supported by the json-library used in this function-sdk-go (see: https://github.com/go-json-experiment/json/blob/master/fields.go#L105-L111)

The error is triggered by calling the json.Marshal() function here: https://github.com/crossplane/function-sdk-go/blob/v0.3.0-rc.0/resource/composed/composed.go#L76

What environment did it happen in?

Crossplane version: 1.17 Provider-Helm: v0.19.0 Function-sdk-go: v0.3.0-rc.0 go-json-experiment: v0.0.0-20231102232822-2e55bd4e08b0

TheBigLee avatar Sep 04 '24 13:09 TheBigLee

Hrm, that's unfortunate.

I wonder if this is a behavior change in go-json-experiment relative to when we started using it. The comment I wrote seems to imply that omitempty used to work.

https://github.com/crossplane/function-sdk-go/blob/c987192d28b47a15f14b305c7f582954db4d2fda/resource/composed/composed.go#L63-L75

Edit: Ah, I missed that the issue is when inline and omitempty are used together. Is that even a valid combination of tags for encoding/json? I wonder if the inlined (non-struct) fields are actually omitted if they're empty. If they're not omitted, it might be best to update provider-helm.

negz avatar Sep 18 '24 18:09 negz