Skip to content

update to function definition types #46

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.api.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import io.serverlessworkflow.api.functions.FunctionDefinition;
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class FunctionDefinitionTypeDeserializer extends StdDeserializer<FunctionDefinition.Type> {

private static final long serialVersionUID = 510l;
private static Logger logger = LoggerFactory.getLogger(FunctionDefinitionTypeDeserializer.class);

private WorkflowPropertySource context;

public FunctionDefinitionTypeDeserializer() {
this(FunctionDefinition.Type.class);
}

public FunctionDefinitionTypeDeserializer(WorkflowPropertySource context) {
this(FunctionDefinition.Type.class);
this.context = context;
}

public FunctionDefinitionTypeDeserializer(Class<?> vc) {
super(vc);
}

@Override
public FunctionDefinition.Type deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException {

String value = jp.getText();
if (context != null) {
try {
String result = context.getPropertySource().getProperty(value);

if (result != null) {
return FunctionDefinition.Type.fromValue(result);
} else {
return FunctionDefinition.Type.fromValue(jp.getText());
}
} catch (Exception e) {
logger.info("Exception trying to evaluate property: {}", e.getMessage());
return FunctionDefinition.Type.fromValue(jp.getText());
}
} else {
return FunctionDefinition.Type.fromValue(jp.getText());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.serverlessworkflow.api.end.End;
import io.serverlessworkflow.api.events.EventDefinition;
import io.serverlessworkflow.api.events.OnEvents;
import io.serverlessworkflow.api.functions.FunctionDefinition;
import io.serverlessworkflow.api.interfaces.Extension;
import io.serverlessworkflow.api.interfaces.State;
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
Expand Down Expand Up @@ -91,6 +92,7 @@ private void addDefaultDeserializers() {
addDeserializer(Start.class, new StartDefinitionDeserializer(workflowPropertySource));
addDeserializer(End.class, new EndDefinitionDeserializer(workflowPropertySource));
addDeserializer(Extension.class, extensionDeserializer);
addDeserializer(FunctionDefinition.Type.class, new FunctionDefinitionTypeDeserializer(workflowPropertySource));

}

Expand Down
11 changes: 10 additions & 1 deletion api/src/main/resources/schema/functions/functiondef.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@
},
"operation": {
"type": "string",
"description": "Combination of the function/service OpenAPI definition URI and the operationID of the operation that needs to be invoked, separated by a '#'. For example 'https://petstore.swagger.io/v2/swagger.json#getPetById'",
"description": "If type `rest`, combination of the function/service OpenAPI definition URI and the operationID of the operation that needs to be invoked, separated by a '#'. If type is `expression` defines the workflow expression.",
"minLength": 1
},
"type": {
"type": "string",
"description": "Defines the function type. Is either `rest` or `expression`. Default is `rest`",
"enum": [
"rest",
"expression"
],
"default": "rest"
},
"metadata": {
"$ref": "../metadata/metadata.json"
}
Expand Down
4 changes: 0 additions & 4 deletions api/src/main/resources/schema/transitions/transition.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"type": "object",
"javaType": "io.serverlessworkflow.api.transitions.Transition",
"properties": {
"expression": {
"type": "string",
"description": "Common Expression Language (CEL) expression. Must evaluate to true for the transition to be valid"
},
"produceEvents": {
"type": "array",
"description": "Array of events to be produced",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package io.serverlessworkflow.api.test;

import io.serverlessworkflow.api.Workflow;
import io.serverlessworkflow.api.functions.FunctionDefinition;
import io.serverlessworkflow.api.interfaces.State;
import io.serverlessworkflow.api.states.EventState;
import io.serverlessworkflow.api.states.OperationState;
import io.serverlessworkflow.api.test.utils.WorkflowTestUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class MarkupToWorkflowTest {
Expand Down Expand Up @@ -117,4 +120,31 @@ public void testSpecFreatureCompensation(String workflowLocation) {

assertTrue(operationState.isUsedForCompensation());
}

@ParameterizedTest
@ValueSource(strings = {"/features/functiontypes.json", "/features/functiontypes.yml"})
public void testFunctionTypes(String workflowLocation) {
Workflow workflow = Workflow.fromSource(WorkflowTestUtils.readWorkflowFile(workflowLocation));

assertNotNull(workflow);
assertNotNull(workflow.getId());
assertNotNull(workflow.getName());
assertNotNull(workflow.getStates());

assertNotNull(workflow.getStates());
assertTrue(workflow.getStates().size() == 1);

State state = workflow.getStates().get(0);
assertTrue(state instanceof OperationState);

List<FunctionDefinition> functionDefs = workflow.getFunctions().getFunctionDefs();
assertNotNull(functionDefs);
assertTrue(functionDefs.size() == 2);

FunctionDefinition restFunc = functionDefs.get(0);
assertEquals(restFunc.getType(), FunctionDefinition.Type.REST);

FunctionDefinition restFunc2 = functionDefs.get(1);
assertEquals(restFunc2.getType(), FunctionDefinition.Type.EXPRESSION);
}
}
35 changes: 35 additions & 0 deletions api/src/test/resources/features/functiontypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"id": "functiontypes",
"version": "1.0",
"name": "Function Types Workflow",
"description": "Determine if applicant request is valid",
"functions": [
{
"name": "restFunction",
"operation": "http://myapis.org/applicationapi.json#emailRejection"
},
{
"name": "expressionFunction",
"operation": "$.my.data",
"type" : "expression"
}
],
"states":[
{
"name":"CheckFunctions",
"type":"operation",
"start": true,
"actions":[
{
"functionRef": {
"refName": "restFunction",
"parameters": {
"data": "{{ fn(expressionFunction) }}"
}
}
}
],
"end": true
}
]
}
20 changes: 20 additions & 0 deletions api/src/test/resources/features/functiontypes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
id: functiontypes
version: '1.0'
name: Function Types Workflow
description: Determine if applicant request is valid
functions:
- name: restFunction
operation: http://myapis.org/applicationapi.json#emailRejection
- name: expressionFunction
operation: "$.my.data"
type: expression
states:
- name: CheckFunctions
type: operation
start: true
actions:
- functionRef:
refName: restFunction
parameters:
data: "{{ fn(expressionFunction) }}"
end: true