Skip to content

Commit c722620

Browse files
committed
Fixed serialization, deserialization and validation of input data schemas
1 parent bed2ede commit c722620

File tree

13 files changed

+165
-39
lines changed

13 files changed

+165
-39
lines changed

src/ServerlessWorkflow.Sdk.UnitTests/Cases/Validation/WorkflowValidationTests.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.IO;
11+
using System.Threading.Tasks;
1112
using Xunit;
1213

1314
namespace ServerlessWorkflow.Sdk.UnitTests.Cases.Validation
@@ -287,7 +288,7 @@ public void Validate_Workflow_DuplicateRetryPolicyNames_ShouldFail()
287288
}
288289

289290
[Fact]
290-
public async void Validate_Workflow_WithExternalReferences_ShouldWork()
291+
public async Task Validate_Workflow_WithExternalReferences_ShouldWork()
291292
{
292293
//arrange
293294
var json = File.ReadAllText(Path.Combine("resources", "workflows", "external-function-definition.json"));
@@ -303,6 +304,40 @@ public async void Validate_Workflow_WithExternalReferences_ShouldWork()
303304
deserializedWorkflow.Functions.Should().BeNullOrEmpty();
304305
}
305306

307+
[Fact]
308+
public async Task Validate_Workflow_WithInputDataSchema_ShouldWork()
309+
{
310+
//arrange
311+
var json = File.ReadAllText(Path.Combine("resources", "workflows", "input-data-schema.json"));
312+
var workflow = JsonConvert.DeserializeObject<WorkflowDefinition>(json);
313+
314+
//act
315+
var result = await this.WorkflowValidator.ValidateAsync(workflow);
316+
var loadedWorflowJson = JsonConvert.SerializeObject(workflow);
317+
var deserializedWorkflow = JsonConvert.DeserializeObject<WorkflowDefinition>(loadedWorflowJson);
318+
319+
//assert
320+
result.IsValid.Should().BeTrue();
321+
deserializedWorkflow.DataInputSchema.Should().NotBeNull();
322+
}
323+
324+
[Fact]
325+
public async Task Validate_Workflow_WithExternalInputDataSchema_ShouldWork()
326+
{
327+
//arrange
328+
var json = File.ReadAllText(Path.Combine("resources", "workflows", "external-input-data-schema.json"));
329+
var workflow = JsonConvert.DeserializeObject<WorkflowDefinition>(json);
330+
331+
//act
332+
var result = await this.WorkflowValidator.ValidateAsync(workflow);
333+
var loadedWorflowJson = JsonConvert.SerializeObject(workflow);
334+
var deserializedWorkflow = JsonConvert.DeserializeObject<WorkflowDefinition>(loadedWorflowJson);
335+
336+
//assert
337+
result.IsValid.Should().BeTrue();
338+
deserializedWorkflow.DataInputSchemaUri.Should().NotBeNull();
339+
}
340+
306341
}
307342

308343
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"schema": "file://resources/schemas/input-data.json"
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "object",
3+
"properties":{
4+
"firstName":{
5+
"type": "string"
6+
},
7+
"lastName":{
8+
"type": "string"
9+
}
10+
},
11+
"required": [ "firstName", "lastName" ]
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "flow-with-input-data",
3+
"name": "flow-with-input-data",
4+
"version": "0.1.0",
5+
"specVersion": "0.8",
6+
"dataInputSchema": "file://resources/dataschemas/input-data-schema.json",
7+
"states": [
8+
{
9+
"name": "inject",
10+
"type": "inject",
11+
"data": {},
12+
"end": true
13+
}
14+
]
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "flow-with-input-data",
3+
"name": "flow-with-input-data",
4+
"version": "0.1.0",
5+
"specVersion": "0.8",
6+
"dataInputSchema": {
7+
"schema": "file://resources/schemas/input-data.json"
8+
},
9+
"states": [
10+
{
11+
"name": "inject",
12+
"type": "inject",
13+
"data": {},
14+
"end": true
15+
}
16+
]
17+
}

