Skip to content

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

Open
@meetwithabhishek

Description

@meetwithabhishek

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions