Skip to content

Commit ec10c82

Browse files
Created a new POJO for DataInputSchema and the required changes to support inline schema (#209)
* Created a new POJO for DataInputSchema to support inline schema Signed-off-by: Vani Haripriya Mudadla <[email protected]> * Included a test for null schema and updated deserializer as per comments Signed-off-by: Vani Haripriya Mudadla <[email protected]> * Updated DataInputSchemaDeserializer Signed-off-by: Vani Haripriya Mudadla <[email protected]> Signed-off-by: Vani Haripriya Mudadla <[email protected]>
1 parent 5ecc196 commit ec10c82

File tree

15 files changed

+228
-53
lines changed

15 files changed

+228
-53
lines changed

api/src/main/java/io/serverlessworkflow/api/deserializers/DataInputSchemaDeserializer.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import com.fasterxml.jackson.databind.DeserializationContext;
2020
import com.fasterxml.jackson.databind.JsonNode;
2121
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
22-
import io.serverlessworkflow.api.datainputschema.DataInputSchema;
2322
import io.serverlessworkflow.api.interfaces.WorkflowPropertySource;
23+
import io.serverlessworkflow.api.workflow.DataInputSchema;
2424
import java.io.IOException;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527

2628
public class DataInputSchemaDeserializer extends StdDeserializer<DataInputSchema> {
2729

2830
private static final long serialVersionUID = 510l;
31+
private static Logger logger = LoggerFactory.getLogger(DataInputSchemaDeserializer.class);
2932

3033
public DataInputSchemaDeserializer() {
3134
this(DataInputSchema.class);
@@ -46,17 +49,17 @@ public DataInputSchema deserialize(JsonParser jp, DeserializationContext ctxt)
4649
JsonNode node = jp.getCodec().readTree(jp);
4750

4851
DataInputSchema dataInputSchema = new DataInputSchema();
52+
JsonNode schemaDefinition = null;
4953

50-
if (!node.isObject()) {
51-
dataInputSchema.setSchema(node.asText());
52-
dataInputSchema.setFailOnValidationErrors(true); // default
53-
54-
return dataInputSchema;
55-
} else {
56-
dataInputSchema.setSchema(node.get("schema").asText());
54+
if (node.isObject() && node.get("schema").isObject() && !node.get("schema").isEmpty()) {
55+
schemaDefinition = node.get("schema");
5756
dataInputSchema.setFailOnValidationErrors(node.get("failOnValidationErrors").asBoolean());
58-
59-
return dataInputSchema;
57+
dataInputSchema.setSchemaDef(schemaDefinition);
58+
} else {
59+
String schemaFileDef = node.isObject() ? node.get("schema").asText() : node.asText();
60+
dataInputSchema.setFailOnValidationErrors(true);
61+
dataInputSchema.setRefValue(schemaFileDef);
6062
}
63+
return dataInputSchema;
6164
}
6265
}

api/src/main/java/io/serverlessworkflow/api/mapper/WorkflowModule.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.fasterxml.jackson.databind.module.SimpleModule;
1919
import io.serverlessworkflow.api.auth.AuthDefinition;
2020
import io.serverlessworkflow.api.cron.Cron;
21-
import io.serverlessworkflow.api.datainputschema.DataInputSchema;
2221
import io.serverlessworkflow.api.deserializers.*;
2322
import io.serverlessworkflow.api.end.ContinueAs;
2423
import io.serverlessworkflow.api.end.End;

api/src/main/java/io/serverlessworkflow/api/serializers/WorkflowSerializer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ public void serialize(Workflow workflow, JsonGenerator gen, SerializerProvider p
6868
}
6969

7070
if (workflow.getDataInputSchema() != null) {
71-
if (workflow.getDataInputSchema().getSchema() != null
72-
&& workflow.getDataInputSchema().getSchema().length() > 0
71+
if (workflow.getDataInputSchema().getRefValue() != null
72+
&& workflow.getDataInputSchema().getRefValue().length() > 0
7373
&& workflow.getDataInputSchema().isFailOnValidationErrors()) {
74-
gen.writeStringField("dataInputSchema", workflow.getDataInputSchema().getSchema());
74+
gen.writeStringField("dataInputSchema", workflow.getDataInputSchema().getRefValue());
7575

76-
} else if (workflow.getDataInputSchema().getSchema() != null
77-
&& workflow.getDataInputSchema().getSchema().length() > 0
76+
} else if (workflow.getDataInputSchema().getSchemaDef() != null
77+
&& !workflow.getDataInputSchema().getSchemaDef().isEmpty()
7878
&& !workflow.getDataInputSchema().isFailOnValidationErrors()) {
79-
gen.writeObjectField("dataInputSchema", workflow.getDataInputSchema());
79+
gen.writeObjectField("dataInputSchema", workflow.getDataInputSchema().getSchemaDef());
8080
}
8181
}
8282

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.api.workflow;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
20+
public class DataInputSchema {
21+
private String refValue;
22+
private JsonNode schemaDef;
23+
private boolean failOnValidationErrors = true;
24+
25+
public DataInputSchema() {}
26+
27+
public DataInputSchema(String refValue){
28+
this.refValue = refValue;
29+
}
30+
public String getRefValue() {
31+
return refValue;
32+
}
33+
34+
public void setRefValue(String refValue) {
35+
this.refValue = refValue;
36+
}
37+
38+
public JsonNode getSchemaDef() {
39+
return schemaDef;
40+
}
41+
42+
public void setSchemaDef(JsonNode schemaDef) {
43+
this.schemaDef = schemaDef;
44+
}
45+
46+
public boolean isFailOnValidationErrors() {
47+
return failOnValidationErrors;
48+
}
49+
50+
public void setFailOnValidationErrors(boolean failOnValidationErrors) {
51+
this.failOnValidationErrors = failOnValidationErrors;
52+
}
53+
}

api/src/main/resources/schema/datainputschema/datainputschema.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

api/src/main/resources/schema/workflow.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
}
3737
},
3838
"dataInputSchema": {
39-
"$ref": "datainputschema/datainputschema.json",
39+
"type": "object",
40+
"existingJavaType": "io.serverlessworkflow.api.workflow.DataInputSchema",
4041
"description": "Workflow data input schema"
4142
},
4243
"start": {
@@ -161,4 +162,4 @@
161162
"version",
162163
"states"
163164
]
164-
}
165+
}

