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

[Ruby] Fix incorrect escaping of Ruby forward slashes #16474

Merged

Conversation

ivgiuliani
Copy link
Contributor

@ivgiuliani ivgiuliani commented Sep 1, 2023

Ruby regexs are always generated as match patterns enclosed in slash characters (i.e. using the /pattern/ syntax). Regular expressions defined in the OpenAPI declaration via the pattern attribute follow ECMA 262 which means they already include the correct escaping of forward slashes as far as Ruby is concerned.

The current Ruby codegen is incorrectly escaping all forward slashes, which ultimately causes the generated code to include additional incorrect escape sequences which cause the generated file to have an invalid syntax.

This commit ports the same fix introduced in #1539 for the Python codegen, as both Ruby and Python use perl-flavored regular expressions so they behave in the same way when it comes to escaping forward slashes.

Fixes #5582.

cc @cliffano (2017/07) @zlx (2017/09) @autopp (2019/02)


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 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date
    
    Commit all changed files.
  • File the PR against the correct branch: master (7.0.1 - patch release), 7.1.x (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.

@ivgiuliani ivgiuliani changed the title Fix ruby forward slash escape [Ruby] Fix incorrect escaping of Ruby forward slashes Sep 1, 2023
Comment on lines +53 to +63
- name: alternation_with_forward_slash
in: header
schema:
type: string
pattern: '/ax$|/bx$'
- name: patten_starts_ends_with_slash
in: header
schema:
type: string
pattern: '/root/'
description: 'Should match only /root/ but not root'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added new test cases here rather than creating a separate file, but please let me know if there's a preference for keeping this separate

Ruby is not correctly escaping pattern sequences containing forward
slashes in their definition. This commit adds tests that verify the
correct behaviour of the code generator.

See issue OpenAPITools#5582.
Ruby regexs are always generated as match patterns enclosed in slash
characters (i.e. using the `/pattern/` syntax). Regular expressions
defined in the OpenAPI declaration via the `pattern` attribute follow
[ECMA 262](https://262.ecma-international.org/5.1/#sec-15.10.1) which
means they already include the correct escaping of forward slashes as
far as Ruby is concerned.

The current Ruby codegen is incorrectly escaping all forward slashes,
which ultimately causes the generated code to include additional
incorrect escape sequences which cause the generated file to have an
invalid syntax.

This commit ports the same fix introduced in OpenAPITools#1539 for the Python
codegen, as both Ruby and Python use perl-flavored regular expressions
so they behave in the same way when it comes to escaping forward
slashes.

Fixes OpenAPITools#5582.
@wing328
Copy link
Member

wing328 commented Sep 4, 2023

@ivgiuliani thanks for the PR. I'll take a look tomorrow.

@wing328
Copy link
Member

wing328 commented Sep 5, 2023

Let's go with what you've so far. Thanks for the PR.

@wing328 wing328 merged commit 065b481 into OpenAPITools:master Sep 5, 2023
16 checks passed
@wing328 wing328 added this to the 7.0.1 milestone Sep 5, 2023
@ivgiuliani ivgiuliani deleted the fix-ruby-forward-slash-escape branch September 19, 2023 16:30
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.

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