Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb409915

Browse files
committed
[Fix#498] Adding possibility to validate before parsing
Fix#498Signed-off-by: Francisco Javier Tirado Sarti <ftirados@redhat.com>
1 parent0939916 commitb409915

File tree

12 files changed

+255
-63
lines changed

12 files changed

+255
-63
lines changed

‎api/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<groupId>com.fasterxml.jackson.core</groupId>
2222
<artifactId>jackson-core</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>com.networknt</groupId>
26+
<artifactId>json-schema-validator</artifactId>
27+
</dependency>
2428
<dependency>
2529
<groupId>com.fasterxml.jackson.core</groupId>
2630
<artifactId>jackson-databind</artifactId>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
packageio.serverlessworkflow.api;
17+
18+
importio.serverlessworkflow.api.types.Workflow;
19+
importjava.io.IOException;
20+
importjava.io.InputStream;
21+
importjava.io.Reader;
22+
23+
classDirectReaderimplementsWorkflowReaderOperations {
24+
25+
@Override
26+
publicWorkflowread(InputStreaminput,WorkflowFormatformat)throwsIOException {
27+
returnformat.mapper().readValue(input,Workflow.class);
28+
}
29+
30+
@Override
31+
publicWorkflowread(Readerinput,WorkflowFormatformat)throwsIOException {
32+
returnformat.mapper().readValue(input,Workflow.class);
33+
}
34+
35+
@Override
36+
publicWorkflowread(byte[]input,WorkflowFormatformat)throwsIOException {
37+
returnformat.mapper().readValue(input,Workflow.class);
38+
}
39+
40+
@Override
41+
publicWorkflowread(Stringinput,WorkflowFormatformat)throwsIOException {
42+
returnformat.mapper().readValue(input,Workflow.class);
43+
}
44+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
packageio.serverlessworkflow.api;
17+
18+
importcom.fasterxml.jackson.databind.JsonNode;
19+
importcom.networknt.schema.InputFormat;
20+
importcom.networknt.schema.JsonSchema;
21+
importcom.networknt.schema.JsonSchemaFactory;
22+
importcom.networknt.schema.SchemaValidatorsConfig;
23+
importcom.networknt.schema.SpecVersion.VersionFlag;
24+
importcom.networknt.schema.ValidationMessage;
25+
importio.serverlessworkflow.api.types.Workflow;
26+
importjava.io.IOException;
27+
importjava.io.InputStream;
28+
importjava.io.Reader;
29+
importjava.util.Set;
30+
importjava.util.stream.Collectors;
31+
32+
classValidationReaderimplementsWorkflowReaderOperations {
33+
privatefinalJsonSchemaschemaObject;
34+
35+
ValidationReader() {
36+
this.schemaObject =
37+
JsonSchemaFactory.getInstance(VersionFlag.V7)
38+
.getSchema(
39+
Thread.currentThread()
40+
.getContextClassLoader()
41+
.getResourceAsStream("schema/workflow.yaml"),
42+
InputFormat.YAML,
43+
SchemaValidatorsConfig.builder().build());
44+
}
45+
46+
@Override
47+
publicWorkflowread(InputStreaminput,WorkflowFormatformat)throwsIOException {
48+
returnvalidate(format.mapper().readValue(input,JsonNode.class),format);
49+
}
50+
51+
@Override
52+
publicWorkflowread(Readerinput,WorkflowFormatformat)throwsIOException {
53+
returnvalidate(format.mapper().readValue(input,JsonNode.class),format);
54+
}
55+
56+
@Override
57+
publicWorkflowread(byte[]input,WorkflowFormatformat)throwsIOException {
58+
returnvalidate(format.mapper().readValue(input,JsonNode.class),format);
59+
}
60+
61+
@Override
62+
publicWorkflowread(Stringinput,WorkflowFormatformat)throwsIOException {
63+
returnvalidate(format.mapper().readValue(input,JsonNode.class),format);
64+
}
65+
66+
privateWorkflowvalidate(JsonNodevalue,WorkflowFormatformat) {
67+
Set<ValidationMessage>validationErrors =schemaObject.validate(value);
68+
if (!validationErrors.isEmpty()) {
69+
thrownewIllegalArgumentException(
70+
validationErrors.stream()
71+
.map(ValidationMessage::toString)
72+
.collect(Collectors.joining("\n")));
73+
}
74+
returnformat.mapper().convertValue(value,Workflow.class);
75+
}
76+
}

‎api/src/main/java/io/serverlessworkflow/api/WorkflowReader.java

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,98 @@
1616
packageio.serverlessworkflow.api;
1717

1818
importio.serverlessworkflow.api.types.Workflow;
19-
importjava.io.ByteArrayInputStream;
2019
importjava.io.FileNotFoundException;
2120
importjava.io.IOException;
2221
importjava.io.InputStream;
2322
importjava.io.Reader;
24-
importjava.io.StringReader;
2523
importjava.nio.file.Files;
2624
importjava.nio.file.Path;
2725

2826
publicclassWorkflowReader {
2927

3028
publicstaticWorkflowreadWorkflow(InputStreaminput,WorkflowFormatformat)throwsIOException {
31-
returnformat.mapper().readValue(input,Workflow.class);
29+
returndefaultReader().read(input,format);
3230
}
3331

3432
publicstaticWorkflowreadWorkflow(Readerinput,WorkflowFormatformat)throwsIOException {
35-
returnformat.mapper().readValue(input,Workflow.class);
33+
returndefaultReader().read(input,format);
3634
}
3735

38-
publicstaticWorkflowreadWorkflow(Pathpath,WorkflowFormatformat)throwsIOException {
39-
returnformat.mapper().readValue(Files.readAllBytes(path),Workflow.class);
36+
publicstaticWorkflowreadWorkflow(byte[]input,WorkflowFormatformat)throwsIOException {
37+
returndefaultReader().read(input,format);
4038
}
4139

42-
publicstaticWorkflowreadWorkflow(byte[]content,WorkflowFormatformat)throwsIOException {
43-
try (InputStreaminput =newByteArrayInputStream(content)) {
44-
returnreadWorkflow(input,format);
45-
}
40+
publicstaticWorkflowreadWorkflow(Pathpath)throwsIOException {
41+
returnreadWorkflow(defaultReader(),path,WorkflowFormat.fromPath(path));
42+
}
43+
44+
publicstaticWorkflowreadWorkflow(Pathpath,WorkflowFormatformat)throwsIOException {
45+
returnreadWorkflow(defaultReader(),path,format);
4646
}
4747

48-
publicstaticWorkflowreadWorkflowFromString(Stringcontent,WorkflowFormatformat)
48+
publicstaticWorkflowreadWorkflowFromString(Stringinput,WorkflowFormatformat)
4949
throwsIOException {
50-
try (Readerreader =newStringReader(content)) {
51-
returnreadWorkflow(reader,format);
52-
}
50+
returndefaultReader().read(input,format);
5351
}
5452

5553
publicstaticWorkflowreadWorkflowFromClasspath(Stringclasspath)throwsIOException {
54+
returnreadWorkflowFromClasspath(defaultReader(),classpath);
55+
}
56+
57+
publicstaticWorkflowreadWorkflowFromClasspath(
58+
Stringclasspath,ClassLoadercl,WorkflowFormatformat)throwsIOException {
59+
returnreadWorkflowFromClasspath(defaultReader(),classpath);
60+
}
61+
62+
publicstaticWorkflowreadWorkflow(WorkflowReaderOperationsreader,Pathpath)
63+
throwsIOException {
64+
returnreadWorkflow(reader,path,WorkflowFormat.fromPath(path));
65+
}
66+
67+
publicstaticWorkflowreadWorkflow(
68+
WorkflowReaderOperationsreader,Pathpath,WorkflowFormatformat)throwsIOException {
69+
returnreader.read(Files.readAllBytes(path),format);
70+
}
71+
72+
publicstaticWorkflowreadWorkflowFromClasspath(
73+
WorkflowReaderOperationsreader,Stringclasspath)throwsIOException {
5674
returnreadWorkflowFromClasspath(
75+
reader,
5776
classpath,
5877
Thread.currentThread().getContextClassLoader(),
5978
WorkflowFormat.fromFileName(classpath));
6079
}
6180

6281
publicstaticWorkflowreadWorkflowFromClasspath(
63-
Stringclasspath,ClassLoadercl,WorkflowFormatformat)throwsIOException {
82+
WorkflowReaderOperationsreader,Stringclasspath,ClassLoadercl,WorkflowFormatformat)
83+
throwsIOException {
6484
try (InputStreamin =cl.getResourceAsStream(classpath)) {
6585
if (in ==null) {
6686
thrownewFileNotFoundException(classpath);
6787
}
68-
returnreadWorkflow(in,format);
88+
returnreader.read(in,format);
6989
}
7090
}
7191

92+
publicstaticWorkflowReaderOperationsnoValidation() {
93+
returnNoValidationHolder.instance;
94+
}
95+
96+
publicstaticWorkflowReaderOperationsvalidation() {
97+
returnValidationHolder.instance;
98+
}
99+
100+
privatestaticclassNoValidationHolder {
101+
privatestaticfinalWorkflowReaderOperationsinstance =newDirectReader();
102+
}
103+
104+
privatestaticclassValidationHolder {
105+
privatestaticfinalWorkflowReaderOperationsinstance =newValidationReader();
106+
}
107+
108+
privatestaticWorkflowReaderOperationsdefaultReader() {
109+
returnNoValidationHolder.instance;
110+
}
111+
72112
privateWorkflowReader() {}
73113
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
packageio.serverlessworkflow.api;
17+
18+
importio.serverlessworkflow.api.types.Workflow;
19+
importjava.io.IOException;
20+
importjava.io.InputStream;
21+
importjava.io.Reader;
22+
23+
publicinterfaceWorkflowReaderOperations {
24+
Workflowread(InputStreaminput,WorkflowFormatformat)throwsIOException;
25+
26+
Workflowread(Readerinput,WorkflowFormatformat)throwsIOException;
27+
28+
Workflowread(byte[]input,WorkflowFormatformat)throwsIOException;
29+
30+
Workflowread(Stringinput,WorkflowFormatformat)throwsIOException;
31+
}

‎api/src/main/java/io/serverlessworkflow/api/WorkflowWriter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
importjava.io.ByteArrayOutputStream;
2020
importjava.io.IOException;
2121
importjava.io.OutputStream;
22-
importjava.io.StringWriter;
2322
importjava.io.Writer;
2423
importjava.nio.file.Files;
2524
importjava.nio.file.Path;
@@ -49,10 +48,7 @@ public static void writeWorkflow(Path output, Workflow workflow, WorkflowFormat
4948

5049
publicstaticStringworkflowAsString(Workflowworkflow,WorkflowFormatformat)
5150
throwsIOException {
52-
try (Writerwriter =newStringWriter()) {
53-
writeWorkflow(writer,workflow,format);
54-
returnwriter.toString();
55-
}
51+
returnformat.mapper().writeValueAsString(workflow);
5652
}
5753

5854
publicstaticbyte[]workflowAsBytes(Workflowworkflow,WorkflowFormatformat)

‎api/src/test/java/io/serverlessworkflow/api/FeaturesTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
importstaticio.serverlessworkflow.api.WorkflowReader.readWorkflow;
1919
importstaticio.serverlessworkflow.api.WorkflowReader.readWorkflowFromClasspath;
20+
importstaticio.serverlessworkflow.api.WorkflowReader.validation;
2021
importstaticio.serverlessworkflow.api.WorkflowWriter.workflowAsBytes;
2122
importstaticio.serverlessworkflow.api.WorkflowWriter.workflowAsString;
2223
importstaticio.serverlessworkflow.api.WorkflowWriter.writeWorkflow;
@@ -53,13 +54,13 @@ public class FeaturesTest {
5354
"features/set.yaml",
5455
"features/switch.yaml",
5556
"features/try.yaml",
56-
"features/listen.yaml",
57+
"features/listen-to-any.yaml",
5758
"features/callFunction.yaml",
5859
"features/callCustomFunction.yaml",
5960
"features/call-http-query-parameters.yaml"
6061
})
6162
publicvoidtestSpecFeaturesParsing(StringworkflowLocation)throwsIOException {
62-
Workflowworkflow =readWorkflowFromClasspath(workflowLocation);
63+
Workflowworkflow =readWorkflowFromClasspath(validation(),workflowLocation);
6364
assertWorkflow(workflow);
6465
assertWorkflowEquals(workflow,writeAndReadInMemory(workflow));
6566
}
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
document:
2-
dsl:1.0.0-alpha5
3-
namespace:test
4-
name:call-example
5-
version:0.1.0
6-
schedule:
7-
cron:0 8 * * *
2+
dsl:'1.0.0-alpha5'
3+
namespace:samples
4+
name:call-custom-function-inline
5+
version:'0.1.0'
6+
use:
7+
functions:
8+
getPetById:
9+
input:
10+
schema:
11+
document:
12+
type:object
13+
properties:
14+
petId:
15+
type:string
16+
required:[ petId ]
17+
call:http
18+
with:
19+
method:get
20+
endpoint:https://petstore.swagger.io/v2/pet/{petId}
821
do:
9-
-getData:
10-
call:http
11-
with:
12-
method:get
13-
endpoint:https://api.agify.io?name=meelad
14-
output:
15-
as:".data.reading"
16-
-filterData:
17-
for:
18-
in:".data.reading"
19-
each:reading
20-
do:
21-
-log:
22-
call:https://raw.githubusercontent.com/serverlessworkflow/catalog/main/functions/log/1.0.0/function.yaml
23-
with:
24-
level:information
25-
format:"{TIMESTAMP} [{LEVEL}] ({CONTEXT}): {MESSAGE}"
26-
message:Hello, world!
27-
timestamp:true
22+
-getPet:
23+
call:getPetById
24+
with:
25+
petId:69

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ do:
88
call:openapi
99
with:
1010
document:
11-
uri:"https://petstore.swagger.io/v2/swagger.json"
11+
endpoint:"https://petstore.swagger.io/v2/swagger.json"
1212
operationId:findPetsByStatus
1313
parameters:
1414
status:${ .status }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp