Skip to content

bug(api): When Route Path Contains No Placeholders, Path Parameters Declared in Request Are Still Generated #5428

@spencercjh

Description

@spencercjh

When Route Path Contains No Placeholders, Path Parameters Declared in Request Are Still Generated (Resulting in Invalid Swagger)

Phenomenon

If a request struct declares path:"id", but the route path has no /:id (or {id}) placeholder, goctl still generates parameters with in: path, name: id in the swagger.

This creates swagger/OpenAPI semantic inconsistency: path parameters must appear in the URL template.

Minimal Reproduction

extra_path_param.api:

syntax = "v1"

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

type Req {
  ID string `path:"id"`
}

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

Run:

goctl api swagger --api extra_path_param.api --dir . --filename extra_path_param

Actual Result (Key Swagger Snippet)

[
  {
    "type": "string",
    "name": "id",
    "in": "path",
    "required": true
  }
]

However, the corresponding path is /api/test/foo, which does not contain {id}.

Expected Result

  • goctl should validate: only generate path parameters when the route path contains corresponding placeholders.
  • Or at minimum print a warning / fail generation and prompt users to fix the .api file.

Possible Fix Directions (For PR Reference)

  • When generating path parameters, the swagger generator should parse the route path (e.g., /foo) to extract the set of placeholders (id, namespace, etc.), and only keep matching path:"..." fields.
  • If request declares extra path parameters, suggest warning (and optionally error in strict mode).

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