Skip to content

Commit ff9d83a

Browse files
authored
skip json field (#1009)
* skip json field * backfill some coverage and tests
1 parent 8c65b35 commit ff9d83a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

jsonschema/json.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,12 @@ func reflectSchemaObject(t reflect.Type) (*Definition, error) {
126126
}
127127
jsonTag := field.Tag.Get("json")
128128
var required = true
129-
if jsonTag == "" {
129+
switch {
130+
case jsonTag == "-":
131+
continue
132+
case jsonTag == "":
130133
jsonTag = field.Name
131-
} else if strings.HasSuffix(jsonTag, ",omitempty") {
134+
case strings.HasSuffix(jsonTag, ",omitempty"):
132135
jsonTag = strings.TrimSuffix(jsonTag, ",omitempty")
133136
required = false
134137
}

jsonschema/json_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,53 @@ func TestStructToSchema(t *testing.T) {
329329
"additionalProperties":false
330330
}`,
331331
},
332+
{
333+
name: "Test with exclude mark",
334+
in: struct {
335+
Name string `json:"-"`
336+
}{
337+
Name: "Name",
338+
},
339+
want: `{
340+
"type":"object",
341+
"additionalProperties":false
342+
}`,
343+
},
344+
{
345+
name: "Test with no json tag",
346+
in: struct {
347+
Name string
348+
}{
349+
Name: "",
350+
},
351+
want: `{
352+
"type":"object",
353+
"properties":{
354+
"Name":{
355+
"type":"string"
356+
}
357+
},
358+
"required":["Name"],
359+
"additionalProperties":false
360+
}`,
361+
},
362+
{
363+
name: "Test with omitempty tag",
364+
in: struct {
365+
Name string `json:"name,omitempty"`
366+
}{
367+
Name: "",
368+
},
369+
want: `{
370+
"type":"object",
371+
"properties":{
372+
"name":{
373+
"type":"string"
374+
}
375+
},
376+
"additionalProperties":false
377+
}`,
378+
},
332379
}
333380

334381
for _, tt := range tests {

0 commit comments

Comments
 (0)