OpenAPITools / OpenAPITools/openapi-generator
[BUG][go] Url Escaped path parameters are double encoded
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator version
6.2.0 and 7.2.0
OpenAPI declaration file content or url
RUN docker-entrypoint.sh generate \
--skip-validate-spec \
-i /spec.json \
--git-host gitlab.com \
--git-user-id abc \
--git-repo-id abc \
-g go \
-o "." \
-p=isGoSubmodule=true \
-p=prependFormOrBodyParameters=true \
-p=enumClassPrefix=true \
-p=generateInterfaces=true \
-p=useOneOfDiscriminatorLookup=true \
--package-name="api" \
1>&2
Steps to reproduce
If you use the generated client like:
nestable_path := "/(root)/team/some-namespace/fake-lease-uuid"
req := target.PublicApiClient().LeaseAPI.ApiV2LeaseUpdateLease(cmd.Context(), nestable_path)
req.Execute()
You would expect the url encoded path to be: /api/v2/lease/%2F%28root%29%2Fteam%2Fsome-namespace%2Ffake-lease-uuid
but instead it is: /api/v2/lease/%252F%2528root%2529%252Fteam%252Fsome-namespace%252Ffake-lease-uuid
Which implies somewhere in the below code the escapes arent being correctly round tripped.
Related issues/PRs
Suggest a fix
The generated code in the Execute has code like:
localVarPath = strings.Replace(localVarPath, "{"+"nestable_path"+"}", url.PathEscape(parameterValueToString(r.nestablePath, "nestablePath")), -1)
and:
url, err := url.Parse(path)
if err != nil {
return nil, err
}
// Override request host, if applicable
if c.cfg.Host != "" {
url.Host = c.cfg.Host
}
// Override request scheme, if applicable
if c.cfg.Scheme != "" {
url.Scheme = c.cfg.Scheme
}
// Adding Query Param
query := url.Query()
for k, v := range queryParams {
for _, iv := range v {
query.Add(k, iv)
}
}
// Encode the parameters.
url.RawQuery = queryParamSplit.ReplaceAllStringFunc(query.Encode(), func(s string) string {
pieces := strings.Split(s, "=")
pieces[0] = queryDescape.Replace(pieces[0])
return strings.Join(pieces, "=")
})
// Generate a new request
if body != nil {
localVarRequest, err = http.NewRequest(method, url.String(), body)
} else {
localVarRequest, err = http.NewRequest(method, url.String(), nil)
}
if err != nil {
return nil, err
}
Which is where i suspect the problem lies.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the generated Go client's Execute method and trace the path through url.PathEscape, url.Parse, and url.String. Reproduce the request using the supplied OpenAPI path and parameter value, then identify where the already escaped path is encoded again. Done means the generated client sends the expected single-escaped path rather than the double-escaped form.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100