-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
protoc-gen-swagger: Add ability to specify custom response objects #663
Conversation
342942d
to
d29e934
Compare
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
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: { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Sorry about this.)
Gonna see if I can add automatic references for namespaced messages as well, so please don't merge just yet. |
Actually, go ahead and merge if it's good, I'll make another PR for the additional functionality. |
d29e934
to
97aefd3
Compare
Wasn't too hard it turns out :) |
} else { | ||
ret.schemaCore.Ref += s.GetJsonSchema().GetRef() | ||
} | ||
} else { |
There was a problem hiding this comment.
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?
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove one tab maybe?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should https://github.com/johanbrandhorst/grpc-gateway/blob/97aefd3e41117e62982ef489dfb516a228a39ea3/protoc-gen-swagger/genswagger/template.go#L231-L245 and other places extracting the schema object be combined with this?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should https://github.com/johanbrandhorst/grpc-gateway/blob/97aefd3e41117e62982ef489dfb516a228a39ea3/protoc-gen-swagger/genswagger/template.go#L233-L241 and other places with ExternalDocs
be combined with this?
There was a problem hiding this comment.
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" | |||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
2bd2081
to
002aef4
Compare
} | ||
if protoSchema.Description != "" { | ||
schema.Description = protoSchema.Description | ||
} |
There was a problem hiding this comment.
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
Retriggered |
I think the error comes from an update to the protobuf generator. Could you update your protoc and go plugin and regenerate with 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"` |
No objections on my part as long as the tests are fixed. |
002aef4
to
7897160
Compare
Cool, I think I should have fixed the failing test, lets get this in :D. |
Looks like there are a few remaining issues. Can you give it another whack? |
@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. |
Last time I was doing it, I just did what was done for Travis. :-)
--
Sent from Gmail Mobile
|
@ivucica thanks I will take a look through the logs. I tried using their docker image and it didn't work. |
8029320
to
9909ef7
Compare
Alright I should've gotten them this time. Sure is fiddly 🤔. |
9909ef7
to
760aa75
Compare
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 |
bb657d1
to
be7eb36
Compare
Bump - any thoughts on the additions? |
There was a problem hiding this 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: s/much/many/
There was a problem hiding this comment.
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." |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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/ ?
There was a problem hiding this comment.
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
be7eb36
to
9ed7b67
Compare
Made changes as requested |
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) |
@achew22 thanks for retriggering it, seems like it passed ;). I appreciate the time you put into maintaining this project, so no worries about forgetting! |
@ivucica anymore comments? |
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. |
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. |
Sorry, travelling. No objection to the PR.
…On Wed, Jun 20, 2018 at 4:13 AM Johan Brandhorst ***@***.***> wrote:
At the risk of becoming a tyrant on my first day, I'm going to commandeer
and this and merge as @ivucica <https://github.com/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 I approve of the action and the attitude. Thanks for merging |
Extends the openapiv2 protofile to allow control over response objects.
Fixes #304