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

No option to indicate an empty value for optional array field #231

Open
meetwithabhishek opened this issue May 26, 2024 · 1 comment
Open

Comments

@meetwithabhishek
Copy link

I'm using the go-jsonschema tool to generate the Go Structures and then use these Go Structures to marshal the values I'd set to JSON.

I've defined the following JSON schema -

{
  "type": "object",
  "title": "Update Object",
  "properties": {
    "fieldValues": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}

The Go Structure generated for the above JSON schema is -

// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.

package main

type UpdateObject struct {
	// FieldValues corresponds to the JSON schema field "fieldValues".
	FieldValues []string `json:"fieldValues,omitempty" yaml:"fieldValues,omitempty" mapstructure:"fieldValues,omitempty"`
}

If I set an empty slice in FieldValues, the json marshaler omits this field from the marshaled data. It is because JSON Marshaler considers an empty slice as an empty value, rather than a nil slice as an empty value.

I want a way where I can indicate in the marshaled JSON if the FieldValues was set but empty. I can achieve this by making the FieldValues as *[]string and then setting a pointer to empty slice in FieldValues, but I don't see any way in the tool using which I can specify something in the schema for the fieldValues or in the options of the tool that I want the array to be generated as a pointer to a slice. Is there any way?

Also, I don't want to make this field required because if the fieldValues is not set, I don't want to marshal anything in the marshaled JSON.

@CarbonC
Copy link

CarbonC commented Oct 8, 2024

@omissis @meetwithabhishek I ran into the same issue, which is a blocker for me. I opened a PR that seems to be working for me #291

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants