Skip to content

Commit

Permalink
protoc-gen-swagger emits method summary and descriptions
Browse files Browse the repository at this point in the history
* Fix protoc-gen-swagger to output gRPC method summary and descriptions as Swagger expects.
* Add a gRPC method comment to examples proto and regenerate examples
  • Loading branch information
Kousuke Ebihara authored and achew22 committed Jun 22, 2018
1 parent ffaaaf2 commit 11bef10
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
3 changes: 2 additions & 1 deletion examples/clients/abe/a_bit_of_everything_service_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func NewABitOfEverythingServiceApiWithBasePath(basePath string) *ABitOfEverythin
}

/**
*
* Create a new ABitOfEverything
* This API creates a new ABitOfEverything
*
* @param floatValue
* @param doubleValue
Expand Down
3 changes: 2 additions & 1 deletion examples/clients/abe/camel_case_service_name_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func NewCamelCaseServiceNameApiWithBasePath(basePath string) *CamelCaseServiceNa
}

/**
*
* Create a new ABitOfEverything
* This API creates a new ABitOfEverything
*
* @return *ProtobufEmpty
*/
Expand Down
22 changes: 14 additions & 8 deletions examples/proto/examplepb/a_bit_of_everything.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/proto/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ service ABitOfEverythingService {
}
};

// Create a new ABitOfEverything
//
// This API creates a new ABitOfEverything
rpc Create(ABitOfEverything) returns (ABitOfEverything) {
// TODO add enum_value
option (google.api.http) = {
Expand Down
4 changes: 4 additions & 0 deletions examples/proto/examplepb/a_bit_of_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@
},
"/v1/example/a_bit_of_everything/{float_value}/{double_value}/{int64_value}/separator/{uint64_value}/{int32_value}/{fixed64_value}/{fixed32_value}/{bool_value}/{string_value}/{uint32_value}/{sfixed32_value}/{sfixed64_value}/{sint32_value}/{sint64_value}/{nonConventionalNameValue}": {
"post": {
"summary": "Create a new ABitOfEverything",
"description": "This API creates a new ABitOfEverything",
"operationId": "Create",
"responses": {
"200": {
Expand Down Expand Up @@ -770,6 +772,8 @@
},
"/v2/example/empty": {
"get": {
"summary": "Create a new ABitOfEverything",
"description": "This API creates a new ABitOfEverything",
"operationId": "Empty",
"responses": {
"200": {
Expand Down
42 changes: 26 additions & 16 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re
}
}

methComments := protoComments(reg, svc.File, nil, "Service", int32(svcIdx), methProtoPath, int32(methIdx))
methComments := protoComments(reg, svc.File, nil, "Method", int32(svcIdx), methProtoPath, int32(methIdx))
if err := updateSwaggerDataFromComments(operationObject, methComments); err != nil {
panic(err)
}
Expand Down Expand Up @@ -1069,6 +1069,8 @@ func protoComments(reg *descriptor.Registry, file *descriptor.File, outers []str
var messageProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "MessageType")
var nestedProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil)), "NestedType")
var packageProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "Package")
var serviceProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil)), "Service")
var methodProtoPath = protoPathIndex(reflect.TypeOf((*pbdescriptor.ServiceDescriptorProto)(nil)), "Method")

func isProtoPathMatches(paths []int32, outerPaths []int32, typeName string, typeIndex int32, fieldPaths []int32) bool {
if typeName == "Package" && typeIndex == packageProtoPath {
Expand All @@ -1084,31 +1086,39 @@ func isProtoPathMatches(paths []int32, outerPaths []int32, typeName string, type
return false
}

typeNameDescriptor := reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil))
if len(outerPaths) > 0 {
if paths[0] != messageProtoPath || paths[1] != outerPaths[0] {
if typeName == "Method" {
if paths[0] != serviceProtoPath || paths[2] != methodProtoPath {
return false
}
paths = paths[2:]
outerPaths = outerPaths[1:]
} else {
typeNameDescriptor := reflect.TypeOf((*pbdescriptor.FileDescriptorProto)(nil))

for i, v := range outerPaths {
if paths[i*2] != nestedProtoPath || paths[i*2+1] != v {
if len(outerPaths) > 0 {
if paths[0] != messageProtoPath || paths[1] != outerPaths[0] {
return false
}
}
paths = paths[len(outerPaths)*2:]
paths = paths[2:]
outerPaths = outerPaths[1:]

for i, v := range outerPaths {
if paths[i*2] != nestedProtoPath || paths[i*2+1] != v {
return false
}
}
paths = paths[len(outerPaths)*2:]

if typeName == "MessageType" {
typeName = "NestedType"
if typeName == "MessageType" {
typeName = "NestedType"
}
typeNameDescriptor = reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil))
}
typeNameDescriptor = reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil))
}

if paths[0] != protoPathIndex(typeNameDescriptor, typeName) || paths[1] != typeIndex {
return false
if paths[0] != protoPathIndex(typeNameDescriptor, typeName) || paths[1] != typeIndex {
return false
}
paths = paths[2:]
}
paths = paths[2:]

for i, v := range fieldPaths {
if paths[i] != v {
Expand Down

0 comments on commit 11bef10

Please sign in to comment.