Skip to content

Commit 9fdd1fd

Browse files
Merge pull request #383 from fjtirado/Fix_#380
[Fix #380] Change item API
2 parents e5828d5 + 9a91a05 commit 9fdd1fd

File tree

16 files changed

+257
-23
lines changed

16 files changed

+257
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CallTaskDeserializer extends JsonDeserializer<CallTask> {
3030

3131
@Override
3232
public CallTask deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
33-
return DeserializeHelper.deserialize(
33+
return DeserializeHelper.deserializeOneOf(
3434
p,
3535
CallTask.class,
3636
List.of(CallHTTP.class, CallAsyncAPI.class, CallOpenAPI.class, CallGRPC.class));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ class CallTaskSerializer extends JsonSerializer<CallTask> {
2525
@Override
2626
public void serialize(CallTask value, JsonGenerator gen, SerializerProvider serializers)
2727
throws IOException {
28-
SerializeHelper.serialize(gen, value);
28+
SerializeHelper.serializeOneOf(gen, value);
2929
}
3030
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class DeserializeHelper {
2626

27-
public static <T> T deserialize(
27+
public static <T> T deserializeOneOf(
2828
JsonParser p, Class<T> targetClass, Collection<Class<?>> unionTypes) throws IOException {
2929
TreeNode node = p.readValueAsTree();
3030
JsonProcessingException ex = new JsonMappingException("Problem deserializing " + targetClass);
@@ -38,4 +38,17 @@ public static <T> T deserialize(
3838
}
3939
throw ex;
4040
}
41+
42+
public static <T> T deserializeItem(JsonParser p, Class<T> targetClass, Class<?> valueClass)
43+
throws IOException {
44+
TreeNode node = p.readValueAsTree();
45+
String fieldName = node.fieldNames().next();
46+
try {
47+
return targetClass
48+
.getConstructor(String.class, valueClass)
49+
.newInstance(fieldName, p.getCodec().treeToValue(node.get(fieldName), valueClass));
50+
} catch (ReflectiveOperationException e) {
51+
throw new IOException(e);
52+
}
53+
}
4154
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
2222
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
2323
import io.serverlessworkflow.api.types.CallTask;
24+
import io.serverlessworkflow.api.types.SwitchItem;
2425
import io.serverlessworkflow.api.types.Task;
26+
import io.serverlessworkflow.api.types.TaskItem;
2527

2628
class ObjectMapperFactory {
2729

@@ -44,6 +46,11 @@ private static ObjectMapper configure(ObjectMapper mapper) {
4446
simpleModule.addSerializer(Task.class, new TaskSerializer());
4547
simpleModule.addDeserializer(CallTask.class, new CallTaskDeserializer());
4648
simpleModule.addSerializer(CallTask.class, new CallTaskSerializer());
49+
simpleModule.addDeserializer(TaskItem.class, new TaskItemDeserializer());
50+
simpleModule.addSerializer(TaskItem.class, new TaskItemSerializer());
51+
simpleModule.addSerializer(SwitchItem.class, new SwitchItemSerializer());
52+
simpleModule.addDeserializer(SwitchItem.class, new SwitchItemDeserializer());
53+
4754
return mapper
4855
.configure(SerializationFeature.INDENT_OUTPUT, true)
4956
.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.lang.reflect.Method;
2121

2222
public class SerializeHelper {
23-
public static void serialize(JsonGenerator jgen, Object item) throws IOException {
23+
public static void serializeOneOf(JsonGenerator jgen, Object item) throws IOException {
2424
try {
2525
for (Method m : item.getClass().getDeclaredMethods()) {
2626
Object value = m.invoke(item);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.databind.DeserializationContext;
20+
import com.fasterxml.jackson.databind.JsonDeserializer;
21+
import io.serverlessworkflow.api.types.SwitchCase;
22+
import io.serverlessworkflow.api.types.SwitchItem;
23+
import java.io.IOException;
24+
25+
class SwitchItemDeserializer extends JsonDeserializer<SwitchItem> {
26+
27+
@Override
28+
public SwitchItem deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
29+
return DeserializeHelper.deserializeItem(p, SwitchItem.class, SwitchCase.class);
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
import com.fasterxml.jackson.core.JsonGenerator;
19+
import com.fasterxml.jackson.databind.JsonSerializer;
20+
import com.fasterxml.jackson.databind.SerializerProvider;
21+
import io.serverlessworkflow.api.types.SwitchItem;
22+
import java.io.IOException;
23+
24+
class SwitchItemSerializer extends JsonSerializer<SwitchItem> {
25+
26+
@Override
27+
public void serialize(SwitchItem value, JsonGenerator gen, SerializerProvider serializers)
28+
throws IOException {
29+
gen.writeStartObject();
30+
gen.writeObjectField(value.getName(), value.getSwitchCase());
31+
gen.writeEndObject();
32+
}
33+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class TaskDeserializer extends JsonDeserializer<Task> {
3838

3939
@Override
4040
public Task deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
41-
return DeserializeHelper.deserialize(
41+
return DeserializeHelper.deserializeOneOf(
4242
p,
4343
Task.class,
4444
List.of(
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.databind.DeserializationContext;
20+
import com.fasterxml.jackson.databind.JsonDeserializer;
21+
import io.serverlessworkflow.api.types.Task;
22+
import io.serverlessworkflow.api.types.TaskItem;
23+
import java.io.IOException;
24+
25+
class TaskItemDeserializer extends JsonDeserializer<TaskItem> {
26+
27+
@Override
28+
public TaskItem deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
29+
return DeserializeHelper.deserializeItem(p, TaskItem.class, Task.class);
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
import com.fasterxml.jackson.core.JsonGenerator;
19+
import com.fasterxml.jackson.databind.JsonSerializer;
20+
import com.fasterxml.jackson.databind.SerializerProvider;
21+
import io.serverlessworkflow.api.types.TaskItem;
22+
import java.io.IOException;
23+
24+
class TaskItemSerializer extends JsonSerializer<TaskItem> {
25+
26+
@Override
27+
public void serialize(TaskItem value, JsonGenerator gen, SerializerProvider serializers)
28+
throws IOException {
29+
gen.writeStartObject();
30+
gen.writeObjectField(value.getName(), value.getTask());
31+
gen.writeEndObject();
32+
}
33+
}

0 commit comments

Comments
 (0)