Skip to content

Commit

Permalink
Fix grpc-ecosystem#1125: JSON-prefix nested-field params. (grpc-ecosy…
Browse files Browse the repository at this point in the history
…stem#1126)

* Fix grpc-ecosystem#1125: JSON-prefix nested-field params.

* Add test case.
  • Loading branch information
jgiles authored Feb 2, 2020
1 parent f6612d1 commit 0ef14fe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ func queryParams(message *descriptor.Message, field *descriptor.Field, prefix st
return nil, fmt.Errorf("unknown message type %s", fieldType)
}
for _, nestedField := range msg.Fields {
p, err := queryParams(msg, nestedField, prefix+field.GetName()+".", reg, pathParams)
var fieldName string
if reg.GetUseJSONNamesForFields() {
fieldName = field.GetJsonName()
} else {
fieldName = field.GetName()
}
p, err := queryParams(msg, nestedField, prefix+fieldName+".", reg, pathParams)
if err != nil {
return nil, err
}
Expand Down
36 changes: 36 additions & 0 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,42 @@ func TestMessageToQueryParametersWithJsonName(t *testing.T) {
},
},
},
{
MsgDescs: []*protodescriptor.DescriptorProto{
&protodescriptor.DescriptorProto{
Name: proto.String("SubMessage"),
Field: []*protodescriptor.FieldDescriptorProto{
{
Name: proto.String("test_field_a"),
Type: protodescriptor.FieldDescriptorProto_TYPE_STRING.Enum(),
Number: proto.Int32(1),
JsonName: proto.String("testFieldA"),
},
},
},
&protodescriptor.DescriptorProto{
Name: proto.String("ExampleMessage"),
Field: []*protodescriptor.FieldDescriptorProto{
{
Name: proto.String("sub_message"),
Type: protodescriptor.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
TypeName: proto.String(".example.SubMessage"),
Number: proto.Int32(1),
JsonName: proto.String("subMessage"),
},
},
},
},
Message: "ExampleMessage",
Params: []swaggerParameterObject{
swaggerParameterObject{
Name: "subMessage.testFieldA",
In: "query",
Required: false,
Type: "string",
},
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit 0ef14fe

Please sign in to comment.