swagger-api / swagger-api/swagger-codegen

[Ruby] Generated client may contains unexpected interpolation when using pattern and/or enum

Open
#9,093 0 comments 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 using pattern and/or enum, the generated Ruby client code may contain an unexpected interpolation.

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.

Command line used for generation
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i 'https://gist.githubusercontent.com/autopp/925901844b9c973fd3c86d06ea95c03a/raw/df474dc0331ee167d337a2cc7aadc49afdd7ee03/swagger.yml' -l ruby -o /tmp/gem
Steps to reproduce
  1. Run the above command to generate client code for Ruby.
  2. The generated lib/swagger_client/api/default_api.rb contains unexpected interpolation:
def foo_get_with_http_info(opts = {})
  if @api_client.config.debugging
    @api_client.config.logger.debug 'Calling API: DefaultApi.foo_get ...'
  end
  if @api_client.config.client_side_validation && !opts[:'x'].nil? && opts[:'x'] !~ Regexp.new(/^injected code: #{injected.code}$/)
    fail ArgumentError, "invalid value for 'opts[:\"x\"]' when calling DefaultApi.foo_get, must conform to the pattern /^injected code: #{injected.code}$/."
  end

In this example, injected.code is executed when request to GET /foo (And error may occur).

Related issues/PRs

#9091 and #9092 are similar probrems.

Suggest a fix/enhancement

Curretly, RubyClientCodegen dose not override escapeText.
Should not this be overridden to accommodate Ruby's string literal characteristics?

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 at RubyClientCodegen and inspect how escapeText is handled during Ruby client generation. Reproduce the issue with the provided Swagger URL and generation command, then verify that a pattern containing #{...} remains literal in lib/swagger_client/api/default_api.rb and does not interpolate when the request runs.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.