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.

Upgrade to swagger-codegen 2.4.0

Also fix a regex-o in .travis.yml. + needed to be escaped.

Fixes: grpc-ecosystem#540, grpc-ecosystem#375, grpc-ecosystem#254, grpc-ecosystem#233
  • Loading branch information
achew22 committed Apr 9, 2018
1 parent bef3d6a commit 3a9bf3a
Show file tree
Hide file tree
Showing 46 changed files with 4,008 additions and 1,889 deletions.
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ cache:
- ${TRAVIS_BUILD_DIR}/examples/browser/node_modules
before_install:
- ./.travis/install-protoc.sh 3.1.0
- ./.travis/install-swagger-codegen.sh 2.2.2
- ./.travis/install-swagger-codegen.sh 2.4.0
- nvm install v6.1 && nvm use v6.1 && node --version
- go get github.com/golang/lint/golint
- go get github.com/dghubble/sling
- 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 All @@ -24,7 +27,7 @@ install:
before_script:
- sh -c 'cd examples/browser && npm install'
script:
- make realclean && make examples SWAGGER_CODEGEN="java -jar $HOME/local/swagger-codegen-cli.jar"
- make realclean && make examples SWAGGER_CODEGEN="java -jar /tmp/swagger-codegen-cli.jar"
- if (go version | grep -q "${GO_VERSION_TO_DIFF_TEST}") && [ -z "${GATEWAY_PLUGIN_FLAGS}" ]; then test -z "$(git status --porcelain)" || (git status; git diff; exit 1); fi
- env GLOG_logtostderr=1 go test -race -v github.com/grpc-ecosystem/grpc-gateway/...
- if (go version | grep -q "${GO_VERSION_TO_DIFF_TEST}") && [ -z "${GATEWAY_PLUGIN_FLAGS}" ]; then env GLOG_logtostderr=1 ./bin/coverage; fi
Expand All @@ -34,8 +37,8 @@ after_success:
- bash <(curl -s https://codecov.io/bash)
env:
global:
- "PATH=$PATH:$HOME/local/bin"
- GO_VERSION_TO_DIFF_TEST="go version go1\.9\.[0-9]+ linux/amd64"
- "PATH=$PATH:/tmp/proto/bin"
- GO_VERSION_TO_DIFF_TEST="go version go1\.9\.[0-9]\+ linux/amd64"
matrix:
- GATEWAY_PLUGIN_FLAGS=
- GATEWAY_PLUGIN_FLAGS=request_context=false
28 changes: 18 additions & 10 deletions .travis/install-protoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ if test -z "${protoc_version}"; then
echo "Usage: .travis/install-protoc.sh protoc-version"
exit 1
fi
if [ "`$HOME/local/bin/protoc-${protoc_version} --version 2>/dev/null | cut -d' ' -f 2`" != ${protoc_version} ]; then
rm -rf $HOME/local/bin $HOME/local/include

mkdir -p $HOME/tmp $HOME/local
cd $HOME/tmp
wget https://github.com/google/protobuf/releases/download/v${protoc_version}/protoc-${protoc_version}-linux-x86_64.zip
unzip protoc-${protoc_version}-linux-x86_64.zip
mv bin $HOME/local/bin
mv include $HOME/local/include
protoc_path="/tmp/proto"
protoc_binary="${protoc_path}/bin/protoc-${protoc_version}"

if [ "$("${protoc_binary}" --version 2>/dev/null | cut -d' ' -f 2)" != "${protoc_version}" ]; then
rm -rf "${protoc_path:?}/bin" "${protoc_path:?}/include"

tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'protoc')

mkdir -p "${protoc_path}"
cd "${tempdir}"

wget "https://github.com/google/protobuf/releases/download/v${protoc_version}/protoc-${protoc_version}-linux-x86_64.zip"
unzip "protoc-${protoc_version}-linux-x86_64.zip"

mv bin "${protoc_path}/bin"
mv include "${protoc_path}/include"
fi

echo \$ $HOME/local/bin/protoc --version
$HOME/local/bin/protoc --version
echo "\$ ${protoc_path}/bin/protoc --version"
"${protoc_path}/bin/protoc" --version
15 changes: 13 additions & 2 deletions .travis/install-swagger-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,16 @@ if test -z "${codegen_version}"; then
exit 1
fi

wget http://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/${codegen_version}/swagger-codegen-cli-${codegen_version}.jar \
-O $HOME/local/swagger-codegen-cli.jar
swagger_codegen_cli="/tmp/swagger-codegen-cli.jar"

