[BUG] Generated Ruby client contains syntax errors when option for enum includes single-quote
Created by: autopp
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? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
When enum
contains item with odd number of single-quote, the generated Ruby client code contains syntax errors.
openapi-generator version
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar version
4.0.0-SNAPSHOT
$ git log --oneline -n 1
dd3e6c6a3b (HEAD -> master, origin/master, origin/HEAD, autopp/master) Use actual value of generateSourceCodeOnly in python generator. (#2015)
OpenAPI declaration file content or url
See: https://gist.github.com/autopp/f1c6f438a050148cefe3d6cbbc0f8a81
The point to notice in this example is that '
is included in two enum
s.
Command line used for generation
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate --skip-validate-spec -g ruby -o /tmp/gem -i 'https://gist.githubusercontent.com/autopp/f1c6f438a050148cefe3d6cbbc0f8a81/raw/4c8cce86d5b258ec08490b2aa28e59400c854150/swagger.yml'
Steps to reproduce
- Run the above command to generate client code for Ruby.
- Run
ruby -c /tmp/gem/lib/openapi_client/models/x.rb
andruby -c /tmp/gem/lib/openapi_client/api/default_api.rb
to check syntax of the generated model code. - Get errors in
lib/openapi_client/models/x.rb
andlib/openapi_client/api/default_api.rb
:/tmp/gem/lib/openapi_client/models/x.rb:86: syntax error, unexpected tCONSTANT, expecting ']' ...mAttributeValidator.new('String', [''']) ... ^~~~~~ /tmp/gem/lib/openapi_client/models/x.rb:86: syntax error, unexpected ']', expecting keyword_end ...teValidator.new('String', [''']) ... ^ /tmp/gem/lib/openapi_client/api/default_api.rb:37: syntax error, unexpected tIDENTIFIER, expecting ']' ...] && !['''].include?(opts[:'foo']) ... ^~~ /tmp/gem/lib/openapi_client/api/default_api.rb:38: syntax error, unexpected tIDENTIFIER, expecting keyword_end ... fail ArgumentError, 'invalid value for "foo", must be one... ... ^~~~~~~ /tmp/gem/lib/openapi_client/api/default_api.rb:38: syntax error, unexpected ',', expecting &. or :: or '[' or '.' ...rror, 'invalid value for "foo", must be one of '' ... ^ /tmp/gem/lib/openapi_client/api/default_api.rb:65: Invalid return in class/module body return data, status_code, headers ^~~~~~ /tmp/gem/lib/openapi_client/api/default_api.rb:111: syntax error, unexpected keyword_end, expecting end-of-input
Related issues/PRs
Suggest a fix
(It is the same as #2069 (closed))
escapeText
of RubyClientCodegen
seems to escape for double-quote string literals only. However, the corresponding template uses a single quote string.
I think it is difficult for the generator to determine whether the expansion destination is a single-quote string or a double-quote string. Instead, I think that it is better to unify it with a double-quote string in the template.