Skip to content

Commit

Permalink
Camel case backward compatible in Swagger doc (#988)
Browse files Browse the repository at this point in the history
* Avoid potential out of range and support the convention that  could be inside

* Add some test cases for testing backward compatible
  • Loading branch information
xin-au authored and johanbrandhorst committed Aug 10, 2019
1 parent b6e6efb commit 86e5755
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "}"}, "")
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 86e5755

Please sign in to comment.