Skip to content

Commit 2a17134

Browse files
authored
Add equals and hashCode methods to Microprofile (#20011)
* Add equals and hashCode methods to microprofile * Add missing import * Update samples * Add missing dependency for reflection equals
1 parent 824f864 commit 2a17134

File tree

52 files changed

+884
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+884
-33
lines changed

bin/configs/java-microprofile-rest-client-3.0-jackson.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ additionalProperties:
99
configKey: petstore
1010
microprofileRestClientVersion: "3.0"
1111
hideGenerationTimestamp: true
12+
useReflectionEqualsHashCode: true

modules/openapi-generator/src/main/resources/Java/libraries/microprofile/model.mustache

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{{>licenseInfo}}
22
package {{package}};
33

4+
{{#useReflectionEqualsHashCode}}
5+
import org.apache.commons.lang3.builder.EqualsBuilder;
6+
import org.apache.commons.lang3.builder.HashCodeBuilder;
7+
{{/useReflectionEqualsHashCode}}
8+
import java.util.Objects;
9+
import java.util.Arrays;
410
{{#imports}}import {{import}};
511
{{/imports}}
612
{{#serializableModel}}

modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache

+1-24
Original file line numberDiff line numberDiff line change
@@ -148,28 +148,5 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}}{{#vendorExtensi
148148
{{/isReadOnly}}
149149

150150
{{/vars}}
151-
152-
/**
153-
* Create a string representation of this pojo.
154-
*/
155-
@Override
156-
public String toString() {
157-
StringBuilder sb = new StringBuilder();
158-
sb.append("class {{classname}} {\n");
159-
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}}
160-
{{#vars}}sb.append(" {{name}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{name}}){{/isPassword}}).append("\n");
161-
{{/vars}}sb.append("}");
162-
return sb.toString();
163-
}
164-
165-
/**
166-
* Convert the given object to string with each line indented by 4 spaces
167-
* (except the first line).
168-
*/
169-
private static String toIndentedString(Object o) {
170-
if (o == null) {
171-
return "null";
172-
}
173-
return o.toString().replace("\n", "\n ");
174-
}
151+
{{>pojoOverrides}}
175152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@Override
2+
public boolean equals(Object o) {
3+
{{#useReflectionEqualsHashCode}}
4+
return EqualsBuilder.reflectionEquals(this, o, false, null, true);
5+
{{/useReflectionEqualsHashCode}}
6+
{{^useReflectionEqualsHashCode}}
7+
if (this == o) {
8+
return true;
9+
}
10+
if (o == null || getClass() != o.getClass()) {
11+
return false;
12+
}{{#hasVars}}
13+
{{classname}} {{classVarName}} = ({{classname}}) o;
14+
return {{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}equalsNullable(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}} &&
15+
{{/-last}}{{/vars}}{{#parent}} &&
16+
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
17+
return {{#parent}}super.equals(o){{/parent}}{{^parent}}true{{/parent}};{{/hasVars}}
18+
{{/useReflectionEqualsHashCode}}
19+
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}}
20+
21+
private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) {
22+
return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get()));
23+
}{{/vendorExtensions.x-jackson-optional-nullable-helpers}}
24+
25+
@Override
26+
public int hashCode() {
27+
{{#useReflectionEqualsHashCode}}
28+
return HashCodeBuilder.reflectionHashCode(this);
29+
{{/useReflectionEqualsHashCode}}
30+
{{^useReflectionEqualsHashCode}}
31+
return Objects.hash({{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
32+
{{/useReflectionEqualsHashCode}}
33+
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}}
34+
35+
private static <T> int hashCodeNullable(JsonNullable<T> a) {
36+
if (a == null) {
37+
return 1;
38+
}
39+
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
40+
}{{/vendorExtensions.x-jackson-optional-nullable-helpers}}
41+
42+
/**
43+
* Create a string representation of this pojo.
44+
*/
45+
@Override
46+
public String toString() {
47+
StringBuilder sb = new StringBuilder();
48+
sb.append("class {{classname}} {\n");
49+
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}}
50+
{{#vars}}sb.append(" {{name}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{name}}){{/isPassword}}).append("\n");
51+
{{/vars}}sb.append("}");
52+
return sb.toString();
53+
}
54+
55+
/**
56+
* Convert the given object to string with each line indented by 4 spaces
57+
* (except the first line).
58+
*/
59+
private static String toIndentedString(Object o) {
60+
if (o == null) {
61+
return "null";
62+
}
63+
return o.toString().replace("\n", "\n ");
64+
}

modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom.mustache

+13-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@
196196
<version>${mutiny.version}</version>
197197
</dependency>
198198
{{/microprofileMutiny}}
199+
{{#useReflectionEqualsHashCode}}
200+
<!-- For equals and hashCode using reflection -->
201+
<dependency>
202+
<groupId>org.apache.commons</groupId>
203+
<artifactId>commons-lang3</artifactId>
204+
<version>${commons.lang3.version}</version>
205+
</dependency>
206+
{{/useReflectionEqualsHashCode}}
199207
</dependencies>
200208
<repositories>
201209
<repository>
@@ -210,6 +218,8 @@
210218
<java.version>1.8</java.version>
211219
<maven.compiler.source>${java.version}</maven.compiler.source>
212220
<maven.compiler.target>${java.version}</maven.compiler.target>
221+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
222+
213223
<swagger.core.version>1.5.18</swagger.core.version>
214224
<jetty.version>9.2.9.v20150224</jetty.version>
215225
<junit.version>5.10.2</junit.version>
@@ -238,9 +248,11 @@
238248
<jandex.maven.plugin.version>1.1.0</jandex.maven.plugin.version>
239249
<maven.failsafe.plugin.version>2.6</maven.failsafe.plugin.version>
240250
<build.helper.maven.plugin.version>1.9.1</build.helper.maven.plugin.version>
241-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
242251
{{#microprofileMutiny}}
243252
<mutiny.version>1.2.0</mutiny.version>
244253
{{/microprofileMutiny}}
254+
{{#useReflectionEqualsHashCode}}
255+
<commons.lang3.version>3.17.0</commons.lang3.version>
256+
{{/useReflectionEqualsHashCode}}
245257
</properties>
246258
</project>

modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom_3.0.mustache

+13-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@
189189
<version>${jakarta.annotation.version}</version>
190190
<scope>provided</scope>
191191
</dependency>
192+
{{#useReflectionEqualsHashCode}}
193+
<!-- For equals and hashCode using reflection -->
194+
<dependency>
195+
<groupId>org.apache.commons</groupId>
196+
<artifactId>commons-lang3</artifactId>
197+
<version>${commons.lang3.version}</version>
198+
</dependency>
199+
{{/useReflectionEqualsHashCode}}
192200
</dependencies>
193201
<repositories>
194202
<repository>
@@ -203,6 +211,8 @@
203211
<java.version>11</java.version>
204212
<maven.compiler.source>${java.version}</maven.compiler.source>
205213
<maven.compiler.target>${java.version}</maven.compiler.target>
214+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
215+
206216
<swagger.core.version>1.5.18</swagger.core.version>
207217
<jetty.version>9.2.9.v20150224</jetty.version>
208218
<junit.version>5.10.2</junit.version>
@@ -231,6 +241,8 @@
231241
<jandex.maven.plugin.version>1.1.0</jandex.maven.plugin.version>
232242
<maven.failsafe.plugin.version>2.6</maven.failsafe.plugin.version>
233243
<build.helper.maven.plugin.version>1.9.1</build.helper.maven.plugin.version>
234-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
244+
{{#useReflectionEqualsHashCode}}
245+
<commons.lang3.version>3.17.0</commons.lang3.version>
246+
{{/useReflectionEqualsHashCode}}
235247
</properties>
236248
</project>

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@
152152
<java.version>11</java.version>
153153
<maven.compiler.source>${java.version}</maven.compiler.source>
154154
<maven.compiler.target>${java.version}</maven.compiler.target>
155+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
156+
155157
<swagger.core.version>1.5.18</swagger.core.version>
156158
<jetty.version>9.2.9.v20150224</jetty.version>
157159
<junit.version>5.10.2</junit.version>
@@ -175,6 +177,5 @@
175177
<jandex.maven.plugin.version>1.1.0</jandex.maven.plugin.version>
176178
<maven.failsafe.plugin.version>2.6</maven.failsafe.plugin.version>
177179
<build.helper.maven.plugin.version>1.9.1</build.helper.maven.plugin.version>
178-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
179180
</properties>
180181
</project>

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Category.java

+19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package org.openapitools.client.model;
1414

15+
import java.util.Objects;
16+
import java.util.Arrays;
1517
import com.fasterxml.jackson.annotation.JsonInclude;
1618
import com.fasterxml.jackson.annotation.JsonProperty;
1719
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -101,6 +103,23 @@ public Category name(String name) {
101103
return this;
102104
}
103105

106+
@Override
107+
public boolean equals(Object o) {
108+
if (this == o) {
109+
return true;
110+
}
111+
if (o == null || getClass() != o.getClass()) {
112+
return false;
113+
}
114+
Category category = (Category) o;
115+
return Objects.equals(this.id, category.id) &&
116+
Objects.equals(this.name, category.name);
117+
}
118+
119+
@Override
120+
public int hashCode() {
121+
return Objects.hash(id, name);
122+
}
104123

105124
/**
106125
* Create a string representation of this pojo.

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/ModelApiResponse.java

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package org.openapitools.client.model;
1414

15+
import java.util.Objects;
16+
import java.util.Arrays;
1517
import com.fasterxml.jackson.annotation.JsonInclude;
1618
import com.fasterxml.jackson.annotation.JsonProperty;
1719
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -133,6 +135,24 @@ public ModelApiResponse message(String message) {
133135
return this;
134136
}
135137

138+
@Override
139+
public boolean equals(Object o) {
140+
if (this == o) {
141+
return true;
142+
}
143+
if (o == null || getClass() != o.getClass()) {
144+
return false;
145+
}
146+
ModelApiResponse _apiResponse = (ModelApiResponse) o;
147+
return Objects.equals(this.code, _apiResponse.code) &&
148+
Objects.equals(this.type, _apiResponse.type) &&
149+
Objects.equals(this.message, _apiResponse.message);
150+
}
151+
152+
@Override
153+
public int hashCode() {
154+
return Objects.hash(code, type, message);
155+
}
136156

137157
/**
138158
* Create a string representation of this pojo.

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Order.java

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package org.openapitools.client.model;
1414

15+
import java.util.Objects;
16+
import java.util.Arrays;
1517
import com.fasterxml.jackson.annotation.JsonInclude;
1618
import com.fasterxml.jackson.annotation.JsonProperty;
1719
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -265,6 +267,27 @@ public Order complete(Boolean complete) {
265267
return this;
266268
}
267269

270+
@Override
271+
public boolean equals(Object o) {
272+
if (this == o) {
273+
return true;
274+
}
275+
if (o == null || getClass() != o.getClass()) {
276+
return false;
277+
}
278+
Order order = (Order) o;
279+
return Objects.equals(this.id, order.id) &&
280+
Objects.equals(this.petId, order.petId) &&
281+
Objects.equals(this.quantity, order.quantity) &&
282+
Objects.equals(this.shipDate, order.shipDate) &&
283+
Objects.equals(this.status, order.status) &&
284+
Objects.equals(this.complete, order.complete);
285+
}
286+
287+
@Override
288+
public int hashCode() {
289+
return Objects.hash(id, petId, quantity, shipDate, status, complete);
290+
}
268291

269292
/**
270293
* Create a string representation of this pojo.

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Pet.java

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package org.openapitools.client.model;
1414

15+
import java.util.Objects;
16+
import java.util.Arrays;
1517
import com.fasterxml.jackson.annotation.JsonInclude;
1618
import com.fasterxml.jackson.annotation.JsonProperty;
1719
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -293,6 +295,27 @@ public Pet status(StatusEnum status) {
293295
return this;
294296
}
295297

298+
@Override
299+
public boolean equals(Object o) {
300+
if (this == o) {
301+
return true;
302+
}
303+
if (o == null || getClass() != o.getClass()) {
304+
return false;
305+
}
306+
Pet pet = (Pet) o;
307+
return Objects.equals(this.id, pet.id) &&
308+
Objects.equals(this.category, pet.category) &&
309+
Objects.equals(this.name, pet.name) &&
310+
Objects.equals(this.photoUrls, pet.photoUrls) &&
311+
Objects.equals(this.tags, pet.tags) &&
312+
Objects.equals(this.status, pet.status);
313+
}
314+
315+
@Override
316+
public int hashCode() {
317+
return Objects.hash(id, category, name, photoUrls, tags, status);
318+
}
296319

297320
/**
298321
* Create a string representation of this pojo.

samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Tag.java

+19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package org.openapitools.client.model;
1414

15+
import java.util.Objects;
16+
import java.util.Arrays;
1517
import com.fasterxml.jackson.annotation.JsonInclude;
1618
import com.fasterxml.jackson.annotation.JsonProperty;
1719
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -101,6 +103,23 @@ public Tag name(String name) {
101103
return this;
102104
}
103105

106+
@Override
107+
public boolean equals(Object o) {
108+
if (this == o) {
109+
return true;
110+
}
111+
if (o == null || getClass() != o.getClass()) {
112+
return false;
113+
}
114+
Tag tag = (Tag) o;
115+
return Objects.equals(this.id, tag.id) &&
116+
Objects.equals(this.name, tag.name);
117+
}
118+
119+
@Override
120+
public int hashCode() {
121+
return Objects.hash(id, name);
122+
}
104123

105124
/**
106125
* Create a string representation of this pojo.

0 commit comments

Comments
 (0)