secrets/database: Fixes marshalling bug for json.Number types (#11451)
This commit is contained in:
parent
4f987907c3
commit
490474a502
|
@ -0,0 +1,3 @@
|
|||
```release-note:bug
|
||||
secrets/database: Fix marshalling to allow providing numeric arguments to external database plugins.
|
||||
```
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue