swagger-api / swagger-api/swagger-codegen

[RUBY] Bug generating aliases of Array

Open
#5,854 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Ruby Issue: Bug Swagger-Parser
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have an API definition that includes a type that is just an array:

"FooArray": {
  "type": "array",
  "items": {
    "$ref": "#/definitions/Foo"
  }
}

In the Ruby generator, responses are deserialized based on the name of their type. If the type is not an obvious one (String, Integer, Float, ...) it proceeds to deserialize it as an object. This is the case here as the class is FooArray (as opposed to Array).

The Ruby generator has it so that objects are first created empty, then populated using build_from_hash. The first thing that this method does is check that we are indeed building from a hash:

def build_from_hash(attributes)
  return nil unless attributes.is_a?(Hash)
  # ...
end

This is always done, and can be seen in the template at modules/swagger-codegen/src/main/resources/ruby/base_object.mustache.

Therefore this behaviour is in conflict with the type shown above. The type is an alias for array. When the generator is figuring out how to convert it, sees it as FooArray and decides it needs to be deserialized as an object. Unfortunately then it expects it will identify as an object/hash, which it doesn't, and returns nil.

Swagger-codegen version

2.2.2

Command line used for generation

swagger-codegen generate --input-spec api-docs.json --lang ruby --output dist

Steps to reproduce
  1. In your Swagger definition, define a type that is an alias for array.
  2. In your Swagger definition, define an endpoint that returns said type.
  3. Generate Ruby code
Suggest a Fix

Instead of this behaviour, I would expect the generator to notice that the type is just an alias for Array, and proceed to parse it as such.

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 modules/swagger-codegen/src/main/resources/ruby/base_object.mustache and the Ruby generator's response deserialization path. Run the supplied swagger-codegen command using an API definition with an array alias and an endpoint returning it; done means the generated client deserializes the alias as an Array instead of returning nil.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.