swagger-api / swagger-api/swagger-codegen
[RUBY] Bug generating models with property name `hash`
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Ruby client produces an infinitely recursive MyModel#hash attribute if a property exists by the name hash. This produces a SystemStackError exception when accessing MyModel.new.hash or any attempts at serializing or calling to_hash on it.
Given the below minimal Petstore example, this is the auto-genned implementation, you can see it calls itself forever:
# ...
# Calculates hash code according to all attributes.
# @return [Fixnum] Hash code
def hash
[id, pet_id, quantity, ship_date, status, complete, hash].hash
end
# ...
Swagger-codegen version
2.4.5 via the cURL-based online generator
Swagger declaration file content or url
{
"swagger": "2.0",
"definitions": {
"Order": {
"type": "object",
"properties": {
"hash": {
"type": "string",
"description": "An opaque, idempotent representation of an order"
}
}
}
}
}
Gist of the original petstore example but with hash attribute added to Order model:
https://gist.github.com/f3ndot/3567cf31bd72db8011eb78d33715385e
Command line used for generation
curl -X POST -H "content-type:application/json" -d '{"swaggerUrl":"https://gist.githubusercontent.com/f3ndot/3567cf31bd72db8011eb78d33715385e/raw/fcdade14e206525f5e020197a1d66896070f6437/petstore-swagger-plus-hash-property.json"}' https://generator.swagger.io/api/gen/clients/ruby
Steps to reproduce
- Run above command
- Download library
bundle && bundle exec irb- Run in Irb:
require 'swagger_client'; SwaggerClient::Order.new.hash
2.5.5 :001 > require 'swagger_client'; SwaggerClient::Order.new.hash
Traceback (most recent call last):
16: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
15: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
14: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
13: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
12: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
11: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
10: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
9: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
8: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
7: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
6: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
5: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
4: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
3: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
2: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
1: from /Users/cchawn/Downloads/ruby-client/lib/swagger_client/models/order.rb:168:in `hash'
SystemStackError (stack level too deep)
Related issues/PRs
https://github.com/swagger-api/swagger-codegen/issues/4236
Suggest a fix/enhancement
Don't define a #hash if model already defines a property. Ruby Objects expect or seem to be ok if Object#hash is overridden for business logic purposes:
class Foo; attr_accessor :hash; end
f = Foo.new
f.hash = 'aaaa'
f.hash
# => "aaaa"
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
Reproduce the failure with SwaggerClient::Order.new.hash and inspect the generated lib/swagger_client/models/order.rb at the reported hash method around line 168. Trace that output back to the Ruby model template, then verify that a property named hash no longer recurses and that serialization and to_hash work without SystemStackError.
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
- 42/100