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

Custom MarshalJSON() ignores omitempty #82

Open
ieure opened this issue Jan 8, 2022 · 0 comments
Open

Custom MarshalJSON() ignores omitempty #82

ieure opened this issue Jan 8, 2022 · 0 comments

Comments

@ieure
Copy link
Contributor

ieure commented Jan 8, 2022

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.

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

1 participant