-
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
write query parameters to swagger definition #199
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,49 @@ import ( | |
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor" | ||
) | ||
|
||
// messageToQueryParameters converts a message to a list of swagger query parameters. | ||
func messageToQueryParameters(message *descriptor.Message, reg *descriptor.Registry, pathParams []descriptor.Parameter) ([]swaggerParameterObject, error) { | ||
var parameters []swaggerParameterObject | ||
var addParameterWithPrefix func(string, *descriptor.Field) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addParameterWithPrefix := func(prefix string, field *descriptor.Field) error { BTW, why do you need to keep this function nested? func queryParams(prefix string, field *descriptor.Field) ([]swaggerParameterObject, error) |
||
addParameterWithPrefix = func(prefix string, field *descriptor.Field) error { | ||
// make sure the parameter is not already listed as a path parameter | ||
for _, pathParam := range pathParams { | ||
if pathParam.Target == field { | ||
return nil | ||
} | ||
} | ||
schema := schemaOfField(field, reg) | ||
if schema.Type != "" { | ||
// basic type, add a basic query parameter | ||
parameters = append(parameters, swaggerParameterObject{ | ||
Name: prefix + field.GetName(), | ||
Description: schema.Description, | ||
In: "query", | ||
Type: schema.Type, | ||
}) | ||
return nil | ||
} | ||
|
||
// nested type, recurse | ||
msg, err := reg.LookupMsg("", field.GetTypeName()) | ||
if err != nil { | ||
return fmt.Errorf("unknown message type %s", field.GetTypeName()) | ||
} | ||
for _, nestedField := range msg.Fields { | ||
if err := addParameterWithPrefix(field.GetName()+".", nestedField); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work fine with deeply nested fields? Also, it is fine if it is out of scope for now. But what do you think about that a protobuf message has the same message type as a field? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also interested in the answer to this. What happens for deeply nested fields? |
||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
for _, field := range message.Fields { | ||
if err := addParameterWithPrefix("", field); err != nil { | ||
return parameters, err | ||
} | ||
} | ||
return parameters, nil | ||
} | ||
|
||
// findServicesMessagesAndEnumerations discovers all messages and enums defined in the RPC methods of the service. | ||
func findServicesMessagesAndEnumerations(s []*descriptor.Service, reg *descriptor.Registry, m messageMap, e enumMap) { | ||
for _, svc := range s { | ||
|
@@ -381,6 +424,13 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re | |
}, | ||
}, | ||
}) | ||
} else if b.HTTPMethod == "GET" { | ||
// add the parameters to the query string | ||
queryParams, err := messageToQueryParameters(meth.RequestType, reg, b.PathParams) | ||
if err != nil { | ||
return err | ||
} | ||
parameters = append(parameters, queryParams...) | ||
} | ||
|
||
pathItemObject, ok := paths[templateToSwaggerPath(b.PathTmpl.Template)] | ||
|
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.
It was even better if you make variable names a bit shorter.
https://github.com/golang/go/wiki/CodeReviewComments#variable-names