Skip to content

Commit

Permalink
Merge pull request #471 from devholic/fix-invalid-escape-sequence
Browse files Browse the repository at this point in the history
fix: use Go-syntax representation to generate description
  • Loading branch information
k8s-ci-robot committed Apr 23, 2024
2 parents dc4e619 + 5cb5474 commit 8948a66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
12 changes: 3 additions & 9 deletions pkg/generators/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,15 +844,9 @@ 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())
if len(postDoc) > 0 {
g.Do("Description: $.$,\n", fmt.Sprintf("%#v", postDoc))
}
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/generators/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 8948a66

Please sign in to comment.