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 {