Skip to content

Commit

Permalink
Merge pull request #624 from jairbubbles/include-start-end-in-excepti…
Browse files Browse the repository at this point in the history
…ons-thrown-by-type-in

Include start / end in exceptions thrown by type inspector
  • Loading branch information
aaubry committed Aug 6, 2021
2 parents 25e1975 + 87118e7 commit 5f91b4f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
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

0 comments on commit 5f91b4f

Please sign in to comment.