Skip to content

Commit

Permalink
secrets/database: Fixes marshalling bug for json.Number types (#11451) (
Browse files Browse the repository at this point in the history
  • Loading branch information
austingebauer authored Apr 23, 2021
1 parent 7d3cc08 commit a64ef99
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/11451.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
secrets/database: Fix marshalling to allow providing numeric arguments to external database plugins.
```
24 changes: 24 additions & 0 deletions sdk/database/dbplugin/v5/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbplugin

import (
"context"
"encoding/json"
"errors"
"reflect"
"testing"
Expand Down Expand Up @@ -62,6 +63,29 @@ func TestGRPCClient_Initialize(t *testing.T) {
},
assertErr: assertErrNil,
},
"JSON number type in initialize request": {
client: fakeClient{
initResp: &proto.InitializeResponse{
ConfigData: marshal(t, map[string]interface{}{
"foo": "bar",
"max": "10",
}),
},
},
req: InitializeRequest{
Config: map[string]interface{}{
"foo": "bar",
"max": json.Number("10"),
},
},
expectedResp: InitializeResponse{
Config: map[string]interface{}{
"foo": "bar",
"max": "10",
},
},
assertErr: assertErrNil,
},
}

for name, test := range tests {
Expand Down
14 changes: 14 additions & 0 deletions sdk/database/dbplugin/v5/marshalling.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package dbplugin

import (
"encoding/json"
"math"

"google.golang.org/protobuf/types/known/structpb"
)

func mapToStruct(m map[string]interface{}) (*structpb.Struct, error) {
// Convert any json.Number typed values to float64, since the
// type does not have a conversion mapping defined in structpb
for k, v := range m {
if n, ok := v.(json.Number); ok {
nf, err := n.Float64()
if err != nil {
return nil, err
}

m[k] = nf
}
}

return structpb.NewStruct(m)
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a64ef99

Please sign in to comment.