Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include start / end in exceptions thrown by type inspector #624

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,13 @@ public void DontIgnoreExtraPropertiesIfWanted()
var text = Lines("aaa: hello", "bbb: world");
var actual = Record.Exception(() => Deserializer.Deserialize<Simple>(UsingReaderFor(text)));
Assert.IsType<YamlException>(actual);
((YamlException)actual).Start.Column.Should().Be(1);
((YamlException)actual).Start.Line.Should().Be(2);
((YamlException)actual).Start.Index.Should().Be(12);
((YamlException)actual).End.Column.Should().Be(4);
((YamlException)actual).End.Line.Should().Be(2);
((YamlException)actual).End.Index.Should().Be(15);
((YamlException)actual).Message.Should().Be("Property 'bbb' not found on type 'YamlDotNet.Test.Serialization.Simple'.");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// SOFTWARE.

using System;
using System.Runtime.Serialization;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization.Utilities;
Expand Down Expand Up @@ -54,27 +55,42 @@ bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func<IPars
while (!parser.TryConsume<MappingEnd>(out var _))
{
var propertyName = parser.Consume<Scalar>();
var property = typeDescriptor.GetProperty(implementationType, null, propertyName.Value, ignoreUnmatched);
if (property == null)
try
{
parser.SkipThisAndNestedEvents();
continue;
}
var property = typeDescriptor.GetProperty(implementationType, null, propertyName.Value, ignoreUnmatched);
if (property == null)
{
parser.SkipThisAndNestedEvents();
continue;
}

var propertyValue = nestedObjectDeserializer(parser, property.Type);
if (propertyValue is IValuePromise propertyValuePromise)
{
var valueRef = value;
propertyValuePromise.ValueAvailable += v =>
var propertyValue = nestedObjectDeserializer(parser, property.Type);
if (propertyValue is IValuePromise propertyValuePromise)
{
var valueRef = value;
propertyValuePromise.ValueAvailable += v =>
{
var convertedValue = TypeConverter.ChangeType(v, property.Type);
property.Write(valueRef, convertedValue);
};
}
else
{
var convertedValue = TypeConverter.ChangeType(v, property.Type);
property.Write(valueRef, convertedValue);
};
var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type);
property.Write(value, convertedValue);
}
}
catch (SerializationException ex)
{
throw new YamlException(propertyName.Start, propertyName.End, ex.Message);
}
catch (YamlException)
{
throw;
}
else
catch (Exception ex)
{
var convertedValue = TypeConverter.ChangeType(propertyValue, property.Type);
property.Write(value, convertedValue);
throw new YamlException(propertyName.Start, propertyName.End, "Exception during deserialization", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public class MappingNodeTypeResolver : INodeTypeResolver

public MappingNodeTypeResolver(IDictionary<Type, Type> mappings)
{
if (mappings == null) throw new ArgumentNullException(nameof(mappings));
if (mappings == null)
{
throw new ArgumentNullException(nameof(mappings));
}

foreach (var pair in mappings)
{
Expand Down