swagger-api / swagger-api/swagger-codegen
[Ruby] generated client contains syntax errors when enum includes single-quote
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When enum contains item single-quote, the generated Ruby client code contains syntax errors.
Swagger-codegen version
$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar version
2.4.2-SNAPSHOT
$ git log --oneline -n 1
c4c5c062c Merge pull request #9080 from swagger-api/update-parser-v1
Swagger 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 enums.
Command line used for generation
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i 'https://gist.githubusercontent.com/autopp/f1c6f438a050148cefe3d6cbbc0f8a81/raw/4c8cce86d5b258ec08490b2aa28e59400c854150/swagger.yml' -l ruby -o /tmp/gem
Steps to reproduce
- Run the above command to generate client code for Ruby.
- Run
ruby -c /tmp/gem/lib/swagger_client/api/default_api.rbandruby -c /tmp/gem/lib/swagger_client/models/x.rbto check syntax of generated codes. - Get errors in
lib/swagger_client/models/x.rbandlib/swagger_client/api_client.rb:
/tmp/gem/lib/swagger_client/api/default_api.rb:37: syntax error, unexpected tIDENTIFIER, expecting ']'
...] && !['''].include?(opts[:'foo'])
... ^~~
/tmp/gem/lib/swagger_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/swagger_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/swagger_client/api/default_api.rb:65: Invalid return in class/module body
return data, status_code, headers
^~~~~~
/tmp/gem/lib/swagger_client/api/default_api.rb:109: syntax error, unexpected keyword_end, expectingend-of-input
/tmp/gem/lib/swagger_client/models/x.rb:86: syntax error, unexpected tCONSTANT, expecting ']'
...mAttributeValidator.new('String', ["'"])
... ^~~~~~
/tmp/gem/lib/swagger_client/models/x.rb:88: syntax error, unexpected tIDENTIFIER, expecting keyword_end
...entError, 'invalid value for "y", must be one of #{validator...
... ^
/tmp/gem/lib/swagger_client/models/x.rb:125: syntax error, unexpected keyword_elsif, expecting keyword_end
elsif !attributes[self.class.attri...
^~~~~
/tmp/gem/lib/swagger_client/models/x.rb:131: syntax error, unexpected keyword_end, expecting end-of-input
end
^~~
Related issues/PRs
#9091 is similar probrem.
Suggest a fix/enhancement
Current escapeText of RubyClientCodegen does not seem to be for single quote strings.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating RubyClientCodegen and its current escapeText implementation, then reproduce the issue with the linked Swagger declaration and the documented generate command. Run ruby -c on the generated default_api.rb, x.rb, and api_client.rb files; done means enum values containing a single quote no longer produce Ruby syntax errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100