Skip to content

Commit

Permalink
[Fix #380] oneOf as Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jun 28, 2024
1 parent a050a53 commit 2e1b9e0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import io.serverlessworkflow.api.types.CallTask;
import io.serverlessworkflow.api.types.SwitchItem;
import io.serverlessworkflow.api.types.Task;
Expand Down Expand Up @@ -55,7 +56,8 @@ private static ObjectMapper configure(ObjectMapper mapper) {
.configure(SerializationFeature.INDENT_OUTPUT, true)
.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
.registerModule(simpleModule);
.registerModule(simpleModule)
.registerModule(new Jdk8Module());
}

private ObjectMapperFactory() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import com.fasterxml.jackson.core.JsonGenerator;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Optional;

public class SerializeHelper {
public static void serializeOneOf(JsonGenerator jgen, Object item) throws IOException {
try {
for (Method m : item.getClass().getDeclaredMethods()) {
Object value = m.invoke(item);
if (value != null) {
Optional<?> value = (Optional<?>) m.invoke(item);
if (value.isPresent()) {
jgen.writeObject(value);
break;
}
Expand Down
15 changes: 8 additions & 7 deletions api/src/test/java/io/serverlessworkflow/api/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.Workflow;
import java.io.IOException;
import java.util.Optional;
import org.junit.jupiter.api.Test;

public class ApiTest {
Expand All @@ -34,12 +35,12 @@ 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");
Optional<CallTask> callTask = task.getCallTask();
assertThat(callTask).isPresent();
assertThat(task.getDoTask()).isEmpty();
Optional<CallHTTP> httpCall = callTask.flatMap(CallTask::getCallHTTP);
assertThat(httpCall).isPresent();
assertThat(callTask.flatMap(CallTask::getCallAsyncAPI)).isEmpty();
assertThat(httpCall.get().getWith().getMethod()).isEqualTo("get");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private JDefinedClass createUnionClass(

private void wrapIt(JDefinedClass definedClass, JType unionType) {
JFieldVar instanceField =
GeneratorUtils.addGetter(
GeneratorUtils.addOptionalGetter(
definedClass, unionType, ruleFactory.getNameHelper(), unionType.name());
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package io.serverlessworkflow.generator;

import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import java.util.Optional;
import org.jsonschema2pojo.util.NameHelper;

public class GeneratorUtils {
Expand All @@ -34,5 +36,19 @@ public static JFieldVar addGetter(
return instanceField;
}

public static JFieldVar addOptionalGetter(
JDefinedClass definedClass, JType type, NameHelper nameHelper, String name) {
JFieldVar instanceField =
definedClass.field(JMod.PRIVATE, type, nameHelper.getPropertyName(name, null));
JClass optionalRef = definedClass.owner().ref(Optional.class).narrow(type);
JMethod method =
definedClass.method(JMod.PUBLIC, optionalRef, nameHelper.getGetterName(name, type, null));
method
.body()
._return(
definedClass.owner().ref(Optional.class).staticInvoke("ofNullable").arg(instanceField));
return instanceField;
}

private GeneratorUtils() {}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
<artifactId>jackson-core</artifactId>
<version>${version.com.fasterxml.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${version.com.fasterxml.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down

0 comments on commit 2e1b9e0

Please sign in to comment.