JSON tag uses "omitempty" as the field name #14163

Closed
opened 2025-11-02 11:04:56 -06:00 by GiteaMirror · 0 comments
Owner

Originally created by @Vasco-jofra on GitHub (Feb 20, 2025).

Description

Hi! While doing a broad search for this pattern, I noticed that you use "omitempty" as the name of a structure field.

cd225d7034/models/admin/task.go (L47)

Instead, I believe you intended to use ",omitempty" to keep the default name and add the omitempty option.

You can test this behavior with this simple Go program. As you can see by the result, the omitempty key in the JSON string gets unmarshaled to the field with the json:"omitempty" tag.

package main

import (
	"encoding/json"
	"fmt"
)

type SomeStruct struct {
	SomeField string `json:"omitempty"`
}

func main() {
	u := SomeStruct{}
	_ = json.Unmarshal([]byte(`{"omitempty": "123"}`), &u)
	fmt.Printf("Result: %#v\n", u)
	// Result: main.SomeStruct{SomeField:"123"}
}

Gitea Version

Can you reproduce the bug on the Gitea demo site?

No

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

No response

How are you running Gitea?

Database

None

Originally created by @Vasco-jofra on GitHub (Feb 20, 2025). ### Description Hi! While doing a broad search for this pattern, I noticed that you use `"omitempty"` as the name of a structure field. https://github.com/go-gitea/gitea/blob/cd225d7034f905f73319b5a9024f6f9d5b6a01c7/models/admin/task.go#L47 Instead, I believe you intended to use `",omitempty"` to keep the default name and add the `omitempty` option. You can test this behavior with this simple Go program. As you can see by the result, the `omitempty` key in the JSON string gets unmarshaled to the field with the `json:"omitempty"` tag. ```go package main import ( "encoding/json" "fmt" ) type SomeStruct struct { SomeField string `json:"omitempty"` } func main() { u := SomeStruct{} _ = json.Unmarshal([]byte(`{"omitempty": "123"}`), &u) fmt.Printf("Result: %#v\n", u) // Result: main.SomeStruct{SomeField:"123"} } ``` ### Gitea Version - ### Can you reproduce the bug on the Gitea demo site? No ### Log Gist _No response_ ### Screenshots _No response_ ### Git Version _No response_ ### Operating System _No response_ ### How are you running Gitea? - ### Database None
GiteaMirror added the type/bug label 2025-11-02 11:04:56 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#14163