Skip to content

Commit 27ac9d3

Browse files
authored
docs: fix removed opensearch.BuildRequest reference in v4 upgrade guide (#978)
* docs: fix removed opensearch.BuildRequest reference in v4 upgrade guide The 4.7.0 migration section and the raw-JSON usage guide told readers to forward a custom Request.GetRequest to opensearch.BuildRequest, but that helper was removed in 4.7.0 -- the very release the section documents. Replace it with a net/http-based before/after example (relative path; the transport prepends the base URL). Backport of the v4-applicable portion of the #977 fix from main. Reported in #977. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent 908a8f5 commit 27ac9d3

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

UPGRADING.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,33 @@ GetRequest() (*http.Request, error)
168168
GetRequest(method string) (*http.Request, error)
169169
```
170170

171-
This change is invisible to almost all callers: the typed `Req` structs that the client consumes (e.g. `opensearchapi.SearchReq`, the v5-preview `opensearchapi.IndexReq`) already implement the new signature. Only code that defines a custom type satisfying `opensearch.Request` is affected. If you maintain such a type, add a `method string` parameter and forward it to your underlying `http.NewRequest` call (or `opensearch.BuildRequest`).
171+
This change is invisible to almost all callers: the typed `Req` structs that the client consumes (e.g. `opensearchapi.SearchReq`, the v5-preview `opensearchapi.IndexReq`) already implement the new signature. Only code that defines a custom type satisfying `opensearch.Request` is affected.
172+
173+
If you maintain such a type, add a `method string` parameter and forward it to your request builder. The `opensearch.BuildRequest` helper that earlier v4 releases exposed for this purpose was **removed in 4.7.0**; construct the request with `net/http` directly instead. Give the path a leading slash (e.g. `/_plugins/my_plugin/status`) -- the transport prepends the base URL by string concatenation, so a path without a leading slash produces a malformed URL.
174+
175+
```go
176+
// Before (<= 4.6.0): method stored on the struct, built via the removed
177+
// opensearch.BuildRequest helper (which set Content-Type for a non-nil body).
178+
func (r customReq) GetRequest() (*http.Request, error) {
179+
return opensearch.BuildRequest(r.method, r.path, r.body, nil, nil)
180+
}
181+
182+
// After (>= 4.7.0): method comes from the caller, built with net/http.
183+
func (r customReq) GetRequest(method string) (*http.Request, error) {
184+
req, err := http.NewRequest(method, r.path, r.body)
185+
if err != nil {
186+
return nil, err
187+
}
188+
// BuildRequest set this automatically for a non-nil body; http.NewRequest
189+
// does not, so set it here or OpenSearch may reject a JSON body with 400/415.
190+
if r.body != nil {
191+
req.Header.Set("Content-Type", "application/json")
192+
}
193+
return req, nil
194+
}
195+
```
196+
197+
`opensearch.BuildRequest` also accepted `params map[string]string` and `headers http.Header` arguments. To preserve those, set them on the `*http.Request` after construction: encode params onto `req.URL.RawQuery` (via `url.Values`) and add headers to `req.Header`.
172198

173199
### Path segment values are percent-encoded
174200

guides/json.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,27 @@ The `Client.Do()` method accepts `any` for its response parameter, which means p
5252
First, define a request type that satisfies `opensearch.Request`:
5353
5454
```go
55-
// customReq wraps opensearch.BuildRequest to satisfy the opensearch.Request interface.
55+
// customReq builds an *http.Request from a path with a leading slash (e.g.
56+
// "/_plugins/my_plugin/status") to satisfy the opensearch.Request interface.
57+
// The transport prepends the base URL. method is an HTTP method
58+
// (e.g. http.MethodGet) forwarded by the caller.
5659
type customReq struct {
5760
path string
5861
body io.Reader
5962
}
6063

6164
func (r customReq) GetRequest(method string) (*http.Request, error) {
62-
return opensearch.BuildRequest(method, r.path, r.body, nil, nil)
65+
req, err := http.NewRequest(method, r.path, r.body)
66+
if err != nil {
67+
return nil, err
68+
}
69+
// opensearch.BuildRequest set this automatically for a non-nil body;
70+
// http.NewRequest does not, so set it here or OpenSearch may reject a
71+
// JSON body with 400/415.
72+
if r.body != nil {
73+
req.Header.Set("Content-Type", "application/json")
74+
}
75+
return req, nil
6376
}
6477
```
6578

0 commit comments

Comments
 (0)