[BUG] Regular expressions which have forward slash generate invalid ruby code.
Created by: mkanoor
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator ([example] -
What's the version of OpenAPI Generator used? -
[X ] Have you search for related issues/PRs?
-
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
In Regular expression the forward slash only needs to be escaped if its inside the /pattern/ format, if the pattern is a string and fed into Regexp.new it doesn't need to be escaped with a backslash. https://github.com/OpenAPITools/openapi-generator/blob/de2753dfc7d17a8afcc14fdd705ade3f6cca3cb2/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L4825 This code only checks if the string starts with a leading slash and assumes it's the /pattern/ format. The pattern can be a string and it could include both the leading and trailing slash e.g /root/. Since Openapi regex pattern supports both the string pattern and the /pattern/ format its ambiguous what the code should generate. For the pattern '/ax$|/bx$' the ruby code generated is Regexp.new(/ax$|/bx$) which fails with a syntax error SyntaxError ((irb):20: unknown regexp option - b) (irb):20: `$)' is not allowed as a global variable name (irb):20: syntax error, unexpected end-of-input, expecting ')' Regexp.new(/ax$|/bx$)
Can you please clarify if this a bug or that openapi regexes need to be always in the /pattern/ format.
openapi-generator version
4.2.2 also tried with master
OpenAPI declaration file content or url
https://gist.github.com/mkanoor/802eb64217e04e4e7b5fc12ac4ebf8c5
Command line used for generation
openapi-generator generate -i /tmp/test_regex.yaml -g ruby -o /tmp
Ruby
Steps to reproduce
openapi-generator generate -i /tmp/test_regex.yaml -g ruby -o /tmp
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/1392
Suggest a fix
Checking if there is a leading slash and a trailing slash before deciding if this is a /pattern/ format versus a string. That works as long as the regexp is not looking for a leading and trailing slash.