Skip to content

Commit

Permalink
[Fix serverlessworkflow#418] Adding const support
Browse files Browse the repository at this point in the history
Signed-off-by: Javier <ftirados@redhat.com>
  • Loading branch information
fjtirado committed Oct 28, 2024
1 parent ec68100 commit a782529
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<usePrimitives>true</usePrimitives>
<useJakartaValidation>true</useJakartaValidation>
<customRuleFactory>io.serverlessworkflow.generator.UnreferencedFactory</customRuleFactory>
<customAnnotator>io.serverlessworkflow.generator.ConstAnnotator</customAnnotator>
</configuration>
<dependencies>
<dependency>
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
18 changes: 18 additions & 0 deletions api/src/test/resources/features/callFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
document:
dsl: 1.0.0-alpha1
namespace: default
name: http-call-with-response-output

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

do:
- getPetFunctionCall:
call: getPet
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());
}
}
}

0 comments on commit a782529

Please sign in to comment.