|
5 | 5 | "encoding/json/jsontext" |
6 | 6 | "encoding/json/v2" |
7 | 7 | "fmt" |
| 8 | + "go/token" |
| 9 | + "go/types" |
8 | 10 | "net/http" |
9 | 11 | "net/url" |
10 | 12 | "slices" |
@@ -276,6 +278,10 @@ func fromParam(p *openapi.Parameter, apiTitle string) (Param, error) { |
276 | 278 | } |
277 | 279 |
|
278 | 280 | param.GoName = strcase.ToGoCamel(p.Name) |
| 281 | + if p.In == openapi.ParameterLocationPath { |
| 282 | + param.GoName = pathParamName(p.Name) |
| 283 | + } |
| 284 | + |
279 | 285 | param.AllowSlashes = p.In == openapi.ParameterLocationPath && allowsSlashes(p) |
280 | 286 |
|
281 | 287 | param.FieldName = goPascal(p.Name) |
@@ -335,7 +341,7 @@ func fromParam(p *openapi.Parameter, apiTitle string) (Param, error) { |
335 | 341 |
|
336 | 342 | if param.VarName == "" { |
337 | 343 | if p.In == openapi.ParameterLocationPath { |
338 | | - param.VarName = strcase.ToGoCamel(p.Name) |
| 344 | + param.VarName = param.GoName |
339 | 345 | } else { |
340 | 346 | param.VarName = "params." + param.FieldName |
341 | 347 | } |
@@ -407,6 +413,29 @@ func parseExpr(goType string, unixTime bool) (expr, conv string, errFree bool) { |
407 | 413 | } |
408 | 414 | } |
409 | 415 |
|
| 416 | +// generatedNames are the identifiers the generated code refers to where a path parameter is in scope: the locals of |
| 417 | +// the client's methods and the server's handlers, the packages they use and the client's receiver. |
| 418 | +var generatedNames = map[string]bool{ |
| 419 | + "api": true, "body": true, "c": true, "civil": true, "cmp": true, "context": true, "ctx": true, "cursor": true, |
| 420 | + "errors": true, "escaped": true, "fmt": true, "http": true, "i": true, "ia": true, "io": true, "iter": true, |
| 421 | + "json": true, "l": true, "maps": true, "msg": true, "mt": true, "net": true, "ok": true, "out": true, |
| 422 | + "paging": true, "params": true, "parts": true, "pr": true, "pw": true, "q": true, |
| 423 | + "r": true, "req": true, "res": true, "rsp": true, "sErr": true, "server": true, "slices": true, "slog": true, |
| 424 | + "strconv": true, "streaming": true, "strings": true, "svc": true, "time": true, "u": true, "url": true, |
| 425 | + "uuid": true, "v": true, "w": true, |
| 426 | +} |
| 427 | + |
| 428 | +// pathParamName is the Go identifier of the path parameter named name, which the generated method takes as an |
| 429 | +// argument: suffixed where it would collide with a keyword, a predeclared name or one of generatedNames. |
| 430 | +func pathParamName(name string) string { |
| 431 | + id := strcase.ToGoCamel(name) |
| 432 | + if generatedNames[id] || token.IsKeyword(id) || types.Universe.Lookup(id) != nil { |
| 433 | + return id + "Param" |
| 434 | + } |
| 435 | + |
| 436 | + return id |
| 437 | +} |
| 438 | + |
410 | 439 | // buildJoinPathArgs produces the ordered list of Go expressions for url.JoinPath. |
411 | 440 | // e.g. "/apis/{id}/items" → [`"apis"`, `strconv.Itoa(id)`, `"items"`] |
412 | 441 | func buildJoinPathArgs(parsed openapi.ParsedPath, params map[string]Param) []string { |
|
0 commit comments