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-swagger: Add ability to specify custom response objects #663

Conversation

johanbrandhorst
Copy link
Collaborator

Extends the openapiv2 protofile to allow control over response objects.

Fixes #304

@johanbrandhorst
Copy link
Collaborator Author

@ivucica @tmc @achew22

@codecov-io
Copy link

codecov-io commented May 31, 2018

Codecov Report

Merging #663 into master will decrease coverage by 1.36%.
The diff coverage is 13.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #663      +/-   ##
==========================================
- Coverage   57.78%   56.42%   -1.37%     
==========================================
  Files          30       30              
  Lines        2914     2995      +81     
==========================================
+ Hits         1684     1690       +6     
- Misses       1066     1141      +75     
  Partials      164      164
Impacted Files Coverage Δ
protoc-gen-swagger/genswagger/template.go 38.52% <13.15%> (-3.59%) ⬇️

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 a5b66c1...be7eb36. Read the comment docs.

@achew22
Copy link
Collaborator

achew22 commented May 31, 2018

I like this PR, I need to look at it in a bigger screen then my phone but it looks like a great additional piece of documentation to include

key: "admin";
value: "Grants read and write access to administrative information";
}
info: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, why did you change everything to be tabs? Can you run this through a modern version of clang-format?

Copy link
Collaborator Author

@johanbrandhorst johanbrandhorst Jun 1, 2018

Choose a reason for hiding this comment

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

Everything except for the examples added by @ivucica was already tabs, so I just made it consistent as it was driving me mad. I could format this (and the other protofiles) with prototool format 1 if you like, but I haven't touched the others.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(Sorry about this.)

@johanbrandhorst
Copy link
Collaborator Author

Gonna see if I can add automatic references for namespaced messages as well, so please don't merge just yet.

@johanbrandhorst
Copy link
Collaborator Author

Actually, go ahead and merge if it's good, I'll make another PR for the additional functionality.

@johanbrandhorst johanbrandhorst force-pushed the add-swagger-response-configuration branch from d29e934 to 97aefd3 Compare June 4, 2018 10:33
@johanbrandhorst
Copy link
Collaborator Author

johanbrandhorst commented Jun 4, 2018

Wasn't too hard it turns out :)

} else {
ret.schemaCore.Ref += s.GetJsonSchema().GetRef()
}
} else {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One thing this doesn't do is include external messages that aren't already inlined. I think we could potentially do some proto registry lookups and add those messages if necessary, but it'd be quite complex so I'm not sure we want to?

Copy link
Collaborator

@ivucica ivucica left a comment

Choose a reason for hiding this comment

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

Mostly in favor. I have just a few nitpicks, and a few requests to evaluate code reuse.

Thanks for this!

@@ -155,7 +183,7 @@ message ABitOfEverything {

google.protobuf.Timestamp timestamp_value = 27;

// repeated enum value. it is comma-separated in query
// repeated enum value. it is comma-separated in query
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove one tab maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Woops! Removed.

@@ -1187,3 +1230,70 @@ func extractSwaggerOptionFromFileDescriptor(file *pbdescriptor.FileDescriptorPro
}
return opts, nil
}

func swaggerSchemaFromProtoSchema(s *swagger_options.Schema, reg *descriptor.Registry) swaggerSchemaObject {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great idea, I'll see if I can make that work.

}
}

