secrets/database: Fixes marshalling bug for json.Number types (#11451)

This commit is contained in:
Austin Gebauer 2021-04-23 14:07:26 -07:00 committed by GitHub
parent 4f987907c3
commit 490474a502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 0 deletions

3
changelog/11451.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
secrets/database: Fix marshalling to allow providing numeric arguments to external database plugins.
```

View File

@ -2,6 +2,7 @@ package dbplugin
import (
"context"
"encoding/json"
"errors"
"reflect"
"testing"
@ -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 {

View File

@ -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)
}

View File

@ -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)
}