Skip to content

[Fix #418] Adding const support #419

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.expressly</groupId>
<artifactId>expressly</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -65,11 +72,6 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down Expand Up @@ -101,6 +103,7 @@
<usePrimitives>true</usePrimitives>
<useJakartaValidation>true</useJakartaValidation>
<customRuleFactory>io.serverlessworkflow.generator.UnreferencedFactory</customRuleFactory>
<customAnnotator>io.serverlessworkflow.generator.ConstAnnotator</customAnnotator>
</configuration>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
import io.serverlessworkflow.serialization.BeanDeserializerModifierWithValidation;

class ObjectMapperFactory {

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

private static ObjectMapper configure(ObjectMapper mapper) {
SimpleModule validationModule = new SimpleModule();
validationModule.setDeserializerModifier(new BeanDeserializerModifierWithValidation());
return mapper
.configure(SerializationFeature.INDENT_OUTPUT, true)
.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
.registerModule(validationModule);
}

private ObjectMapperFactory() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.serialization;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;

public class BeanDeserializerModifierWithValidation extends BeanDeserializerModifier {

private static final long serialVersionUID = 1L;

@Override
public JsonDeserializer<?> modifyDeserializer(
DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
return deserializer instanceof BeanDeserializer
? new BeanDeserializerWithValidation((BeanDeserializer) deserializer)
: deserializer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.serialization;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import java.io.IOException;
import java.util.Set;

public class BeanDeserializerWithValidation extends BeanDeserializer {
private static final long serialVersionUID = 1L;
private static final Validator validator =
Validation.buildDefaultValidatorFactory().getValidator();

protected BeanDeserializerWithValidation(BeanDeserializerBase src) {
super(src);
}

private <T> void validate(T t) throws IOException {
Set<ConstraintViolation<T>> violations = validator.validate(t);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
}

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Object instance = super.deserialize(p, ctxt);
validate(instance);
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.JsonMappingException;
import jakarta.validation.ConstraintViolationException;
import java.io.IOException;
import java.util.Collection;

Expand All @@ -33,7 +34,7 @@ public static <T> T deserializeOneOf(
try {
Object object = p.getCodec().treeToValue(node, unionType);
return targetClass.getConstructor(unionType).newInstance(object);
} catch (IOException | ReflectiveOperationException io) {
} catch (IOException | ReflectiveOperationException | ConstraintViolationException io) {
ex.addSuppressed(io);
}
}
Expand Down
19 changes: 19 additions & 0 deletions api/src/test/java/io/serverlessworkflow/api/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.serverlessworkflow.api.WorkflowReader.readWorkflowFromClasspath;
import static org.assertj.core.api.Assertions.assertThat;

import io.serverlessworkflow.api.types.CallFunction;
import io.serverlessworkflow.api.types.CallHTTP;
import io.serverlessworkflow.api.types.CallTask;
import io.serverlessworkflow.api.types.Task;
Expand All @@ -44,4 +45,22 @@ void testCallHTTPAPI() throws IOException {
assertThat(httpCall.getWith().getMethod()).isEqualTo("get");
}
}

@Test
void testCallFunctionAPIWithoutArguments() throws IOException {
Workflow workflow = readWorkflowFromClasspath("features/callFunction.yaml");
assertThat(workflow.getDo()).isNotEmpty();
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(callTask.get()).isInstanceOf(CallFunction.class);
if (callTask.get() instanceof CallFunction) {
CallFunction functionCall = callTask.getCallFunction();
assertThat(functionCall).isNotNull();
assertThat(callTask.getCallAsyncAPI()).isNull();
assertThat(functionCall.getWith()).isNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class FeaturesTest {
"features/set.yaml",
"features/switch.yaml",
"features/try.yaml",
"features/listen.yaml"
"features/listen.yaml",
"features/callFunction.yaml"
})
public void testSpecFeaturesParsing(String workflowLocation) throws IOException {
Workflow workflow = readWorkflowFromClasspath(workflowLocation);
Expand Down
19 changes: 19 additions & 0 deletions api/src/test/resources/features/callFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
document:
dsl: 1.0.0-alpha1
namespace: default
name: http-call-with-response-output
version: 1.0.0

use:
functions:
getPet:
call: http
with:
method: get
endpoint:
uri: https://petstore.swagger.io/v2/pet/{petId}
output: response

do:
- getPetFunctionCall:
call: getPet
1 change: 1 addition & 0 deletions api/src/test/resources/features/callHttp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: http-call-with-response-output
version: 1.0.0
do:
- getPet:
call: http
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/callOpenAPI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: openapi-call-with-content-output
version: 1.0.0
do:
- findPet:
call: openapi
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/composite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: do
version: 1.0.0
do:
- compositeExample:
do:
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/data-flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: output-filtering
version: 1.0.0
do:
- getPet:
call: http
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/emit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: emit
version: 1.0.0
do:
- emitEvent:
emit:
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: implicit-sequence
version: 1.0.0
do:
- setRed:
set:
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/for.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: for
version: 1.0.0
do:
- loopColors:
for:
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/listen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: listen-task
version: 1.0.0
do:
- listenToSomething:
listen:
Expand Down
4 changes: 3 additions & 1 deletion api/src/test/resources/features/raise.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: raise-custom-error
version: 1.0.0
do:
- raiseError:
raise:
error:
status: 400
type: https://serverlessworkflow.io/errors/types/compliance
title: Compliance Error
title: Compliance Error
instance: raiseError
1 change: 1 addition & 0 deletions api/src/test/resources/features/set.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: set
version: 1.0.0
do:
- setShape:
set:
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/switch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: switch-match
version: 1.0.0
do:
- switchColor:
switch:
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/features/try.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ document:
dsl: 1.0.0-alpha1
namespace: default
name: try-catch-404
version: 1.0.0
do:
- tryGetPet:
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.generator;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import jakarta.validation.constraints.Pattern;
import org.jsonschema2pojo.AbstractAnnotator;
import org.jsonschema2pojo.GenerationConfig;

public class ConstAnnotator extends AbstractAnnotator {

private static final String CONST = "const";

public ConstAnnotator(GenerationConfig generationConfig) {
super(generationConfig);
}

@Override
public void propertyField(
JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
if (propertyNode.has(CONST)) {
field.annotate(Pattern.class).param("regexp", propertyNode.get(CONST).asText());
}
}
}
Loading
Loading