diff --git a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs index f050104dd0..7095630f1b 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs @@ -43,14 +43,25 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c { using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(data))) { - var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Dictionary)); - var dict = serializer.ReadObject(stream) as Dictionary; + // Converting Json data to array of KeyValuePairs with duplicate keys. + var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(TraitObject[])); + var listOfTraitObjects = serializer.ReadObject(stream) as TraitObject[]; - return dict?.ToArray(); + return listOfTraitObjects.Select(i => new KeyValuePair(i.Key, i.Value)).ToArray();; } } return base.ConvertFrom(context, culture, value); } + + [System.Runtime.Serialization.DataContract] + private class TraitObject + { + [System.Runtime.Serialization.DataMember(Name = "Key")] + public string Key { get; set; } + + [System.Runtime.Serialization.DataMember(Name = "Value")] + public string Value { get; set; } + } } } \ No newline at end of file diff --git a/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs b/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs index 148866e83f..44acbd3f32 100644 --- a/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs +++ b/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs @@ -60,25 +60,30 @@ public void CustomKeyValueConverterShouldDeserializeEmptyArray() } [TestMethod] - public void CustomKeyValueConverterShouldThrowOnDeserializeNullKeyOrValue() + public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue() { - var json = "[{ \"Key\": null, \"Value\": \"\" }]"; + var json = "[{ \"Key\": \"\", \"Value\": \"\" }]"; - var action = new Action(() => this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json)); + var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair[]; - Assert.ThrowsException(action); + Assert.AreEqual(1, data.Length); + Assert.AreEqual(string.Empty, data[0].Key); + Assert.AreEqual(string.Empty, data[0].Value); } [TestMethod] - public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue() + public void CustomKeyValueConverterShouldDeserializeDuplicateKeysKvps() { - var json = "[{ \"Key\": \"\", \"Value\": \"\" }]"; + var json = "[{ \"Key\": \"key1\", \"Value\": \"val1\" }, { \"Key\": \"key1\", \"Value\": \"val2\" }]"; var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair[]; - Assert.AreEqual(1, data.Length); - Assert.AreEqual(string.Empty, data[0].Key); - Assert.AreEqual(string.Empty, data[0].Value); + Assert.IsNotNull(data); + Assert.AreEqual(2, data.Length); + Assert.AreEqual("key1", data[0].Key); + Assert.AreEqual("val1", data[0].Value); + Assert.AreEqual("key1", data[1].Key); + Assert.AreEqual("val2", data[1].Value); } } }