From 636efe5eb65246fa96d8475983db0e736189a6bc Mon Sep 17 00:00:00 2001 From: Yvan Guidoin Date: Thu, 13 Feb 2025 16:21:44 +0000 Subject: [PATCH 1/2] Support omitzero json tags --- reflect.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reflect.go b/reflect.go index 73ce7e4..8f1de12 100644 --- a/reflect.go +++ b/reflect.go @@ -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 @@ -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 @@ -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 } From fa5964b7dfd2b065c19956cb43c7602bb637b413 Mon Sep 17 00:00:00 2001 From: Yvan Date: Thu, 13 Feb 2025 16:35:42 +0000 Subject: [PATCH 2/2] Add test on time.Time and example in README Signed-off-by: Yvan --- README.md | 2 +- reflect_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 27b362e..ca59efd 100644 --- a/README.md +++ b/README.md @@ -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"` diff --git a/reflect_test.go b/reflect_test.go index 93bee67..0c36379 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -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"`