Skip to content

Commit

Permalink
Code-review change requests accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Romer committed May 4, 2020
1 parent 02f5169 commit 245886a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ func messageToQueryParameters(message *descriptor.Message, reg *descriptor.Regis
return params, nil
}

// queryParams converts a field to a list of swagger query parameters recursively throught the use of nestedQueryParams.
// queryParams converts a field to a list of swagger query parameters recursively through the use of nestedQueryParams.
func queryParams(message *descriptor.Message, field *descriptor.Field, prefix string, reg *descriptor.Registry, pathParams []descriptor.Parameter) (params []swaggerParameterObject, err error) {
return nestedQueryParams(message, field, prefix, reg, pathParams, map[string]bool{})
}

// nestedQueryParams converts a field to a list of swagger query parameters recursively.
// This function is a helper function for queryParams, that keeps track of circular message references
// This function is a helper function for queryParams, that keeps track of cyclical message references
// through the use of
// touched map[string]bool
// If a circular reference is discovered, a error is returned, as circular datastructures isnt allowed
// in query-parameters.
// If a cycle is discovered, an error is returned, as cyclical data structures aren't allowed
// in query parameters.
func nestedQueryParams(message *descriptor.Message, field *descriptor.Field, prefix string, reg *descriptor.Registry, pathParams []descriptor.Parameter, touched map[string]bool) (params []swaggerParameterObject, err error) {
// make sure the parameter is not already listed as a path parameter
for _, pathParam := range pathParams {
Expand Down Expand Up @@ -226,12 +226,12 @@ func nestedQueryParams(message *descriptor.Message, field *descriptor.Field, pre
if err != nil {
return nil, fmt.Errorf("unknown message type %s", fieldType)
}
// Check for circular message reference:
isCircular := touched[*msg.Name]
if isCircular {
return nil, fmt.Errorf("Recursive types are not allowed for query parameters, Circle found on %s", fieldType)
// Check for cyclical message reference:
isCycle := touched[*msg.Name]
if isCycle {
return nil, fmt.Errorf("Recursive types are not allowed for query parameters, cycle found on %s", fieldType)
}
// Update touched-map with the massage name so a circle further down the recursive path can be detected.
// Update map with the massage name so a cycle further down the recursive path can be detected.
touched[*msg.Name] = true

for _, nestedField := range msg.Fields {
Expand Down
10 changes: 5 additions & 5 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ func TestMessageToQueryParameters(t *testing.T) {
}
}

// TestMessagetoQueryParametersRecursive, is a check that circular references between messages
// is handled gracefully. The goal is to insure that atemps to add messages with circular
// references to query-parameters returns a error message.
// TestMessagetoQueryParametersRecursive, is a check that cyclical references between messages
// are handled gracefully. The goal is to insure that attempts to add messages with cyclical
// references to query-parameters returns an error message.
func TestMessageToQueryParametersRecursive(t *testing.T) {
type test struct {
MsgDescs []*protodescriptor.DescriptorProto
Expand All @@ -418,7 +418,7 @@ func TestMessageToQueryParametersRecursive(t *testing.T) {

tests := []test{
// First test:
// Here we test that a message that references it self through a field will return a error.
// Here we test that a message that references it self through a field will return an error.
// Example proto:
// message DirectRecursiveMessage {
// DirectRecursiveMessage nested = 1;
Expand All @@ -441,7 +441,7 @@ func TestMessageToQueryParametersRecursive(t *testing.T) {
Message: "DirectRecursiveMessage",
},
// Second test:
// Here we test that a circle through multiple messages also is detected and that a error is returned.
// Here we test that a circle through multiple messages also is detected and that an error is returned.
// Sample:
// message Root { NodeMessage nested = 1; }
// message NodeMessage { CircleMessage nested = 1; }
Expand Down

0 comments on commit 245886a

Please sign in to comment.