Skip to content

Commit 49f0820

Browse files
authored
docs: fix removed opensearch.BuildRequest reference and sweep upgrade guides (#979)
* docs: fix removed opensearch.BuildRequest reference and sweep upgrade guides The v4.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). A verification sweep of the remaining upgrade guides fixed: - UPGRADING_V5.md: RawBody() comment wrongly listed error responses as nil. - opensearchapi/UPGRADING_V4_TO_V5.md: SearchParams.Size is *int; the example used a bare int literal that would not compile. - UPGRADING_V3.md: InsecureSkipVerify did not exist in v2.3.0/v3.0.0; error snippet had a variable-name mismatch and a missing return; Msearch/ MsearchTemplate casing in the v2.3.0 before-column. - opensearchapi/UPGRADING_V3_TO_V4.md: scoped the CausedBy addition to v4.6.0. Reported in #977. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent 1e7c13b commit 49f0820

6 files changed

Lines changed: 63 additions & 22 deletions

File tree

UPGRADING_V3.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ client, err := opensearch.NewDefaultClient()
1515
// with config
1616
client, err := opensearch.NewClient(
1717
opensearch.Config{
18-
InsecureSkipVerify: true,
1918
Addresses: []string{"https://localhost:9200"},
2019
Username: "admin",
2120
Password: "admin",
@@ -33,7 +32,6 @@ client, err := opensearchapi.NewDefaultClient()
3332
client, err := opensearchapi.NewClient(
3433
opensearchapi.Config{
3534
Client: opensearch.Config{
36-
InsecureSkipVerify: true, // For testing only. Use certificate for validation.
3735
Addresses: []string{"https://localhost:9200"},
3836
Username: "admin", // For testing only. Don't store credentials in code.
3937
Password: "admin",
@@ -161,8 +159,8 @@ if err != nil {
161159
defer resp.Body.Close()
162160

163161
// Check if the status code is >299
164-
if createIndexResp.IsError() {
165-
fmt.Errorf("Opensearch returned an error. Status: %d", createIndexResp.StatusCode)
162+
if resp.IsError() {
163+
return fmt.Errorf("Opensearch returned an error. Status: %d", resp.StatusCode)
166164
}
167165
```
168166

@@ -310,8 +308,8 @@ Version 3.0.0 reorganized APIs into logical sub-clients. The following tables co
310308
| `client.Count(...)` | `client.Indices.Count(ctx, req)` |
311309
| `client.FieldCaps(...)` | `client.Indices.FieldCaps(ctx, req)` |
312310
| `client.Mget(...)` | `client.MGet(ctx, req)` |
313-
| `client.MSearch(...)` | `client.MSearch(ctx, req)` |
314-
| `client.MSearchTemplate(...)` | `client.MSearchTemplate(ctx, req)` |
311+
| `client.Msearch(...)` | `client.MSearch(ctx, req)` |
312+
| `client.MsearchTemplate(...)` | `client.MSearchTemplate(ctx, req)` |
315313
| `client.Mtermvectors(...)` | `client.MTermvectors(ctx, req)` |
316314
| `client.Indices.AddBlock(...)` | `client.Indices.Block(ctx, req)` |
317315
| `client.Indices.ResolveIndex(...)` | `client.Indices.Resolve(ctx, req)` |

UPGRADING_V4.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,35 @@ GetRequest() (*http.Request, error)
1414
GetRequest(method string) (*http.Request, error)
1515
```
1616

17-
This change is invisible to almost all callers: the typed `Req` structs that the client consumes (e.g. `opensearchapi.SearchReq`, `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`).
17+
This change is invisible to almost all callers: the typed `Req` structs that the client consumes (e.g. `opensearchapi.SearchReq`, `opensearchapi.IndexReq`) already implement the new signature. Only code that defines a custom type satisfying `opensearch.Request` is affected.
18+
19+
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.
20+
21+
```go
22+
// Before (<= 4.6.0): method stored on the struct, built via the removed
23+
// opensearch.BuildRequest helper (which set Content-Type for a non-nil body).
24+
func (r customReq) GetRequest() (*http.Request, error) {
25+
return opensearch.BuildRequest(r.method, r.path, r.body, nil, nil)
26+
}
27+
28+
// After (>= 4.7.0): method comes from the caller, built with net/http.
29+
func (r customReq) GetRequest(method string) (*http.Request, error) {
30+
req, err := http.NewRequest(method, r.path, r.body)
31+
if err != nil {
32+
return nil, err
33+
}
34+
// BuildRequest set this automatically for a non-nil body; http.NewRequest
35+
// does not, so set it here or OpenSearch may reject a JSON body with 400/415.
36+
if r.body != nil {
37+
req.Header.Set("Content-Type", "application/json")
38+
}
39+
return req, nil
40+
}
41+
```
42+
43+
`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`.
44+
45+
> The path must begin with a leading slash (e.g. `r.path == "/_plugins/my_plugin/status"`). The transport builds the final URL by concatenating the base URL with the request path (`base + req.URL.Path` in `opensearchtransport.setReqURL`), not via `url.ResolveReference`, so a path without a leading slash produces a malformed URL.
1846
1947
### Path segment values are percent-encoded
2048

UPGRADING_V5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ body, err := io.ReadAll(resp.Body)
214214
For responses decoded by `opensearch.Execute`, the buffered bytes are also available without consuming the body reader via the `RawBody() []byte` method (useful for inspection or comparison testing):
215215

216216
```go
217-
raw := resp.RawBody() // nil for streamed or error responses; read resp.Body directly there
217+
raw := resp.RawBody() // nil for streamed responses (Client.Stream); read resp.Body directly there
218218
```
219219

220220
## `signer/aws` removed in favor of `signer/awsv2`

guides/usage-json.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,27 @@ When you need to call an API that `opensearchapi` doesn't cover -- plugin endpoi
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

opensearchapi/UPGRADING_V3_TO_V4.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ Files behind custom build tags (`//go:build <tag>`) are loaded under the default
2929

3030
In v3 the API error types lived in `opensearchapi` (`opensearchapi/error.go`). In v4 they moved to the root `opensearch` package and were redesigned. Update the imports and package qualifiers by hand:
3131

32-
| v3 | v4 |
33-
| ----------------------------------------------- | ----------------------------------------------------- |
34-
| `opensearchapi.Error` (`{Err Err; Status int}`) | `opensearch.StructError` (same shape) |
35-
|| `opensearch.Error` (new, simpler `{Err string}`) |
36-
| `opensearchapi.Err` | `opensearch.Err` (adds optional `CausedBy *CausedBy`) |
37-
| `opensearchapi.RootCause` | `opensearch.RootCause` |
38-
| `opensearchapi.StringError` | `opensearch.StringError` |
39-
40-
The v3 detailed-error type `opensearchapi.Error{Err Err; Status int}` is now `opensearch.StructError`; the v4 `opensearch.Error` is a different, simpler type. Re-point type switches and assertions that decoded the detailed error to `opensearch.StructError`. `opensearch.Err` gains an optional `CausedBy *CausedBy` field for nested causes; existing field access is unaffected.
32+
| v3 | v4 |
33+
| ----------------------------------------------- | ----------------------------------------------------------------------- |
34+
| `opensearchapi.Error` (`{Err Err; Status int}`) | `opensearch.StructError` (same shape) |
35+
|| `opensearch.Error` (new, simpler `{Err string}`) |
36+
| `opensearchapi.Err` | `opensearch.Err` (later gains optional `CausedBy *CausedBy`, see below) |
37+
| `opensearchapi.RootCause` | `opensearch.RootCause` |
38+
| `opensearchapi.StringError` | `opensearch.StringError` |
39+
40+
The v3 detailed-error type `opensearchapi.Error{Err Err; Status int}` is now `opensearch.StructError`; the v4 `opensearch.Error` is a different, simpler type. Re-point type switches and assertions that decoded the detailed error to `opensearch.StructError`. In a later v4 release (`v4.6.0`), `opensearch.Err` gained an optional `CausedBy *CausedBy` field for nested causes; existing field access is unaffected.
4141

4242
## Response types no longer expose raw maps
4343

opensearchapi/UPGRADING_V4_TO_V5.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,19 @@ Within `opensearchapi/`, the bulk, NDJSON, and single-document write operations
6161
// v4
6262
client.Search(ctx, &opensearchapi.SearchReq{
6363
Indices: []string{"products"},
64-
Params: opensearchapi.SearchParams{Size: 20},
64+
Params: opensearchapi.SearchParams{Size: opensearch.ToPointer(20)},
6565
})
6666

6767
// v5
6868
client.Search(ctx, &opensearchapi.SearchReq{
6969
Indices: []string{"products"},
70-
Params: &opensearchapi.SearchParams{Size: 20},
70+
Params: &opensearchapi.SearchParams{Size: opensearch.ToPointer(20)},
7171
})
7272
```
7373

74-
Pointer-typed `Params` lets callers pass `nil` when no parameters are needed and keeps the struct cheap to copy.
74+
Pointer-typed `Params` lets callers pass `nil` when no parameters are needed and keeps the struct cheap to copy. `Size` itself has been `*int` since v4.0.0 and is unchanged here -- only the surrounding `Params` value became a pointer.
75+
76+
> On Go 1.26+ you can write `new(20)` in place of `opensearch.ToPointer(20)`; both produce a `*int`.
7577
7678
### Shared parameters move into embedded structs
7779

0 commit comments

Comments
 (0)