Skip to content

Commit a2e7989

Browse files
committed
[Fix #1333] Fixing WorkflowModelCollection serialization/deserialization
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 3aadb94 commit a2e7989

8 files changed

Lines changed: 155 additions & 1 deletion

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.impl.model.func;
17+
18+
import io.serverlessworkflow.impl.marshaller.CustomObjectMarshaller;
19+
import io.serverlessworkflow.impl.marshaller.WorkflowInputBuffer;
20+
import io.serverlessworkflow.impl.marshaller.WorkflowOutputBuffer;
21+
import java.util.Collection;
22+
23+
public class JavaModelCollectionMarshaller implements CustomObjectMarshaller<JavaModelCollection> {
24+
25+
@Override
26+
public void write(WorkflowOutputBuffer buffer, JavaModelCollection object) {
27+
buffer.writeObject(object.asJavaObject());
28+
}
29+
30+
@Override
31+
public JavaModelCollection read(
32+
WorkflowInputBuffer buffer, Class<? extends JavaModelCollection> clazz) {
33+
return new JavaModelCollection((Collection<?>) buffer.readObject());
34+
}
35+
36+
@Override
37+
public Class<JavaModelCollection> getObjectClass() {
38+
return JavaModelCollection.class;
39+
}
40+
41+
@Override
42+
public int priority() {
43+
return Integer.MAX_VALUE;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
io.serverlessworkflow.impl.model.func.JavaModelMarshaller
2+
io.serverlessworkflow.impl.model.func.JavaModelCollectionMarshaller
23
io.serverlessworkflow.impl.model.func.SerializableObjectMarshaller

‎impl/core/src/main/java/io/serverlessworkflow/impl/marshaller/AbstractOutputBuffer.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.impl.marshaller;
1717

18+
import io.serverlessworkflow.impl.WorkflowModel;
1819
import java.time.Instant;
1920
import java.util.Collection;
2021
import java.util.Map;
@@ -64,6 +65,9 @@ public WorkflowOutputBuffer writeCollection(Collection<Object> col) {
6465
public WorkflowOutputBuffer writeObject(Object object) {
6566
if (object == null) {
6667
writeType(Type.NULL);
68+
} else if (object instanceof WorkflowModel model) {
69+
writeType(Type.CUSTOM);
70+
writeCustomObject(object);
6771
} else if (object instanceof Short number) {
6872
writeType(Type.SHORT);
6973
writeShort(number);

‎impl/model/src/main/java/io/serverlessworkflow/impl/model/jackson/JacksonModelCollection.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package io.serverlessworkflow.impl.model.jackson;
1717

1818
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
20+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1921
import com.fasterxml.jackson.databind.node.ArrayNode;
2022
import io.serverlessworkflow.impl.CollectionConversionUtils;
2123
import io.serverlessworkflow.impl.WorkflowModel;
@@ -27,9 +29,11 @@
2729
import java.util.List;
2830
import java.util.Optional;
2931

32+
@JsonSerialize(using = JacksonModelCollectionSerializer.class)
33+
@JsonDeserialize(using = JacksonModelCollectionDeserializer.class)
3034
public class JacksonModelCollection implements WorkflowModelCollection {
3135

32-
private ArrayNode node;
36+
protected ArrayNode node;
3337

3438
JacksonModelCollection() {
3539
this.node = JsonUtils.array();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.impl.model.jackson;
17+
18+
import com.fasterxml.jackson.core.JacksonException;
19+
import com.fasterxml.jackson.core.JsonParser;
20+
import com.fasterxml.jackson.databind.DeserializationContext;
21+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
22+
import java.io.IOException;
23+
24+
public class JacksonModelCollectionDeserializer extends StdDeserializer<JacksonModelCollection> {
25+
26+
private static final long serialVersionUID = 1L;
27+
28+
protected JacksonModelCollectionDeserializer() {
29+
super(JacksonModelCollection.class);
30+
}
31+
32+
@Override
33+
public JacksonModelCollection deserialize(JsonParser p, DeserializationContext ctxt)
34+
throws IOException, JacksonException {
35+
return new JacksonModelCollection(p.readValueAsTree());
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.impl.model.jackson;
17+
18+
public class JacksonModelCollectionMarshaller
19+
extends AbstractJacksonMarshaller<JacksonModelCollection> {
20+
21+
@Override
22+
public Class<JacksonModelCollection> getObjectClass() {
23+
return JacksonModelCollection.class;
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.impl.model.jackson;
17+
18+
import com.fasterxml.jackson.core.JsonGenerator;
19+
import com.fasterxml.jackson.databind.SerializerProvider;
20+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
21+
import java.io.IOException;
22+
23+
public class JacksonModelCollectionSerializer extends StdSerializer<JacksonModelCollection> {
24+
25+
private static final long serialVersionUID = 1L;
26+
27+
protected JacksonModelCollectionSerializer() {
28+
super(JacksonModelCollection.class);
29+
}
30+
31+
@Override
32+
public void serialize(
33+
JacksonModelCollection value, JsonGenerator gen, SerializerProvider provider)
34+
throws IOException {
35+
gen.writeTree(value.node);
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
io.serverlessworkflow.impl.model.jackson.JacksonModelMarshaller
2+
io.serverlessworkflow.impl.model.jackson.JacksonModelCollectionMarshaller
23
io.serverlessworkflow.impl.model.jackson.JacksonObjectMarshaller

0 commit comments

Comments
 (0)