Skip to content

Commit 3e7f297

Browse files
committed
Fix equalsAndHashCode generation (#12)
1 parent 1bf1e35 commit 3e7f297

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

src/main/resources/templates/javaClassGraphqlType.ftl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@ public class ${className} <#if implements?has_content>implements <#list implemen
4141
if (this == obj) {
4242
return true;
4343
}
44-
if (that == null || getClass() != obj.getClass()) {
44+
if (obj == null || getClass() != obj.getClass()) {
4545
return false;
4646
}
47-
final ${className} that = (FormField) obj;
48-
return <#list fields as field>Objects.equals(${field.name}, that.${field.name}) <#if field_has_next>
47+
final ${className} that = (${className}) obj;
48+
return <#list fields as field>Objects.equals(${field.name}, that.${field.name})<#if field_has_next>
4949
&& </#if></#list>;
5050
}
5151

5252
@Override
5353
public int hashCode() {
54-
return Objects.hash(
55-
<#list fields as field>
56-
${field.name}<#if field_has_next>, </#if>
57-
</#list>;
54+
<#if fields?has_content>
55+
return Objects.hash(<#list fields as field>${field.name}<#if field_has_next>, </#if></#list>);
56+
<#else>
57+
return 0;
58+
</#if>
5859
}
5960
</#if>
6061
}

src/test/java/com/kobylynskyi/graphql/codegen/GraphqlCodegenTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ void generate_EqualsAndHashCode() throws Exception {
217217
mappingConfig.setGenerateEqualsAndHashCode(true);
218218
mappingConfig.setModelNameSuffix("TO");
219219

220-
221220
generator.generate();
222221

223222
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
@@ -239,7 +238,10 @@ void generate_EqualsAndHashCode() throws Exception {
239238
}
240239
}
241240

242-
241+
assertEquals(Utils.getFileContent("src/test/resources/expected-classes/EventPropertyTO_withEqualsAndHashCode.java.txt"),
242+
Utils.getFileContent(Arrays.stream(files)
243+
.filter(f -> f.getName().equals("EventPropertyTO.java"))
244+
.map(File::getPath).findFirst().orElseThrow(FileNotFoundException::new)));
243245
}
244246

245247
@Test
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
import java.util.*;
4+
5+
public class EventPropertyTO {
6+
7+
private Float floatVal;
8+
private Boolean booleanVal;
9+
private Integer intVal;
10+
private String stringVal;
11+
private Collection<EventPropertyTO> child;
12+
private EventTO parent;
13+
14+
public EventPropertyTO() {
15+
}
16+
17+
public EventPropertyTO(Float floatVal, Boolean booleanVal, Integer intVal, String stringVal, Collection<EventPropertyTO> child, EventTO parent) {
18+
this.floatVal = floatVal;
19+
this.booleanVal = booleanVal;
20+
this.intVal = intVal;
21+
this.stringVal = stringVal;
22+
this.child = child;
23+
this.parent = parent;
24+
}
25+
26+
public Float getFloatVal() {
27+
return floatVal;
28+
}
29+
public void setFloatVal(Float floatVal) {
30+
this.floatVal = floatVal;
31+
}
32+
33+
public Boolean getBooleanVal() {
34+
return booleanVal;
35+
}
36+
public void setBooleanVal(Boolean booleanVal) {
37+
this.booleanVal = booleanVal;
38+
}
39+
40+
public Integer getIntVal() {
41+
return intVal;
42+
}
43+
public void setIntVal(Integer intVal) {
44+
this.intVal = intVal;
45+
}
46+
47+
public String getStringVal() {
48+
return stringVal;
49+
}
50+
public void setStringVal(String stringVal) {
51+
this.stringVal = stringVal;
52+
}
53+
54+
public Collection<EventPropertyTO> getChild() {
55+
return child;
56+
}
57+
public void setChild(Collection<EventPropertyTO> child) {
58+
this.child = child;
59+
}
60+
61+
public EventTO getParent() {
62+
return parent;
63+
}
64+
public void setParent(EventTO parent) {
65+
this.parent = parent;
66+
}
67+
68+
@Override
69+
public boolean equals(Object obj) {
70+
if (this == obj) {
71+
return true;
72+
}
73+
if (obj == null || getClass() != obj.getClass()) {
74+
return false;
75+
}
76+
final EventPropertyTO that = (EventPropertyTO) obj;
77+
return Objects.equals(floatVal, that.floatVal)
78+
&& Objects.equals(booleanVal, that.booleanVal)
79+
&& Objects.equals(intVal, that.intVal)
80+
&& Objects.equals(stringVal, that.stringVal)
81+
&& Objects.equals(child, that.child)
82+
&& Objects.equals(parent, that.parent);
83+
}
84+
85+
@Override
86+
public int hashCode() {
87+
return Objects.hash(floatVal, booleanVal, intVal, stringVal, child, parent);
88+
}
89+
}

0 commit comments

Comments
 (0)