Skip to content

Commit eb4f2af

Browse files
Merge pull request #403 from uasouz/main
Add helper function to facilitate use of library with oneOf types
2 parents 22c5ead + ed00379 commit eb4f2af

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.api;
17+
18+
public interface OneOfValueProvider {
19+
Object get();
20+
}

api/src/test/java/io/serverlessworkflow/api/ApiTest.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ void testCallHTTPAPI() throws IOException {
3434
assertThat(workflow.getDo().get(0).getName()).isNotNull();
3535
assertThat(workflow.getDo().get(0).getTask()).isNotNull();
3636
Task task = workflow.getDo().get(0).getTask();
37-
CallTask callTask = task.getCallTask();
38-
assertThat(callTask).isNotNull();
39-
assertThat(task.getDoTask()).isNull();
40-
CallHTTP httpCall = callTask.getCallHTTP();
41-
assertThat(httpCall).isNotNull();
42-
assertThat(callTask.getCallAsyncAPI()).isNull();
43-
assertThat(httpCall.getWith().getMethod()).isEqualTo("get");
37+
if (task.get() instanceof CallTask) {
38+
CallTask callTask = task.getCallTask();
39+
assertThat(callTask).isNotNull();
40+
assertThat(task.getDoTask()).isNull();
41+
CallHTTP httpCall = callTask.getCallHTTP();
42+
assertThat(httpCall).isNotNull();
43+
assertThat(callTask.getCallAsyncAPI()).isNull();
44+
assertThat(httpCall.getWith().getMethod()).isEqualTo("get");
45+
}
4446
}
4547
}

custom-generator/src/main/java/io/serverlessworkflow/generator/AllAnyOneOfSchemaRule.java

+22-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.sun.codemodel.JMod;
3232
import com.sun.codemodel.JPackage;
3333
import com.sun.codemodel.JType;
34+
import com.sun.codemodel.JVar;
3435
import java.io.UnsupportedEncodingException;
3536
import java.net.URI;
3637
import java.net.URLDecoder;
@@ -102,13 +103,27 @@ private boolean isCandidateForCreation(Collection<JType> unionTypes) {
102103

103104
private JDefinedClass populateClass(
104105
JDefinedClass definedClass, Optional<JType> refType, Collection<JType> unionTypes) {
105-
unionTypes.forEach(unionType -> wrapIt(definedClass, unionType));
106+
JType clazzClass = definedClass.owner()._ref(Object.class);
107+
108+
JFieldVar valueField =
109+
definedClass.field(
110+
JMod.PRIVATE,
111+
clazzClass,
112+
ruleFactory.getNameHelper().getPropertyName("value", null),
113+
null);
114+
115+
definedClass._implements(
116+
definedClass.owner().ref(GeneratorUtils.ONE_OF_VALUE_PROVIDER_INTERFACE_NAME));
117+
118+
GeneratorUtils.implementInterface(definedClass, valueField);
119+
120+
unionTypes.forEach(unionType -> wrapIt(definedClass, valueField, unionType));
106121
refType.ifPresent(
107122
type -> {
108123
if (type instanceof JClass) {
109124
definedClass._extends((JClass) type);
110125
} else {
111-
wrapIt(definedClass, type);
126+
wrapIt(definedClass, valueField, type);
112127
}
113128
});
114129
if (definedClass.constructors().hasNext()
@@ -166,23 +181,25 @@ private JDefinedClass createUnionClass(
166181
definedClass
167182
.annotate(JsonDeserialize.class)
168183
.param("using", generateDeserializer(definedClass, unionTypes));
184+
169185
return populateClass(definedClass, refType, unionTypes);
170186
} catch (JClassAlreadyExistsException e) {
171187
throw new IllegalArgumentException(e);
172188
}
173189
}
174190

175-
private void wrapIt(JDefinedClass definedClass, JType unionType) {
191+
private void wrapIt(JDefinedClass definedClass, JFieldVar valueField, JType unionType) {
176192
final String name = unionType.name();
177193
JFieldVar instanceField =
178194
definedClass.field(
179195
JMod.PRIVATE, unionType, ruleFactory.getNameHelper().getPropertyName(name, null));
180196
GeneratorUtils.buildMethod(definedClass, instanceField, ruleFactory.getNameHelper(), name);
181197
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
198+
JVar instanceParam = constructor.param(unionType, instanceField.name());
182199
constructor
183200
.body()
184-
.assign(
185-
JExpr._this().ref(instanceField), constructor.param(unionType, instanceField.name()));
201+
.assign(JExpr._this().ref(valueField), instanceParam)
202+
.assign(JExpr._this().ref(instanceField), instanceParam);
186203
}
187204

188205
private void unionType(

custom-generator/src/main/java/io/serverlessworkflow/generator/GeneratorUtils.java

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class GeneratorUtils {
3636
"io.serverlessworkflow.serialization.SerializeHelper";
3737
public static final String DESERIALIZE_HELPER_NAME =
3838
"io.serverlessworkflow.serialization.DeserializeHelper";
39+
public static final String ONE_OF_VALUE_PROVIDER_INTERFACE_NAME =
40+
"io.serverlessworkflow.api.OneOfValueProvider";
3941

4042
@FunctionalInterface
4143
public interface SerializerFiller {
@@ -55,6 +57,13 @@ public static JDefinedClass deserializerClass(JDefinedClass relatedClass) {
5557
return createClass(relatedClass, JsonDeserializer.class, "Deserializer");
5658
}
5759

60+
public static JMethod implementInterface(JDefinedClass definedClass, JFieldVar valueField) {
61+
JMethod method = definedClass.method(JMod.PUBLIC, Object.class, "get");
62+
method.annotate(Override.class);
63+
method.body()._return(valueField);
64+
return method;
65+
}
66+
5867
public static JMethod buildMethod(
5968
JDefinedClass definedClass, JFieldVar instanceField, NameHelper nameHelper, String name) {
6069
JMethod method =

0 commit comments

Comments
 (0)