# Want to test with an unreleased version of swagger-codegne-cli? Try out the sonatype repo for the SNAPSHOT builds.
if false; then
# Directory listing can be found at
# https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-cli/2.4.0-SNAPSHOT/
# Replace the version number with the appropriate version.
wget https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-cli/2.4.0-SNAPSHOT/swagger-codegen-cli-2.4.0-20180407.135302-217.jar \
-O "${swagger_codegen_cli}"
else
wget "http://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/${codegen_version}/swagger-codegen-cli-${codegen_version}.jar" \
-O "${swagger_codegen_cli}"
fi
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Here's the recommended process of contribution.
4. Make sure that your change follows best practices in Go
* [Effective Go](https://golang.org/doc/effective_go.html)
* [Go Code Review Comments](https://golang.org/wiki/CodeReviewComments)
5. Make sure that `make test` passes. (use swagger-codegen 2.2.2, not newer versions)
5. Make sure that `make test` passes. (use swagger-codegen 2.4.0, not newer versions)
6. Sign [a Contributor License Agreement](https://cla.developers.google.com/clas)
7. Open a pull request in Github

Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,20 @@ $(EXAMPLE_SWAGGERSRCS): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES)
$(ECHO_EXAMPLE_SRCS): $(ECHO_EXAMPLE_SPEC)
$(SWAGGER_CODEGEN) generate -i $(ECHO_EXAMPLE_SPEC) \
-l go -o examples/clients/echo --additional-properties packageName=echo
@rm -f $(EXAMPLE_CLIENT_DIR)/echo/README.md \
@rm -rf $(EXAMPLE_CLIENT_DIR)/echo/README.md \
$(EXAMPLE_CLIENT_DIR)/echo/docs \
$(EXAMPLE_CLIENT_DIR)/echo/git_push.sh \
$(EXAMPLE_CLIENT_DIR)/echo/.gitignore \
$(EXAMPLE_CLIENT_DIR)/echo/.swagger-codegen \
$(EXAMPLE_CLIENT_DIR)/echo/.travis.yml
$(ABE_EXAMPLE_SRCS): $(ABE_EXAMPLE_SPEC)
$(SWAGGER_CODEGEN) generate -i $(ABE_EXAMPLE_SPEC) \
-l go -o examples/clients/abe --additional-properties packageName=abe
@rm -f $(EXAMPLE_CLIENT_DIR)/abe/README.md \
@rm -rf $(EXAMPLE_CLIENT_DIR)/abe/README.md \
$(EXAMPLE_CLIENT_DIR)/abe/docs \
$(EXAMPLE_CLIENT_DIR)/abe/git_push.sh \
$(EXAMPLE_CLIENT_DIR)/abe/.gitignore \
$(EXAMPLE_CLIENT_DIR)/abe/.swagger-codegen \
$(EXAMPLE_CLIENT_DIR)/abe/.travis.yml

examples: $(EXAMPLE_SVCSRCS) $(EXAMPLE_GWSRCS) $(EXAMPLE_DEPSRCS) $(EXAMPLE_SWAGGERSRCS) $(EXAMPLE_CLIENT_SRCS)
Expand Down
76 changes: 51 additions & 25 deletions examples/client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"reflect"
"testing"

"golang.org/x/net/context"

"github.com/go-test/deep"

"github.com/grpc-ecosystem/grpc-gateway/examples/clients/abe"
"github.com/grpc-ecosystem/grpc-gateway/examples/clients/echo"
)
Expand All @@ -17,10 +20,12 @@ func TestEchoClient(t *testing.T) {
return
}

cl := echo.NewEchoServiceApiWithBasePath("http://localhost:8080")
resp, _, err := cl.Echo("foo")
config := echo.NewConfiguration()
config.BasePath = "http://localhost:8080"
cl := echo.NewAPIClient(config)
resp, _, err := cl.EchoServiceApi.Echo(context.Background(), "foo")
if err != nil {
t.Errorf(`cl.Echo("foo") failed with %v; want success`, err)
t.Errorf(`cl.EchoServiceApi.Echo("foo") failed with %v; want success`, err)
}
if got, want := resp.Id, "foo"; got != want {
t.Errorf("resp.Id = %q; want %q", got, want)
Expand All @@ -33,11 +38,13 @@ func TestEchoBodyClient(t *testing.T) {
return
}

cl := echo.NewEchoServiceApiWithBasePath("http://localhost:8080")
config := echo.NewConfiguration()
config.BasePath = "http://localhost:8080"
cl := echo.NewAPIClient(config)
req := echo.ExamplepbSimpleMessage{Id: "foo"}
resp, _, err := cl.EchoBody(req)
resp, _, err := cl.EchoServiceApi.EchoBody(context.Background(), req)
if err != nil {
t.Errorf("cl.EchoBody(%#v) failed with %v; want success", req, err)
t.Errorf("cl.EchoServiceApi.EchoBody(%#v) failed with %v; want success", req, err)
}
if got, want := resp.Id, "foo"; got != want {
t.Errorf("resp.Id = %q; want %q", got, want)
Expand All @@ -50,13 +57,18 @@ func TestAbitOfEverythingClient(t *testing.T) {
return
}

cl := abe.NewABitOfEverythingServiceApiWithBasePath("http://localhost:8080")
config := abe.NewConfiguration()
config.BasePath = "http://localhost:8080"
cl := abe.NewAPIClient(config)
testABEClientCreate(t, cl)
testABEClientCreateBody(t, cl)
}

func testABEClientCreate(t *testing.T, cl *abe.ABitOfEverythingServiceApi) {
func testABEClientCreate(t *testing.T, cl *abe.APIClient) {
abeZERO := abe.ZERO

want := &abe.ExamplepbABitOfEverything{
Nested: []abe.ABitOfEverythingNested{},
FloatValue: 1.5,
DoubleValue: 2.5,
Int64Value: "4294967296",
Expand All @@ -72,8 +84,16 @@ func testABEClientCreate(t *testing.T, cl *abe.ABitOfEverythingServiceApi) {
Sint32Value: 2147483647,
Sint64Value: "4611686018427387903",
NonConventionalNameValue: "camelCase",
// Fields that aren't explictly sent so they will come back with defaults.
EnumValue: &abeZERO,
RepeatedStringValue: []string{},
MapValue: map[string]abe.ExamplepbNumericEnum{},
MappedStringValue: map[string]string{},
MappedNestedValue: map[string]abe.ABitOfEverythingNested{},
RepeatedEnumValue: []abe.ExamplepbNumericEnum{},
}
resp, _, err := cl.Create(
resp, _, err := cl.ABitOfEverythingServiceApi.Create(
context.Background(),
want.FloatValue,
want.DoubleValue,
want.Int64Value,
Expand All @@ -91,21 +111,21 @@ func testABEClientCreate(t *testing.T, cl *abe.ABitOfEverythingServiceApi) {
want.NonConventionalNameValue,
)
if err != nil {
t.Errorf("cl.Create(%#v) failed with %v; want success", want, err)
t.Errorf("cl.EchoServiceApi.Create(%#v) failed with %v; want success", want, err)
}
if resp.Uuid == "" {
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.Errorf("Create: %v", diff)
}
}

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

func testABEClientCreateBody(t *testing.T, cl *abe.APIClient) {
abeOne := abe.ONE
abeFalse := abe.FALSE
abeTrue := abe.TRUE
want := abe.ExamplepbABitOfEverything{
FloatValue: 1.5,
DoubleValue: 2.5,
Expand All @@ -125,38 +145,44 @@ func testABEClientCreateBody(t *testing.T, cl *abe.ABitOfEverythingServiceApi) {

Nested: []abe.ABitOfEverythingNested{
{
Ok: &abeFalse,
Name: "bar",
Amount: 10,
},
{
Ok: &abeTrue,
Name: "baz",
Amount: 20,
},
},
RepeatedStringValue: []string{"a", "b", "c"},
OneofString: "x",
MapValue: map[string]abe.ExamplepbNumericEnum{
// "a": abe.ExamplepbNumericEnum_ONE,
// "b": abe.ExamplepbNumericEnum_ZERO,
//TODO: Fix enums
//"a": &abeOne,
//"b": &abeZero,
},
MappedStringValue: map[string]string{
"a": "x",
"b": "y",
},
MappedNestedValue: map[string]abe.ABitOfEverythingNested{
"a": {Name: "x", Amount: 1},
"b": {Name: "y", Amount: 2},
"a": {Ok: &abeFalse, Name: "x", Amount: 1},
"b": {Ok: &abeTrue, Name: "y", Amount: 2},
},

RepeatedEnumValue: []abe.ExamplepbNumericEnum{},
EnumValue: &abeOne,
}
resp, _, err := cl.CreateBody(want)
resp, _, err := cl.ABitOfEverythingServiceApi.CreateBody(context.Background(), want)
if err != nil {
t.Errorf("cl.CreateBody(%#v) failed with %v; want success", want, err)
t.Errorf("cl.ABitOfEverythingServiceApi.CreateBody(%#v) failed with %v; want success", want, err)
}
if resp.Uuid == "" {
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.Errorf("CreateBody: %v", diff)
}
}
1 change: 0 additions & 1 deletion examples/clients/abe/.gitignore

This file was deleted.

24 changes: 23 additions & 1 deletion examples/clients/abe/.swagger-codegen-ignore
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
.gitignore
# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
10 changes: 0 additions & 10 deletions examples/clients/abe/ProtobufDuration.go

This file was deleted.

Loading

0 comments on commit 3a9bf3a

Please sign in to comment.