Skip to content
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

JSON marshalling not working with the inline and omitempty options #161

Open
TheBigLee opened this issue Sep 4, 2024 · 1 comment
Open
Labels
bug Something isn't working

Comments

@TheBigLee
Copy link

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 TheBigLee added the bug Something isn't working label Sep 4, 2024
@negz
Copy link
Member

negz commented Sep 18, 2024

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.

// Round-trip the supplied object through JSON to convert it. We use the
// go-json-experiment package for this because it honors the omitempty field
// for non-pointer struct fields.
//
// At the time of writing many Crossplane structs contain fields that have
// the omitempty struct tag, but non-pointer struct values. pkg/json does
// not omit these fields. It instead includes them as empty JSON objects.
// Crossplane will interpret this as part of a server-side apply fully
// specified intent and assume the function actually has opinion about the
// field when it doesn't. We should make these fields pointers, but it's
// easier and safer in the meantime to work around it here.
//
// https://github.com/go-json-experiment/json#behavior-changes

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants