Fix Golang pattern validation with regex fails on commas #20079
In a struct, for a type: string accompanied by a pattern, these generated Go directives are corrected:
json:"creditCard,omitempty" validate:"regexp=^4[0-9]{3}\\s[0-9]{4}\\s[0-9]{4}\\s[0-9]{4}$"`
Test cases added
18 new regular expressions has been taken for test cases, from an SAP help page.
A go test has been run with various values, and it succeeds except for one test case: windowsAbsolutePath that might encounter a go validator.v2 package internal trouble.
Tests are in modules/openapi-generator/src/test/resources/3_0:
ls modules/openapi-generator/src/test/resources/3_0/issue_20079_*
issue_20079_go_regex_wrongly_translated.yaml
# For testing purpose
issue_20079_api_default_test.go # In the sample, replace test/api_default_test.go by this one
issue_20079_matching01.json # Copy these candidates json files along
issue_20079_matching02.json
issue_20079_matching03.json
issue_20079_matching04.json
issue_20079_non_matching01.json
issue_20079_non_matching02.json
issue_20079_non_matching03.json
issue_20079_non_matching04.json
The following command should then show the success:
go mod tidy && go vet ./... && go test ./... -v
Remaining problem
This fix mostly fixes the handling of regular expressions with Go.
But there's still two problems remaining, that will require other issues to be handled :+1:
-
For
javaCommentstest case, when it should use the^/\*.*\*/|//[^\\n]*$pattern, theorg.yaml.snakeyaml.scannerparser that scans the spec file, wrongly tells that\*is an invalid escape code.
But attempting to enter^/\\*.*\\*/|//[^\\n]*$keeps the\\*and generates^/\\*.*\\*/|//[^\n]*$instead of the wished^/\*.*\*/|//[^\n]*$ -
For
windowsAbsolutePathtest case, Govalidator.package v2doesn't look handling this regexp well, and tell it having a bad parameter.
PR checklist
- Read the contribution guidelines.
- Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- Run the following to build the project and update samples:
(For Windows users, please run the script in Git BASH)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.