swagger-api / swagger-api/swagger-codegen
[RUBY] Bug generating aliases of Array
Nobody has claimed this yet.
- 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
- In your Swagger definition, define a type that is an alias for
array. - In your Swagger definition, define an endpoint that returns said type.
- 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
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 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