Skip to content

skip json field #1009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions jsonschema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions jsonschema/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading