diff --git a/protoc-gen-swagger/genswagger/template.go b/protoc-gen-swagger/genswagger/template.go index 3b1e8348c20..0a32b49e68d 100644 --- a/protoc-gen-swagger/genswagger/template.go +++ b/protoc-gen-swagger/genswagger/template.go @@ -626,7 +626,7 @@ func templateToSwaggerPath(path string, reg *descriptor.Registry) string { buffer += string(char) if reg.GetUseJSONNamesForFields() && len(jsonBuffer) > 1 { - jsonSnakeCaseName := string(jsonBuffer[1 : len(buffer)-1]) + jsonSnakeCaseName := string(jsonBuffer[1:]) jsonCamelCaseName := string(lowerCamelCase(jsonSnakeCaseName)) prev := string(buffer[:len(buffer)-len(jsonSnakeCaseName)-2]) buffer = strings.Join([]string{prev, "{", jsonCamelCaseName, "}"}, "") @@ -641,6 +641,7 @@ func templateToSwaggerPath(path string, reg *descriptor.Registry) string { continue } buffer += string(char) + jsonBuffer += string(char) default: buffer += string(char) jsonBuffer += string(char) diff --git a/protoc-gen-swagger/genswagger/template_test.go b/protoc-gen-swagger/genswagger/template_test.go index 200f563d544..385785ffc8f 100644 --- a/protoc-gen-swagger/genswagger/template_test.go +++ b/protoc-gen-swagger/genswagger/template_test.go @@ -914,6 +914,14 @@ func TestTemplateToSwaggerPath(t *testing.T) { {"/{parent=prefix/*}/children:customMethod", "/{parent=prefix/*}/children:customMethod"}, } reg := descriptor.NewRegistry() + reg.SetUseJSONNamesForFields(false) + for _, data := range tests { + actual := templateToSwaggerPath(data.input, reg) + if data.expected != actual { + t.Errorf("Expected templateToSwaggerPath(%v) = %v, actual: %v", data.input, data.expected, actual) + } + } + reg.SetUseJSONNamesForFields(true) for _, data := range tests { actual := templateToSwaggerPath(data.input, reg) if data.expected != actual { @@ -991,6 +999,14 @@ func TestFQMNtoSwaggerName(t *testing.T) { {"/{test1}/{test2}/", "/{test1}/{test2}/"}, } reg := descriptor.NewRegistry() + reg.SetUseJSONNamesForFields(false) + for _, data := range tests { + actual := templateToSwaggerPath(data.input, reg) + if data.expected != actual { + t.Errorf("Expected templateToSwaggerPath(%v) = %v, actual: %v", data.input, data.expected, actual) + } + } + reg.SetUseJSONNamesForFields(true) for _, data := range tests { actual := templateToSwaggerPath(data.input, reg) if data.expected != actual {