Title: Request Editors are broken · Issue #298 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: Request Editors are broken · Issue #298 · oapi-codegen/oapi-codegen
X Title: Request Editors are broken · Issue #298 · oapi-codegen/oapi-codegen
Description: There seems to be an bug introduced with the recent PR #228, resulting in request editors to not work as advertised. Here's a snippet of code to illustrate the issue: using the example petstore-expanded.yaml OpenAPI spec provided in this...
Open Graph Description: There seems to be an bug introduced with the recent PR #228, resulting in request editors to not work as advertised. Here's a snippet of code to illustrate the issue: using the example petstore-exp...
X Description: There seems to be an bug introduced with the recent PR #228, resulting in request editors to not work as advertised. Here's a snippet of code to illustrate the issue: using the example petstore...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/298
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Request Editors are broken","articleBody":"There seems to be an bug introduced with the recent PR #228, resulting in request editors to not work as advertised.\r\n\r\nHere's a snippet of code to illustrate the issue: using the example [`petstore-expanded.yaml`](https://github.com/deepmap/oapi-codegen/blob/master/examples/petstore-expanded/petstore-expanded.yaml) OpenAPI spec provided in this project, I've generated a Go file of the client bindings (`oapi-codegen -generate types,client -package main -o petstore.gen.go petstore-expanded.yaml`), then I added some print statements inside the generated `Client.FindPets()` method as well as in `Client.applyEditors()`:\r\n\r\n```go\r\n// petstore.gen.go\r\nfunc (c *Client) FindPets(ctx context.Context, params *FindPetsParams, reqEditors ...RequestEditorFn) (*http.Response, error) {\r\n req, err := NewFindPetsRequest(c.Server, params)\r\n if err != nil {\r\n return nil, err\r\n }\r\n fmt.Printf(\"FindPets before applyEditors: req.Host=%s\\n\", req.Host)\r\n if err := c.applyEditors(ctx, req, reqEditors); err != nil {\r\n return nil, err\r\n }\r\n fmt.Printf(\"FindPets after applyEditors: req.Host=%s\\n\", req.Host)\r\n return c.Client.Do(req)\r\n}\r\n\r\n// ...\r\n\r\nfunc (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error {\r\n req = req.WithContext(ctx)\r\n for _, r := range c.RequestEditors {\r\n if err := r(ctx, req); err != nil {\r\n return err\r\n }\r\n }\r\n for _, r := range additionalEditors {\r\n if err := r(ctx, req); err != nil {\r\n return err\r\n }\r\n }\r\n fmt.Printf(\"applyEditors: req.Host=%s\\n\", req.Host)\r\n return nil\r\n}\r\n```\r\n\r\nThen, I wrote a little snippet that calls the generated code, and provides a `RequestEditorFn` that rewrites the request `Host` header:\r\n\r\n```go\r\n// main.go\r\npackage main\r\n\r\nimport (\r\n \"context\"\r\n \"net/http\"\r\n)\r\n\r\nfunc editRequest(ctx context.Context, req *http.Request) error {\r\n req.Host = \"www.another-petstore.net\"\r\n\r\n return nil\r\n}\r\n\r\nfunc main() {\r\n ctx := context.Background()\r\n client, err := NewClient(\"https://www.petstore.net/\")\r\n if err != nil {\r\n panic(err)\r\n }\r\n\r\n client.FindPets(ctx, \u0026FindPetsParams{}, editRequest)\r\n}\r\n```\r\n\r\nWhen running the code, we can clearly see that the modifications performed by the `reqEditors` are only local to the `Client.applyEditors()` method, and not to the HTTP request actually sent by the `Client`:\r\n\r\n```\r\n$ go run .\r\nFindPets before applyEditors: req.Host=www.petstore.net\r\napplyEditors: req.Host=www.another-petstore.net\r\nFindPets after applyEditors: req.Host=www.petstore.net\r\n```\r\n\r\nThis looks to be caused by the use of `req.WithContext(ctx)` in the `applyEditors()` method, which cause the modifications performed by the request editor functions to be only local to the method – where in the previous versions, the `req` variable would still be accessible to the `c.Client.Do()` method: https://github.com/deepmap/oapi-codegen/pull/228/files#diff-c8306209ecceed0e799909503c21e674164c27a31308bf57f75c68cd159cc33eR104-R112\r\n\r\nP.S.: It would have been nice to include the new `reqEditors` parameter to the `*WithResponse` methods as well.","author":{"url":"https://github.com/falzm","@type":"Person","name":"falzm"},"datePublished":"2021-02-18T13:34:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/298/oapi-codegen/issues/298"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2ee03aad-3742-683c-6fff-f64c75e8906f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E036:30F730:56B0B3:7A9B80:6A4E701E |
| html-safe-nonce | 12158e7799c4393ab1bbc17352318211f33c86b54bac61f39c6e26f526a3c9de |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDM2OjMwRjczMDo1NkIwQjM6N0E5QjgwOjZBNEU3MDFFIiwidmlzaXRvcl9pZCI6IjY0NTkzNjk3MTk2MTA2MzQyNzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5373cf7e36eca9241782a8c13daa02aad5983dcd8662bdff3e7ac935c8c86efa |
| hovercard-subject-tag | issue:811104035 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/oapi-codegen/oapi-codegen/298/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b5a86ceea16d88168d6520aad91378c167213ab487a20f9f555956d61536a569/oapi-codegen/oapi-codegen/issues/298 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b5a86ceea16d88168d6520aad91378c167213ab487a20f9f555956d61536a569/oapi-codegen/oapi-codegen/issues/298 |
| og:image:alt | There seems to be an bug introduced with the recent PR #228, resulting in request editors to not work as advertised. Here's a snippet of code to illustrate the issue: using the example petstore-exp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | falzm |
| hostname | github.com |
| expected-hostname | github.com |
| None | 41b6ab3ba6d20a71766ac245b5a4a94c6fc672a9cd4da7d44c1b33ab8bf6a21c |
| turbo-cache-control | no-preview |
| go-import | github.com/oapi-codegen/oapi-codegen git https://github.com/oapi-codegen/oapi-codegen.git |
| octolytics-dimension-user_id | 142752710 |
| octolytics-dimension-user_login | oapi-codegen |
| octolytics-dimension-repository_id | 173009358 |
| octolytics-dimension-repository_nwo | oapi-codegen/oapi-codegen |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 173009358 |
| octolytics-dimension-repository_network_root_nwo | oapi-codegen/oapi-codegen |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e6a744804e8e70f97b4d5a18a94dcc63db22f97a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width