Skip to content

Commit

Permalink
revert de0b679... add support for resource name in swagger plugin (#702)
Browse files Browse the repository at this point in the history
This commit was found to break the representation of path variables in
the generated swagger files.
  • Loading branch information
johanbrandhorst committed Aug 20, 2019
1 parent fd2d159 commit 47f5067
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
14 changes: 0 additions & 14 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,26 +672,12 @@ func templateToSwaggerPath(path string, reg *descriptor.Registry) string {
// memory.
re := regexp.MustCompile("{([a-zA-Z][a-zA-Z0-9_.]*).*}")
for index, part := range parts {
// If part is a resource name such as "parent", "name", "user.name", the format info must be retained.
prefix := re.ReplaceAllString(part, "$1")
if isResourceName(prefix) {
continue
}
parts[index] = re.ReplaceAllString(part, "{$1}")
}

return strings.Join(parts, "/")
}

func isResourceName(prefix string) bool {
words := strings.Split(prefix, ".")
l := len(words)
field := words[l-1]
words = strings.Split(field, ":")
field = words[0]
return field == "parent" || field == "name"
}

func renderServices(services []*descriptor.Service, paths swaggerPathsObject, reg *descriptor.Registry, requestResponseRefs, customRefs refMap) error {
// Correctness of svcIdx and methIdx depends on 'services' containing the services in the same order as the 'file.Service' array.
for svcIdx, svc := range services {
Expand Down
20 changes: 10 additions & 10 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,16 +902,16 @@ func TestTemplateToSwaggerPath(t *testing.T) {
{"/{test=prefix/that/has/multiple/parts/to/it/*}", "/{test}"},
{"/{test1}/{test2}", "/{test1}/{test2}"},
{"/{test1}/{test2}/", "/{test1}/{test2}/"},
{"/{name=prefix/*}", "/{name=prefix/*}"},
{"/{name=prefix1/*/prefix2/*}", "/{name=prefix1/*/prefix2/*}"},
{"/{user.name=prefix/*}", "/{user.name=prefix/*}"},
{"/{user.name=prefix1/*/prefix2/*}", "/{user.name=prefix1/*/prefix2/*}"},
{"/{parent=prefix/*}/children", "/{parent=prefix/*}/children"},
{"/{name=prefix/*}:customMethod", "/{name=prefix/*}:customMethod"},
{"/{name=prefix1/*/prefix2/*}:customMethod", "/{name=prefix1/*/prefix2/*}:customMethod"},
{"/{user.name=prefix/*}:customMethod", "/{user.name=prefix/*}:customMethod"},
{"/{user.name=prefix1/*/prefix2/*}:customMethod", "/{user.name=prefix1/*/prefix2/*}:customMethod"},
{"/{parent=prefix/*}/children:customMethod", "/{parent=prefix/*}/children:customMethod"},
{"/{name=prefix/*}", "/{name}"},
{"/{name=prefix1/*/prefix2/*}", "/{name}"},
{"/{user.name=prefix/*}", "/{user.name}"},
{"/{user.name=prefix1/*/prefix2/*}", "/{user.name}"},
{"/{parent=prefix/*}/children", "/{parent}/children"},
{"/{name=prefix/*}:customMethod", "/{name}:customMethod"},
{"/{name=prefix1/*/prefix2/*}:customMethod", "/{name}:customMethod"},
{"/{user.name=prefix/*}:customMethod", "/{user.name}:customMethod"},
{"/{user.name=prefix1/*/prefix2/*}:customMethod", "/{user.name}:customMethod"},
{"/{parent=prefix/*}/children:customMethod", "/{parent}/children:customMethod"},
}
reg := descriptor.NewRegistry()
reg.SetUseJSONNamesForFields(false)
Expand Down

0 comments on commit 47f5067

Please sign in to comment.