src/ServerlessWorkflow.Sdk.UnitTests/ServerlessWorkflow.Sdk.UnitTests.csproj

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="FluentAssertions" Version="6.7.0" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
10+
<PackageReference Include="FluentAssertions" Version="6.8.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
1212
<PackageReference Include="Moq" Version="4.18.2" />
1313
<PackageReference Include="xunit" Version="2.4.2" />
1414
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
18-
<PackageReference Include="coverlet.collector" Version="3.1.2">
18+
<PackageReference Include="coverlet.collector" Version="3.2.0">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>
@@ -38,6 +38,9 @@
3838
<None Update="Resources\constants\petstore.yaml">
3939
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4040
</None>
41+
<None Update="Resources\dataSchemas\input-data-schema.json">
42+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43+
</None>
4144
<None Update="Resources\events\petstore.json">
4245
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4346
</None>
@@ -56,6 +59,9 @@
5659
<None Update="Resources\schemas\externalref.json">
5760
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
5861
</None>
62+
<None Update="Resources\schemas\input-data.json">
63+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
64+
</None>
5965
<None Update="Resources\secrets\default.json">
6066
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
6167
</None>
@@ -68,6 +74,12 @@
6874
<None Update="Resources\workflows\externalref.json">
6975
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
7076
</None>
77+
<None Update="Resources\workflows\external-input-data-schema.json">
78+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
79+
</None>
80+
<None Update="Resources\workflows\input-data-schema.json">
81+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
82+
</None>
7183
<None Update="Resources\workflows\missing-transition.json">
7284
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7385
</None>

src/ServerlessWorkflow.Sdk/Models/DataInputSchemaDefinition.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Newtonsoft.Json.Schema;
1818
using System;
1919
using System.ComponentModel;
20-
using System.ComponentModel.DataAnnotations;
2120
using YamlDotNet.Serialization;
2221

2322
namespace ServerlessWorkflow.Sdk.Models
@@ -42,7 +41,7 @@ public class DataInputSchemaDefinition
4241
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.OneOfConverter<JSchema, Uri>))]
4342
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Converters.OneOfConverter<JSchema, Uri>))]
4443

45-
protected virtual OneOf<JSchema, Uri> SchemaValue { get; set; } = null!;
44+
protected virtual OneOf<JSchema, Uri>? SchemaValue { get; set; }
4645

4746
/// <summary>
4847
/// Gets/sets the object used to configure the <see cref="WorkflowDefinition"/>'s data input schema

src/ServerlessWorkflow.Sdk/Models/WorkflowDefinition.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public class WorkflowDefinition
117117
[ProtoMember(10, Name = "dataInputSchema")]
118118
[DataMember(Order = 10, Name = "dataInputSchema")]
119119
[YamlMember(Alias = "dataInputSchema")]
120-
[Newtonsoft.Json.JsonProperty(PropertyName = "dataInputSchema", Order = 10), Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.OneOfConverter<DataInputSchemaDefinition, Uri>))]
121-
[System.Text.Json.Serialization.JsonPropertyName("dataInputSchema"), System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Converters.OneOfConverter<DataInputSchemaDefinition, Uri>))]
122-
protected virtual OneOf<DataInputSchemaDefinition, Uri>? DataInputSchemaValue { get; set; }
120+
[Newtonsoft.Json.JsonProperty(PropertyName = "dataInputSchema", Order = 10), Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.OneOfConverter<Uri, DataInputSchemaDefinition>))]
121+
[System.Text.Json.Serialization.JsonPropertyName("dataInputSchema"), System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Converters.OneOfConverter<Uri, DataInputSchemaDefinition>))]
122+
protected virtual OneOf<Uri, DataInputSchemaDefinition>? DataInputSchemaValue { get; set; }
123123

124124
/// <summary>
125125
/// Gets/sets the object used to configure the <see cref="WorkflowDefinition"/>'s data input schema
@@ -133,7 +133,7 @@ public virtual DataInputSchemaDefinition? DataInputSchema
133133
{
134134
get
135135
{
136-
return this.DataInputSchemaValue?.T1Value;
136+
return this.DataInputSchemaValue?.T2Value;
137137
}
138138
set
139139
{
@@ -156,7 +156,7 @@ public virtual Uri? DataInputSchemaUri
156156
{
157157
get
158158
{
159-
return this.DataInputSchemaValue?.T2Value;
159+
return this.DataInputSchemaValue?.T1Value;
160160
}
161161
set
162162
{

src/ServerlessWorkflow.Sdk/Serialization/NewtonsoftJson/OneOfConverter.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public class OneOfConverter<T1, T2>
3434
public override OneOf<T1, T2>? ReadJson(JsonReader reader, Type objectType, OneOf<T1, T2>? existingValue, bool hasExistingValue, JsonSerializer serializer)
3535
{
3636
var token = JToken.ReadFrom(reader);
37-
if (token == null)
38-
return null;
37+
if (token == null) return null;
3938
try
4039
{
4140
return new(token.ToObject<T1>()!);
@@ -49,12 +48,9 @@ public class OneOfConverter<T1, T2>
4948
/// <inheritdoc/>
5049
public override void WriteJson(JsonWriter writer, OneOf<T1, T2>? value, JsonSerializer serializer)
5150
{
52-
if (value == null)
53-
return;
54-
if (value.T1Value != null)
55-
serializer.Serialize(writer, value.T1Value);
56-
else if(value.T2Value != null)
57-
serializer.Serialize(writer, value.T2Value);
51+
if (value == null) return;
52+
if (value.T1Value != null) serializer.Serialize(writer, value.T1Value);
53+
else if(value.T2Value != null) serializer.Serialize(writer, value.T2Value);
5854
}
5955

6056
}

src/ServerlessWorkflow.Sdk/ServerlessWorkflow.Sdk.csproj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<NeutralLanguage>en</NeutralLanguage>
7-
<AssemblyVersion>0.8.4</AssemblyVersion>
8-
<FileVersion>0.8.4</FileVersion>
9-
<Version>0.8.4</Version>
7+
<AssemblyVersion>0.8.5</AssemblyVersion>
8+
<FileVersion>0.8.5</FileVersion>
9+
<Version>0.8.5</Version>
1010
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1111
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1212
<PackageLicenseFile>LICENSE</PackageLicenseFile>
@@ -43,23 +43,23 @@
4343
</ItemGroup>
4444

4545
<ItemGroup>
46-
<PackageReference Include="CloudNative.CloudEvents" Version="2.3.1" />
46+
<PackageReference Include="CloudNative.CloudEvents" Version="2.5.1" />
4747
<PackageReference Include="Cronos" Version="0.7.1" />
48-
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.2.1" />
48+
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.3.0" />
4949
<PackageReference Include="Iso8601DurationHelper" Version="1.0.5" />
5050
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
5151
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
5252
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
53-
<PackageReference Include="Neuroglia.Serialization.Dynamic" Version="2.0.1.78" />
54-
<PackageReference Include="Neuroglia.Serialization.Json" Version="2.0.1.78" />
55-
<PackageReference Include="Neuroglia.Serialization.NewtonsoftJson" Version="2.0.1.78" />
56-
<PackageReference Include="Neuroglia.Serialization.Protobuf" Version="2.0.1.78" />
57-
<PackageReference Include="Neuroglia.Serialization.YamlDotNet" Version="2.0.1.78" />
53+
<PackageReference Include="Neuroglia.Serialization.Dynamic" Version="2.0.10" />
54+
<PackageReference Include="Neuroglia.Serialization.Json" Version="2.0.10" />
55+
<PackageReference Include="Neuroglia.Serialization.NewtonsoftJson" Version="2.0.10" />
56+
<PackageReference Include="Neuroglia.Serialization.Protobuf" Version="2.0.10" />
57+
<PackageReference Include="Neuroglia.Serialization.YamlDotNet" Version="2.0.10" />
5858
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
5959
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
60-
<PackageReference Include="Octokit" Version="2.0.1" />
61-
<PackageReference Include="protobuf-net.Core" Version="3.1.17" />
62-
<PackageReference Include="YamlDotNet" Version="12.0.0" />
60+
<PackageReference Include="Octokit" Version="4.0.1" />
61+
<PackageReference Include="protobuf-net.Core" Version="3.1.25" />
62+
<PackageReference Include="YamlDotNet" Version="12.0.2" />
6363
</ItemGroup>
6464

6565
</Project>

0 commit comments

Comments
 (0)