skip json field (#1009)

* skip json field

* backfill some coverage and tests
This commit is contained in:
JT A.
2025-05-29 04:31:35 -06:00
committed by GitHub
parent 8c65b35c57
commit ff9d83a485
2 changed files with 52 additions and 2 deletions

View File

@@ -126,9 +126,12 @@ func reflectSchemaObject(t reflect.Type) (*Definition, error) {
} }
jsonTag := field.Tag.Get("json") jsonTag := field.Tag.Get("json")
var required = true var required = true
if jsonTag == "" { switch {
case jsonTag == "-":
continue
case jsonTag == "":
jsonTag = field.Name jsonTag = field.Name
} else if strings.HasSuffix(jsonTag, ",omitempty") { case strings.HasSuffix(jsonTag, ",omitempty"):
jsonTag = strings.TrimSuffix(jsonTag, ",omitempty") jsonTag = strings.TrimSuffix(jsonTag, ",omitempty")
required = false required = false
} }

View File

@@ -329,6 +329,53 @@ func TestStructToSchema(t *testing.T) {
"additionalProperties":false "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 { for _, tt := range tests {