Skip to content

bug(api): form:"-" Not Treated as "Ignore Field" #5427

@spencercjh

Description

@spencercjh

form:"-" Not Treated as "Ignore Field", Instead Generates Query Parameter name: "-" (And Duplicates)

Phenomenon

In the Go ecosystem, struct tags json:"-" / form:"-" semantically mean "ignore this field". However, goctl's swagger generation treats form:"-" as the actual parameter name - and outputs it to query parameters. Multiple form:"-" generate multiple name: "-", directly violating OpenAPI's "(in,name) uniqueness" constraint.

Minimal Reproduction

form_dash.api:

syntax = "v1"

info(
  title: "t"
  desc: "t"
  author: "t"
  email: "t"
)

type Req {
  A string `form:"a,optional"`
  Hidden1 bool  `form:"-"`
  Hidden2 []int `form:"-"`
}

type Resp {
  Ok bool `json:"ok"`
}

@server(
  prefix: /api/test
  group: test
)
service t {
  @handler H
  get /q (Req) returns (Resp)
}

Run:

goctl api swagger --api form_dash.api --dir . --filename form_dash

Actual Result (Key Swagger Snippet)

[
  {
    "type": "string",
    "name": "a",
    "in": "query",
    "allowEmptyValue": true
  },
  {
    "type": "boolean",
    "name": "-",
    "in": "query",
    "required": true
  },
  {
    "type": "array",
    "items": {
      "type": "integer"
    },
    "name": "-",
    "in": "query",
    "required": true
  }
]

Expected Result

  • Fields with form:"-" should be completely ignored and not generate any parameters.

Real-World Case

In stability-alert-center's swagger.json:

  • GET /api/alertcenter/alert-config/my-configs has two query parameters with name: "-" (corresponding to two form:"-" fields).

Possible Fix Directions (For PR Reference)

In tools/goctl/api/swagger/parameter.go, the current logic directly uses tag.Name for formTag / pathParameterTag, lacking special handling for "-".

Suggestions:

  • When formTag.Name == "-", directly return (skip parameter generation).
  • Apply consistent handling to similar tags like json:"-", header:"-", path:"-", etc.
  • Add unit tests covering "-" tags.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions