From 2a53997dddaea452c2ef89fb4cee0b1c30cc87d1 Mon Sep 17 00:00:00 2001 From: Antoine Aubry Date: Wed, 29 May 2013 23:25:23 +0100 Subject: [PATCH] Merge pull request #27 : Respect YamlIgnore on deserialization --- .../Serialization/TypeDescriptorSkeleton.cs | 44 +++++++++++++------ .../Serialization/YamlSerializer.cs | 16 ++++--- .../RepresentationModel/SerializationTests.cs | 43 +++++++++--------- 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/YamlDotNet.RepresentationModel/Serialization/TypeDescriptorSkeleton.cs b/YamlDotNet.RepresentationModel/Serialization/TypeDescriptorSkeleton.cs index 25e2687..afa990e 100644 --- a/YamlDotNet.RepresentationModel/Serialization/TypeDescriptorSkeleton.cs +++ b/YamlDotNet.RepresentationModel/Serialization/TypeDescriptorSkeleton.cs @@ -32,22 +32,40 @@ namespace YamlDotNet.RepresentationModel.Serialization public IPropertyDescriptor GetProperty(Type type, string name) { - var property = GetProperties(type) - .FirstOrDefault(p => p.Name == name); + var candidates = GetProperties(type) + .Where(p => p.Name == name); - if(property == null) + using(var enumerator = candidates.GetEnumerator()) { - throw new SerializationException( - string.Format( - CultureInfo.InvariantCulture, - "Property '{0}' not found on type '{1}'.", - name, - type.FullName - ) - ); + if(!enumerator.MoveNext()) + { + throw new SerializationException( + string.Format( + CultureInfo.InvariantCulture, + "Property '{0}' not found on type '{1}'.", + name, + type.FullName + ) + ); + } + + var property = enumerator.Current; + + if(enumerator.MoveNext()) + { + throw new SerializationException( + string.Format( + CultureInfo.InvariantCulture, + "Multiple properties with the name/alias '{0}' already exists on type '{1}', maybe you're misusing YamlAlias or maybe you are using the wrong naming convention? The matching properties are: {2}", + name, + type.FullName, + string.Join(", ", candidates.Select(p => p.Property.Name).ToArray()) + ) + ); + } + + return property; } - - return property; } } } diff --git a/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs b/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs index 01bd3d2..00ffb25 100644 --- a/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs +++ b/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs @@ -758,15 +758,18 @@ namespace YamlDotNet.RepresentationModel.Serialization /// Aliases for this type private IDictionary GetPropertyAliases(Type type) { + IDictionary mapping; + // Check if it's already cached - if (propertyAliases.ContainsKey(type)) - { - return propertyAliases[type]; - } + if (propertyAliases.TryGetValue(type, out mapping)) + return mapping; - var mapping = new Dictionary(); + mapping = new Dictionary(); foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { + if (property.GetCustomAttributes(typeof (YamlIgnoreAttribute), true).Length > 0) + continue; + // Default to the property name var alias = property.Name; @@ -775,6 +778,9 @@ namespace YamlDotNet.RepresentationModel.Serialization if (aliasProps.Length != 0) alias = ((YamlAliasAttribute)aliasProps[0]).Alias; + if (mapping.ContainsKey(alias)) + throw new Exception(String.Format("A property with the name/alias {0} already exists, maybe you're misusing YamlAlias?", alias)); + mapping.Add(alias, property); } diff --git a/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs b/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs index ef9c34e..49f3a3b 100644 --- a/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs +++ b/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs @@ -17,24 +17,24 @@ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. - -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text.RegularExpressions; -using Xunit; -using YamlDotNet.Core; -using YamlDotNet.Core.Events; -using YamlDotNet.RepresentationModel.Serialization; -using YamlDotNet.RepresentationModel.Serialization.NamingConventions; - +// SOFTWARE. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text.RegularExpressions; +using Xunit; +using YamlDotNet.Core; +using YamlDotNet.Core.Events; +using YamlDotNet.RepresentationModel.Serialization; +using YamlDotNet.RepresentationModel.Serialization.NamingConventions; + namespace YamlDotNet.UnitTests.RepresentationModel { public class SerializationTests : YamlTest @@ -876,6 +876,8 @@ namespace YamlDotNet.UnitTests.RepresentationModel public string ThirdTest { get; set; } [YamlAlias("fourthTest")] public string AliasTest { get; set; } + [YamlIgnore] + public string fourthTest { get; set; } } private void DeserializeUsingNamingConvention(string yaml, INamingConvention convention) @@ -1023,8 +1025,8 @@ namespace YamlDotNet.UnitTests.RepresentationModel [Fact] public void NullValuesInListsAreAlwaysEmittedWithEmitDefaults() { - var input = new string[] { "foo", null, "bar" }; - + var input = new string[] { "foo", null, "bar" }; + var serializer = new Serializer(SerializationOptions.EmitDefaults); var writer = new StringWriter(); serializer.Serialize(writer, input); @@ -1094,3 +1096,4 @@ Name: Charles } } } +