From 63f03105b182affd81739019e9b1e1c74f3d68cf Mon Sep 17 00:00:00 2001 From: Michael Bohn Date: Wed, 23 Aug 2023 15:25:39 +0200 Subject: [PATCH 1/2] fix path with : in the middle #3517 issue --- protoc-gen-openapiv2/internal/genopenapi/template.go | 7 ------- protoc-gen-openapiv2/internal/genopenapi/template_test.go | 1 + 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/protoc-gen-openapiv2/internal/genopenapi/template.go b/protoc-gen-openapiv2/internal/genopenapi/template.go index 3515dedcb8e..a8c5a1feabb 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/template.go +++ b/protoc-gen-openapiv2/internal/genopenapi/template.go @@ -987,13 +987,6 @@ pathLoop: jsonBuffer = "" } case '/': - if depth == 0 { - parts = append(parts, buffer) - buffer = "" - // Since the stack was empty when we hit the '/' we are done with this - // section. - continue - } buffer += string(char) jsonBuffer += string(char) case ':': diff --git a/protoc-gen-openapiv2/internal/genopenapi/template_test.go b/protoc-gen-openapiv2/internal/genopenapi/template_test.go index 80c4e48d214..9dcadeb4396 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/template_test.go +++ b/protoc-gen-openapiv2/internal/genopenapi/template_test.go @@ -3918,6 +3918,7 @@ func TestTemplateToOpenAPIPath(t *testing.T) { {"/{user.name=prefix/*}:customMethod", "/{user.name}:customMethod"}, {"/{user.name=prefix1/*/prefix2/*}:customMethod", "/{user.name}:customMethod"}, {"/{parent=prefix/*}/children:customMethod", "/{parent}/children:customMethod"}, + {"/item/search:items/{item_no_query}", "/item/search:items/{item_no_query}"}, } reg := descriptor.NewRegistry() reg.SetUseJSONNamesForFields(false) From 119f622fc727fb46f68d93c2996643f1e373d881 Mon Sep 17 00:00:00 2001 From: Michael Bohn Date: Wed, 23 Aug 2023 17:10:12 +0200 Subject: [PATCH 2/2] better test and correct fix --- .../internal/genopenapi/template.go | 17 ++++++++--------- .../internal/genopenapi/template_test.go | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/protoc-gen-openapiv2/internal/genopenapi/template.go b/protoc-gen-openapiv2/internal/genopenapi/template.go index a8c5a1feabb..3298dc4652e 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/template.go +++ b/protoc-gen-openapiv2/internal/genopenapi/template.go @@ -962,8 +962,7 @@ func templateToParts(path string, reg *descriptor.Registry, fields []*descriptor depth := 0 buffer := "" jsonBuffer := "" -pathLoop: - for i, char := range path { + for _, char := range path { switch char { case '{': // Push on the stack @@ -987,18 +986,18 @@ pathLoop: jsonBuffer = "" } case '/': - buffer += string(char) - jsonBuffer += string(char) - case ':': if depth == 0 { - // As soon as we find a ":" outside a variable, - // everything following is a verb parts = append(parts, buffer) - buffer = path[i:] - break pathLoop + buffer = "" + // Since the stack was empty when we hit the '/' we are done with this + // section. + continue } buffer += string(char) jsonBuffer += string(char) + case ':': + buffer += string(char) + jsonBuffer += string(char) default: buffer += string(char) jsonBuffer += string(char) diff --git a/protoc-gen-openapiv2/internal/genopenapi/template_test.go b/protoc-gen-openapiv2/internal/genopenapi/template_test.go index 9dcadeb4396..7c6df9d10d1 100644 --- a/protoc-gen-openapiv2/internal/genopenapi/template_test.go +++ b/protoc-gen-openapiv2/internal/genopenapi/template_test.go @@ -3858,6 +3858,7 @@ func TestTemplateWithJsonCamelCase(t *testing.T) { {"test/{ab_c}", "test/{abC}"}, {"test/{json_name}", "test/{jsonNAME}"}, {"test/{field_abc.field_newName}", "test/{fieldAbc.RESERVEDJSONNAME}"}, + {"/item/search:items/{item_no_query}", "/item/search:items/{itemNoQuery}"}, } reg := descriptor.NewRegistry() reg.SetUseJSONNamesForFields(true) @@ -3918,7 +3919,6 @@ func TestTemplateToOpenAPIPath(t *testing.T) { {"/{user.name=prefix/*}:customMethod", "/{user.name}:customMethod"}, {"/{user.name=prefix1/*/prefix2/*}:customMethod", "/{user.name}:customMethod"}, {"/{parent=prefix/*}/children:customMethod", "/{parent}/children:customMethod"}, - {"/item/search:items/{item_no_query}", "/item/search:items/{item_no_query}"}, } reg := descriptor.NewRegistry() reg.SetUseJSONNamesForFields(false)