|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Text.Json; |
| 5 | +using Microsoft.DurableTask.Converters; |
| 6 | + |
| 7 | +namespace Microsoft.DurableTask.Tests; |
| 8 | + |
| 9 | +public class JsonDataConverterTests |
| 10 | +{ |
| 11 | + readonly JsonDataConverter converter = JsonDataConverter.Default; |
| 12 | + |
| 13 | + [Fact] |
| 14 | + public void Serialize_Null_ReturnsNull() |
| 15 | + { |
| 16 | + // Act |
| 17 | + string? result = this.converter.Serialize(null); |
| 18 | + |
| 19 | + // Assert |
| 20 | + result.Should().BeNull(); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void Deserialize_Null_ReturnsNull() |
| 25 | + { |
| 26 | + // Act |
| 27 | + object? result = this.converter.Deserialize(null, typeof(object)); |
| 28 | + |
| 29 | + // Assert |
| 30 | + result.Should().BeNull(); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void RoundTrip_SimpleTypes_PreservesTypes() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + Dictionary<string, object> input = new() |
| 38 | + { |
| 39 | + { "string", "value" }, |
| 40 | + { "int", 42 }, |
| 41 | + { "long", 9223372036854775807L }, |
| 42 | + { "double", 3.14 }, |
| 43 | + { "bool", true }, |
| 44 | + { "null", null! }, |
| 45 | + }; |
| 46 | + |
| 47 | + // Act |
| 48 | + string serialized = this.converter.Serialize(input)!; |
| 49 | + Dictionary<string, object>? result = this.converter.Deserialize<Dictionary<string, object>>(serialized); |
| 50 | + |
| 51 | + // Assert |
| 52 | + result.Should().NotBeNull(); |
| 53 | + result!["string"].Should().BeOfType<string>().And.Be("value"); |
| 54 | + result["int"].Should().BeOfType<int>().And.Be(42); |
| 55 | + |
| 56 | + // Note: Large integers that don't fit in int32 will be deserialized as long |
| 57 | + result["long"].Should().Match<object>(o => o is long || o is double); |
| 58 | + |
| 59 | + result["double"].Should().BeOfType<double>().And.Be(3.14); |
| 60 | + result["bool"].Should().BeOfType<bool>().And.Be(true); |
| 61 | + result["null"].Should().BeNull(); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void RoundTrip_NestedDictionary_PreservesStructure() |
| 66 | + { |
| 67 | + // Arrange |
| 68 | + Dictionary<string, object> input = new() |
| 69 | + { |
| 70 | + { |
| 71 | + "nested", new Dictionary<string, object> |
| 72 | + { |
| 73 | + { "key1", "value1" }, |
| 74 | + { "key2", 123 }, |
| 75 | + } |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + // Act |
| 80 | + string serialized = this.converter.Serialize(input)!; |
| 81 | + Dictionary<string, object>? result = this.converter.Deserialize<Dictionary<string, object>>(serialized); |
| 82 | + |
| 83 | + // Assert |
| 84 | + result.Should().NotBeNull(); |
| 85 | + result!["nested"].Should().BeOfType<Dictionary<string, object>>(); |
| 86 | + Dictionary<string, object> nested = (Dictionary<string, object>)result["nested"]; |
| 87 | + nested["key1"].Should().BeOfType<string>().And.Be("value1"); |
| 88 | + nested["key2"].Should().BeOfType<int>().And.Be(123); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void RoundTrip_Array_PreservesElements() |
| 93 | + { |
| 94 | + // Arrange |
| 95 | + Dictionary<string, object> input = new() |
| 96 | + { |
| 97 | + { "array", new object[] { "string", 42, true } }, |
| 98 | + }; |
| 99 | + |
| 100 | + // Act |
| 101 | + string serialized = this.converter.Serialize(input)!; |
| 102 | + Dictionary<string, object>? result = this.converter.Deserialize<Dictionary<string, object>>(serialized); |
| 103 | + |
| 104 | + // Assert |
| 105 | + result.Should().NotBeNull(); |
| 106 | + result!["array"].Should().BeOfType<object[]>(); |
| 107 | + object[] array = (object[])result["array"]; |
| 108 | + array.Should().HaveCount(3); |
| 109 | + array[0].Should().BeOfType<string>().And.Be("string"); |
| 110 | + array[1].Should().BeOfType<int>().And.Be(42); |
| 111 | + array[2].Should().BeOfType<bool>().And.Be(true); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void RoundTrip_ComplexObject_PreservesStructure() |
| 116 | + { |
| 117 | + // Arrange - simulate the issue described in the GitHub issue |
| 118 | + Dictionary<string, object> input = new() |
| 119 | + { |
| 120 | + { "ComponentContext", new { Name = "TestComponent", Id = 123 } }, |
| 121 | + { "PlanResult", new { Status = "Success", Count = 5 } }, |
| 122 | + }; |
| 123 | + |
| 124 | + // Act |
| 125 | + string serialized = this.converter.Serialize(input)!; |
| 126 | + Dictionary<string, object>? result = this.converter.Deserialize<Dictionary<string, object>>(serialized); |
| 127 | + |
| 128 | + // Assert |
| 129 | + result.Should().NotBeNull(); |
| 130 | + |
| 131 | + // The anonymous objects should be deserialized as dictionaries, not JsonElements |
| 132 | + result!["ComponentContext"].Should().BeOfType<Dictionary<string, object>>(); |
| 133 | + Dictionary<string, object> componentContext = (Dictionary<string, object>)result["ComponentContext"]; |
| 134 | + componentContext["Name"].Should().BeOfType<string>().And.Be("TestComponent"); |
| 135 | + componentContext["Id"].Should().BeOfType<int>().And.Be(123); |
| 136 | + |
| 137 | + result["PlanResult"].Should().BeOfType<Dictionary<string, object>>(); |
| 138 | + Dictionary<string, object> planResult = (Dictionary<string, object>)result["PlanResult"]; |
| 139 | + planResult["Status"].Should().BeOfType<string>().And.Be("Success"); |
| 140 | + planResult["Count"].Should().BeOfType<int>().And.Be(5); |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public void Deserialize_JsonWithoutConverter_ProducesJsonElements() |
| 145 | + { |
| 146 | + // Arrange - use standard System.Text.Json without our custom converter |
| 147 | + JsonSerializerOptions standardOptions = new() { IncludeFields = true }; |
| 148 | + Dictionary<string, object> input = new() |
| 149 | + { |
| 150 | + { "key", "value" }, |
| 151 | + }; |
| 152 | + string json = JsonSerializer.Serialize(input, standardOptions); |
| 153 | + |
| 154 | + // Act |
| 155 | + Dictionary<string, object>? result = JsonSerializer.Deserialize<Dictionary<string, object>>(json, standardOptions); |
| 156 | + |
| 157 | + // Assert - without our converter, values are JsonElements |
| 158 | + result.Should().NotBeNull(); |
| 159 | + result!["key"].Should().BeOfType<JsonElement>(); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void Deserialize_JsonWithConverter_ProducesConcreteTypes() |
| 164 | + { |
| 165 | + // Arrange |
| 166 | + Dictionary<string, object> input = new() |
| 167 | + { |
| 168 | + { "key", "value" }, |
| 169 | + }; |
| 170 | + string json = this.converter.Serialize(input)!; |
| 171 | + |
| 172 | + // Act |
| 173 | + Dictionary<string, object>? result = this.converter.Deserialize<Dictionary<string, object>>(json); |
| 174 | + |
| 175 | + // Assert - with our converter, values are concrete types |
| 176 | + result.Should().NotBeNull(); |
| 177 | + result!["key"].Should().BeOfType<string>().And.Be("value"); |
| 178 | + } |
| 179 | + |
| 180 | + [Fact] |
| 181 | + public void RoundTrip_RecordType_PreservesProperties() |
| 182 | + { |
| 183 | + // Arrange |
| 184 | + TestRecord input = new("TestValue", 42, new Dictionary<string, object> |
| 185 | + { |
| 186 | + { "nested", "data" }, |
| 187 | + }); |
| 188 | + |
| 189 | + // Act |
| 190 | + string serialized = this.converter.Serialize(input)!; |
| 191 | + TestRecord? result = this.converter.Deserialize<TestRecord>(serialized); |
| 192 | + |
| 193 | + // Assert |
| 194 | + result.Should().NotBeNull(); |
| 195 | + result!.Name.Should().Be("TestValue"); |
| 196 | + result.Value.Should().Be(42); |
| 197 | + result.Properties.Should().ContainKey("nested"); |
| 198 | + result.Properties["nested"].Should().BeOfType<string>().And.Be("data"); |
| 199 | + } |
| 200 | + |
| 201 | + record TestRecord(string Name, int Value, Dictionary<string, object> Properties); |
| 202 | +} |
0 commit comments