Skip to content

Commit f2d5ab0

Browse files
committed
Introduce model annotations (for Lombok) #10
1 parent 8e332ed commit f2d5ab0

File tree

8 files changed

+124
-7
lines changed

8 files changed

+124
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Please refer to:
2626
| modelValidationAnnotation | String | @javax.validation.<br>constraints.NotNull | Annotation for mandatory (NonNull) fields. Can be null/empty. |
2727
| modelNamePrefix | String | Empty | Sets the prefix for GraphQL model classes (type, input, interface, enum, union). |
2828
| modelNameSuffix | String | Empty | Sets the suffix for GraphQL model classes (type, input, interface, enum, union). |
29+
| modelAnnotations | String | Empty | Annotations that will be added to model classes (type, input). |
2930
| generateEqualsAndHashCode | Boolean | False | Specifies whether generated model classes should have equals and hashCode methods defined. |
3031
| generateToString | Boolean | False | Specifies whether generated model classes should have toString method defined. |
3132

src/main/java/com/kobylynskyi/graphql/codegen/mapper/InputDefinitionToDataModelMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public static Map<String, Object> map(MappingConfig mappingConfig, InputObjectTy
2828
dataModel.put(PACKAGE, packageName);
2929
dataModel.put(IMPORTS, MapperUtils.getImports(mappingConfig, packageName));
3030
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, typeDefinition));
31+
dataModel.put(ANNOTATIONS, mappingConfig.getModelAnnotations());
3132
dataModel.put(NAME, typeDefinition.getName());
3233
dataModel.put(FIELDS, InputValueDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getInputValueDefinitions(), typeDefinition.getName()));
3334
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
3435
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());
35-
3636
return dataModel;
3737
}
3838

src/main/java/com/kobylynskyi/graphql/codegen/mapper/TypeDefinitionToDataModelMapper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
3333
dataModel.put(PACKAGE, packageName);
3434
dataModel.put(IMPORTS, MapperUtils.getImports(mappingConfig, packageName));
3535
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, typeDefinition));
36-
Set<String> allInterfaces = new LinkedHashSet<>();
37-
allInterfaces.addAll(MapperUtils.getUnionsHavingType(mappingConfig, typeDefinition, document));
36+
dataModel.put(ANNOTATIONS, mappingConfig.getModelAnnotations());
37+
Set<String> allInterfaces = new LinkedHashSet<>(
38+
MapperUtils.getUnionsHavingType(mappingConfig, typeDefinition, document));
3839
typeDefinition.getImplements().stream()
3940
.map(anImplement -> GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, anImplement))
4041
.forEach(allInterfaces::add);
4142
dataModel.put(IMPLEMENTS, allInterfaces);
4243

43-
Set<ParameterDefinition> allParameters = new LinkedHashSet<>();
4444
// Merge attributes from the type and attributes from the interface
45-
allParameters.addAll(FieldDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getFieldDefinitions(), typeDefinition.getName()));
45+
Set<ParameterDefinition> allParameters = new LinkedHashSet<>(FieldDefinitionToParameterMapper
46+
.map(mappingConfig, typeDefinition.getFieldDefinitions(), typeDefinition.getName()));
4647
List<InterfaceTypeDefinition> interfaces = getInterfacesOfType(mappingConfig, typeDefinition, document);
4748
interfaces.stream()
4849
.map(i -> FieldDefinitionToParameterMapper.map(mappingConfig, i.getFieldDefinitions(), i.getName()))
@@ -51,7 +52,6 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
5152
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
5253
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());
5354

54-
5555
return dataModel;
5656
}
5757

src/main/java/com/kobylynskyi/graphql/codegen/model/DataModelFields.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public final class DataModelFields {
1313
public static final String NAME = "name";
1414
public static final String FIELDS = "fields";
1515
public static final String IMPLEMENTS = "implements";
16+
public static final String ANNOTATIONS = "annotations";
1617
public static final String OPERATIONS = "operations";
1718
public static final String EQUALS_AND_HASH_CODE = "equalsAndHashCode";
1819
public static final String TO_STRING = "toString";

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.kobylynskyi.graphql.codegen.model;
22

33
import java.util.HashMap;
4+
import java.util.HashSet;
45
import java.util.Map;
6+
import java.util.Set;
57

68
import lombok.Data;
79

@@ -27,6 +29,11 @@ public class MappingConfig implements Combinable<MappingConfig> {
2729
*/
2830
private Map<String, String> customAnnotationsMapping = new HashMap<>();
2931

32+
/**
33+
* Custom annotations for model classes.
34+
*/
35+
private Set<String> modelAnnotations = new HashSet<>();
36+
3037
private Boolean generateApis;
3138
private String packageName;
3239
private String apiPackageName;
@@ -67,6 +74,11 @@ public void combine(MappingConfig source) {
6774
} else if (this.customAnnotationsMapping == null && source.customAnnotationsMapping != null) {
6875
this.customAnnotationsMapping = source.customAnnotationsMapping;
6976
}
77+
if (this.modelAnnotations != null && source.modelAnnotations != null) {
78+
this.modelAnnotations.addAll(source.modelAnnotations);
79+
} else if (this.modelAnnotations == null && source.modelAnnotations != null) {
80+
this.modelAnnotations = source.modelAnnotations;
81+
}
7082
this.generateApis = source.generateApis != null ? source.generateApis : this.generateApis;
7183
this.packageName = source.packageName != null ? source.packageName : this.packageName;
7284
this.apiPackageName = source.apiPackageName != null ? source.apiPackageName : this.apiPackageName;
@@ -76,7 +88,6 @@ public void combine(MappingConfig source) {
7688
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
7789
this.generateEqualsAndHashCode = source.generateEqualsAndHashCode != null ? source.generateEqualsAndHashCode : this.generateEqualsAndHashCode;
7890
this.generateToString = source.generateToString != null ? source.generateToString : this.generateToString;
79-
8091
}
8192
}
8293
}

