Skip to content

Commit

Permalink
Fix incorrectly generated repeated_path_param_separator
Browse files Browse the repository at this point in the history
  • Loading branch information
500poundbear committed Dec 8, 2023
1 parent 02ddfb2 commit 8e06127
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/descriptor/registry.go
Expand Up @@ -182,7 +182,7 @@ func NewRegistry() *Registry {
openAPINamingStrategy: "legacy",
visibilityRestrictionSelectors: make(map[string]bool),
repeatedPathParamSeparator: repeatedFieldSeparator{
name: "csv",
name: "multi",
sep: ',',
},
fileOptions: make(map[string]*options.Swagger),
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-openapiv2/internal/genopenapi/template.go
Expand Up @@ -330,7 +330,7 @@ func nestedQueryParams(message *descriptor.Message, field *descriptor.Field, pre
Enum: schema.Enum,
}
if param.Type == "array" {
param.CollectionFormat = "multi"
param.CollectionFormat = reg.GetRepeatedPathParamSeparatorName()
}

param.Name = prefix + reg.FieldName(field)
Expand Down
113 changes: 113 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Expand Up @@ -486,6 +486,119 @@ func TestMessageToQueryParametersWithOmitEnumDefaultValue(t *testing.T) {
}
}

func TestMessageToQueryParametersWithRepeatedPathParamSeparator(t *testing.T) {
type test struct {
MsgDescs []*descriptorpb.DescriptorProto
Message string
Params []openapiParameterObject
RepatedPathParam string
}

tests := []test{
{
MsgDescs: []*descriptorpb.DescriptorProto{
{
Name: proto.String("ExampleMessage"),
Field: []*descriptorpb.FieldDescriptorProto{
{
Name: proto.String("c"),
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
Label: descriptorpb.FieldDescriptorProto_LABEL_REPEATED.Enum(),
Number: proto.Int32(3),
},
},
},
},
Message: "ExampleMessage",
Params: []openapiParameterObject{
{
Name: "c",
In: "query",
Required: false,
Type: "array",
CollectionFormat: "ssv",
},
},
RepatedPathParam: "ssv",
},
{
MsgDescs: []*descriptorpb.DescriptorProto{
{
Name: proto.String("ExampleMessage"),
Field: []*descriptorpb.FieldDescriptorProto{
{
Name: proto.String("c"),
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
Label: descriptorpb.FieldDescriptorProto_LABEL_REPEATED.Enum(),
Number: proto.Int32(3),
},
},
},
},
Message: "ExampleMessage",
Params: []openapiParameterObject{
{
Name: "c",
In: "query",
Required: false,
Type: "array",
CollectionFormat: "pipes",
},
},
RepatedPathParam: "pipes",
},
}

for _, test := range tests {
reg := descriptor.NewRegistry()
reg.SetRepeatedPathParamSeparator(test.RepatedPathParam)

Check failure on line 554 in protoc-gen-openapiv2/internal/genopenapi/template_test.go

View workflow job for this annotation

GitHub Actions / golangci

Error return value of `reg.SetRepeatedPathParamSeparator` is not checked (errcheck)
msgs := []*descriptor.Message{}
for _, msgdesc := range test.MsgDescs {
msgs = append(msgs, &descriptor.Message{DescriptorProto: msgdesc})
}
file := descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
SourceCodeInfo: &descriptorpb.SourceCodeInfo{},
Name: proto.String("example.proto"),
Package: proto.String("example"),
Dependency: []string{},
MessageType: test.MsgDescs,
Service: []*descriptorpb.ServiceDescriptorProto{},
Options: &descriptorpb.FileOptions{
GoPackage: proto.String("github.com/grpc-ecosystem/grpc-gateway/runtime/internal/examplepb;example"),
},
},
GoPkg: descriptor.GoPackage{
Path: "example.com/path/to/example/example.pb",
Name: "example_pb",
},
Messages: msgs,
}
err := reg.Load(&pluginpb.CodeGeneratorRequest{
ProtoFile: []*descriptorpb.FileDescriptorProto{file.FileDescriptorProto},
})
if err != nil {
t.Fatalf("failed to load code generator request: %v", err)
}

message, err := reg.LookupMsg("", ".example."+test.Message)
if err != nil {
t.Fatalf("failed to lookup message: %s", err)
}
params, err := messageToQueryParameters(message, reg, []descriptor.Parameter{}, nil, "")
if err != nil {
t.Fatalf("failed to convert message to query parameters: %s", err)
}
// avoid checking Items for array types
for i := range params {
params[i].Items = nil
}
if !reflect.DeepEqual(params, test.Params) {
t.Errorf("expected %v, got %v", test.Params, params)
}
}
}

func TestMessageToQueryParameters(t *testing.T) {
type test struct {
MsgDescs []*descriptorpb.DescriptorProto
Expand Down

0 comments on commit 8e06127

Please sign in to comment.