-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] [Go] Go client query parameters generated as reflect.Value value
instead of actual string values
#14798
Comments
Confirmed same problem in version 6.6.0. |
This is fixed for me on master as of now (ba0c73e) |
What are your steps to verify? Could you paste an example of the generated code? Would love to close this issue. Thanks. |
Tested with the latest main, the client code is fixed. The multi-parameter request generation on the client is correct:
There's still one issue on the server side. In // GETBook -
func (c *DefaultAPIController) GETBook(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
...
bookDetailsParam := strings.Split(query.Get("book-details"), ",")
...
result, err := c.service.GETBook(r.Context(), bookIdParam, bookDetailsParam, bookAuthorParam)
... which assumes that the client parameters are concatenated with comma, while the default behavior on client is the query shown above:
A quick fix would be to simply return all the list as bookDetailsParam := query["book-details"] There's a workaround to this, which is to use comma-concatenation on the client side as well. Please let me know if this is worth a fix. Right now I'm happy to close this issue as is. Thanks. |
Nice breakdown, @hxy9243. My tests really only covered the generated client. |
It should be commit 117e511 which fixed this by unwrapping the reflect.Value (though it would not have been my preferred solution). I think changing the first lines of the function // previously: var v = reflect.ValueOf(obj)
var v reflect.Value
if refValue, ok := obj.(reflect.Value); ok {
v = refValue
} else {
v = reflect.ValueOf(obj)
} would be more future proof? |
If this is confirmed fixed for now, I'd like to close this issue. For the improvement mentioned above, I'd prefer if you could create another ticket. @mytlogos Thoughts? |
I tested it in version 7.0.0 (and manually) and this fixes it. |
Sounds good. Thanks all for the effort. |
Bug Report Checklist
Description
openapi-generator version
7.0.0 snapshot from https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.0.0-SNAPSHOT/
Jar file link address: https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.0.0-SNAPSHOT/openapi-generator-cli-7.0.0-20221012.083708-4.jar
Verified the behaviors are correct (not generating
reflect.Value value
but actual string value correctly) on:It might be a regression in 6.3.0.
OpenAPI declaration file content or URL
Complete API spec example along with example client code here as a gist: https://gist.github.com/hxy9243/0378a0601bc329f5c20cc5d8efaf9c6c#file-api-yaml-L80
An example project here with full example server and client code: https://github.com/hxy9243/learn-go-by-examples/tree/main/src/openapi
Generation Details
We used the client code in the actual client code, and try to get the actual request URL and parameters sent to the server.
The output that's related to the code snippet above:
where
book-details
are not using the actual string value but instead showed asreflect.Value
in request URL.In previous versions, the URL looks more correct:
There's another problem on the server side where the server is only getting one of the parameters instead of three. From the Go-server generated code, it seems it's only querying the first query parameter and expect it to be comma separated. And this is not the default behavior on client side. See example Go server code below:
And the
query.Get
function only gets the first parameter as the default behavior of Go'snet/url
package:It'll be nice to have both behaviors consistent on server and client, and the generated code can be used out of box without hacks.
Steps to reproduce
Go Server code generation command:
Go Client code generation command:
See more at gist: https://gist.github.com/hxy9243/0378a0601bc329f5c20cc5d8efaf9c6c#file-api-yaml-L80
Related issues/PRs
Suggest a fix
There are two issues with the code that can be improved:
The text was updated successfully, but these errors were encountered: