Skip to content

Commit cf0a6b8

Browse files
committed
[Fix #418] Adding Jsr 303 validation support
Notice that this will force us to add version and other mandatory field we were not including in the examples
1 parent d0f17a0 commit cf0a6b8

19 files changed

+130
-10
lines changed

api/pom.xml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@
3333
<groupId>jakarta.validation</groupId>
3434
<artifactId>jakarta.validation-api</artifactId>
3535
</dependency>
36-
36+
<dependency>
37+
<groupId>org.hibernate.validator</groupId>
38+
<artifactId>hibernate-validator</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.glassfish.expressly</groupId>
42+
<artifactId>expressly</artifactId>
43+
</dependency>
3744
<!-- test -->
3845
<dependency>
3946
<groupId>org.junit.jupiter</groupId>
@@ -65,11 +72,6 @@
6572
<artifactId>logback-classic</artifactId>
6673
<scope>test</scope>
6774
</dependency>
68-
<dependency>
69-
<groupId>org.assertj</groupId>
70-
<artifactId>assertj-core</artifactId>
71-
<scope>test</scope>
72-
</dependency>
7375
</dependencies>
7476

7577

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919
import com.fasterxml.jackson.databind.SerializationFeature;
20+
import com.fasterxml.jackson.databind.module.SimpleModule;
2021
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
2122
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
23+
import io.serverlessworkflow.serialization.BeanDeserializerModifierWithValidation;
2224

2325
class ObjectMapperFactory {
2426

@@ -36,10 +38,13 @@ public static final ObjectMapper yamlMapper() {
3638
}
3739

3840
private static ObjectMapper configure(ObjectMapper mapper) {
41+
SimpleModule validationModule = new SimpleModule();
42+
validationModule.setDeserializerModifier(new BeanDeserializerModifierWithValidation());
3943
return mapper
4044
.configure(SerializationFeature.INDENT_OUTPUT, true)
4145
.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)
42-
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
46+
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
47+
.registerModule(validationModule);
4348
}
4449

4550
private ObjectMapperFactory() {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.serialization;
17+
18+
import com.fasterxml.jackson.databind.BeanDescription;
19+
import com.fasterxml.jackson.databind.DeserializationConfig;
20+
import com.fasterxml.jackson.databind.JsonDeserializer;
21+
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
22+
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
23+
24+
public class BeanDeserializerModifierWithValidation extends BeanDeserializerModifier {
25+
26+
private static final long serialVersionUID = 1L;
27+
28+
@Override
29+
public JsonDeserializer<?> modifyDeserializer(
30+
DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
31+
return deserializer instanceof BeanDeserializer
32+
? new BeanDeserializerWithValidation((BeanDeserializer) deserializer)
33+
: deserializer;
34+
}
35+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.serialization;
17+
18+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.databind.DeserializationContext;
20+
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
21+
import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;
22+
import jakarta.validation.ConstraintViolation;
23+
import jakarta.validation.ConstraintViolationException;
24+
import jakarta.validation.Validation;
25+
import jakarta.validation.Validator;
26+
import java.io.IOException;
27+
import java.util.Set;
28+
29+
public class BeanDeserializerWithValidation extends BeanDeserializer {
30+
private static final long serialVersionUID = 1L;
31+
private static final Validator validator =
32+
Validation.buildDefaultValidatorFactory().getValidator();
33+
34+
protected BeanDeserializerWithValidation(BeanDeserializerBase src) {
35+
super(src);
36+
}
37+
38+
private <T> void validate(T t) throws IOException {
39+
Set<ConstraintViolation<T>> violations = validator.validate(t);
40+
if (!violations.isEmpty()) {
41+
throw new ConstraintViolationException(violations);
42+
}
43+
}
44+
45+
@Override
46+
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
47+
Object instance = super.deserialize(p, ctxt);
48+
validate(instance);
49+
return instance;
50+
}
51+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
import com.fasterxml.jackson.core.TreeNode;
2121
import com.fasterxml.jackson.databind.JsonMappingException;
22+
import jakarta.validation.ConstraintViolationException;
2223
import java.io.IOException;
2324
import java.util.Collection;
2425

@@ -33,7 +34,7 @@ public static <T> T deserializeOneOf(
3334
try {
3435
Object object = p.getCodec().treeToValue(node, unionType);
3536
return targetClass.getConstructor(unionType).newInstance(object);
36-
} catch (IOException | ReflectiveOperationException io) {
37+
} catch (IOException | ReflectiveOperationException | ConstraintViolationException io) {
3738
ex.addSuppressed(io);
3839
}
3940
}

api/src/test/resources/features/callFunction.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ document:
22
dsl: 1.0.0-alpha1
33
namespace: default
44
name: http-call-with-response-output
5+
version: 1.0.0
56

67
use:
78
functions:

api/src/test/resources/features/callHttp.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ document:
22
dsl: 1.0.0-alpha1
33
namespace: default
44
name: http-call-with-response-output
5+
version: 1.0.0
56
do:
67
- getPet:
78
call: http

api/src/test/resources/features/callOpenAPI.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ document:
22
dsl: 1.0.0-alpha1
33
namespace: default
44
name: openapi-call-with-content-output
5+
version: 1.0.0
56
do:
67
- findPet:
78
call: openapi

api/src/test/resources/features/composite.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ document:
22
dsl: 1.0.0-alpha1
33
namespace: default
44
name: do
5+
version: 1.0.0
56
do:
67
- compositeExample:
78
do:

api/src/test/resources/features/data-flow.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ document:
22
dsl: 1.0.0-alpha1
33
namespace: default
44
name: output-filtering
5+
version: 1.0.0
56
do:
67
- getPet:
78
call: http

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy