skip json field (#1009)
* skip json field * backfill some coverage and tests
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user