Skip to content

Commit 2e1b9e0

Browse files
committed
[Fix #380] oneOf as Optional
1 parent a050a53 commit 2e1b9e0

File tree

7 files changed

+40
-11
lines changed

7 files changed

+40
-11
lines changed

api/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<groupId>com.fasterxml.jackson.dataformat</groupId>
3030
<artifactId>jackson-dataformat-yaml</artifactId>
3131
</dependency>
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.datatype</groupId>
34+
<artifactId>jackson-datatype-jdk8</artifactId>
35+
</dependency>
3236
<dependency>
3337
<groupId>jakarta.validation</groupId>
3438
<artifactId>jakarta.validation-api</artifactId>

api/src/main/java/io/serverlessworkflow/api/ObjectMapperFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.databind.module.SimpleModule;
2121
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
2222
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
23+
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
2324
import io.serverlessworkflow.api.types.CallTask;
2425
import io.serverlessworkflow.api.types.SwitchItem;
2526
import io.serverlessworkflow.api.types.Task;
@@ -55,7 +56,8 @@ private static ObjectMapper configure(ObjectMapper mapper) {
5556
.configure(SerializationFeature.INDENT_OUTPUT, true)
5657
.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)
5758
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
58-
.registerModule(simpleModule);
59+
.registerModule(simpleModule)
60+
.registerModule(new Jdk8Module());
5961
}
6062

6163
private ObjectMapperFactory() {}

api/src/main/java/io/serverlessworkflow/api/SerializeHelper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
import com.fasterxml.jackson.core.JsonGenerator;
1919
import java.io.IOException;
2020
import java.lang.reflect.Method;
21+
import java.util.Optional;
2122

2223
public class SerializeHelper {
2324
public static void serializeOneOf(JsonGenerator jgen, Object item) throws IOException {
2425
try {
2526
for (Method m : item.getClass().getDeclaredMethods()) {
26-
Object value = m.invoke(item);
27-
if (value != null) {
27+
Optional<?> value = (Optional<?>) m.invoke(item);
28+
if (value.isPresent()) {
2829
jgen.writeObject(value);
2930
break;
3031
}

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.serverlessworkflow.api.types.Task;
2424
import io.serverlessworkflow.api.types.Workflow;
2525
import java.io.IOException;
26+
import java.util.Optional;
2627
import org.junit.jupiter.api.Test;
2728

2829
public class ApiTest {
@@ -34,12 +35,12 @@ void testCallHTTPAPI() throws IOException {
3435
assertThat(workflow.getDo().get(0).getName()).isNotNull();
3536
assertThat(workflow.getDo().get(0).getTask()).isNotNull();
3637
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");
38+
Optional<CallTask> callTask = task.getCallTask();
39+
assertThat(callTask).isPresent();
40+
assertThat(task.getDoTask()).isEmpty();
41+
Optional<CallHTTP> httpCall = callTask.flatMap(CallTask::getCallHTTP);
42+
assertThat(httpCall).isPresent();
43+
assertThat(callTask.flatMap(CallTask::getCallAsyncAPI)).isEmpty();
44+
assertThat(httpCall.get().getWith().getMethod()).isEqualTo("get");
4445
}
4546
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private JDefinedClass createUnionClass(
125125

126126
private void wrapIt(JDefinedClass definedClass, JType unionType) {
127127
JFieldVar instanceField =
128-
GeneratorUtils.addGetter(
128+
GeneratorUtils.addOptionalGetter(
129129
definedClass, unionType, ruleFactory.getNameHelper(), unionType.name());
130130
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
131131
constructor

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

+16
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package io.serverlessworkflow.generator;
1717

18+
import com.sun.codemodel.JClass;
1819
import com.sun.codemodel.JDefinedClass;
1920
import com.sun.codemodel.JFieldVar;
2021
import com.sun.codemodel.JMethod;
2122
import com.sun.codemodel.JMod;
2223
import com.sun.codemodel.JType;
24+
import java.util.Optional;
2325
import org.jsonschema2pojo.util.NameHelper;
2426

2527
public class GeneratorUtils {
@@ -34,5 +36,19 @@ public static JFieldVar addGetter(
3436
return instanceField;
3537
}
3638

39+
public static JFieldVar addOptionalGetter(
40+
JDefinedClass definedClass, JType type, NameHelper nameHelper, String name) {
41+
JFieldVar instanceField =
42+
definedClass.field(JMod.PRIVATE, type, nameHelper.getPropertyName(name, null));
43+
JClass optionalRef = definedClass.owner().ref(Optional.class).narrow(type);
44+
JMethod method =
45+
definedClass.method(JMod.PUBLIC, optionalRef, nameHelper.getGetterName(name, type, null));
46+
method
47+
.body()
48+
._return(
49+
definedClass.owner().ref(Optional.class).staticInvoke("ofNullable").arg(instanceField));
50+
return instanceField;
51+
}
52+
3753
private GeneratorUtils() {}
3854
}

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
<artifactId>jackson-core</artifactId>
115115
<version>${version.com.fasterxml.jackson}</version>
116116
</dependency>
117+
<dependency>
118+
<groupId>com.fasterxml.jackson.datatype</groupId>
119+
<artifactId>jackson-datatype-jdk8</artifactId>
120+
<version>${version.com.fasterxml.jackson}</version>
121+
</dependency>
117122
<dependency>
118123
<groupId>com.fasterxml.jackson.core</groupId>
119124
<artifactId>jackson-databind</artifactId>

0 commit comments

Comments
 (0)