Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc-gen-openapiv2: Remove path parameters from body when body is a snake_case field #2600

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion protoc-gen-openapiv2/internal/genopenapi/template.go
Expand Up @@ -1171,7 +1171,7 @@ func renderServices(services []*descriptor.Service, paths openapiPathsObject, re
bodyFieldName = bodyField.Name
}
// Align pathParams with body field path.
pathParams := subPathParams(bodyFieldName, b.PathParams)
pathParams := subPathParams(bodyField.Name, b.PathParams)
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
var err error
schema, err = renderFieldAsDefinition(bodyField.Target, reg, customRefs, pathParams)
if err != nil {
Expand Down
176 changes: 176 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Expand Up @@ -6019,3 +6019,179 @@ func TestSubPathParams(t *testing.T) {
t.Fatalf("Wrong path param 1, element 1, got %s want %s", got, want)
}
}

func TestRenderServicesWithBodyFieldNameInCamelCase(t *testing.T) {
userDesc := &descriptorpb.DescriptorProto{
Name: proto.String("User"),
Field: []*descriptorpb.FieldDescriptorProto{
{
Name: proto.String("name"),
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
Number: proto.Int32(1),
},
{
Name: proto.String("role"),
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
Number: proto.Int32(2),
},
},
}
updateDesc := &descriptorpb.DescriptorProto{
Name: proto.String("UpdateUserRequest"),
Field: []*descriptorpb.FieldDescriptorProto{
{
Name: proto.String("user_object"),
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
TypeName: proto.String(".example.User"),
Number: proto.Int32(1),
},
},
}
meth := &descriptorpb.MethodDescriptorProto{
Name: proto.String("UpdateUser"),
InputType: proto.String("UpdateUserRequest"),
OutputType: proto.String("User"),
}
svc := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("UserService"),
Method: []*descriptorpb.MethodDescriptorProto{meth},
}
userMsg := &descriptor.Message{
DescriptorProto: userDesc,
}
updateMsg := &descriptor.Message{
DescriptorProto: updateDesc,
}
nameField := &descriptor.Field{
Message: userMsg,
FieldDescriptorProto: userMsg.GetField()[0],
}
nameField.JsonName = proto.String("name")
roleField := &descriptor.Field{
Message: userMsg,
FieldDescriptorProto: userMsg.GetField()[1],
}
roleField.JsonName = proto.String("role")
userMsg.Fields = []*descriptor.Field{nameField, roleField}
userField := &descriptor.Field{
Message: updateMsg,
FieldMessage: userMsg,
FieldDescriptorProto: updateMsg.GetField()[0],
}
userField.JsonName = proto.String("userObject")
updateMsg.Fields = []*descriptor.Field{userField}

file := descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
SourceCodeInfo: &descriptorpb.SourceCodeInfo{},
Package: proto.String("example"),
Name: proto.String("user_service.proto"),
MessageType: []*descriptorpb.DescriptorProto{userDesc, updateDesc},
Service: []*descriptorpb.ServiceDescriptorProto{svc},
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: []*descriptor.Message{userMsg, updateMsg},
Services: []*descriptor.Service{
{
ServiceDescriptorProto: svc,
Methods: []*descriptor.Method{
{
MethodDescriptorProto: meth,
RequestType: updateMsg,
ResponseType: userMsg,
Bindings: []*descriptor.Binding{
{
HTTPMethod: "POST",
PathTmpl: httprule.Template{
Version: 1,
OpCodes: []int{0, 0},
Template: "/v1/users/{user_object.name}",
},
PathParams: []descriptor.Parameter{
{
FieldPath: descriptor.FieldPath([]descriptor.FieldPathComponent{
{
Name: "user_object",
},
{
Name: "name",
},
}),
Target: nameField,
},
},
Body: &descriptor.Body{
FieldPath: []descriptor.FieldPathComponent{
{
Name: "user_object",
Target: userField,
},
},
},
},
},
},
},
},
},
}
reg := descriptor.NewRegistry()
reg.SetUseJSONNamesForFields(true)
err := reg.Load(&pluginpb.CodeGeneratorRequest{ProtoFile: []*descriptorpb.FileDescriptorProto{file.FileDescriptorProto}})
if err != nil {
t.Fatalf("failed to reg.Load(): %v", err)
}
result, err := applyTemplate(param{File: crossLinkFixture(&file), reg: reg})
if err != nil {
t.Fatalf("applyTemplate(%#v) failed with %v; want success", file, err)
}

if got, want := len(result.Paths), 1; got != want {
t.Fatalf("Results path length differed, got %d want %d", got, want)
}

var operation = *result.Paths["/v1/users/{userObject.name}"].Post
if got, want := len(operation.Parameters), 2; got != want {
t.Fatalf("Parameters length differed, got %d want %d", got, want)
}

if got, want := operation.Parameters[0].Name, "userObject.name"; got != want {
t.Fatalf("Wrong parameter name, got %s want %s", got, want)
}

if got, want := operation.Parameters[0].In, "path"; got != want {
t.Fatalf("Wrong parameter location, got %s want %s", got, want)
}

if got, want := operation.Parameters[1].Name, "userObject"; got != want {
t.Fatalf("Wrong parameter name, got %s want %s", got, want)
}

if got, want := operation.Parameters[1].In, "body"; got != want {
t.Fatalf("Wrong parameter location, got %s want %s", got, want)
}

// The body parameter should be inlined and not contain 'name', as this is a path parameter.
schema := operation.Parameters[1].Schema
if got, want := schema.Ref, ""; got != want {
t.Fatalf("Wrong reference, got %s want %s", got, want)
}
props := schema.Properties
if props == nil {
t.Fatal("No properties on body parameter")
}
if got, want := len(*props), 1; got != want {
t.Fatalf("Properties length differed, got %d want %d", got, want)
}
for _, v := range *props {
if got, want := v.Key, "role"; got != want {
t.Fatalf("Wrong key for property, got %s want %s", got, want)
}
}
}