Skip to content

Commit

Permalink
protoc-gen-swagger/genswagger: sort extensions by key, expand test
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Sep 8, 2019
1 parent 0290729 commit 409ca3a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions examples/proto/examplepb/a_bit_of_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2317,9 +2317,9 @@
"description": "More about gRPC-Gateway",
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
},
"x-grpc-gateway-foo": "bar",
"x-grpc-gateway-baz-list": [
"one",
true
]
],
"x-grpc-gateway-foo": "bar"
}
1 change: 1 addition & 0 deletions protoc-gen-swagger/genswagger/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ go_test(
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
],
)
2 changes: 2 additions & 0 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -1233,6 +1234,7 @@ func applyTemplate(p param) (*swaggerObject, error) {
}
exts = append(exts, extension{key: k, value: json.RawMessage(ext)})
}
sort.Slice(exts, func(i, j int) bool { return exts[i].key < exts[j].key })
s.extensions = exts
}

Expand Down
6 changes: 5 additions & 1 deletion protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func TestApplyTemplateTopLevelOptions(t *testing.T) {
swagger := swagger_options.Swagger{
Extensions: map[string]*structpb.Value{
"x-foo": &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: "bar"}},
"x-bar": &structpb.Value{Kind: &structpb.Value_ListValue{ListValue: &structpb.ListValue{Values: []*structpb.Value{{Kind: &structpb.Value_StringValue{StringValue: "baz"}}}}}},
},
}
err := proto.SetExtension(proto.Message(file.FileDescriptorProto.Options), swagger_options.E_Openapiv2Swagger, &swagger)
Expand All @@ -407,7 +408,10 @@ func TestApplyTemplateTopLevelOptions(t *testing.T) {
if want, is, name := "2.0", result.Swagger, "Swagger"; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
}
if want, is, name := []extension{{key: "x-foo", value: json.RawMessage("\"bar\"")}}, result.extensions, "Extensions"; !reflect.DeepEqual(is, want) {
if want, is, name := []extension{
{key: "x-bar", value: json.RawMessage("[\n \"baz\"\n ]")},
{key: "x-foo", value: json.RawMessage("\"bar\"")},
}, result.extensions, "Extensions"; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
}
}
Expand Down

0 comments on commit 409ca3a

Please sign in to comment.