openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Regular expressions which have forward slash generate invalid ruby code.

Open mkanoor opened this issue 6 years ago • 2 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?

  • [x] Have you validated the input using an OpenAPI validator ([example]

  • [X] What's the version of OpenAPI Generator used?

  • [X ] Have you search for related issues/PRs?

  • [X] 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.

mkanoor avatar Mar 11 '20 18:03 mkanoor

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

auto-labeler avatar Mar 11 '20 18:03 auto-labeler

Just bumped into this issue. I think this is the same problem as #1517 but for Ruby, however because Python/Ruby both use perl-flavored regexes, I imagine the fix is likely going to be very similar to what was done in #1539 (I have a test PR that I will try to clean up now that backports the python fix to the ruby code generator and it seems to work).

ivgiuliani avatar Sep 01 '23 07:09 ivgiuliani