diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index 1091ab5174a..35742384c26 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -43,6 +43,19 @@ module {{moduleName}} @{{{name}}} = {{{name}}} end {{/isEnum}}{{/vars}} + def ==(o) + return true if self.equal?(o) + self.class == o.class{{#vars}} && + {{name}} == o.{{name}}{{/vars}} + end + + def eql?(o) + self == o + end + + def hash + [{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash + end end {{/model}} {{/models}} diff --git a/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java index 0dd8b310df9..c0bee9328d5 100644 --- a/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/default/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -179,19 +179,25 @@ public class PetApiTest { Pet pet1 = new Pet(); Pet pet2 = new Pet(); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); pet2.setName("really-happy"); pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertFalse(pet1.equals(pet2)); - assertFalse(pet1.equals(pet2)); + assertFalse(pet2.equals(pet1)); assertFalse(pet1.hashCode() == (pet2.hashCode())); + assertTrue(pet2.equals(pet2)); + assertTrue(pet2.hashCode() == pet2.hashCode()); pet1.setName("really-happy"); pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); } diff --git a/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java index 0dd8b310df9..c0bee9328d5 100644 --- a/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/jersey2/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -179,19 +179,25 @@ public class PetApiTest { Pet pet1 = new Pet(); Pet pet2 = new Pet(); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); pet2.setName("really-happy"); pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertFalse(pet1.equals(pet2)); - assertFalse(pet1.equals(pet2)); + assertFalse(pet2.equals(pet1)); assertFalse(pet1.hashCode() == (pet2.hashCode())); + assertTrue(pet2.equals(pet2)); + assertTrue(pet2.hashCode() == pet2.hashCode()); pet1.setName("really-happy"); pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); } diff --git a/samples/client/petstore/java/okhttp-gson/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/okhttp-gson/src/test/java/io/swagger/petstore/test/PetApiTest.java index b0997a86335..b6d8286d7ef 100644 --- a/samples/client/petstore/java/okhttp-gson/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/okhttp-gson/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -277,19 +277,25 @@ public class PetApiTest { Pet pet1 = new Pet(); Pet pet2 = new Pet(); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); pet2.setName("really-happy"); pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertFalse(pet1.equals(pet2)); - assertFalse(pet1.equals(pet2)); + assertFalse(pet2.equals(pet1)); assertFalse(pet1.hashCode() == (pet2.hashCode())); + assertTrue(pet2.equals(pet2)); + assertTrue(pet2.hashCode() == pet2.hashCode()); pet1.setName("really-happy"); pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); } diff --git a/samples/client/petstore/java/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java index 51d84c5cff4..1ff1dbb10e2 100644 --- a/samples/client/petstore/java/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/retrofit/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -148,19 +148,25 @@ public class PetApiTest { Pet pet1 = new Pet(); Pet pet2 = new Pet(); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); pet2.setName("really-happy"); pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertFalse(pet1.equals(pet2)); - assertFalse(pet1.equals(pet2)); + assertFalse(pet2.equals(pet1)); assertFalse(pet1.hashCode() == (pet2.hashCode())); + assertTrue(pet2.equals(pet2)); + assertTrue(pet2.hashCode() == pet2.hashCode()); pet1.setName("really-happy"); pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); } diff --git a/samples/client/petstore/java/retrofit2/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/retrofit2/src/test/java/io/swagger/petstore/test/PetApiTest.java index 309914b434e..8506fc08f5c 100644 --- a/samples/client/petstore/java/retrofit2/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/retrofit2/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -147,19 +147,25 @@ public class PetApiTest { Pet pet1 = new Pet(); Pet pet2 = new Pet(); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); pet2.setName("really-happy"); pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertFalse(pet1.equals(pet2)); - assertFalse(pet1.equals(pet2)); + assertFalse(pet2.equals(pet1)); assertFalse(pet1.hashCode() == (pet2.hashCode())); + assertTrue(pet2.equals(pet2)); + assertTrue(pet2.hashCode() == pet2.hashCode()); pet1.setName("really-happy"); pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); assertTrue(pet1.equals(pet2)); - assertTrue(pet1.equals(pet2)); + assertTrue(pet2.equals(pet1)); + assertTrue(pet1.hashCode() == pet2.hashCode()); + assertTrue(pet1.equals(pet1)); assertTrue(pet1.hashCode() == pet1.hashCode()); } diff --git a/samples/client/petstore/ruby/lib/petstore/models/category.rb b/samples/client/petstore/ruby/lib/petstore/models/category.rb index 1e36eac4147..ab5b9cabbaa 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/category.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/category.rb @@ -41,5 +41,19 @@ module Petstore end + def ==(o) + return true if self.equal?(o) + self.class == o.class && + id == o.id && + name == o.name + end + + def eql?(o) + self == o + end + + def hash + [id, name].hash + end end end diff --git a/samples/client/petstore/ruby/lib/petstore/models/order.rb b/samples/client/petstore/ruby/lib/petstore/models/order.rb index ab6f8f00189..74eab3436e3 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/order.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/order.rb @@ -81,5 +81,23 @@ module Petstore @status = status end + def ==(o) + return true if self.equal?(o) + self.class == o.class && + id == o.id && + pet_id == o.pet_id && + quantity == o.quantity && + ship_date == o.ship_date && + status == o.status && + complete == o.complete + end + + def eql?(o) + self == o + end + + def hash + [id, pet_id, quantity, ship_date, status, complete].hash + end end end diff --git a/samples/client/petstore/ruby/lib/petstore/models/pet.rb b/samples/client/petstore/ruby/lib/petstore/models/pet.rb index a208cf3f87c..8c5fe6e17e7 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/pet.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/pet.rb @@ -85,5 +85,23 @@ module Petstore @status = status end + def ==(o) + return true if self.equal?(o) + self.class == o.class && + id == o.id && + category == o.category && + name == o.name && + photo_urls == o.photo_urls && + tags == o.tags && + status == o.status + end + + def eql?(o) + self == o + end + + def hash + [id, category, name, photo_urls, tags, status].hash + end end end diff --git a/samples/client/petstore/ruby/lib/petstore/models/tag.rb b/samples/client/petstore/ruby/lib/petstore/models/tag.rb index 3fb5e1f969c..51439de5846 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/tag.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/tag.rb @@ -41,5 +41,19 @@ module Petstore end + def ==(o) + return true if self.equal?(o) + self.class == o.class && + id == o.id && + name == o.name + end + + def eql?(o) + self == o + end + + def hash + [id, name].hash + end end end diff --git a/samples/client/petstore/ruby/lib/petstore/models/user.rb b/samples/client/petstore/ruby/lib/petstore/models/user.rb index 3a6d5f354e0..3d20ab95c4f 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/user.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/user.rb @@ -89,5 +89,25 @@ module Petstore end + def ==(o) + return true if self.equal?(o) + self.class == o.class && + id == o.id && + username == o.username && + first_name == o.first_name && + last_name == o.last_name && + email == o.email && + password == o.password && + phone == o.phone && + user_status == o.user_status + end + + def eql?(o) + self == o + end + + def hash + [id, username, first_name, last_name, email, password, phone, user_status].hash + end end end diff --git a/samples/client/petstore/ruby/spec/pet_spec.rb b/samples/client/petstore/ruby/spec/pet_spec.rb index c9858eca37a..612fc536147 100644 --- a/samples/client/petstore/ruby/spec/pet_spec.rb +++ b/samples/client/petstore/ruby/spec/pet_spec.rb @@ -125,5 +125,40 @@ describe "Pet" do result = @pet_api.upload_file(10002, file: File.new('hello.txt'), additional_metadata: 'metadata') result.should be_nil end + + it "should implement eql? and hash" do + pet1 = Petstore::Pet.new + pet2 = Petstore::Pet.new + pet1.should == pet2 + pet2.should == pet1 + pet1.eql?(pet2).should == true + pet2.eql?(pet1).should == true + pet1.hash.should == pet2.hash + pet1.should == pet1 + pet1.eql?(pet1).should == true + pet1.hash.should == pet1.hash + + pet1.name = 'really-happy' + pet1.photo_urls = ['http://foo.bar.com/1', 'http://foo.bar.com/2'] + pet1.should_not == pet2 + pet2.should_not == pet1 + pet1.eql?(pet2).should == false + pet2.eql?(pet1).should == false + pet1.hash.should_not == pet2.hash + pet1.should == pet1 + pet1.eql?(pet1).should == true + pet1.hash.should == pet1.hash + + pet2.name = 'really-happy' + pet2.photo_urls = ['http://foo.bar.com/1', 'http://foo.bar.com/2'] + pet1.should == pet2 + pet2.should == pet1 + pet1.eql?(pet2).should == true + pet2.eql?(pet1).should == true + pet1.hash.should == pet2.hash + pet2.should == pet2 + pet2.eql?(pet2).should == true + pet2.hash.should == pet2.hash + end end end