Skip to content

Commit

Permalink
Add helper function to facilitate use of library with oneOf types by …
Browse files Browse the repository at this point in the history
…selecting current type of item instead of checking all of them
  • Loading branch information
uasouz authored and vinicius.lopes committed Aug 13, 2024
1 parent 22c5ead commit 56e0074
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.JsonMappingException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collection;

public class DeserializeHelper {
Expand All @@ -32,7 +33,12 @@ public static <T> T deserializeOneOf(
for (Class<?> unionType : unionTypes) {
try {
Object object = p.getCodec().treeToValue(node, unionType);
return targetClass.getConstructor(unionType).newInstance(object);
T instance = targetClass.getConstructor(unionType).newInstance(object);
Field typeField = targetClass.getDeclaredField("definition");
typeField.setAccessible(true);
typeField.set(instance, object);

return instance;
} catch (IOException | ReflectiveOperationException io) {
ex.addSuppressed(io);
}
Expand Down
16 changes: 9 additions & 7 deletions api/src/test/java/io/serverlessworkflow/api/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ void testCallHTTPAPI() throws IOException {
assertThat(workflow.getDo().get(0).getName()).isNotNull();
assertThat(workflow.getDo().get(0).getTask()).isNotNull();
Task task = workflow.getDo().get(0).getTask();
CallTask callTask = task.getCallTask();
assertThat(callTask).isNotNull();
assertThat(task.getDoTask()).isNull();
CallHTTP httpCall = callTask.getCallHTTP();
assertThat(httpCall).isNotNull();
assertThat(callTask.getCallAsyncAPI()).isNull();
assertThat(httpCall.getWith().getMethod()).isEqualTo("get");
if (task.getDefinition() instanceof CallTask) {
CallTask callTask = task.getCallTask();
assertThat(callTask).isNotNull();
assertThat(task.getDoTask()).isNull();
CallHTTP httpCall = callTask.getCallHTTP();
assertThat(httpCall).isNotNull();
assertThat(callTask.getCallAsyncAPI()).isNull();
assertThat(httpCall.getWith().getMethod()).isEqualTo("get");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ private JDefinedClass createUnionClass(
definedClass
.annotate(JsonDeserialize.class)
.param("using", generateDeserializer(definedClass, unionTypes));

JType clazzClass = definedClass.owner()._ref(Object.class);

JFieldVar typeField =
definedClass.field(
JMod.PRIVATE,
clazzClass,
ruleFactory.getNameHelper().getPropertyName("definition", null),
null);

GeneratorUtils.buildMethod(
definedClass, typeField, ruleFactory.getNameHelper(), "definition");

return populateClass(definedClass, refType, unionTypes);
} catch (JClassAlreadyExistsException e) {
throw new IllegalArgumentException(e);
Expand Down

0 comments on commit 56e0074

Please sign in to comment.