Skip to content

Custom MarshalJSON() ignores omitempty #82

@ieure

Description

@ieure

I have a schema with a UUID property. It has a regexp to validate its contents, but isn't marked as required:

"uuid": {
    "type": "string",
    "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
}

Non-required fields get an omitempty added to their json struct tag:

	Uud string `json:"uuid,omitempty"`

However, MarshalJSON doesn't understand omitempty, so it always emits the field:

{"uuid": ""}

This causes schema validation to fail, since the empty string doesn't match the declared pattern.

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