diff --git a/protoc-gen-swagger/genswagger/template.go b/protoc-gen-swagger/genswagger/template.go index 005a3474f61..99170d72dab 100644 --- a/protoc-gen-swagger/genswagger/template.go +++ b/protoc-gen-swagger/genswagger/template.go @@ -525,7 +525,7 @@ func templateToSwaggerPath(path string) string { } // Pop from the stack depth-- - buffer += "}" + buffer += string(char) case '/': if depth == 0 { parts = append(parts, buffer) @@ -534,6 +534,7 @@ func templateToSwaggerPath(path string) string { // section. continue } + buffer += string(char) default: buffer += string(char) break @@ -548,12 +549,24 @@ func templateToSwaggerPath(path string) 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] + 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 { diff --git a/protoc-gen-swagger/genswagger/template_test.go b/protoc-gen-swagger/genswagger/template_test.go index f0ecd1e370d..67f449432d6 100644 --- a/protoc-gen-swagger/genswagger/template_test.go +++ b/protoc-gen-swagger/genswagger/template_test.go @@ -697,6 +697,11 @@ 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"}, } for _, data := range tests {