-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Golang pattern validation with regex fails on commas #20079 #20369
Open
mlebihan
wants to merge
2
commits into
OpenAPITools:master
Choose a base branch
from
mlebihan:go_pattern_generation_20079
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
modules/openapi-generator/src/test/resources/3_0/issue_20079_api_default_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package openapi | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
openapiclient "github.com/GIT_USER_ID/GIT_REPO_ID" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func Test_openapi_DefaultAPIService(t *testing.T) { | ||
|
||
configuration := openapiclient.NewConfiguration() | ||
apiClient := openapiclient.NewAPIClient(configuration) | ||
|
||
t.Run("Matching regex", func(t *testing.T) { | ||
filesNames := []string{"issue_20079_matching01.json", "issue_20079_matching02.json", "issue_20079_matching03.json", "issue_20079_matching04.json"} | ||
|
||
for _, fileName := range filesNames { | ||
data, errLoad := os.ReadFile(fileName) | ||
|
||
if errLoad != nil { | ||
t.Errorf("Cannot read test file resource %s: %s", fileName, errLoad.Error()) | ||
} | ||
|
||
var importCode *openapiclient.FooGet200ResponseCode | ||
|
||
errParse := json.Unmarshal(data, &importCode) | ||
|
||
if errParse != nil { | ||
t.Errorf("The resource file %s that was expected matching, doesn't: %s", fileName, errParse.Error()) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Non-matching regex", func(t *testing.T) { | ||
filesNames := []string{"issue_20079_non_matching01.json", "issue_20079_non_matching02.json", "issue_20079_non_matching03.json", "issue_20079_non_matching04.json"} | ||
|
||
for _, fileName := range filesNames { | ||
data, errLoad := os.ReadFile(fileName) | ||
|
||
if errLoad != nil { | ||
t.Errorf("Cannot read test file resource %s: %s", fileName, errLoad.Error()) | ||
} | ||
|
||
var importCode *openapiclient.FooGet200ResponseCode | ||
|
||
errParse := json.Unmarshal(data, &importCode) | ||
|
||
if errParse == nil { | ||
t.Errorf("The resource file %s that was expected non-matching, does", fileName) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Test DefaultAPIService FooGet", func(t *testing.T) { | ||
|
||
t.Skip("skip test") // remove to run test | ||
|
||
resp, httpRes, err := apiClient.DefaultAPI.FooGet(context.Background()).Execute() | ||
|
||
require.Nil(t, err) | ||
require.NotNil(t, resp) | ||
assert.Equal(t, 200, httpRes.StatusCode) | ||
|
||
}) | ||
|
||
} |
191 changes: 191 additions & 0 deletions
191
...les/openapi-generator/src/test/resources/3_0/issue_20079_go_regex_wrongly_translated.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
openapi: '3.0.3' | ||
info: | ||
title: API Test | ||
version: '1.0' | ||
paths: | ||
/foo: | ||
get: | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
code: | ||
oneOf: | ||
- $ref: "#/components/schemas/importCode" | ||
|
||
components: | ||
schemas: | ||
importCode: | ||
type: object | ||
properties: | ||
code: | ||
type: string | ||
pattern: "^[0-9]{2,}$" | ||
|
||
creditCard: | ||
description: "Visa credit card\n | ||
matches: 4123 6453 2222 1746\n | ||
non-matches: 3124 5675 4400 4567, 4123-6453-2222-1746" | ||
type: string | ||
|
||
pattern: "^4[0-9]{3}\\s[0-9]{4}\\s[0-9]{4}\\s[0-9]{4}$" | ||
# Original was: 4[0-9]{3}\s[0-9]{4}\s[0-9]{4}\s[0-9]{4} | ||
|
||
date: | ||
description: "Some dates\n | ||
matches: 31/04/1999, 15/12/4567\n | ||
non-matches: 31/4/1999, 31/4/99, 1999/04/19, 42/67/25456" | ||
type: string | ||
pattern: "^([0-2][0-9]|30|31)/(0[1-9]|1[0-2])/[0-9]{4}$" | ||
# Original was: ([0-2][0-9]|30|31)/(0[1-9]|1[0-2])/[0-9]{4} : unchanged | ||
|
||
windowsAbsolutePath: | ||
description: "Windows absolute path\n | ||
matches: \\\\server\\share\\file\n | ||
non-matches: \\directory\\directory2, /directory2" | ||
type: string | ||
|
||
# This test case doesn't work due to a problem (?) in validator.v2 (?) | ||
# it issues an unexpected unknown tag or Bad Parameter. | ||
|
||
# pattern: "^([A-Za-z]:|\\)\\[[:alnum:][:whitespace:]!\"#$%&'()+,-.;=@[]^_`{}~.]*$" | ||
# Original was: ([A-Za-z]:|\\)\\[[:alnum:][:whitespace:]!"#$%&'()+,-.\\;=@\[\]^_`{}~.]* | ||
|
||
email1: | ||
description: "Email Address 1\n | ||
matches: abc.123@def456.com, _123@abc.ca\n | ||
non-matches: abc@dummy, ab*cd@efg.hijkl" | ||
type: string | ||
|
||
pattern: "^[[:word:]\\-.]+@[[:word:]\\-.]+\\.[[:alpha:]]{2,3}$" | ||
# Original was: [[:word:]\-.]+@[[:word:]\-.]+\.[[:alpha:]]{2,3} | ||
|
||
email2: | ||
description: "Email Address 2\n | ||
matches: *@qrstuv@wxyz.12345.com, __1234^%@@abc.def.ghijkl\n | ||
non-matches: abc.123.*&ca, ^%abcdefg123" | ||
type: string | ||
|
||
pattern: "^.+@.+\\..+$" | ||
# Original was: .+@.+\..+ | ||
|
||
htmlHexadecimalColorCode1: | ||
description: "HTML Hexadecimal Color Code 1\n | ||
matches: AB1234, CCCCCC, 12AF3B\n | ||
non-matches: 123G45, 12-44-CC" | ||
type: string | ||
pattern: "^[A-F0-9]{6}$" | ||
# Original was: [A-F0-9]{6} : unchanged | ||
|
||
htmlHexadecimalColorCode2: | ||
description: "HTML Hexadecimal Color Code 2\n | ||
matches: AB 11 00, CC 12 D3\n | ||
non-matches: SS AB CD, AA BB CC DD, 1223AB" | ||
type: string | ||
|
||
pattern: "^[A-F0-9]{2}\\s[A-F0-9]{2}\\s[A-F0-9]{2}$" | ||
# Original was: [A-F0-9]{2}\s[A-F0-9]{2}\s[A-F0-9]{2} | ||
|
||
ipAddress: | ||
description: "IP Address\n | ||
matches: 10.25.101.216\n | ||
non-matches: 0.0.0, 256.89.457.02" | ||
type: string | ||
|
||
pattern: "^((2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])\\.){3}(2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])$" | ||
# Original was: ((2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])\.){3}(2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9]) | ||
|
||
javaComments: | ||
description: "Java Comments\n | ||
matches: Matches Java comments that are between /* and */, or one line comments prefaced by //\n | ||
non-matches: a=1" | ||
type: string | ||
|
||
# This test case doesn't work due to a problem (?) in validator.v2 (?) | ||
# org.yaml.snakeyaml.scanner declares \* being an invalid escape code at yaml checking step | ||
|
||
# pattern: "^/\*.*\*/|//[^\\n]*$" | ||
# Original was: /\*.*\*/|//[^\n]* | ||
|
||
money: | ||
description: "\n | ||
matches: $1.00, -$97.65 | ||
non-matches: $1, 1.00$, $-75.17" | ||
type: string | ||
|
||
pattern: "^(\\+|-)?\\$[0-9]*\\.[0-9]{2}$" | ||
# Original was: (\+|-)?\$[0-9]*\.[0-9]{2} | ||
|
||
positiveNegativeDecimalValue: | ||
description: "Positive, negative numbers, and decimal values\n | ||
matches: +41, -412, 2, 7968412, 41, +41.1, -3.141592653 | ||
non-matches: ++41, 41.1.19, -+97.14" | ||
type: string | ||
|
||
pattern: "^(\\+|-)?[0-9]+(\\.[0-9]+)?$" | ||
# Original was: (\+|-)?[0-9]+(\.[0-9]+)? | ||
|
||
password1: | ||
description: "Passwords 1\n | ||
matches: abcd, 1234, A1b2C3d4, 1a2B3\n | ||
non-matches: abc, *ab12, abcdefghijkl" | ||
type: string | ||
pattern: "^[[:alnum:]]{4,10}$" | ||
# Original was: [[:alnum:]]{4,10} : unchanged | ||
|
||
password2: | ||
description: "Passwords 2\n | ||
matches: AB_cd, A1_b2c3, a123_\n | ||
non-matches: *&^g, abc, 1bcd" | ||
type: string | ||
|
||
pattern: "^[a-zA-Z]\\w{3,7}$" | ||
# Original was: [a-zA-Z]\w{3,7} : unchanged | ||
|
||
phoneNumber: | ||
description: "Phone Numbers\n | ||
matches: 519-883-6898, 519 888 6898\n | ||
non-matches: 888 6898, 5198886898, 519 883-6898" | ||
type: string | ||
|
||
pattern: "^([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\\s[2-9][0-9]{2}\\s[0-9]{4})$" | ||
# Original was: ([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\s[2-9][0-9]{2}\s[0-9]{4}) | ||
|
||
sentence1: | ||
description: "Sentences 1\n | ||
matches: Hello, how are you?\n | ||
non-matches: i am fine" | ||
type: string | ||
|
||
pattern: "^[A-Z0-9].*(\\.|\\?|!)$" | ||
# Original was: [A-Z0-9].*(\.|\?|!) | ||
|
||
sentence2: | ||
description: "Sentences 2\n | ||
matches: Hello, how are you?n | ||
non-matches: i am fine" | ||
type: string | ||
pattern: "^[[:upper:]0-9].*[.?!]$" | ||
# Original was: [[:upper:]0-9].*[.?!] : unchanged | ||
|
||
socialSecurityNumber: | ||
description: "Social Security Number\n | ||
matches: 123-45-6789\n | ||
non-matches: 123 45 6789, 123456789, 1234-56-7891" | ||
type: string | ||
pattern: "^[0-9]{3}-[0-9]{2}-[0-9]{4}$" | ||
# Original was: [0-9]{3}-[0-9]{2}-[0-9]{4} : unchanged | ||
|
||
url: | ||
description: "URL\n | ||
matches: http://www.sample.com, www.sample.com\n | ||
non-matches: http://sample.com, http://www.sample.comm" | ||
type: string | ||
|
||
# \. ==> \\. | ||
pattern: "^(http://)?www\\.[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$" | ||
# Original was: (http://)?www\.[a-zA-Z0-9]+\.[a-zA-Z]{2,3} |
24 changes: 24 additions & 0 deletions
24
modules/openapi-generator/src/test/resources/3_0/issue_20079_matching01.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"code": "52", | ||
"creditCard": "4123 6453 2222 1746", | ||
"date": "31/04/1999", | ||
"windowsAbsolutePath": "\\\\server\\share\\file", | ||
|
||
"email1": "abc.123@def456.com", | ||
"email2": "*@qrstuv@wxyz.12345.com", | ||
"htmlHexadecimalColorCode1": "AB1234", | ||
"htmlHexadecimalColorCode2": "AB 11 00", | ||
"ipAddress": "10.25.101.216", | ||
|
||
"javaComments": "/* a comment */", | ||
"money": "$1.00", | ||
"positiveNegativeDecimalValue": "+41", | ||
"password1": "abcd", | ||
"password2": "AB_cd", | ||
|
||
"phoneNumber": "519-883-6898", | ||
"sentence1": "Hello, how are you?", | ||
"sentence2": "Hello, how are you?", | ||
"socialSecurityNumber": "123-45-6789", | ||
"url": "http://www.sample.com" | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the PR and adding the tests.
can you please PM me to discuss the integration tests when you've time?
Slack: https://join.slack.com/t/openapi-generator/shared_invite/zt-2wmkn4s8g-n19PJ99Y6Vei74WMUIehQA