Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mlebihan
Copy link
Contributor

@mlebihan mlebihan commented Dec 23, 2024

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 javaComments test case, when it should use the ^/\*.*\*/|//[^\\n]*$ pattern, the org.yaml.snakeyaml.scanner parser 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]*$

  2. For windowsAbsolutePath test case, Go validator.package v2 doesn'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:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    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 (upcoming 7.x.0 minor 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.

@wing328
Copy link
Member

wing328 commented Dec 26, 2024

thanks for the PR

can you please rebase/merge the latest master, which should have the gradle tests fixed?

@wing328
Copy link
Member

wing328 commented Dec 26, 2024

cc @antihax (2017/11) @grokify (2018/07) @kemokemo (2018/09) @jirikuncar (2021/01) @ph4r5h4d (2021/04) @lwj5 (2023/04)

@wing328 wing328 added this to the 7.11.0 milestone Dec 26, 2024
@mlebihan
Copy link
Contributor Author

It should be done, now.

@@ -0,0 +1,70 @@
package openapi
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants