Skip to content

Commit bccfe3f

Browse files
committed
https://github.com/mulesoft-labs/raml-for-jax-rs/issues/223
equalsAndHashCode for unions.
1 parent 17243b9 commit bccfe3f

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

raml-to-pojo/src/main/java/org/raml/ramltopojo/GenerationContextImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ private<T> void loadBasePlugins(Set<T> plugins, Class<T> pluginType, TypeDeclara
134134
public ObjectTypeHandlerPlugin pluginsForObjects(TypeDeclaration... typeDeclarations) {
135135

136136
List<PluginDef> data = Annotations.PLUGINS.get(Collections.<PluginDef>emptyList(), api, typeDeclarations);
137-
System.err.println("annotation defined plugins for " + typeDeclarations[0].name() + "are " + data);
137+
//System.err.println("annotation defined plugins for " + typeDeclarations[0].name() + "are " + data);
138138
Set<ObjectTypeHandlerPlugin> plugins = new HashSet<>();
139139
loadBasePlugins(plugins, ObjectTypeHandlerPlugin.class);
140140
for (PluginDef datum : data) {
141141
plugins.addAll(pluginManager.getClassesForName(datum.getPluginName(), datum.getArguments() , ObjectTypeHandlerPlugin.class));
142142
}
143-
System.err.println("plugin definitions for object type " + plugins + " for " + typeDeclarations[0].name());
143+
//System.err.println("plugin definitions for object type " + plugins + " for " + typeDeclarations[0].name());
144144
return new ObjectTypeHandlerPlugin.Composite(plugins);
145145
}
146146

raml-to-pojo/src/main/java/org/raml/ramltopojo/extensions/AllTypesPluginHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
/**
1313
* Created. There, you have it.
1414
*/
15-
public class AllTypesPluginHelper implements ObjectTypeHandlerPlugin, UnionTypeHandlerPlugin, EnumerationTypeHandlerPlugin {
15+
public class AllTypesPluginHelper implements ObjectTypeHandlerPlugin, UnionTypeHandlerPlugin, EnumerationTypeHandlerPlugin, ArrayTypeHandlerPlugin {
1616

1717
private final ObjectTypeHandlerPlugin.Helper objectTypeHandlerPlugin= new ObjectTypeHandlerPlugin.Helper();
1818
private final UnionTypeHandlerPlugin.Helper unionTypeHandlerPlugin= new UnionTypeHandlerPlugin.Helper();
1919
private final EnumerationTypeHandlerPlugin.Helper enumerationTypeHandlerPlugin= new EnumerationTypeHandlerPlugin.Helper();
20+
private final ArrayTypeHandlerPlugin.Helper arrayTypeHandlerPlugin = new ArrayTypeHandlerPlugin.Helper();
2021

2122
@Override
2223
public ClassName className(ObjectPluginContext objectPluginContext, ObjectTypeDeclaration ramlType, ClassName currentSuggestion, EventType eventType) {
@@ -78,4 +79,13 @@ public TypeSpec.Builder enumValue(EnumerationPluginContext enumerationPluginCont
7879
return enumerationTypeHandlerPlugin.enumValue(enumerationPluginContext, declaration, incoming, value, eventType);
7980
}
8081

82+
@Override
83+
public ClassName className(ArrayPluginContext arrayPluginContext, TypeDeclaration ramlType, ClassName currentSuggestion, EventType eventType) {
84+
return arrayTypeHandlerPlugin.className(arrayPluginContext, ramlType, currentSuggestion, eventType);
85+
}
86+
87+
@Override
88+
public TypeSpec.Builder classCreated(ArrayPluginContext arrayPluginContext, TypeDeclaration ramlType, TypeSpec.Builder incoming, EventType eventType) {
89+
return arrayTypeHandlerPlugin.classCreated(arrayPluginContext, ramlType, incoming, eventType);
90+
}
8191
}

raml-to-pojo/src/main/java/org/raml/ramltopojo/extensions/tools/AddEqualsAndHashCode.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ private void createEquals(List<FieldSpec> specs, TypeSpec.Builder incoming) {
114114

115115
@Override
116116
public TypeSpec.Builder classCreated(UnionPluginContext unionPluginContext, UnionTypeDeclaration ramlType, TypeSpec.Builder incoming, EventType eventType) {
117-
return super.classCreated(unionPluginContext, ramlType, incoming, eventType);
117+
118+
if ( eventType != EventType.IMPLEMENTATION) {
119+
120+
return incoming;
121+
}
122+
123+
List<FieldSpec> specs = incoming.build().fieldSpecs;
124+
createEquals(specs, incoming);
125+
createHashCode(specs, incoming);
126+
return incoming;
118127
}
119128
}

raml-to-pojo/src/main/java/org/raml/ramltopojo/union/UnionTypeHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ public Optional<CreationResult> create(GenerationContext generationContext, Crea
7272

7373
private TypeSpec.Builder getImplementation(ClassName interfaceName, GenerationContext generationContext, UnionPluginContext context, CreationResult preCreationResult) {
7474
TypeSpec.Builder typeSpec = TypeSpec.classBuilder(preCreationResult.getJavaName(EventType.IMPLEMENTATION)).addModifiers(Modifier.PUBLIC).addSuperinterface(interfaceName);
75-
typeSpec = generationContext.pluginsForUnions(union).classCreated(context, union, typeSpec, EventType.IMPLEMENTATION);
76-
if ( typeSpec == null ) {
77-
return null;
78-
}
7975

8076
FieldSpec.Builder anyType = FieldSpec.builder(Object.class, "anyType", Modifier.PRIVATE);
8177
anyType = generationContext.pluginsForUnions(union).anyFieldCreated(context, union, typeSpec, anyType, EventType.IMPLEMENTATION);
@@ -119,6 +115,12 @@ private TypeSpec.Builder getImplementation(ClassName interfaceName, GenerationCo
119115
.returns(TypeName.BOOLEAN).build()
120116
).build();
121117
}
118+
119+
typeSpec = generationContext.pluginsForUnions(union).classCreated(context, union, typeSpec, EventType.IMPLEMENTATION);
120+
if ( typeSpec == null ) {
121+
return null;
122+
}
123+
122124
return typeSpec;
123125
}
124126

0 commit comments

Comments
 (0)