Commit 3070d78d authored by Jacob Vosmaer's avatar Jacob Vosmaer Committed by Nick Thomas

Allow unknown fields in jsonpb gitaly-proto messages

parent 37de93e4
...@@ -4,8 +4,6 @@ import ( ...@@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/golang/protobuf/jsonpb"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly" "gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly"
...@@ -29,7 +27,7 @@ func (d *diff) Inject(w http.ResponseWriter, r *http.Request, sendData string) { ...@@ -29,7 +27,7 @@ func (d *diff) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
} }
request := &gitalypb.RawDiffRequest{} request := &gitalypb.RawDiffRequest{}
if err := jsonpb.UnmarshalString(params.RawDiffRequest, request); err != nil { if err := gitaly.UnmarshalJSON(params.RawDiffRequest, request); err != nil {
helper.Fail500(w, r, fmt.Errorf("diff.RawDiff: %v", err)) helper.Fail500(w, r, fmt.Errorf("diff.RawDiff: %v", err))
return return
} }
......
...@@ -4,8 +4,6 @@ import ( ...@@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/golang/protobuf/jsonpb"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly" "gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly"
...@@ -29,7 +27,7 @@ func (p *patch) Inject(w http.ResponseWriter, r *http.Request, sendData string) ...@@ -29,7 +27,7 @@ func (p *patch) Inject(w http.ResponseWriter, r *http.Request, sendData string)
} }
request := &gitalypb.RawPatchRequest{} request := &gitalypb.RawPatchRequest{}
if err := jsonpb.UnmarshalString(params.RawPatchRequest, request); err != nil { if err := gitaly.UnmarshalJSON(params.RawPatchRequest, request); err != nil {
helper.Fail500(w, r, fmt.Errorf("diff.RawPatch: %v", err)) helper.Fail500(w, r, fmt.Errorf("diff.RawPatch: %v", err))
return return
} }
......
...@@ -5,8 +5,6 @@ import ( ...@@ -5,8 +5,6 @@ import (
"io" "io"
"net/http" "net/http"
"github.com/golang/protobuf/jsonpb"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly" "gitlab.com/gitlab-org/gitlab-workhorse/internal/gitaly"
...@@ -36,7 +34,7 @@ func (s *snapshot) Inject(w http.ResponseWriter, r *http.Request, sendData strin ...@@ -36,7 +34,7 @@ func (s *snapshot) Inject(w http.ResponseWriter, r *http.Request, sendData strin
} }
request := &gitalypb.GetSnapshotRequest{} request := &gitalypb.GetSnapshotRequest{}
if err := jsonpb.UnmarshalString(params.GetSnapshotRequest, request); err != nil { if err := gitaly.UnmarshalJSON(params.GetSnapshotRequest, request); err != nil {
helper.Fail500(w, r, fmt.Errorf("SendSnapshot: unmarshal GetSnapshotRequest: %v", err)) helper.Fail500(w, r, fmt.Errorf("SendSnapshot: unmarshal GetSnapshotRequest: %v", err))
return return
} }
......
package gitaly package gitaly
import ( import (
"strings"
"sync" "sync"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
...@@ -24,9 +27,12 @@ type connectionsCache struct { ...@@ -24,9 +27,12 @@ type connectionsCache struct {
connections map[Server]*grpc.ClientConn connections map[Server]*grpc.ClientConn
} }
var cache = connectionsCache{ var (
jsonUnMarshaler = jsonpb.Unmarshaler{AllowUnknownFields: true}
cache = connectionsCache{
connections: make(map[Server]*grpc.ClientConn), connections: make(map[Server]*grpc.ClientConn),
} }
)
func NewSmartHTTPClient(server Server) (*SmartHTTPClient, error) { func NewSmartHTTPClient(server Server) (*SmartHTTPClient, error) {
conn, err := getOrCreateConnection(server) conn, err := getOrCreateConnection(server)
...@@ -131,3 +137,7 @@ func newConnection(server Server) (*grpc.ClientConn, error) { ...@@ -131,3 +137,7 @@ func newConnection(server Server) (*grpc.ClientConn, error) {
return gitalyclient.Dial(server.Address, connOpts) return gitalyclient.Dial(server.Address, connOpts)
} }
func UnmarshalJSON(s string, msg proto.Message) error {
return jsonUnMarshaler.Unmarshal(strings.NewReader(s), msg)
}
package gitaly
import (
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
)
func TestUnmarshalJSON(t *testing.T) {
testCases := []struct {
desc string
in string
out gitalypb.Repository
}{
{
desc: "basic example",
in: `{"relative_path":"foo/bar.git"}`,
out: gitalypb.Repository{RelativePath: "foo/bar.git"},
},
{
desc: "unknown field",
in: `{"relative_path":"foo/bar.git","unknown_field":12345}`,
out: gitalypb.Repository{RelativePath: "foo/bar.git"},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result := gitalypb.Repository{}
require.NoError(t, UnmarshalJSON(tc.in, &result))
require.Equal(t, tc.out, result)
})
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment