From 23d5b2fd6f8a8b48433799bce374dff1acc1dcd7 Mon Sep 17 00:00:00 2001 From: Sunghoon Kang Date: Tue, 23 Apr 2024 09:44:48 -0700 Subject: [PATCH 1/2] fix: use `strconv.Quote` to generate description Generated code cannot be built if the field's comment has an invalid escape sequence (ex. `\.`). This commit fixes this issue by replacing current description escape logic with `strconv.Quote` to properly escape characters in string. Signed-off-by: Sunghoon Kang --- pkg/generators/openapi.go | 14 +++++--------- pkg/generators/openapi_test.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/generators/openapi.go b/pkg/generators/openapi.go index 745bac6b7..7bf9824e1 100644 --- a/pkg/generators/openapi.go +++ b/pkg/generators/openapi.go @@ -25,6 +25,7 @@ import ( "reflect" "regexp" "sort" + "strconv" "strings" "k8s.io/gengo/v2" @@ -844,15 +845,10 @@ func (g openAPITypeWriter) generateDescription(CommentLines []string) { } } - postDoc := strings.TrimLeft(buffer.String(), "\n") - postDoc = strings.TrimRight(postDoc, "\n") - postDoc = strings.Replace(postDoc, "\\\"", "\"", -1) // replace user's \" to " - postDoc = strings.Replace(postDoc, "\"", "\\\"", -1) // Escape " - postDoc = strings.Replace(postDoc, "\n", "\\n", -1) - postDoc = strings.Replace(postDoc, "\t", "\\t", -1) - postDoc = strings.Trim(postDoc, " ") - if postDoc != "" { - g.Do("Description: \"$.$\",\n", postDoc) + postDoc := strings.TrimSpace(buffer.String()) + postDoc = strconv.Quote(postDoc) + if postDoc != `""` { + g.Do("Description: $.$,\n", postDoc) } } diff --git a/pkg/generators/openapi_test.go b/pkg/generators/openapi_test.go index 3fa753d14..4ffdf6c92 100644 --- a/pkg/generators/openapi_test.go +++ b/pkg/generators/openapi_test.go @@ -150,6 +150,9 @@ func TestSimple(t *testing.T) { // an int member with a default // +default=1 OmittedInt int ` + "`" + `json:"omitted,omitempty"` + "`" + ` + // a field with an invalid escape sequence in comment + // ex) regexp:^.*\.yaml$ + InvalidEscapeSequenceInComment string }` packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { @@ -384,8 +387,16 @@ Type: []string{"integer"}, Format: "int32", }, }, +"InvalidEscapeSequenceInComment": { +SchemaProps: spec.SchemaProps{ +Description: "a field with an invalid escape sequence in comment ex) regexp:^.*\\.yaml$", +Default: "", +Type: []string{"string"}, +Format: "", +}, +}, }, -Required: []string{"String","Int64","Int32","Int16","Int8","Uint","Uint64","Uint32","Uint16","Uint8","Byte","Bool","Float64","Float32","ByteArray","WithExtension","WithStructTagExtension","WithListType","Map","StringPointer"}, +Required: []string{"String","Int64","Int32","Int16","Int8","Uint","Uint64","Uint32","Uint16","Uint8","Byte","Bool","Float64","Float32","ByteArray","WithExtension","WithStructTagExtension","WithListType","Map","StringPointer","InvalidEscapeSequenceInComment"}, }, VendorExtensible: spec.VendorExtensible{ Extensions: spec.Extensions{ From 5cb5474018333877c24c0fa1a73963d4c85dd075 Mon Sep 17 00:00:00 2001 From: Sunghoon Kang Date: Tue, 23 Apr 2024 13:16:09 -0700 Subject: [PATCH 2/2] refactor: use `Sprintf("%#v")` instead of `strconv.Quote` https://github.com/kubernetes/kube-openapi/pull/471#discussion_r1576822918 Signed-off-by: Sunghoon Kang --- pkg/generators/openapi.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/generators/openapi.go b/pkg/generators/openapi.go index 7bf9824e1..1ffcf9094 100644 --- a/pkg/generators/openapi.go +++ b/pkg/generators/openapi.go @@ -25,7 +25,6 @@ import ( "reflect" "regexp" "sort" - "strconv" "strings" "k8s.io/gengo/v2" @@ -846,9 +845,8 @@ func (g openAPITypeWriter) generateDescription(CommentLines []string) { } postDoc := strings.TrimSpace(buffer.String()) - postDoc = strconv.Quote(postDoc) - if postDoc != `""` { - g.Do("Description: $.$,\n", postDoc) + if len(postDoc) > 0 { + g.Do("Description: $.$,\n", fmt.Sprintf("%#v", postDoc)) } }