[QUESTION] ReadQuery cannot parse fields with underscores.
Describe the bug When using the ReadQuery method in the Iris framework to parse URL query parameters from a GET request, parameters with snake_case naming (e.g., team_id) are not parsed correctly. In contrast, camelCase parameters (e.g., environmentId) are parsed successfully. Even when the struct field is tagged with form:"team_id", ReadQuery fails to bind the value, leaving the field with its zero value (e.g., team_id = 0).
To Reproduce Steps to reproduce the behavior:
- Define the following struct:
type GetSelfHostProxiesRequest struct {
ID int32 `form:"id" json:"id"` // Proxy ID
TeamID int32 `form:"team_id" json:"team_id"` // Team ID
EnvironmentID int32 `form:"environmentId" json:"environmentId"` // Environment ID
Type int16 `form:"type" json:"type"` // Proxy Type
Name string `form:"name" json:"name"` // Proxy Name
Limit int `form:"limit" json:"limit"` // Page Limit
Offset int `form:"offset" json:"offset"` // Page Offset
}
- Use ReadQuery in your handler to parse the parameters:
var request v1.GetSelfHostProxiesRequest
if err := ctx.ReadQuery(&request); err != nil {
h.HandleBadRequest(ctx, err, "Invalid query parameters")
return
}
- Send a GET request with both snake_case and camelCase parameters:
http://0.0.0.0:52618/api/v1/admin/self-host-proxies?id=1&team_id=2&environmentId=3&type=4&name=5&limit=6&offset=7
Expected behavior The TeamID field should be correctly assigned the value 2 (from team_id=2), and all other fields should also be parsed correctly. The expected parsed result should be:
{
"id": 1,
"team_id": 2,
"environmentId": 3,
"type": 4,
"name": "5",
"limit": 6,
"offset": 7
}
Screenshots The actual content of the request struct printed by the backend:
{
"environmentId": 3,
"id": 1,
"limit": 6,
"name": "5",
"offset": 7,
"team_id": 0,
"type": 4
}
❌ The value of team_id is 0 (parsing failed). ✅ All other fields, including the camelCase environmentId, were parsed successfully. Desktop (please complete the following information):
- OS: Linux / macOS / Windows
- Go version: 1.25.1
- Iris version: v12.2.11
iris.Version
- e.g. v12.2.11 or main
Additional context I’m not sure if this is actually a bug, since I couldn’t find anything in the documentation saying that parsing parameters with underscores isn’t supported. I’m new to Go, so please excuse me if I’m not being clear. I’ve done my best to provide as much detail as possible.
Got it, I understand now. I need to add the url tag in the struct to make ReadQuery correctly parse the fields. But I'm confused about something: why were fields without underscores parsed correctly even when I only used the json tag before?
Hello @0854321, the json tags have nothing to do with it, fields without underscore worked without url tags because if tag (e.g. url) is missing from a struct field its name is the struct field's name itself, in your case the teamid. To ignore a field from URL/Form/... reading you pass the tag value -, e.g. url:"-".