func protoExternalDocumentationToSwaggerExternalDocumentation(in *swagger_options.ExternalDocumentation) *swaggerExternalDocumentationObject {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I squashed these.

@@ -4,12 +4,13 @@ import (
"reflect"
"testing"

"fmt"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually if this is being moved, maybe move it together with "testing" and "reflect" and others? Seems strange to have it hang on its own

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Didn't even realise I had changed this, thanks linters!

// field 3 is reserved for '$ref', although it is unclear how it would be used.
reserved 3;
// Ref is used to define an external reference to include in the message.
// This should be a fully qualified message reference, and that type must be imported
Copy link
Collaborator

Choose a reason for hiding this comment

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

qualified message reference

I'd be slightly confused about this, maybe fully qualified protobuf message reference? Not sure.

// field 3 is reserved for '$ref', although it is unclear how it would be used.
reserved 3;
// Ref is used to define an external reference to include in the message.
// This should be a fully qualified message reference, and that type must be imported
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be

This could be, I suppose?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've adjusted it accordingly. I hope together with the example it is obvious enough.

@johanbrandhorst johanbrandhorst force-pushed the add-swagger-response-configuration branch 3 times, most recently from 2bd2081 to 002aef4 Compare June 5, 2018 13:14
}
if protoSchema.Description != "" {
schema.Description = protoSchema.Description
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added these because I need them :P

@johanbrandhorst
Copy link
Collaborator Author

@ivucica are you happy with the changes? @achew22 could you rerun the failure please, seems like a github TLS timeout error?

@achew22
Copy link
Collaborator

achew22 commented Jun 6, 2018

Retriggered

@achew22
Copy link
Collaborator

achew22 commented Jun 6, 2018

I think the error comes from an update to the protobuf generator. Could you update your protoc and go plugin and regenerate with make examples then upload again?

I expect it should add the following

index cb6fb3a..d416004 100644
--- a/examples/clients/abe/examplepb_a_bit_of_everything.go
+++ b/examples/clients/abe/examplepb_a_bit_of_everything.go
@@ -14,6 +14,7 @@ import (
 	"time"
 )
 
+// Intentionaly complicated message type to cover much features of Protobuf.
 type ExamplepbABitOfEverything struct {
 
 	SingleNested ABitOfEverythingNested `json:"single_nested,omitempty"`

@ivucica
Copy link
Collaborator

ivucica commented Jun 6, 2018

No objections on my part as long as the tests are fixed.

@johanbrandhorst johanbrandhorst force-pushed the add-swagger-response-configuration branch from 002aef4 to 7897160 Compare June 6, 2018 22:21
@johanbrandhorst
Copy link
Collaborator Author

johanbrandhorst commented Jun 6, 2018

Cool, I think I should have fixed the failing test, lets get this in :D.

@achew22
Copy link
Collaborator

achew22 commented Jun 6, 2018

Looks like there are a few remaining issues. Can you give it another whack?

@johanbrandhorst
Copy link
Collaborator Author

johanbrandhorst commented Jun 6, 2018

@achew22 I can't seem to get swagger-codegen installed - I hate to be a bother but any chance you could just clone and regenerate the examples? I'll take another stab tomorrow at work but haven't got time now.

@ivucica
Copy link
Collaborator

ivucica commented Jun 7, 2018 via email

@johanbrandhorst
Copy link
Collaborator Author

@ivucica thanks I will take a look through the logs. I tried using their docker image and it didn't work.

@johanbrandhorst johanbrandhorst force-pushed the add-swagger-response-configuration branch 2 times, most recently from 8029320 to 9909ef7 Compare June 7, 2018 09:08
@johanbrandhorst
Copy link
Collaborator Author

Alright I should've gotten them this time. Sure is fiddly 🤔.

@johanbrandhorst johanbrandhorst force-pushed the add-swagger-response-configuration branch from 9909ef7 to 760aa75 Compare June 7, 2018 11:45
@johanbrandhorst
Copy link
Collaborator Author

johanbrandhorst commented Jun 7, 2018

OK so I had some time to look into recursive resolution of manually referenced types - I got it working but I can't help but feel like it's a bit hacky. I'd be happy with any tips on improving it, but basically it required me to keep a separate set of referenced definitions and render them after everything else, recursively.

@johanbrandhorst johanbrandhorst force-pushed the add-swagger-response-configuration branch 2 times, most recently from bb657d1 to be7eb36 Compare June 7, 2018 22:20
@johanbrandhorst
Copy link
Collaborator Author

Bump - any thoughts on the additions?

Copy link
Collaborator

@ivucica ivucica left a comment

Choose a reason for hiding this comment

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

A few nits to correct and I will approve.

As they're tiny issues, I would be willing to approve even as is.

@@ -14,6 +14,7 @@ import (
"time"
)

// Intentionaly complicated message type to cover much features of Protobuf.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: s/much/many/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeh I intentionally didn't change this existing typo but may as well :D

};


// Intentionaly complicated message type to cover much features of Protobuf.
// NEXT ID: 30
message ABitOfEverything {
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_schema) = {
json_schema: {
title: "A bit of everything"
description: "Intentionaly complicated message type to cover much features of Protobuf."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: s/much/many/

We can skip this if it'd take you a lot of time to regenerate examples.

@@ -512,7 +515,7 @@ func templateToSwaggerPath(path string) string {
return strings.Join(parts, "/")
}

func renderServices(services []*descriptor.Service, paths swaggerPathsObject, reg *descriptor.Registry, refs refMap) error {
func renderServices(services []*descriptor.Service, paths swaggerPathsObject, reg *descriptor.Registry, requestResponseRefs refMap, customRefs refMap) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

requestResponseRefs, customRefs refMap is also valid

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

// Find all the service's messages and enumerations that are defined (recursively) and then
// write their request and response types out as definition objects.
// Find all the service's messages and enumerations that are defined (recursively)
// and write request, response and referenced types out as definition objects.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe: s/referenced types/other custom (but referenced) types/ ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Extends the openapiv2 protofile to allow control over response objects.

Fixes grpc-ecosystem#304
@johanbrandhorst johanbrandhorst force-pushed the add-swagger-response-configuration branch from be7eb36 to 9ed7b67 Compare June 15, 2018 18:23
@johanbrandhorst
Copy link
Collaborator Author

Made changes as requested

@johanbrandhorst
Copy link
Collaborator Author

@tmc @achew22 I think the test failure was a flaky test, could one of you rerun it please?

@achew22
Copy link
Collaborator

achew22 commented Jun 16, 2018

Rerun. I'll try to check on it in 20 but I'll probably forget. If I don't do something by tomorrow morning please reply here, it means I forgot (sorry in advance)

@johanbrandhorst
Copy link
Collaborator Author

@achew22 thanks for retriggering it, seems like it passed ;). I appreciate the time you put into maintaining this project, so no worries about forgetting!

@johanbrandhorst
Copy link
Collaborator Author

@ivucica anymore comments?

@johanbrandhorst
Copy link
Collaborator Author

Friendly Monday bump @achew22 @ivucica

@achew22
Copy link
Collaborator

achew22 commented Jun 18, 2018

LGTM. Let's give @ivucica 24 hours from your ping before merging. I will rebase tomorrow around this time. If I don't please reping and I'll merge when I get to a computer.

PS: Sorry this is takling forever, it's a great feature and I am REALLY looking forward to using it. Taking a little extra time is definitely worth it.

@johanbrandhorst
Copy link
Collaborator Author

At the risk of becoming a tyrant on my first day, I'm going to commandeer and this and merge as @ivucica has been given ample time to add any more opinions and he conceded that he was happy with the PR even without the minor changes, that have been addressed.

@johanbrandhorst johanbrandhorst merged commit 9b9677c into grpc-ecosystem:master Jun 20, 2018
@johanbrandhorst johanbrandhorst deleted the add-swagger-response-configuration branch June 20, 2018 08:13
@ivucica
Copy link
Collaborator

ivucica commented Jun 20, 2018 via email

@achew22
Copy link
Collaborator

achew22 commented Jun 20, 2018

@johanbrandhorst I approve of the action and the attitude. Thanks for merging

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.

5 participants