swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[RUBY] Bug generating models with property name `hash`

Open cchawn opened this issue 7 years ago • 0 comments

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
  1. Run above command
  2. Download library
  3. bundle && bundle exec irb
  4. 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"

cchawn avatar Jun 07 '19 20:06 cchawn