Skip to content

Commit 56e0074

Browse files
uasouzvinicius.lopes
authored and
vinicius.lopes
committed
Add helper function to facilitate use of library with oneOf types by selecting current type of item instead of checking all of them
1 parent 22c5ead commit 56e0074

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

api/src/main/java/io/serverlessworkflow/serialization/DeserializeHelper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.core.TreeNode;
2121
import com.fasterxml.jackson.databind.JsonMappingException;
2222
import java.io.IOException;
23+
import java.lang.reflect.Field;
2324
import java.util.Collection;
2425

2526
public class DeserializeHelper {
@@ -32,7 +33,12 @@ public static <T> T deserializeOneOf(
3233
for (Class<?> unionType : unionTypes) {
3334
try {
3435
Object object = p.getCodec().treeToValue(node, unionType);
35-
return targetClass.getConstructor(unionType).newInstance(object);
36+
T instance = targetClass.getConstructor(unionType).newInstance(object);
37+
Field typeField = targetClass.getDeclaredField("definition");
38+
typeField.setAccessible(true);
39+
typeField.set(instance, object);
40+
41+
return instance;
3642
} catch (IOException | ReflectiveOperationException io) {
3743
ex.addSuppressed(io);
3844
}

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

Lines changed: 9 additions & 7 deletions
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.getDefinition() 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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ private JDefinedClass createUnionClass(
166166
definedClass
167167
.annotate(JsonDeserialize.class)
168168
.param("using", generateDeserializer(definedClass, unionTypes));
169+
170+
JType clazzClass = definedClass.owner()._ref(Object.class);
171+
172+
JFieldVar typeField =
173+
definedClass.field(
174+
JMod.PRIVATE,
175+
clazzClass,
176+
ruleFactory.getNameHelper().getPropertyName("definition", null),
177+
null);
178+
179+
GeneratorUtils.buildMethod(
180+
definedClass, typeField, ruleFactory.getNameHelper(), "definition");
181+
169182
return populateClass(definedClass, refType, unionTypes);
170183
} catch (JClassAlreadyExistsException e) {
171184
throw new IllegalArgumentException(e);

0 commit comments

Comments
 (0)