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

fix rpc comment generation against allow_merge #1445

Merged

Conversation

kentdotn
Copy link
Contributor

@kentdotn kentdotn commented Jun 7, 2020

References to other Issues or PRs

Fixes #664

Have you read the Contributing Guidelines?

Yes

Brief description of what is fixed or changed

Currently protoc-gen-swagger does not make rpc comments properly when you give allow_merge=yes option.
This PR fixes the problem.

For example, here is a diff between ones generated by current master and this PR:

diff --git a/examples/internal/proto/examplepb/swagger_merge.swagger.json b/examples/internal/proto/examplepb/swagger_merge.swagger.json
index b8d4ca0..39e37d5 100644
--- a/examples/internal/proto/examplepb/swagger_merge.swagger.json
+++ b/examples/internal/proto/examplepb/swagger_merge.swagger.json
@@ -82,6 +82,8 @@
     },
     "/v1/example/b/1": {
       "post": {
+        "summary": "ServiceB.MethodOne receives InMessageB and returns OutMessageB",
+        "description": "Here is the detail explanation about ServiceB.MethodOne.",
         "operationId": "ServiceB_MethodOne",
         "responses": {
           "200": {
@@ -114,6 +116,8 @@
     },
     "/v1/example/b/2": {
       "post": {
+        "summary": "ServiceB.MethodTwo receives OutMessageB and returns InMessageB",
+        "description": "Here is the detail explanation about ServiceB.MethodTwo.",
         "operationId": "ServiceB_MethodTwo",
         "responses": {
           "200": {

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@codecov-commenter
Copy link

codecov-commenter commented Jun 7, 2020

Codecov Report

Merging #1445 into master will increase coverage by 0.05%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1445      +/-   ##
==========================================
+ Coverage   54.27%   54.33%   +0.05%     
==========================================
  Files          42       42              
  Lines        4394     4399       +5     
==========================================
+ Hits         2385     2390       +5     
  Misses       1752     1752              
  Partials      257      257              
Impacted Files Coverage Δ
protoc-gen-swagger/genswagger/generator.go 10.31% <0.00%> (ø)
protoc-gen-swagger/genswagger/template.go 57.90% <100.00%> (+0.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6dd8bd6...e10141e. Read the comment docs.

@kentdotn
Copy link
Contributor Author

kentdotn commented Jun 7, 2020

@googlebot I signed it!

@kentdotn kentdotn force-pushed the fix-comment-gen-allow-merge branch from 1328637 to 4568865 Compare June 7, 2020 09:25
@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes and removed cla: no labels Jun 7, 2020
Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! Could you may add an example that shows how this fixes the generated comments? Could add two simple files to examples/internal/proto/examplepb and merge them with a command in the Makefile?

@kentdotn
Copy link
Contributor Author

kentdotn commented Jun 7, 2020

Could you may add an example

In this PR comment? OK, hold on, please.

Could add two simple files

You mean adding, say, a.proto and b.proto under examples/internal/proto/examplepb and then add one more target in the Makefile like

SWAGGER_EXAMPLES_MERGE=examples/internal/proto/examplepb/a.proto \
                     examples/internal/proto/examplepb/b.prot
$(EXAMPLE_SWAGGERSRCS_MERGE): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES_MERGE)
        protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(SWAGGER_PLUGIN) --swagger_out=logtostderr=true,allow_merge=true:. $(SWAGGER_EXAMPLES_MERGE)

Or is it suffice simply adding allow_merge=true to existing $(EXAMPLE_SWAGGERSRCS)?

@kentdotn
Copy link
Contributor Author

kentdotn commented Jun 7, 2020

Hmm... the patch works for my *.proto files, but it looks it doesn't help some other (rather simple) cases.
I'm digging into it further.

@johanbrandhorst
Copy link
Collaborator

Could you may add an example

In this PR comment? OK, hold on, please.

Could add two simple files

You mean adding, say, a.proto and b.proto under examples/internal/proto/examplepb and then add one more target in the Makefile like

SWAGGER_EXAMPLES_MERGE=examples/internal/proto/examplepb/a.proto \
                     examples/internal/proto/examplepb/b.prot
$(EXAMPLE_SWAGGERSRCS_MERGE): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES_MERGE)
        protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(SWAGGER_PLUGIN) --swagger_out=logtostderr=true,allow_merge=true:. $(SWAGGER_EXAMPLES_MERGE)

Or is it suffice simply adding allow_merge=true to existing $(EXAMPLE_SWAGGERSRCS)?

Lets add a new example as you suggest. Maybe name them something like swagger_merge_a.proto and swagger_merge_b.proto?

@kentdotn
Copy link
Contributor Author

kentdotn commented Jun 7, 2020

Ok, so I figured out what going wrong.
The code I posted solves a bug but there's another one.

The following might solve the other bug but I'm not sure this is no side effect.

diff --git a/protoc-gen-swagger/genswagger/template.go b/protoc-gen-swagger/genswagger/template.go
index da90927..27a8e76 100644
--- a/protoc-gen-swagger/genswagger/template.go
+++ b/protoc-gen-swagger/genswagger/template.go
@@ -1659,7 +1659,7 @@ func isProtoPathMatches(paths []int32, outerPaths []int32, typeName string, type
                        typeNameDescriptor = reflect.TypeOf((*pbdescriptor.DescriptorProto)(nil))
                }
 
-               if paths[0] != protoPathIndex(typeNameDescriptor, typeName) || paths[1] != typeIndex {
+               if paths[0] != protoPathIndex(typeNameDescriptor, typeName) {
                        return false
                }
                paths = paths[2:]

The problem is occurred because isProtoPathMatches doesn't care about typeIndex for merged target. Perhaps safer way to solve this is to have proper index for each method defined in a same svc.File.

What do you think of the snippet above? @johanbrandhorst

@kentdotn
Copy link
Contributor Author

kentdotn commented Jun 7, 2020

OK, the patch above doesn't work properly if you define two ore more services in one proto file.
Now I'll go the 'safer' way...

@kentdotn kentdotn force-pushed the fix-comment-gen-allow-merge branch 2 times, most recently from 086b2da to 783c28f Compare June 7, 2020 13:03
@kentdotn kentdotn force-pushed the fix-comment-gen-allow-merge branch from 783c28f to 83bef3a Compare June 7, 2020 13:07
@kentdotn
Copy link
Contributor Author

kentdotn commented Jun 7, 2020

Could you may add an example

Added in PR comment.

Could add two simple files

Added.

@johanbrandhorst johanbrandhorst merged commit 4c1f33f into grpc-ecosystem:master Jun 7, 2020
@johanbrandhorst
Copy link
Collaborator

Thanks for your contribution! Could you cherry pick this against v2?

@kentdotn
Copy link
Contributor Author

kentdotn commented Jun 7, 2020

Alright, I'll push another one.

@kentdotn kentdotn deleted the fix-comment-gen-allow-merge branch June 7, 2020 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Merging swagger specs fails to use rpc comments
4 participants