api/src/test/java/io/serverlessworkflow/api/test/MarkupToWorkflowTest.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.serverlessworkflow.api.actions.Action;
2323
import io.serverlessworkflow.api.auth.AuthDefinition;
2424
import io.serverlessworkflow.api.branches.Branch;
25-
import io.serverlessworkflow.api.datainputschema.DataInputSchema;
2625
import io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition;
2726
import io.serverlessworkflow.api.end.End;
2827
import io.serverlessworkflow.api.events.EventDefinition;
@@ -39,10 +38,7 @@
3938
import io.serverlessworkflow.api.switchconditions.DataCondition;
4039
import io.serverlessworkflow.api.test.utils.WorkflowTestUtils;
4140
import io.serverlessworkflow.api.timeouts.WorkflowExecTimeout;
42-
import io.serverlessworkflow.api.workflow.Constants;
43-
import io.serverlessworkflow.api.workflow.Events;
44-
import io.serverlessworkflow.api.workflow.Retries;
45-
import io.serverlessworkflow.api.workflow.Secrets;
41+
import io.serverlessworkflow.api.workflow.*;
4642
import java.util.List;
4743
import org.junit.jupiter.params.ParameterizedTest;
4844
import org.junit.jupiter.params.provider.ValueSource;
@@ -467,7 +463,12 @@ public void testRetriesProps(String workflowLocation) {
467463

468464
@ParameterizedTest
469465
@ValueSource(
470-
strings = {"/features/datainputschemastring.json", "/features/datainputschemastring.yml"})
466+
strings = {
467+
"/features/datainputschemastring.json",
468+
"/features/datainputschemastring.yml",
469+
"/features/datainputschemaobjstring.json",
470+
"/features/datainputschemaobjstring.yml"
471+
})
471472
public void testDataInputSchemaFromString(String workflowLocation) {
472473
Workflow workflow = Workflow.fromSource(WorkflowTestUtils.readWorkflowFile(workflowLocation));
473474

@@ -478,7 +479,23 @@ public void testDataInputSchemaFromString(String workflowLocation) {
478479

479480
DataInputSchema dataInputSchema = workflow.getDataInputSchema();
480481
assertNotNull(dataInputSchema);
481-
assertEquals("somejsonschema.json", dataInputSchema.getSchema());
482+
assertEquals("features/somejsonschema.json", dataInputSchema.getRefValue());
483+
assertTrue(dataInputSchema.isFailOnValidationErrors());
484+
}
485+
486+
@ParameterizedTest
487+
@ValueSource(strings = {"/features/datainputschemawithnullschema.json"})
488+
public void testDataInputSchemaWithNullSchema(String workflowLocation) {
489+
Workflow workflow = Workflow.fromSource(WorkflowTestUtils.readWorkflowFile(workflowLocation));
490+
491+
assertNotNull(workflow);
492+
assertNotNull(workflow.getId());
493+
assertNotNull(workflow.getName());
494+
assertNotNull(workflow.getStates());
495+
496+
DataInputSchema dataInputSchema = workflow.getDataInputSchema();
497+
assertNotNull(dataInputSchema);
498+
assertEquals("null", dataInputSchema.getRefValue());
482499
assertTrue(dataInputSchema.isFailOnValidationErrors());
483500
}
484501

@@ -492,9 +509,17 @@ public void testDataInputSchemaFromObject(String workflowLocation) {
492509
assertNotNull(workflow.getName());
493510
assertNotNull(workflow.getStates());
494511

512+
assertNotNull(workflow.getDataInputSchema());
495513
DataInputSchema dataInputSchema = workflow.getDataInputSchema();
496-
assertNotNull(dataInputSchema);
497-
assertEquals("somejsonschema.json", dataInputSchema.getSchema());
514+
assertNotNull(dataInputSchema.getSchemaDef());
515+
516+
JsonNode schemaObj = dataInputSchema.getSchemaDef();
517+
assertNotNull(schemaObj.get("properties"));
518+
JsonNode properties = schemaObj.get("properties");
519+
assertNotNull(properties.get("firstName"));
520+
JsonNode typeNode = properties.get("firstName");
521+
JsonNode stringNode = typeNode.get("type");
522+
assertEquals("string", stringNode.asText());
498523
assertFalse(dataInputSchema.isFailOnValidationErrors());
499524
}
500525

api/src/test/resources/features/datainputschemaobj.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
"specVersion": "0.8",
55
"name": "Data Input Schema test",
66
"dataInputSchema": {
7-
"schema": "somejsonschema.json",
7+
"schema":{
8+
"title": "MyJSONSchema",
9+
"properties":{
10+
"firstName":{
11+
"type": "string"
12+
},
13+
"lastName":{
14+
"type": "string"
15+
}
16+
}
17+
},
818
"failOnValidationErrors": false
919
},
1020
"start": "TestFunctionRefs",

api/src/test/resources/features/datainputschemaobj.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
---
12
id: datainputschemaobj
23
version: '1.0'
34
specVersion: '0.8'
45
name: Data Input Schema test
56
dataInputSchema:
6-
schema: somejsonschema.json
7+
schema:
8+
title: MyJSONSchema
9+
properties:
10+
firstName:
11+
type: string
12+
lastName:
13+
type: string
714
failOnValidationErrors: false
815
start: TestFunctionRefs
916
states:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"id": "datainputschemaobj",
3+
"version": "1.0",
4+
"specVersion": "0.8",
5+
"name": "Data Input Schema test",
6+
"dataInputSchema": "features/somejsonschema.json",
7+
"start": "TestFunctionRefs",
8+
"states": [
9+
{
10+
"name": "TestFunctionRefs",
11+
"type": "operation",
12+
"actionMode": "sequential",
13+
"actions": [
14+
{
15+
"functionRef": "creditCheckFunction"
16+
},
17+
{
18+
"functionRef": {
19+
"refName": "sendRejectionEmailFunction",
20+
"arguments": {
21+
"applicant": "${ .customer }"
22+
}
23+
}
24+
}
25+
],
26+
"end": true
27+
}
28+
]
29+
}

0 commit comments

Comments
 (0)