From 6ff9139981da600ac637991b5b44a3e87ebb3d7b Mon Sep 17 00:00:00 2001 From: Mathias Loesch Date: Thu, 9 Jan 2025 13:51:31 +0100 Subject: [PATCH 1/3] Make tag regexes non-greedy --- operation.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/operation.go b/operation.go index f7a3736f2..6404ff5c2 100644 --- a/operation.go +++ b/operation.go @@ -426,23 +426,23 @@ const ( var regexAttributes = map[string]*regexp.Regexp{ // for Enums(A, B) - enumsTag: regexp.MustCompile(`(?i)\s+enums\(.*\)`), + enumsTag: regexp.MustCompile(`(?Ui)\s+enums\(.*\)`), // for maximum(0) - maximumTag: regexp.MustCompile(`(?i)\s+maxinum|maximum\(.*\)`), + maximumTag: regexp.MustCompile(`(?Ui)\s+maxinum|maximum\(.*\)`), // for minimum(0) - minimumTag: regexp.MustCompile(`(?i)\s+mininum|minimum\(.*\)`), + minimumTag: regexp.MustCompile(`(?Ui)\s+mininum|minimum\(.*\)`), // for default(0) - defaultTag: regexp.MustCompile(`(?i)\s+default\(.*\)`), + defaultTag: regexp.MustCompile(`(?Ui)\s+default\(.*\)`), // for minlength(0) - minLengthTag: regexp.MustCompile(`(?i)\s+minlength\(.*\)`), + minLengthTag: regexp.MustCompile(`(?Ui)\s+minlength\(.*\)`), // for maxlength(0) - maxLengthTag: regexp.MustCompile(`(?i)\s+maxlength\(.*\)`), + maxLengthTag: regexp.MustCompile(`(?Ui)\s+maxlength\(.*\)`), // for format(email) - formatTag: regexp.MustCompile(`(?i)\s+format\(.*\)`), + formatTag: regexp.MustCompile(`(?Ui)\s+format\(.*\)`), // for extensions(x-example=test) - extensionsTag: regexp.MustCompile(`(?i)\s+extensions\(.*\)`), + extensionsTag: regexp.MustCompile(`(?Ui)\s+extensions\(.*\)`), // for collectionFormat(csv) - collectionFormatTag: regexp.MustCompile(`(?i)\s+collectionFormat\(.*\)`), + collectionFormatTag: regexp.MustCompile(`(?Ui)\s+collectionFormat\(.*\)`), // example(0) exampleTag: regexp.MustCompile(`(?i)\s+example\(.*\)`), // schemaExample(0) From bbdc5f01bf5b55b696683e7ffa6da2d35a1644b3 Mon Sep 17 00:00:00 2001 From: Mathias Loesch Date: Thu, 9 Jan 2025 13:53:07 +0100 Subject: [PATCH 2/3] Use last parenthesis to find attr --- operation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operation.go b/operation.go index 6404ff5c2..0d3a816a4 100644 --- a/operation.go +++ b/operation.go @@ -490,7 +490,7 @@ func (operation *Operation) parseParamAttribute(comment, objectType, schemaType, func findAttr(re *regexp.Regexp, commentLine string) (string, error) { attr := re.FindString(commentLine) - l, r := strings.Index(attr, "("), strings.Index(attr, ")") + l, r := strings.Index(attr, "("), strings.LastIndex(attr, ")") if l == -1 || r == -1 { return "", fmt.Errorf("can not find regex=%s, comment=%s", re.String(), commentLine) } From b8dd84fc3d51ff1ddc7c4866c2397b606e8b08b4 Mon Sep 17 00:00:00 2001 From: Mathias Loesch Date: Thu, 9 Jan 2025 13:53:18 +0100 Subject: [PATCH 3/3] Extend test cases --- operation_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/operation_test.go b/operation_test.go index aa77a8325..9306706c9 100644 --- a/operation_test.go +++ b/operation_test.go @@ -1919,7 +1919,7 @@ func TestParseParamCommentByExampleInt(t *testing.T) { func TestParseParamCommentByExampleString(t *testing.T) { t.Parallel() - comment := `@Param some_id query string true "Some ID" Example(True feelings)` + comment := `@Param some_id query string true "Some ID" Example(True feelings (maybe))` operation := NewOperation(nil) err := operation.ParseComment(comment, nil) @@ -1928,7 +1928,7 @@ func TestParseParamCommentByExampleString(t *testing.T) { expected := `[ { "type": "string", - "example": "True feelings", + "example": "True feelings (maybe)", "description": "Some ID", "name": "some_id", "in": "query", @@ -1958,7 +1958,7 @@ func TestParseParamCommentByExampleUnsupportedType(t *testing.T) { func TestParseParamCommentBySchemaExampleString(t *testing.T) { t.Parallel() - comment := `@Param some_id body string true "Some ID" SchemaExample(True feelings)` + comment := `@Param some_id body string true "Some ID" SchemaExample(True feelings (maybe))` operation := NewOperation(nil) err := operation.ParseComment(comment, nil) @@ -1972,7 +1972,7 @@ func TestParseParamCommentBySchemaExampleString(t *testing.T) { "required": true, "schema": { "type": "string", - "example": "True feelings" + "example": "True feelings (maybe)" } } ]`