swagger-api / swagger-api/swagger-codegen

[Ruby] generated client contains syntax errors when pattern of properties includes single-quote

Open
#9,091 1 comment 0 reactions 0 assignees View on GitHub

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 has an option including an odd number of single quotes, 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/e707d5d2ec36f7cfe6a3950d76d54b6f#file-swagger-yml

The point to notice in this example is that ' is included in the pattern constraint of the property y of the model x.

Command line used for generation
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i '' -l ruby -o /tmp/gem
Steps to reproduce
  1. Run the above command to generate client code for Ruby.
  2. Run ruby -c /tmp/gem/lib/swagger_client/models/x.rb to check syntax of the generated model code.
  3. Get errors in lib/swagger_client/models/x.rb:
/tmp/gem/lib/swagger_client/models/x.rb:51: syntax error, unexpected '.'
...ust conform to the pattern /'/.')
...                              ^
/tmp/gem/lib/swagger_client/models/x.rb:60: syntax error, unexpected ')'
...y.nil? && @y !~ Regexp.new(/'/)
...                              ^
/tmp/gem/lib/swagger_client/models/x.rb:68: syntax error, unexpected '.'
...ust conform to the pattern /'/.'
...                              ^
/tmp/gem/lib/swagger_client/models/x.rb:198: unterminated string meets end of file
/tmp/gem/lib/swagger_client/models/x.rb:198: syntax error, unexpected end-of-input, expecting keyword_end
Related issues/PRs

#9092 is similar probrem.

Suggest a fix/enhancement

escapeText of RubyClientCodegen seems to escape for double-quote string literals.
However, the corresponding template uses a single quote string. (Ruby allows both styles)
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.

I'll try and get a PR going.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with RubyClientCodegen.escapeText and the Ruby model template that generates x.rb. Reproduce the issue using the provided Swagger declaration and generation command, then run ruby -c on the generated model; done means a pattern containing a single quote produces syntactically valid Ruby.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.