Add generic Do[T] function for compile-time pointer enforcement - #809
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #809 +/- ##
=======================================
Coverage 72.90% 72.91%
=======================================
Files 428 428
Lines 15647 15648 +1
=======================================
+ Hits 11408 11410 +2
+ Misses 2600 2597 -3
- Partials 1639 1641 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Add opensearch.Do[T]() that enforces pointer response types at compile time, preventing a class of bugs where non-pointer values silently fail JSON unmarshaling at runtime. Mark Client.Do() as deprecated in favor of the generic alternative. Ref: opensearch-project#809 Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Add opensearch.Do[T]() that enforces pointer response types at compile time, preventing a class of bugs where non-pointer values silently fail JSON unmarshaling at runtime. Mark Client.Do() as deprecated in favor of the generic alternative. Ref: opensearch-project#809 Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Add opensearch.Do[T]() that enforces pointer response types at compile time, preventing a class of bugs where non-pointer values silently fail JSON unmarshaling at runtime. Mark Client.Do() as deprecated in favor of the generic alternative. Ref: opensearch-project#809 Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Add opensearch.Do[T]() that enforces pointer response types at compile time, preventing a class of bugs where non-pointer values silently fail JSON unmarshaling at runtime. Mark Client.Do() as deprecated in favor of the generic alternative. Ref: opensearch-project#809 Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Add opensearch.Do[T]() that enforces pointer response types at compile time, preventing a class of bugs where non-pointer values silently fail JSON unmarshaling at runtime. Mark Client.Do() as deprecated in favor of the generic alternative. Ref: opensearch-project#809 Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
5808bb9 to
6f3f8de
Compare
Address PR opensearch-project#809 review feedback from @tender-barbarian: - Add a nil check in Do[T] before widening dataPointer to any. A typed nil (e.g. (*MyResp)(nil)) produces a non-nil interface that bypasses the guard in Client.Do and reaches json.Unmarshal. Forwarding as untyped nil lets Client.Do skip unmarshalling as intended. - Fix the "Generic Do() nil pointer" test to use Path "/" so the mock returns a JSON body. Previously the empty path produced a nil body, causing unmarshal to be skipped regardless of the nil guard. - Use t.Context() instead of context.TODO() in both generic Do tests. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
|
Could it be an alternative to introduce a 'NoBody' type of type nil, that we use for endpoints that return nothing? So we don't need the additional |
Introduce opensearch.NoBody as a sentinel type for Do[T] calls that expect no response body. Replace the separate doRequest() function in opensearchapi with do(ctx, c, req, noBody), routing all internal dispatch through a single generic path. This eliminates the dual-function split and leverages the existing nil-pointer guard in opensearch.Do to skip json.Unmarshal when the typed-nil noBody sentinel is passed. Resolves review feedback from opensearch-project#809. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Introduce opensearch.NoBody as a sentinel type for Do[T] calls that expect no response body. Replace the separate doRequest() function in opensearchapi with do(ctx, c, req, noBody), routing all internal dispatch through a single generic path. This eliminates the dual-function split and leverages the existing nil-pointer guard in opensearch.Do to skip json.Unmarshal when the typed-nil noBody sentinel is passed. Resolves review feedback from opensearch-project#809. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Oh, that's a good idea. Yeah, that removes the Thanks for the nudge on this, @Jakob3xD. |
Jakob3xD
left a comment
There was a problem hiding this comment.
I would suggest waiting for a go release that allow generics
OpenSearch < 2.2.0 with the security plugin throws java.io.OptionalDataException on shard-routed requests due to non-thread-safe HashSet/HashMap in User serialization. Fixed in 2.2.0 by opensearch-project/security#1970 (50a94b47). Widen the existing 2.1.0-only skip to cover all versions below 2.2.0. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Client.Do() accepts `any` for its dataPointer parameter, deferring pointer validation to json.Unmarshal at runtime. Add a top-level opensearch.Do[T]() function whose *T signature catches non-pointer arguments at compile time. The generic wrapper is trivially inlined by the compiler -- zero runtime overhead. Mark Client.Do() with a Deprecated doc annotation to nudge callers toward the safer alternative via staticcheck SA1019 and IDE tooling. The method remains fully functional and will not be removed. Convert the unexported `do` method in opensearchapi, plugins/security, and plugins/ism to package-level generic functions, enforcing pointer safety across all internal call sites. Fixes: opensearch-project#808 Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Replace magic status codes with http.StatusOK constants, use t.Context() instead of manual context.WithCancel, and prefer require.Len/require.Positive over less specific assertions. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Replace require.NotNil/require.Nil with the semantically correct error assertion helpers. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Introduce opensearch.NoBody as a marker type for Do[T] calls that expect no response body, replacing the separate doRequest() function with a unified dispatch path through the generic function. Guard Do[T] against typed-nil pointers that would otherwise widen into a non-nil any interface and reach json.Unmarshal. When dataPointer is nil and the error response has no body, return a quoted-status error instead of the misleading ErrUnexpectedEmptyBody. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
|
I meant the “Generic Methods for Go” that is planned for GO 1.27. With that we could have just adjusted the Do Functions with a generic one. However, as we don't try to run this lib on the latest go version, we can merge this one and maybe have a look later on. |
Add generic Do[T] function for compile-time pointer enforcement
Client.Do() accepts
anyfor its dataPointer parameter, deferringpointer validation to json.Unmarshal at runtime. Add a top-level
opensearch.DoT function whose *T signature catches non-pointer
arguments at compile time. The generic wrapper is trivially inlined
by the compiler — zero runtime overhead.
Mark Client.Do() with a Deprecated doc annotation to nudge callers
toward the safer alternative via staticcheck SA1019 and IDE tooling.
The method remains fully functional and will not be removed.
Convert the unexported
domethod in opensearchapi, plugins/security,and plugins/ism to package-level generic functions, enforcing pointer
safety across all ~200 internal call sites. Requests that expect no
response body use a separate doRequest() function.
Fixes: #808
Signed-off-by: Sean Chittenden sean.chittenden@crowdstrike.com