From 83beaba8c8c17344acbcc417f9aa88d8707b45ee Mon Sep 17 00:00:00 2001 From: Joe Amenta Date: Fri, 9 Feb 2024 09:02:51 -0500 Subject: [PATCH 1/2] It was actually fixed in e1986a45 So just add a test for it --- .../Serialization/SerializationTests.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/YamlDotNet.Test/Serialization/SerializationTests.cs b/YamlDotNet.Test/Serialization/SerializationTests.cs index d46bc25ec..bf37668e2 100644 --- a/YamlDotNet.Test/Serialization/SerializationTests.cs +++ b/YamlDotNet.Test/Serialization/SerializationTests.cs @@ -2455,6 +2455,16 @@ public void NamingConventionAppliedToEnumWhenDeserializing() Assert.Equal(expected, actual); } + [Fact] + [Trait("motive", "issue #656")] + public void NestedDictionaryTypes_ShouldRoundtrip() + { + var serializer = new SerializerBuilder().EnsureRoundtrip().Build(); + var yaml = serializer.Serialize(new HasNestedDictionary(), typeof(HasNestedDictionary)); + var dct = new DeserializerBuilder().Build().Deserialize(yaml); + Assert.Contains(new KeyValuePair(1, new HasNestedDictionary.Payload { I = 1 }), dct.Lookups); + } + public class TestState { public int OnSerializedCallCount { get; set; } @@ -2546,5 +2556,15 @@ public void WriteYaml(IEmitter emitter, object value, Type type) emitter.Emit(new Scalar(((NonSerializable)value).Text)); } } + + public sealed class HasNestedDictionary + { + public Dictionary Lookups { get; set; } = new Dictionary { [1] = new Payload { I = 1 } }; + + public struct Payload + { + public int I { get; set; } + } + } } } From 912e5f9ca0f6cda263d191a33db78df7e4fd68ec Mon Sep 17 00:00:00 2001 From: Joe Amenta Date: Fri, 9 Feb 2024 09:11:49 -0500 Subject: [PATCH 2/2] Ensure that this new test can't just pass by leaving the object in its default state. --- YamlDotNet.Test/Serialization/SerializationTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/YamlDotNet.Test/Serialization/SerializationTests.cs b/YamlDotNet.Test/Serialization/SerializationTests.cs index bf37668e2..5345dc9cd 100644 --- a/YamlDotNet.Test/Serialization/SerializationTests.cs +++ b/YamlDotNet.Test/Serialization/SerializationTests.cs @@ -2460,7 +2460,7 @@ public void NamingConventionAppliedToEnumWhenDeserializing() public void NestedDictionaryTypes_ShouldRoundtrip() { var serializer = new SerializerBuilder().EnsureRoundtrip().Build(); - var yaml = serializer.Serialize(new HasNestedDictionary(), typeof(HasNestedDictionary)); + var yaml = serializer.Serialize(new HasNestedDictionary { Lookups = { [1] = new HasNestedDictionary.Payload { I = 1 } } }, typeof(HasNestedDictionary)); var dct = new DeserializerBuilder().Build().Deserialize(yaml); Assert.Contains(new KeyValuePair(1, new HasNestedDictionary.Payload { I = 1 }), dct.Lookups); } @@ -2559,7 +2559,7 @@ public void WriteYaml(IEmitter emitter, object value, Type type) public sealed class HasNestedDictionary { - public Dictionary Lookups { get; set; } = new Dictionary { [1] = new Payload { I = 1 } }; + public Dictionary Lookups { get; set; } = new Dictionary(); public struct Payload {