Skip to content

Commit

Permalink
Standardize default marshaller
Browse files Browse the repository at this point in the history
One of the recurring themes of this project has been trouble around the
default marshaller. This change modifies it to be more what people
expect when they first start the project.

1.  It emits the proto3 json style version of field names instead of the
    field name as it appeared in the .proto file.
2.  It emits zero values for fields. This means that if you have a field
    that is unset it will now have a value unlike before.

Fixes: grpc-ecosystem#540, grpc-ecosystem#375, grpc-ecosystem#254, grpc-ecosystem#233
  • Loading branch information
achew22 committed Feb 10, 2018
1 parent 93c1dea commit 231b75f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ before_install:
- go get github.com/go-resty/resty
- go get golang.org/x/oauth2
- go get golang.org/x/net/context
- go get github.com/go-test/deep
install:
- go get github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
- go get github.com/grpc-ecosystem/grpc-gateway/runtime
Expand Down
21 changes: 9 additions & 12 deletions examples/client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"reflect"
"testing"

"github.com/go-test/deep"
"golang.org/x/net/context"

"github.com/grpc-ecosystem/grpc-gateway/examples/clients/abe"
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestAbitOfEverythingClient(t *testing.T) {
}

func testABEClientCreate(t *testing.T, cl *abe.APIClient) {
want := &abe.ExamplepbABitOfEverything{
want := abe.ExamplepbABitOfEverything{
FloatValue: 1.5,
DoubleValue: 2.5,
Int64Value: "4294967296",
Expand Down Expand Up @@ -106,15 +106,12 @@ func testABEClientCreate(t *testing.T, cl *abe.APIClient) {
t.Errorf("resp.Uuid is empty; want not empty")
}
resp.Uuid = ""
if got := resp; !reflect.DeepEqual(got, want) {
t.Errorf("resp = %#v; want %#v", got, want)
if diff := deep.Equal(resp, want); diff != nil {
t.Error(diff)
}
}

func testABEClientCreateBody(t *testing.T, cl *abe.APIClient) {
t.Log("TODO: support enum")
return

want := abe.ExamplepbABitOfEverything{
FloatValue: 1.5,
DoubleValue: 2.5,
Expand Down Expand Up @@ -144,9 +141,9 @@ func testABEClientCreateBody(t *testing.T, cl *abe.APIClient) {
},
RepeatedStringValue: []string{"a", "b", "c"},
OneofString: "x",
MapValue: map[string]abe.ExamplepbNumericEnum{
// "a": abe.ExamplepbNumericEnum_ONE,
// "b": abe.ExamplepbNumericEnum_ZERO,
MapValue: map[string]abe.ExamplepbNumericEnum{
"a": abe.ONE,
"b": abe.ZERO,
},
MappedStringValue: map[string]string{
"a": "x",
Expand All @@ -165,7 +162,7 @@ func testABEClientCreateBody(t *testing.T, cl *abe.APIClient) {
t.Errorf("resp.Uuid is empty; want not empty")
}
resp.Uuid = ""
if got := resp; !reflect.DeepEqual(got, want) {
t.Errorf("resp = %#v; want %#v", got, want)
if diff := deep.Equal(resp, want); diff != nil {
t.Error(diff)
}
}
2 changes: 1 addition & 1 deletion protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func renderMessagesAsDefinition(messages messageMap, d swaggerDefinitionsObject,
panic(err)
}

schema.Properties = append(schema.Properties, keyVal{f.GetName(), fieldValue})
schema.Properties = append(schema.Properties, keyVal{f.GetJsonName(), fieldValue})
}
d[fullyQualifiedNameToSwaggerName(msg.FQMN(), reg)] = schema
}
Expand Down
6 changes: 5 additions & 1 deletion runtime/marshaler_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ var (
acceptHeader = http.CanonicalHeaderKey("Accept")
contentTypeHeader = http.CanonicalHeaderKey("Content-Type")

defaultMarshaler = &JSONPb{OrigName: true}
defaultMarshaler = &JSONPb{
// Rewrite the field names to the proto3 json spec.
OrigName: false,
EmitDefaults: true,
}
)

// MarshalerForRequest returns the inbound/outbound marshalers for this request.
Expand Down

0 comments on commit 231b75f

Please sign in to comment.