src/main/resources/templates/javaClassGraphqlType.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package ${package};
66
import ${import}.*;
77
</#list>
88

9+
<#list annotations as annotation>
10+
@${annotation}
11+
</#list>
912
public class ${className} <#if implements?has_content>implements <#list implements as interface>${interface}<#if interface_has_next>, </#if></#list></#if>{
1013

1114
<#list fields as field>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ void generate_CustomAnnotationMappings() throws Exception {
143143
System.lineSeparator() + " private org.joda.time.DateTime createdDateTime;"));
144144
}
145145

146+
@Test
147+
void generate_ModelAnnotations() throws Exception {
148+
mappingConfig.setModelAnnotations(new HashSet<>(Arrays.asList("lombok.Builder", "lombok.Data")));
149+
150+
generator.generate();
151+
152+
File eventFile = Arrays.stream(Objects.requireNonNull(outputJavaClassesDir.listFiles()))
153+
.filter(file -> file.getName().equalsIgnoreCase("Event.java"))
154+
.findFirst().orElseThrow(FileNotFoundException::new);
155+
assertEquals(Utils.getFileContent(new File("src/test/resources/expected-classes/Event_with_lombok_annotations.java.txt").getPath()),
156+
Utils.getFileContent(eventFile.getPath()));
157+
}
158+
146159
@Test
147160
void generate_CustomAnnotationMappings_FieldType() throws Exception {
148161
mappingConfig.setCustomTypesMapping(new HashMap<>(Collections.singletonMap(
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
import java.util.*;
4+
5+
@lombok.Builder
6+
@lombok.Data
7+
public class Event {
8+
9+
private String id;
10+
private String categoryId;
11+
private Collection<EventProperty> properties;
12+
private EventStatus status;
13+
private String createdBy;
14+
private String createdDateTime;
15+
private Boolean active;
16+
private Integer rating;
17+
18+
public Event() {
19+
}
20+
21+
public Event(String id, String categoryId, Collection<EventProperty> properties, EventStatus status, String createdBy, String createdDateTime, Boolean active, Integer rating) {
22+
this.id = id;
23+
this.categoryId = categoryId;
24+
this.properties = properties;
25+
this.status = status;
26+
this.createdBy = createdBy;
27+
this.createdDateTime = createdDateTime;
28+
this.active = active;
29+
this.rating = rating;
30+
}
31+
32+
public String getId() {
33+
return id;
34+
}
35+
public void setId(String id) {
36+
this.id = id;
37+
}
38+
39+
public String getCategoryId() {
40+
return categoryId;
41+
}
42+
public void setCategoryId(String categoryId) {
43+
this.categoryId = categoryId;
44+
}
45+
46+
public Collection<EventProperty> getProperties() {
47+
return properties;
48+
}
49+
public void setProperties(Collection<EventProperty> properties) {
50+
this.properties = properties;
51+
}
52+
53+
public EventStatus getStatus() {
54+
return status;
55+
}
56+
public void setStatus(EventStatus status) {
57+
this.status = status;
58+
}
59+
60+
public String getCreatedBy() {
61+
return createdBy;
62+
}
63+
public void setCreatedBy(String createdBy) {
64+
this.createdBy = createdBy;
65+
}
66+
67+
public String getCreatedDateTime() {
68+
return createdDateTime;
69+
}
70+
public void setCreatedDateTime(String createdDateTime) {
71+
this.createdDateTime = createdDateTime;
72+
}
73+
74+
public Boolean getActive() {
75+
return active;
76+
}
77+
public void setActive(Boolean active) {
78+
this.active = active;
79+
}
80+
81+
public Integer getRating() {
82+
return rating;
83+
}
84+
public void setRating(Integer rating) {
85+
this.rating = rating;
86+
}
87+
88+
}

0 commit comments

Comments
 (0)