Skip to content

Support omitzero json tags #161

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type TestUser struct {
Name string `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"`
Friends []int `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"`
Tags map[string]interface{} `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"`
BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
BirthDate time.Time `json:"birth_date,omitzero" jsonschema:"oneof_required=date"` // omitzero requires Go 1.24+
YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
Metadata interface{} `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
Expand Down
6 changes: 3 additions & 3 deletions reflect.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package jsonschema uses reflection to generate JSON Schemas from Go types [1].
//
// If json tags are present on struct fields, they will be used to infer
// property names and if a property is required (omitempty is present).
// property names and if a property is required (omitempty or omitzero is present).
//
// [1] http://json-schema.org/latest/json-schema-validation.html
package jsonschema
Expand Down Expand Up @@ -103,7 +103,7 @@ type Reflector struct {

// RequiredFromJSONSchemaTags will cause the Reflector to generate a schema
// that requires any key tagged with `jsonschema:required`, overriding the
// default of requiring any key *not* tagged with `json:,omitempty`.
// default of requiring any key *not* tagged with `json:,omitempty` or `json:,omitzero`.
RequiredFromJSONSchemaTags bool

// Do not reference definitions. This will remove the top-level $defs map and
Expand Down Expand Up @@ -931,7 +931,7 @@ func requiredFromJSONTags(tags []string, val *bool) {
}

for _, tag := range tags[1:] {
if tag == "omitempty" {
if tag == "omitempty" || tag == "omitzero" {
*val = false
return
}
Expand Down
2 changes: 1 addition & 1 deletion reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type TestUser struct {
IgnoredCounter int `json:"-"`

// Tests for RFC draft-wright-json-schema-validation-00, section 7.3
BirthDate time.Time `json:"birth_date,omitempty"`
BirthDate time.Time `json:"birth_date,omitzero"`
Website url.URL `json:"website,omitempty"`
IPAddress net.IP `json:"network_address,omitempty"`

Expand Down