From ff9d83a4854790ecbd16e6328415a32c7497efaf Mon Sep 17 00:00:00 2001 From: "JT A." Date: Thu, 29 May 2025 04:31:35 -0600 Subject: [PATCH] skip json field (#1009) * skip json field * backfill some coverage and tests --- jsonschema/json.go | 7 ++++-- jsonschema/json_test.go | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/jsonschema/json.go b/jsonschema/json.go index d458418..03bb688 100644 --- a/jsonschema/json.go +++ b/jsonschema/json.go @@ -126,9 +126,12 @@ func reflectSchemaObject(t reflect.Type) (*Definition, error) { } jsonTag := field.Tag.Get("json") var required = true - if jsonTag == "" { + switch { + case jsonTag == "-": + continue + case jsonTag == "": jsonTag = field.Name - } else if strings.HasSuffix(jsonTag, ",omitempty") { + case strings.HasSuffix(jsonTag, ",omitempty"): jsonTag = strings.TrimSuffix(jsonTag, ",omitempty") required = false } diff --git a/jsonschema/json_test.go b/jsonschema/json_test.go index 17f0aba..84f25fa 100644 --- a/jsonschema/json_test.go +++ b/jsonschema/json_test.go @@ -329,6 +329,53 @@ func TestStructToSchema(t *testing.T) { "additionalProperties":false }`, }, + { + name: "Test with exclude mark", + in: struct { + Name string `json:"-"` + }{ + Name: "Name", + }, + want: `{ + "type":"object", + "additionalProperties":false + }`, + }, + { + name: "Test with no json tag", + in: struct { + Name string + }{ + Name: "", + }, + want: `{ + "type":"object", + "properties":{ + "Name":{ + "type":"string" + } + }, + "required":["Name"], + "additionalProperties":false + }`, + }, + { + name: "Test with omitempty tag", + in: struct { + Name string `json:"name,omitempty"` + }{ + Name: "", + }, + want: `{ + "type":"object", + "properties":{ + "name":{ + "type":"string" + } + }, + "additionalProperties":false + }`, + }, } for _, tt := range tests {