Skip to content

[QUESTION] ReadQuery cannot parse fields with underscores. #2588

@0854321

Description

@0854321

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:

  1. 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
}
  1. 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
}
  1. 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.

Image

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions