-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net Processes - Support Complex Type Serialization for Dapr Events a…
…nd Messages (#9525) ### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> DAPR events and messages result in runtime failure when contain anything other than the supported primitive types: https://docs.dapr.io/developing-applications/sdks/dotnet/dotnet-actors/dotnet-actors-serialization/#supported-primitive-types This means arrays, lists, and objects are not currently able to be emitted by steps. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Translating `ProcessMessage`, `ProcessEvent`, and `KernelProcessMessage` into JSON strings prior to data-contract serialization. As part of this, any relevant type information is included in the json payload. On deserialization, the type information is utilized to convert any `JsonElement` data values to the expected type. **Notes:** - Added unit tests (DAPR specific project) - Removed `DataContract` where not needed (and tests) - Verified DAPR demo - Attempted to minimize postfix null-supression operator as able. - Replaced `var` with explicit types for my own clarity...and left them for posterity. - Eliminated redundant namespace scoping ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
- Loading branch information
Showing
31 changed files
with
809 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...t/src/Experimental/Process.Runtime.Dapr.UnitTests/KernelProcessEventSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Process.Runtime; | ||
using Microsoft.SemanticKernel.Process.Serialization; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Process.Dapr.Runtime.UnitTests; | ||
|
||
/// <summary> | ||
/// Unit tests for the <see cref="ProcessEvent"/> class. | ||
/// </summary> | ||
public class KernelProcessEventSerializationTests | ||
{ | ||
/// <summary> | ||
/// Validates that a <see cref="KernelProcessEvent"/> can be serialized and deserialized correctly | ||
/// with out an explicit type definition for <see cref="KernelProcessEvent.Data"/> | ||
/// </summary> | ||
[Fact] | ||
public void VerifySerializeEventSingleTest() | ||
{ | ||
// Arrange, Act & Assert | ||
VerifyContainerSerialization([new() { Id = "Test", Data = 3 }]); | ||
VerifyContainerSerialization([new() { Id = "Test", Data = "test" }]); | ||
VerifyContainerSerialization([new() { Id = "Test", Data = Guid.NewGuid() }]); | ||
VerifyContainerSerialization([new() { Id = "Test", Data = new int[] { 1, 2, 3, 4 } }]); | ||
VerifyContainerSerialization([new() { Id = "Test", Data = new ComplexData { Id = "test", Value = 3 } }]); | ||
VerifyContainerSerialization([new() { Id = "testid", Data = KernelProcessError.FromException(new InvalidOperationException()) }]); | ||
} | ||
|
||
/// <summary> | ||
/// Validates that a list <see cref="KernelProcessEvent"/> can be serialized and deserialized correctly | ||
/// with out varying types assigned to for <see cref="KernelProcessEvent.Data"/> | ||
/// </summary> | ||
[Fact] | ||
public void VerifySerializeEventMixedTest() | ||
{ | ||
// Arrange, Act & Assert | ||
VerifyContainerSerialization( | ||
[ | ||
new() { Id = "Test", Data = 3 }, | ||
new() { Id = "Test", Data = "test" }, | ||
new() { Id = "Test", Data = Guid.NewGuid() }, | ||
new() { Id = "Test", Data = new int[] { 1, 2, 3, 4 } }, | ||
new() { Id = "Test", Data = new ComplexData { Id = "test", Value = 3 } }, | ||
new() { Id = "testid", Data = KernelProcessError.FromException(new InvalidOperationException()) }, | ||
]); | ||
} | ||
|
||
/// <summary> | ||
/// Validates that a list <see cref="KernelProcessEvent"/> can be serialized and deserialized correctly | ||
/// with out varying types assigned to for <see cref="KernelProcessEvent.Data"/> | ||
/// </summary> | ||
[Fact] | ||
public void VerifyDataContractSerializationTest() | ||
{ | ||
// Arrange | ||
KernelProcessEvent[] processEvents = | ||
[ | ||
new() { Id = "Test", Data = 3 }, | ||
new() { Id = "Test", Data = "test" }, | ||
new() { Id = "Test", Data = Guid.NewGuid() }, | ||
new() { Id = "Test", Data = new int[] { 1, 2, 3, 4 } }, | ||
new() { Id = "Test", Data = new ComplexData { Id = "test", Value = 3 } }, | ||
new() { Id = "testid", Data = KernelProcessError.FromException(new InvalidOperationException()) }, | ||
]; | ||
List<string> jsonEvents = []; | ||
foreach (KernelProcessEvent processEvent in processEvents) | ||
{ | ||
jsonEvents.Add(KernelProcessEventSerializer.ToJson(processEvent)); | ||
} | ||
|
||
// Act | ||
using MemoryStream stream = new(); | ||
jsonEvents.Serialize(stream); | ||
stream.Position = 0; | ||
|
||
List<string>? copy = stream.Deserialize<List<string>>(); | ||
|
||
// Assert | ||
Assert.NotNull(copy); | ||
|
||
// Act | ||
IList<KernelProcessEvent> copiedEvents = KernelProcessEventSerializer.ToKernelProcessEvents(jsonEvents); | ||
|
||
// Assert | ||
Assert.Equivalent(processEvents, copiedEvents); | ||
} | ||
|
||
private static void VerifyContainerSerialization(KernelProcessEvent[] processEvents) | ||
{ | ||
// Arrange | ||
List<string> jsonEvents = []; | ||
foreach (KernelProcessEvent processEvent in processEvents) | ||
{ | ||
jsonEvents.Add(KernelProcessEventSerializer.ToJson(processEvent)); | ||
} | ||
|
||
// Act | ||
IList<KernelProcessEvent> copiedEvents = KernelProcessEventSerializer.ToKernelProcessEvents(jsonEvents); | ||
|
||
// Assert | ||
Assert.Equivalent(processEvents, copiedEvents); | ||
} | ||
|
||
internal sealed class ComplexData | ||
{ | ||
public string Id { get; init; } = string.Empty; | ||
|
||
public int Value { get; init; } | ||
} | ||
} |
Oops, something went wrong.