Skip to content

Commit

Permalink
Adding the option to generate an equals and hashcode method (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-stewart authored and kobylynskyi committed Sep 24, 2019
1 parent ff14469 commit 1bf1e35
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ Please refer to:
| outputDir | String | None | The output target directory into which code will be generated. |
| apiPackage | String | Empty | Java package for generated api classes (Query, Mutation, Subscription). |
| modelPackage | String | Empty | Java package for generated model classes (type, input, interface, enum, union). |
| generateApis | Boolean | True | Java package for generated model classes (type, input, interface, enum, union). |
| generateApis | Boolean | True | Specifies whether api classes should be generated as well as model classes. |
| customTypesMapping | Map(String,String) | Empty | Can be used to supply custom mappings for scalars. <br/> Supports:<br/> * Map of (GraphqlObjectName.fieldName) to (JavaType) <br/> * Map of (GraphqlType) to (JavaType) |
| customAnnotationsMapping | Map(String,String) | Empty | Can be used to supply custom annotations (serializers) for scalars. <br/> Supports:<br/> * Map of (GraphqlObjectName.fieldName) to (JavaType) <br/> * Map of (GraphqlType) to (JavaType) |
| modelValidationAnnotation | String | @javax.validation.<br>constraints.NotNull | Annotation for mandatory (NonNull) fields. Can be null/empty. |
| modelNamePrefix | String | Empty | Sets the prefix for GraphQL model classes (type, input, interface, enum, union). |
| modelNameSuffix | String | Empty | Sets the suffix for GraphQL model classes (type, input, interface, enum, union). |
| generateEqualsAndHashCode | Boolean | False | Specifies whether generated model classes should have equals and hashCode methods defined. |


### Inspired by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static Map<String, Object> map(MappingConfig mappingConfig, InputObjectTy
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, typeDefinition));
dataModel.put(NAME, typeDefinition.getName());
dataModel.put(FIELDS, InputValueDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getInputValueDefinitions(), typeDefinition.getName()));
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.isGenerateEqualsAndHashCode());
return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
.map(i -> FieldDefinitionToParameterMapper.map(mappingConfig, i.getFieldDefinitions(), i.getName()))
.forEach(allParameters::addAll);
dataModel.put(FIELDS, allParameters);
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.isGenerateEqualsAndHashCode());

return dataModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public final class DataModelFields {
public static final String FIELDS = "fields";
public static final String IMPLEMENTS = "implements";
public static final String OPERATIONS = "operations";
public static final String EQUALS_AND_HASH_CODE = "equalsAndHashCode";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class DefaultMappingConfigValues {

public static final String DEFAULT_VALIDATION_ANNOTATION = "javax.validation.constraints.NotNull";
public static final boolean DEFAULT_GENERATE_APIS = true;
public static final boolean DEFAULT_EQUALS_AND_HASHCODE = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class MappingConfig {
private String modelNamePrefix;
private String modelNameSuffix;
private String modelValidationAnnotation = DefaultMappingConfigValues.DEFAULT_VALIDATION_ANNOTATION;
private boolean generateEqualsAndHashCode = DefaultMappingConfigValues.DEFAULT_EQUALS_AND_HASHCODE;

public void putCustomTypeMappingIfAbsent(String from, String to) {
if (customTypesMapping == null) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/templates/javaClassGraphqlType.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,26 @@ public class ${className} <#if implements?has_content>implements <#list implemen
}

</#list>
<#if equalsAndHashCode>
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (that == null || getClass() != obj.getClass()) {
return false;
}
final ${className} that = (FormField) obj;
return <#list fields as field>Objects.equals(${field.name}, that.${field.name}) <#if field_has_next>
&& </#if></#list>;
}

@Override
public int hashCode() {
return Objects.hash(
<#list fields as field>
${field.name}<#if field_has_next>, </#if>
</#list>;
}
</#if>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.util.stream.Collectors.toList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.fail;

class GraphqlCodegenTest {
Expand Down Expand Up @@ -211,6 +212,36 @@ void generate_CustomModelAndApiPackages() throws Exception {
});
}

@Test
void generate_EqualsAndHashCode() throws Exception {
mappingConfig.setGenerateEqualsAndHashCode(true);
mappingConfig.setModelNameSuffix("TO");


generator.generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertNotEquals(files.length, 0);

for (File eventFile : files) {
if (eventFile.getName().endsWith("TO.java")) {
String content = Utils.getFileContent(eventFile.getPath());

if (content.contains("public interface ") || content.contains("public enum ")) {
continue;
}

assertThat(content,
StringContains.containsString("public boolean equals(Object obj)"));

assertThat(content,
StringContains.containsString("public int hashCode()"));
}
}


}

@Test
void generate_NoSchemas() throws Exception {
generator.setSchemas(Collections.emptyList());
Expand Down

0 comments on commit 1bf1e35

Please sign in to comment.