diff --git a/site/docs/serialization/converters.md b/site/docs/serialization/converters.md index 18b3c09..24114ff 100644 --- a/site/docs/serialization/converters.md +++ b/site/docs/serialization/converters.md @@ -105,6 +105,33 @@ public sealed class TemperatureConverter : YamlConverter An exception thrown from `Read`, such as the `FormatException` of `double.Parse` above, reaches the caller of `YamlSerializer.Deserialize` as a [`YamlException`](xref:SharpYaml.YamlException) that wraps it. See [Errors and diagnostics](error-handling.md#exceptions-thrown-by-converters). +### Rejecting a value + +A converter that finds a value invalid should throw [`reader.CreateException(message)`](xref:SharpYaml.Serialization.YamlReader.CreateException(System.String)) +before it consumes any of the value. The exception carries the source name and the location of the current token, and +because nothing was consumed the serializer can skip the value and report it when +[`InvalidValueHandling`](error-handling.md#invalid-values) is `FailAtEnd` or `Skip`. An exception thrown after `reader.Read()`, +or an exception of another type, always ends the call. + +```csharp +public override Temperature Read(YamlReader reader) +{ + if (reader.TokenType != YamlTokenType.Scalar) + { + throw YamlThrowHelper.ThrowExpectedScalar(reader); + } + + var text = reader.ScalarValue!; + if (text.Length < 2 || !double.TryParse(text[..^1], NumberStyles.Float, CultureInfo.InvariantCulture, out var value)) + { + throw reader.CreateException($"'{text}' is not a temperature such as '21.5C'."); + } + + reader.Read(); + return new Temperature { Value = value, Unit = text[^1..] }; +} +``` + ## Converter factories For open generic types or families of types, extend [`YamlConverterFactory`](xref:SharpYaml.Serialization.YamlConverterFactory): diff --git a/site/docs/serialization/error-handling.md b/site/docs/serialization/error-handling.md index 9ad0d1c..5c79836 100644 --- a/site/docs/serialization/error-handling.md +++ b/site/docs/serialization/error-handling.md @@ -13,6 +13,8 @@ When possible, exceptions include: - a span (start/end location) These are available through [`YamlException.SourceName`](xref:SharpYaml.YamlException.SourceName), [`YamlException.Start`](xref:SharpYaml.YamlException.Start), and [`YamlException.End`](xref:SharpYaml.YamlException.End). +`Message` carries them as a prefix; [`YamlException.Reason`](xref:SharpYaml.YamlException.Reason) is the message without it, for reporting the +location in your own format. ```csharp var options = new YamlSerializerOptions @@ -45,6 +47,112 @@ Two exceptions propagate unchanged because they are not about the content: `NotS such as a type without a converter or a context [generated without readers](../source-generation.md#keeping-the-generated-code-small), and `IOException`, for a failure of the source `TextReader` or `Stream`. +By default the first rejected value ends the call. To collect every rejected value in one pass, or to keep the rest of the +document, see [Invalid values](#invalid-values). + +## Invalid values + +A converter that rejects a value, for example `abc` for an `int` member, throws a `YamlException` and the call fails at that +value. [`InvalidValueHandling`](xref:SharpYaml.YamlSerializerOptions.InvalidValueHandling) selects what happens instead: + +| Mode | Behavior | +| --- | --- | +| [`FailFast`](xref:SharpYaml.YamlInvalidValueHandling.FailFast) (default) | The first invalid value throws, as before. | +| [`FailAtEnd`](xref:SharpYaml.YamlInvalidValueHandling.FailAtEnd) | Every recoverable value is skipped and the read continues. At the end the call throws one [`YamlInvalidValuesException`](xref:SharpYaml.YamlInvalidValuesException) listing all of them. | +| [`Skip`](xref:SharpYaml.YamlInvalidValueHandling.Skip) | Every recoverable value is skipped and the call succeeds. | + +A skipped member keeps the value its initializer gave it, or its existing instance under +[`JsonObjectCreationHandling.Populate`](xref:System.Text.Json.Serialization.JsonObjectCreationHandling.Populate). A skipped constructor +parameter keeps its default value. The key still counts as present, so a required member whose value was skipped does not cause a +missing-member error, and a second occurrence of the key is still a duplicate. + +```csharp +var options = new YamlSerializerOptions +{ + SourceName = path, + InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd, +}; + +try +{ + return YamlSerializer.Deserialize(yaml, options); +} +catch (YamlInvalidValuesException e) +{ + foreach (var value in e.InvalidValues) + { + logger.LogError("{File}({Line},{Column}): {Path}: {Message}", + value.SourceName, value.ValueStart.Line + 1, value.ValueStart.Column + 1, value.Path, value.Message); + } + + throw; +} +``` + +The exception is a `YamlException` located at the first skipped value, so `catch (YamlException)` and `TryDeserialize` keep working: +`TryDeserialize` returns `false` under `FailAtEnd` and `true` under `Skip`. Its message lists every value with its path. + +### Observing skipped values + +Under `FailAtEnd` and `Skip`, two delegates observe the skipped values, shaped like the delegates for +[unknown members](extension-data.md#handling-unknown-members-globally): + +- [`InvalidValueCallback`](xref:SharpYaml.YamlSerializerOptions.InvalidValueCallback) runs once per skipped value, in document order. +- [`InvalidValuesFinalizer`](xref:SharpYaml.YamlSerializerOptions.InvalidValuesFinalizer) runs once at the end of the call with every + skipped value, only when there is at least one. Under `FailAtEnd` it runs before the exception is thrown and can throw to replace it. + +Neither runs under `FailFast`. Both receive [`YamlInvalidValue`](xref:SharpYaml.Serialization.YamlInvalidValue) instances: + +| Property | Description | +| --- | --- | +| `SourceName` | `YamlSerializerOptions.SourceName` (for example the file path) | +| `Path` | Document path, for example `$.servers[2].port` (`$['a.b']` for keys that are not simple identifiers) | +| `KeyStart`, `ValueStart` | Line, column and index of the key and of its value | +| `Name`, `TargetType` | The key and the CLR type of the member or constructor parameter the value was meant for | +| `DeclaringType`, `Instance` | The CLR type and the object being populated (`null` for constructor-bound types) | +| `Value` | The rejected value as a `YamlElement`; `null` unless a callback or finalizer is set | +| `Message`, `Exception` | Why the value was rejected, without a location, and the `YamlException` the converter threw, whose `Start` and `End` locate the value | + +```csharp +var options = new YamlSerializerOptions +{ + SourceName = path, + InvalidValueHandling = YamlInvalidValueHandling.Skip, + InvalidValueCallback = v => logger.LogWarning( + "{File}({Line},{Column}): {Path}: {Message} The default is used.", + v.SourceName, v.ValueStart.Line + 1, v.ValueStart.Column + 1, v.Path, v.Message), +}; +``` + +### What is recoverable + +A value is skipped when a `YamlException` is thrown while the reader is still on the first token of the value, that is before +any of it was consumed. This covers the built-in scalar converters, a shape mismatch such as a mapping where a scalar is +expected, and a custom converter that throws [`reader.CreateException(...)`](xref:SharpYaml.Serialization.YamlReader.CreateException(System.String)) +before reading, see [Converters](converters.md#rejecting-a-value). Everything else stays fatal, in every mode: + +- Other exception types. A `FormatException` from a converter is still wrapped in a `YamlException` and ends the call, so a + converter bug is never reported as invalid content. +- An exception thrown after part of the value was consumed, for example by a nested object whose own member was fine. +- Parser errors (`SyntaxErrorException`, `SemanticErrorException`). +- Values that are not member values or constructor arguments: collection elements, dictionary values, the root value and + extension data. +- Errors that no value read throws: `UnmappedMemberHandling = Disallow`, `DuplicateKeyHandling = Error`, a missing required + member and polymorphic discriminator errors. + +When such an error ends a read that already skipped values, `FailAtEnd` throws `YamlInvalidValuesException` with the skipped +values and the error as its `InnerException`, while `Skip` lets the error propagate unchanged. The finalizer does not run in +either case, because the read did not complete. + +### Notes + +- Options stay immutable and can be shared: the state of a deserialization lives in the reader, not in the options. +- Under `FailFast` there is no extra work. The other modes track the document path only when a report is consumed. +- Source-generated contexts are supported through the same options (`context.CreateOptions(o => o with { ... })`). +- Inside polymorphic nodes the `Path` is complete, but `KeyStart`, `ValueStart` and the exception marks are relative to the buffered node. +- Member values and constructor arguments reached through a merge key (`<<`) are recovered like any other. +- Recovery applies to deserialization only. A converter that cannot write a value has a bug, not bad input. + ## Required members If a required member is missing (for example [`YamlRequiredAttribute`](xref:SharpYaml.Serialization.YamlRequiredAttribute) or [`JsonRequiredAttribute`](xref:System.Text.Json.Serialization.JsonRequiredAttribute)), deserialization throws [`YamlException`](xref:SharpYaml.YamlException). diff --git a/site/docs/serialization/overview.md b/site/docs/serialization/overview.md index 10f07b2..877b006 100644 --- a/site/docs/serialization/overview.md +++ b/site/docs/serialization/overview.md @@ -124,6 +124,9 @@ is not a lossless alternative: it currently materializes aliases as copies. | [`SourceName`](xref:SharpYaml.YamlSerializerOptions.SourceName) | `null` | Used for error messages (file/path) when throwing [`YamlException`](xref:SharpYaml.YamlException). | | [`UnmappedMemberCallback`](xref:SharpYaml.YamlSerializerOptions.UnmappedMemberCallback) | `null` | Invoked for every unknown key while reading. See [Handling unknown members globally](extension-data.md#handling-unknown-members-globally). | | [`UnmappedMembersFinalizer`](xref:SharpYaml.YamlSerializerOptions.UnmappedMembersFinalizer) | `null` | Invoked once per deserialization call with all unknown keys; may throw to fail the call. | +| [`InvalidValueHandling`](xref:SharpYaml.YamlSerializerOptions.InvalidValueHandling) | [`YamlInvalidValueHandling.FailFast`](xref:SharpYaml.YamlInvalidValueHandling.FailFast) | Controls whether a value a converter rejects fails the call at once, is skipped and fails the call at the end, or is skipped. See [Invalid values](error-handling.md#invalid-values). | +| [`InvalidValueCallback`](xref:SharpYaml.YamlSerializerOptions.InvalidValueCallback) | `null` | Invoked for every skipped value while reading under `FailAtEnd` or `Skip`. | +| [`InvalidValuesFinalizer`](xref:SharpYaml.YamlSerializerOptions.InvalidValuesFinalizer) | `null` | Invoked once per deserialization call with all skipped values; may throw to replace the `FailAtEnd` exception. | ### Block sequence item style diff --git a/src/SharpYaml.SourceGenerator/YamlSerializerContextGenerator.cs b/src/SharpYaml.SourceGenerator/YamlSerializerContextGenerator.cs index accee08..fc5bdb3 100644 --- a/src/SharpYaml.SourceGenerator/YamlSerializerContextGenerator.cs +++ b/src/SharpYaml.SourceGenerator/YamlSerializerContextGenerator.cs @@ -2471,6 +2471,11 @@ preferredObjectCreationHandling is null || builder.AppendLine(" void ReadMember(string key)"); builder.AppendLine(" {"); builder.AppendLine(" var matched = false;"); + builder.AppendLine(" var valueTokenType = reader.TokenType;"); + builder.AppendLine(" var valueStart = reader.Start;"); + builder.AppendLine(" global::System.Type? valueType = null;"); + builder.AppendLine(" try"); + builder.AppendLine(" {"); foreach (var member in readCandidates) { builder.Append(" if (!matched && global::System.String.Equals(key, ").Append(member.SerializedNameExpressionForRead) @@ -2490,6 +2495,7 @@ preferredObjectCreationHandling is null || builder.Append(" __required").Append(index).Append("_").Append(member.Symbol.Name).AppendLine(" = true;"); } + builder.Append(" valueType = typeof(").Append(member.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)).AppendLine(");"); EmitReadObjectMemberValueWithObjectCreationHandling( builder, member, @@ -2501,6 +2507,13 @@ preferredObjectCreationHandling is null || builder.AppendLine(" }"); } + builder.AppendLine(" }"); + builder.AppendLine(" catch (global::SharpYaml.YamlException exception) when (valueType is not null && CanRecoverInvalidValue(reader, exception, valueTokenType, valueStart))"); + builder.AppendLine(" {"); + builder.Append(" RecoverInvalidValue(reader, exception, typeof(").Append(typeName).AppendLine("), instance, key, valueType);"); + builder.AppendLine(" return;"); + builder.AppendLine(" }"); + builder.AppendLine(" if (!matched)"); builder.AppendLine(" {"); if (extensionData is not null) @@ -2805,6 +2818,11 @@ private static void EmitReadObjectCoreWithConstructor( builder.AppendLine(" void ReadMember(string key)"); builder.AppendLine(" {"); builder.AppendLine(" var matched = false;"); + builder.AppendLine(" var valueTokenType = reader.TokenType;"); + builder.AppendLine(" var valueStart = reader.Start;"); + builder.AppendLine(" global::System.Type? valueType = null;"); + builder.AppendLine(" try"); + builder.AppendLine(" {"); // Read-ignored members are matched before constructor parameters so they are skipped, not captured as extension data. for (var i = 0; i < readIgnoredMembers.Length; i++) @@ -2850,6 +2868,7 @@ private static void EmitReadObjectCoreWithConstructor( } var tmpVar = $"__ctor{index}_tmp{i}"; + builder.Append(" valueType = typeof(").Append(parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)).AppendLine(");"); EmitReadKnownType(builder, sourceGenerationOptions, parameter.Type, indexByType, tmpVar, indent: " "); builder.Append(" ").Append(parameterValueVarNames[i]).Append(" = ").Append(tmpVar).AppendLine(";"); builder.Append(" ").Append(parameterSeenVarNames[i]).AppendLine(" = true;"); @@ -2889,6 +2908,7 @@ private static void EmitReadObjectCoreWithConstructor( { builder.Append(" ").Append(requiredVar).AppendLine(" = true;"); } + builder.Append(" valueType = typeof(").Append(member.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)).AppendLine(");"); EmitReadMemberValueWithCustomConverter(builder, localModel, indexByType, sourceGenerationOptions); builder.Append(" ").Append(localSeenVar).AppendLine(" = true;"); builder.AppendLine(" }"); @@ -2920,7 +2940,13 @@ private static void EmitReadObjectCoreWithConstructor( builder.AppendLine(" }"); } - builder.AppendLine(); + builder.AppendLine(" }"); + builder.AppendLine(" catch (global::SharpYaml.YamlException exception) when (valueType is not null && CanRecoverInvalidValue(reader, exception, valueTokenType, valueStart))"); + builder.AppendLine(" {"); + builder.Append(" RecoverInvalidValue(reader, exception, typeof(").Append(typeName).AppendLine("), null, key, valueType);"); + builder.AppendLine(" return;"); + builder.AppendLine(" }"); + builder.AppendLine(" if (!matched)"); builder.AppendLine(" {"); if (extensionData is not null) diff --git a/src/SharpYaml.Tests/Serialization/YamlInvalidValueTests.cs b/src/SharpYaml.Tests/Serialization/YamlInvalidValueTests.cs new file mode 100644 index 0000000..b1614a5 --- /dev/null +++ b/src/SharpYaml.Tests/Serialization/YamlInvalidValueTests.cs @@ -0,0 +1,678 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json.Serialization; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SharpYaml.Model; +using SharpYaml.Serialization; + +namespace SharpYaml.Tests.Serialization; + +[TestClass] +public sealed class YamlInvalidValueTests +{ + public static IEnumerable Modes => new[] { new object[] { false }, new object[] { true } }; + + private static T? Deserialize(string yaml, YamlSerializerOptions options, bool generated) + => generated ? (T?)YamlSerializer.Deserialize(yaml, typeof(T), new InvalidValueContext(options)) : YamlSerializer.Deserialize(yaml, options); + + private static T? Deserialize(TextReader reader, YamlSerializerOptions options, bool generated) + => generated ? (T?)YamlSerializer.Deserialize(reader, typeof(T), new InvalidValueContext(options)) : YamlSerializer.Deserialize(reader, options); + + private static bool TryDeserialize(string yaml, YamlSerializerOptions options, bool generated, out T? value) + => generated ? YamlSerializer.TryDeserialize(yaml, new InvalidValueContext(options), out value) : YamlSerializer.TryDeserialize(yaml, out value, options); + + [TestMethod] + public void InvalidValueHandling_DefaultsToFailFast() + { + Assert.AreEqual(YamlInvalidValueHandling.FailFast, new YamlSerializerOptions().InvalidValueHandling); + Assert.AreEqual(YamlInvalidValueHandling.FailFast, YamlSerializerOptions.Default.InvalidValueHandling); + } + + [TestMethod] + public void CreateException_IsLocatedAtTheCurrentToken() + { + var reader = YamlReader.Create("a: 1\n", new YamlSerializerOptions { SourceName = "config.yaml" }); + Assert.IsTrue(reader.Read()); + Assert.IsTrue(reader.Read()); + Assert.IsTrue(reader.Read()); + Assert.AreEqual("1", reader.GetScalarValue()); + + var exception = reader.CreateException("not a port"); + + Assert.AreEqual("config.yaml", exception.SourceName); + Assert.AreEqual(reader.Start, exception.Start); + Assert.AreEqual(reader.End, exception.End); + Assert.AreEqual(0, exception.Start.Line); + Assert.AreEqual(3, exception.Start.Column); + StringAssert.EndsWith(exception.Message, "not a port"); + } + + [TestMethod] + public void Session_ExistsOnlyWhenAReportIsConsumed() + { + static void Callback(YamlInvalidValue value) + { + } + + Assert.IsNull(YamlReportingSession.Create(new YamlSerializerOptions())); + Assert.IsNull(YamlReportingSession.Create(new YamlSerializerOptions { InvalidValueCallback = Callback })); + Assert.IsNull(YamlReportingSession.Create(new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip })); + Assert.IsNotNull(YamlReportingSession.Create(new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd })); + Assert.IsNotNull(YamlReportingSession.Create(new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = Callback })); + } + + [TestMethod] + public void IsReportingUnmappedMembers_StaysFalseWithOnlyInvalidValueReporting() + { + var failAtEnd = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + var skip = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValuesFinalizer = static _ => { } }; + + Assert.IsFalse(YamlReader.Create("a: 1\n", failAtEnd).IsReportingUnmappedMembers); + Assert.IsFalse(YamlReader.Create("a: 1\n", skip).IsReportingUnmappedMembers); + } + + [TestMethod] + public void Exception_ListsEveryValueAndIsLocatedAtTheFirst() + { + var first = CreateValue("$.port", new Mark(8, 2, 7), new Mark(11, 2, 10), "'abc' is not a port"); + var second = CreateValue("$.servers[1].timeout", new Mark(40, 5, 12), new Mark(42, 5, 14), "'-1' is negative"); + + var exception = new YamlInvalidValuesException(new[] { first, second }); + + Assert.HasCount(2, exception.InvalidValues); + Assert.AreEqual("config.yaml", exception.SourceName); + Assert.AreEqual(first.Exception.Start, exception.Start); + Assert.AreEqual(first.Exception.End, exception.End); + Assert.IsNull(exception.InnerException); + StringAssert.Contains(exception.Message, "2 invalid values were skipped while deserializing:"); + StringAssert.Contains(exception.Message, "\n $.port: " + first.Message); + StringAssert.Contains(exception.Message, "\n $.servers[1].timeout: " + second.Message); + } + + [TestMethod] + public void Exception_NamesTheErrorThatEndedTheDeserialization() + { + var value = CreateValue("$.port", new Mark(8, 2, 7), new Mark(11, 2, 10), "'abc' is not a port"); + var fatal = new YamlException("Required member 'Name' is missing."); + + var exception = new YamlInvalidValuesException(new[] { value }, fatal); + + Assert.AreSame(fatal, exception.InnerException); + StringAssert.Contains(exception.Message, "1 invalid value was skipped before the deserialization failed:"); + StringAssert.EndsWith(exception.Message, "\n " + fatal.Message); + } + + [TestMethod] + public void Exception_RequiresAtLeastOneValue() + { + Assert.ThrowsExactly(() => new YamlInvalidValuesException((IReadOnlyList)null!)); + Assert.ThrowsExactly(() => new YamlInvalidValuesException(Array.Empty())); + Assert.IsEmpty(new YamlInvalidValuesException().InvalidValues); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void FailFast_ThrowsAtTheFirstInvalidValueAndRunsNoDelegate(bool generated) + { + var reported = 0; + var options = new YamlSerializerOptions { InvalidValueCallback = _ => reported++, InvalidValuesFinalizer = _ => reported++ }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Port: abc\nTimeout: xyz\n", options, generated)); + + StringAssert.Contains(exception.Message, "Invalid integer scalar 'abc'"); + Assert.AreEqual(0, exception.Start.Line); + Assert.AreEqual(0, reported); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_KeepsTheInitializerValueAndReportsEveryDetail(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { SourceName = "config.yaml", InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var root = Deserialize("Name: a\nPort: abc\nTimeout: 5\n", options, generated)!; + + Assert.AreEqual("a", root.Name); + Assert.AreEqual(8080, root.Port); + Assert.AreEqual(5, root.Timeout); + Assert.HasCount(1, values); + + var value = values[0]; + Assert.AreEqual("config.yaml", value.SourceName); + Assert.AreEqual("$.Port", value.Path); + Assert.AreEqual("Port", value.Name); + Assert.AreEqual(1, value.KeyStart.Line); + Assert.AreEqual(0, value.KeyStart.Column); + Assert.AreEqual(1, value.ValueStart.Line); + Assert.AreEqual(6, value.ValueStart.Column); + Assert.AreEqual(typeof(int), value.TargetType); + Assert.AreEqual(typeof(InvalidValueRoot), value.DeclaringType); + Assert.AreSame(root, value.Instance); + Assert.AreEqual("abc", ((YamlValue)value.Value!).Value); + StringAssert.Contains(value.Message, "Invalid integer scalar 'abc'"); + Assert.AreEqual(value.ValueStart.Index, value.Exception.Start.Index); + Assert.AreEqual(value.Exception.Reason, value.Message); + Assert.IsFalse(value.Message.Contains("Lin:"), value.Message); + StringAssert.EndsWith(value.Exception.Message, ": " + value.Message); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_WithoutDelegatesSucceedsSilently(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip }; + + var root = Deserialize("Port: abc\nName: a\n", options, generated)!; + + Assert.AreEqual(8080, root.Port); + Assert.AreEqual("a", root.Name); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_RecoversAConverterThatRejectsBeforeConsuming(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var root = Deserialize("Rejecting: nope\nName: a\n", options, generated)!; + + Assert.IsNull(root.Rejecting); + Assert.AreEqual("a", root.Name); + Assert.HasCount(1, values); + Assert.AreEqual(typeof(InvalidValueRejecting), values[0].TargetType); + StringAssert.EndsWith(values[0].Message, "rejected before consuming"); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_LeavesAConverterThatThrowsAfterConsumingFatal(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Consuming: nope\nName: a\n", options, generated)); + + StringAssert.EndsWith(exception.Message, "rejected after consuming"); + Assert.IsEmpty(values); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_LeavesANonYamlExceptionFatalAndWrapped(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Formatted: nope\n", options, generated)); + + Assert.IsInstanceOfType(exception.InnerException); + Assert.IsEmpty(values); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_SkipsAMappingWhereAScalarIsExpectedWhole(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var root = Deserialize("Port:\n nested: 1\n more: [1, 2]\nName: after\n", options, generated)!; + + Assert.AreEqual(8080, root.Port); + Assert.AreEqual("after", root.Name); + Assert.HasCount(1, values); + Assert.IsInstanceOfType(values[0].Value); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_CountsARequiredMemberWithAnInvalidValueAsPresent(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip }; + + var root = Deserialize("Port: abc\n", options, generated)!; + + Assert.AreEqual(0, root.Port); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_KeepsTheDefaultOfAConstructorParameter(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var root = Deserialize("Port: abc\nName: a\nTimeout: xyz\n", options, generated)!; + + Assert.AreEqual(8080, root.Port); + Assert.AreEqual("a", root.Name); + Assert.AreEqual(30, root.Timeout); + Assert.HasCount(2, values); + Assert.AreEqual("$.Port", values[0].Path); + Assert.IsNull(values[0].Instance); + Assert.AreEqual(typeof(InvalidValueConstructed), values[0].DeclaringType); + Assert.AreEqual("$.Timeout", values[1].Path); + Assert.IsNull(values[1].Instance); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_RecoversInsideAMergedMapping(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var root = Deserialize("Server:\n <<: {Port: abc, Timeout: 7}\n Name: a\nConstructed:\n <<: {Port: abc, Timeout: xyz}\n Name: c\n", options, generated)!; + + Assert.AreEqual(8080, root.Server!.Port); + Assert.AreEqual(7, root.Server.Timeout); + Assert.AreEqual("a", root.Server.Name); + Assert.AreEqual("c", root.Constructed!.Name); + Assert.AreEqual(8080, root.Constructed.Port); + Assert.AreEqual(30, root.Constructed.Timeout); + Assert.HasCount(3, values); + Assert.AreEqual("$.Server['<<'].Port", values[0].Path); + Assert.AreSame(root.Server, values[0].Instance); + Assert.AreEqual("$.Constructed['<<'].Port", values[1].Path); + Assert.AreEqual("$.Constructed['<<'].Timeout", values[2].Path); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_KeepsThePopulatedInstanceWhenTheReadFailsBeforeConsuming(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var root = Deserialize("Server: abc\n", options, generated)!; + + Assert.AreEqual("keep", root.Server.Name); + Assert.HasCount(1, values); + Assert.AreEqual(typeof(InvalidValueRoot), values[0].TargetType); + Assert.AreEqual("abc", ((YamlValue)values[0].Value!).Value); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_ReportsTheCompletePathInsideAPolymorphicNode(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var zoo = Deserialize("Pets:\n - $type: dog\n Name: a\n BarkVolume: loud\n", options, generated)!; + + var dog = (InvalidValueDog)zoo.Pets[0]; + Assert.AreEqual("a", dog.Name); + Assert.AreEqual(3, dog.BarkVolume); + Assert.HasCount(1, values); + Assert.AreEqual("$.Pets[0].BarkVolume", values[0].Path); + Assert.AreEqual(typeof(InvalidValueDog), values[0].DeclaringType); + Assert.AreSame(dog, values[0].Instance); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_RecoversAnAnchoredValueAndItsAlias(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var root = Deserialize("Port: &p abc\nTimeout: *p\n", options, generated)!; + + Assert.AreEqual(8080, root.Port); + Assert.AreEqual(30, root.Timeout); + Assert.HasCount(2, values); + Assert.AreEqual("abc", ((YamlValue)values[0].Value!).Value); + Assert.AreEqual("abc", ((YamlValue)values[1].Value!).Value); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_LeavesAnInvalidCollectionElementFatal(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Retries: [1, abc]\n", options, generated)); + + StringAssert.Contains(exception.Message, "Invalid integer scalar 'abc'"); + Assert.IsEmpty(values); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_StillDisallowsUnmappedMembers(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Port: abc\nOops: 1\n", options, generated)); + + StringAssert.Contains(exception.Message, "Oops"); + } + + [TestMethod] + public void Skip_StillTreatsASkippedKeyAsPresentForDuplicateDetection() + { + // Reflection only: generated contexts do not detect duplicate member keys, whatever the invalid value handling. + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, DuplicateKeyHandling = YamlDuplicateKeyHandling.Error }; + + var member = Assert.ThrowsExactly(() => YamlSerializer.Deserialize("Port: abc\nPort: 1\n", options)); + var parameter = Assert.ThrowsExactly(() => YamlSerializer.Deserialize("Name: a\nPort: abc\nPort: 1\n", options)); + + StringAssert.Contains(member.Message, "Port"); + StringAssert.Contains(parameter.Message, "Port"); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void FailAtEnd_ThrowsOnceListingEveryValueInDocumentOrder(bool generated) + { + var options = new YamlSerializerOptions { SourceName = "config.yaml", InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + const string yaml = "Port: abc\nName: a\nTimeout: xyz\nRejecting: nope\n"; + + var exception = Assert.ThrowsExactly(() => Deserialize(yaml, options, generated)); + + Assert.IsNull(exception.InnerException); + Assert.HasCount(3, exception.InvalidValues); + CollectionAssert.AreEqual(new[] { "$.Port", "$.Timeout", "$.Rejecting" }, exception.InvalidValues.Select(static v => v.Path).ToArray()); + Assert.IsTrue(exception.InvalidValues.All(static v => v.Value is null)); + Assert.AreEqual("config.yaml", exception.SourceName); + Assert.AreEqual(0, exception.Start.Line); + Assert.AreEqual(6, exception.Start.Column); + StringAssert.Contains(exception.Message, "3 invalid values were skipped while deserializing:"); + StringAssert.Contains(exception.Message, "$.Rejecting: "); + Assert.IsFalse(TryDeserialize(yaml, options, generated, out var value)); + Assert.IsNull(value); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void FailAtEnd_ThrowsFromTheTextReaderOverloadToo(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + + using var reader = new StringReader("Port: abc\n"); + var exception = Assert.ThrowsExactly(() => Deserialize(reader, options, generated)); + + Assert.HasCount(1, exception.InvalidValues); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void FailAtEnd_FoldsAFatalErrorIntoTheAggregate(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Port: abc\nTimeout: xyz\nConsuming: nope\n", options, generated)); + + Assert.HasCount(2, exception.InvalidValues); + Assert.IsInstanceOfType(exception.InnerException); + StringAssert.EndsWith(exception.InnerException!.Message, "rejected after consuming"); + StringAssert.Contains(exception.Message, "2 invalid values were skipped before the deserialization failed:"); + StringAssert.EndsWith(exception.Message, "rejected after consuming"); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void FailAtEnd_WrapsANonYamlFatalErrorBeforeFoldingIt(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Port: abc\nFormatted: nope\n", options, generated)); + + Assert.HasCount(1, exception.InvalidValues); + Assert.IsInstanceOfType(exception.InnerException!.InnerException); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void FailAtEnd_FoldsAMissingConstructorParameterAfterItsValueWasSkipped(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Name: [1]\n", options, generated)); + + Assert.HasCount(1, exception.InvalidValues); + StringAssert.Contains(exception.InnerException!.Message, "Missing required constructor parameter 'name'"); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void FailAtEnd_PropagatesAFatalErrorUnchangedWhenNothingWasRecovered(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Consuming: nope\n", options, generated)); + + StringAssert.EndsWith(exception.Message, "rejected after consuming"); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_PropagatesAFatalErrorUnchangedAfterRecoveries(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Port: abc\nConsuming: nope\n", options, generated)); + + StringAssert.EndsWith(exception.Message, "rejected after consuming"); + Assert.HasCount(1, values); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_SucceedsUnderTryDeserialize(bool generated) + { + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip }; + + Assert.IsTrue(TryDeserialize("Port: abc\n", options, generated, out var value)); + Assert.AreEqual(8080, value!.Port); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Finalizer_RunsOnceWithEveryValueUnderSkipAndBeforeTheFailAtEndThrow(bool generated) + { + var calls = new List(); + var skip = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValuesFinalizer = values => calls.Add(values.Select(static v => v.Path).ToArray()) }; + var failAtEnd = skip with { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd }; + const string yaml = "Port: abc\nTimeout: xyz\n"; + + Deserialize(yaml, skip, generated); + Assert.ThrowsExactly(() => Deserialize(yaml, failAtEnd, generated)); + + Assert.HasCount(2, calls); + CollectionAssert.AreEqual(new[] { "$.Port", "$.Timeout" }, calls[0]); + CollectionAssert.AreEqual(new[] { "$.Port", "$.Timeout" }, calls[1]); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Finalizer_DoesNotRunWithoutInvalidValuesOrAfterAFatalError(bool generated) + { + var called = false; + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd, InvalidValuesFinalizer = _ => called = true }; + + Deserialize("Port: 1\n", options, generated); + Assert.ThrowsExactly(() => Deserialize("Port: abc\nConsuming: nope\n", options, generated)); + + Assert.IsFalse(called); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Finalizer_CanReplaceTheAggregate(bool generated) + { + var options = new YamlSerializerOptions + { + InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd, + InvalidValuesFinalizer = values => throw new InvalidOperationException($"{values.Count} invalid"), + }; + + var exception = Assert.ThrowsExactly(() => Deserialize("Port: abc\n", options, generated)); + + Assert.AreEqual("1 invalid", exception.Message); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Handlers_AreSafeToShareAcrossConcurrentDeserializations(bool generated) + { + var total = 0; + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.FailAtEnd, InvalidValueCallback = _ => Interlocked.Increment(ref total) }; + + Parallel.For(0, 200, _ => + { + var exception = Assert.ThrowsExactly(() => Deserialize("Port: abc\nTimeout: xyz\n", options, generated)); + Assert.HasCount(2, exception.InvalidValues); + }); + + Assert.AreEqual(400, total); + } + + [TestMethod] + [DynamicData(nameof(Modes))] + public void Skip_LeavesASyntaxErrorFatal(bool generated) + { + var values = new List(); + var options = new YamlSerializerOptions { InvalidValueHandling = YamlInvalidValueHandling.Skip, InvalidValueCallback = values.Add }; + + var exception = Assert.Throws(() => Deserialize("Port: [1, 2\nName: a\n", options, generated)); + + Assert.IsTrue(exception is SyntaxErrorException or SemanticErrorException, exception.GetType().Name); + Assert.IsEmpty(values); + } + + private static YamlInvalidValue CreateValue(string path, Mark start, Mark end, string message) + { + var exception = new YamlException("config.yaml", start, end, message); + return new YamlInvalidValue("config.yaml", typeof(object), null, path[(path.LastIndexOf('.') + 1)..], path, start, start, typeof(int), exception); + } +} + +internal sealed class InvalidValueRoot +{ + public string Name { get; set; } = string.Empty; + public int Port { get; set; } = 8080; + public int Timeout { get; set; } = 30; + public List Retries { get; set; } = new(); + public InvalidValueRejecting? Rejecting { get; set; } + public InvalidValueConsuming? Consuming { get; set; } + public InvalidValueFormatted? Formatted { get; set; } +} + +internal sealed class InvalidValueRequired +{ + [YamlRequired] + public int Port { get; set; } +} + +internal sealed class InvalidValueConstructed +{ + public InvalidValueConstructed(string name, int port = 8080, int timeout = 30) + { + Name = name; + Port = port; + Timeout = timeout; + } + + public string Name { get; } + public int Port { get; } + public int Timeout { get; } +} + +internal sealed class InvalidValueMergeRoot +{ + public InvalidValueRoot? Server { get; set; } + public InvalidValueConstructed? Constructed { get; set; } +} + +internal sealed class InvalidValuePopulateRoot +{ + [JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] + public InvalidValueRoot Server { get; } = new() { Name = "keep" }; +} + +internal sealed class InvalidValueZoo +{ + public List Pets { get; set; } = new(); +} + +[YamlPolymorphic] +[YamlDerivedType(typeof(InvalidValueDog), "dog")] +internal abstract class InvalidValueAnimal +{ + public string Name { get; set; } = string.Empty; +} + +internal sealed class InvalidValueDog : InvalidValueAnimal +{ + public int BarkVolume { get; set; } = 3; +} + +[YamlConverter(typeof(InvalidValueRejectingConverter))] +internal sealed class InvalidValueRejecting +{ +} + +[YamlConverter(typeof(InvalidValueConsumingConverter))] +internal sealed class InvalidValueConsuming +{ +} + +[YamlConverter(typeof(InvalidValueFormattedConverter))] +internal sealed class InvalidValueFormatted +{ +} + +internal sealed class InvalidValueRejectingConverter : YamlConverter +{ + public override InvalidValueRejecting Read(YamlReader reader) => throw reader.CreateException("rejected before consuming"); + + public override void Write(YamlWriter writer, InvalidValueRejecting value) => writer.WriteScalar(string.Empty); +} + +internal sealed class InvalidValueConsumingConverter : YamlConverter +{ + public override InvalidValueConsuming Read(YamlReader reader) + { + reader.Read(); + throw reader.CreateException("rejected after consuming"); + } + + public override void Write(YamlWriter writer, InvalidValueConsuming value) => writer.WriteScalar(string.Empty); +} + +internal sealed class InvalidValueFormattedConverter : YamlConverter +{ + public override InvalidValueFormatted Read(YamlReader reader) => throw new FormatException("not formatted"); + + public override void Write(YamlWriter writer, InvalidValueFormatted value) => writer.WriteScalar(string.Empty); +} + +[YamlSerializable(typeof(InvalidValueRoot))] +[YamlSerializable(typeof(InvalidValueRequired))] +[YamlSerializable(typeof(InvalidValueConstructed))] +[YamlSerializable(typeof(InvalidValueMergeRoot))] +[YamlSerializable(typeof(InvalidValuePopulateRoot))] +[YamlSerializable(typeof(InvalidValueZoo))] +[YamlSerializable(typeof(InvalidValueRejecting))] +[YamlSerializable(typeof(InvalidValueConsuming))] +[YamlSerializable(typeof(InvalidValueFormatted))] +internal partial class InvalidValueContext : YamlSerializerContext +{ + public InvalidValueContext() + { + } + + public InvalidValueContext(YamlSerializerOptions options) : base(options) + { + } +} diff --git a/src/SharpYaml.Tests/Serialization/YamlUnmappedMemberTests.cs b/src/SharpYaml.Tests/Serialization/YamlUnmappedMemberTests.cs index 49c3ecc..e7a8ddf 100644 --- a/src/SharpYaml.Tests/Serialization/YamlUnmappedMemberTests.cs +++ b/src/SharpYaml.Tests/Serialization/YamlUnmappedMemberTests.cs @@ -373,6 +373,17 @@ public void DefaultOptions_DoNotReportAnything() Assert.IsTrue(value.Enabled); } + + [TestMethod] + public void IsReportingUnmappedMembers_ReflectsTheConfiguredDelegates() + { + var callback = new YamlSerializerOptions { UnmappedMemberCallback = static _ => { } }; + var finalizer = new YamlSerializerOptions { UnmappedMembersFinalizer = static _ => { } }; + + Assert.IsTrue(YamlReader.Create("a: 1\n", callback).IsReportingUnmappedMembers); + Assert.IsTrue(YamlReader.Create("a: 1\n", finalizer).IsReportingUnmappedMembers); + Assert.IsFalse(YamlReader.Create("a: 1\n").IsReportingUnmappedMembers); + } } internal sealed class UnmappedRoot diff --git a/src/SharpYaml.Tests/YamlExceptionTests.cs b/src/SharpYaml.Tests/YamlExceptionTests.cs new file mode 100644 index 0000000..fea90f8 --- /dev/null +++ b/src/SharpYaml.Tests/YamlExceptionTests.cs @@ -0,0 +1,32 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace SharpYaml.Tests; + +[TestClass] +public sealed class YamlExceptionTests +{ + [TestMethod] + public void Reason_IsTheMessageWithoutTheLocation() + { + var located = new YamlException("config.yaml", new Mark(7, 1, 3), new Mark(10, 1, 6), "'abc' is not a port"); + var unnamed = new YamlException(new Mark(7, 1, 3), new Mark(10, 1, 6), "'abc' is not a port", new FormatException()); + + Assert.AreEqual("'abc' is not a port", located.Reason); + Assert.AreEqual("config.yaml: (Lin: 1, Col: 3, Chr: 7) - (Lin: 1, Col: 6, Chr: 10): 'abc' is not a port", located.Message); + Assert.AreEqual("'abc' is not a port", unnamed.Reason); + StringAssert.StartsWith(unnamed.Message, "(Lin: 1, Col: 3, Chr: 7)"); + } + + [TestMethod] + public void Reason_EqualsTheMessageWithoutALocation() + { + var plain = new YamlException("boom"); + var wrapped = new YamlException("boom", new InvalidOperationException()); + var empty = new YamlException(); + + Assert.AreEqual(plain.Message, plain.Reason); + Assert.AreEqual(wrapped.Message, wrapped.Reason); + Assert.AreEqual(empty.Message, empty.Reason); + } +} diff --git a/src/SharpYaml/Serialization/Converters/YamlObjectConverter.cs b/src/SharpYaml/Serialization/Converters/YamlObjectConverter.cs index 165e2f6..c2c8f32 100644 --- a/src/SharpYaml/Serialization/Converters/YamlObjectConverter.cs +++ b/src/SharpYaml/Serialization/Converters/YamlObjectConverter.cs @@ -562,11 +562,18 @@ private static void PopulateObjectCore(YamlReader reader, Contract contract, obj { var parameterType = constructor.GetParameterType(parameterIndex); var converter = reader.GetConverter(parameterType); + var valueTokenType = reader.TokenType; + var valueStart = reader.Start; object? value; try { value = converter.Read(reader, parameterType); } + catch (YamlException exception) when (reader.CanRecoverInvalidValue(exception, valueTokenType, valueStart)) + { + reader.RecoverInvalidValue(exception, contract.DeclaringType, null, key, parameterType); + continue; + } catch (YamlException) { throw; @@ -599,11 +606,18 @@ private static void PopulateObjectCore(YamlReader reader, Contract contract, obj } var converter = member.Converter ??= reader.GetConverter(member.MemberType); + var valueTokenType = reader.TokenType; + var valueStart = reader.Start; object? value; try { value = converter.Read(reader, member.MemberType); } + catch (YamlException exception) when (reader.CanRecoverInvalidValue(exception, valueTokenType, valueStart)) + { + reader.RecoverInvalidValue(exception, contract.DeclaringType, null, key, member.MemberType); + continue; + } catch (YamlException) { throw; @@ -1059,6 +1073,8 @@ private static void ReadAndApplyMemberValue(YamlReader reader, object instance, return; } + var valueTokenType = reader.TokenType; + var valueStart = reader.Start; var converter = member.Converter ??= reader.GetConverter(member.MemberType); var canPopulate = converter.CanPopulate(member.MemberType); var canPopulateMember = canPopulate && (!member.MemberType.IsValueType || member.CanWrite); @@ -1093,6 +1109,11 @@ private static void ReadAndApplyMemberValue(YamlReader reader, object instance, { populatedValue = converter.Populate(reader, member.MemberType, currentValue); } + catch (YamlException exception) when (reader.CanRecoverInvalidValue(exception, valueTokenType, valueStart)) + { + reader.RecoverInvalidValue(exception, contract.DeclaringType, instance, key, member.MemberType); + return; + } catch (YamlException) { throw; @@ -1141,6 +1162,11 @@ private static void ReadAndApplyMemberValue(YamlReader reader, object instance, { value = converter.Read(reader, member.MemberType); } + catch (YamlException exception) when (reader.CanRecoverInvalidValue(exception, valueTokenType, valueStart)) + { + reader.RecoverInvalidValue(exception, contract.DeclaringType, instance, key, member.MemberType); + return; + } catch (YamlException) { throw; @@ -1272,11 +1298,18 @@ private void ApplyMergeMappingToConstructorBuffers( { var parameterType = constructor.GetParameterType(parameterIndex); var converter = reader.GetConverter(parameterType); + var valueTokenType = reader.TokenType; + var valueStart = reader.Start; object? value; try { value = converter.Read(reader, parameterType); } + catch (YamlException exception) when (reader.CanRecoverInvalidValue(exception, valueTokenType, valueStart)) + { + reader.RecoverInvalidValue(exception, contract.DeclaringType, null, key, parameterType); + continue; + } catch (YamlException) { throw; @@ -1309,11 +1342,18 @@ private void ApplyMergeMappingToConstructorBuffers( } var converter = member.Converter ??= reader.GetConverter(member.MemberType); + var valueTokenType = reader.TokenType; + var valueStart = reader.Start; object? value; try { value = converter.Read(reader, member.MemberType); } + catch (YamlException exception) when (reader.CanRecoverInvalidValue(exception, valueTokenType, valueStart)) + { + reader.RecoverInvalidValue(exception, contract.DeclaringType, null, key, member.MemberType); + continue; + } catch (YamlException) { throw; diff --git a/src/SharpYaml/Serialization/YamlInvalidValue.cs b/src/SharpYaml/Serialization/YamlInvalidValue.cs new file mode 100644 index 0000000..c9fa984 --- /dev/null +++ b/src/SharpYaml/Serialization/YamlInvalidValue.cs @@ -0,0 +1,90 @@ +// // Copyright (c) Alexandre Mutel. All rights reserved. +// // Licensed under the MIT license. +// // See LICENSE.txt file in the project root for full license information. + +using System; +using SharpYaml.Model; + +namespace SharpYaml.Serialization; + +/// +/// Describes a YAML value that a converter rejected while deserializing a member or a constructor argument, and that +/// was skipped because is not . +/// +/// +/// Instances are passed to and +/// , and listed by . +/// +public sealed class YamlInvalidValue +{ + internal YamlInvalidValue( + string? sourceName, + Type declaringType, + object? instance, + string name, + string path, + Mark keyStart, + Mark valueStart, + Type targetType, + YamlException exception) + { + SourceName = sourceName; + DeclaringType = declaringType; + Instance = instance; + Name = name; + Path = path; + KeyStart = keyStart; + ValueStart = valueStart; + TargetType = targetType; + Exception = exception; + } + + /// Gets the source name (for example a file path) from . + public string? SourceName { get; } + + /// Gets the CLR type that was being deserialized when the value was rejected. + public Type DeclaringType { get; } + + /// + /// Gets the instance being populated, or when it does not exist yet + /// (for example for types deserialized through a parameterized constructor). + /// + public object? Instance { get; } + + /// Gets the YAML key of the member or constructor argument. + public string Name { get; } + + /// + /// Gets the path of the value from the document root, for example $.servers[2].port. + /// Keys that are not simple identifiers are written as $['a.b']. + /// + /// + /// For values inside polymorphic nodes the path is complete, but and + /// are relative to the buffered node rather than the document. + /// + public string Path { get; } + + /// Gets the location of the key. + public Mark KeyStart { get; } + + /// Gets the location where the value starts. + public Mark ValueStart { get; } + + /// Gets the CLR type of the member or constructor parameter the value was meant for. + public Type TargetType { get; } + + /// + /// Gets the rejected value as a YAML element, or when no callback or finalizer consumes it. + /// + public YamlElement? Value { get; private set; } + + /// + /// Gets the reason the converter gave, without the location that prefixes. + /// + public string Message => Exception.Reason; + + /// Gets the exception the converter threw. Its and locate the value. + public YamlException Exception { get; } + + internal void SetValue(YamlElement? value) => Value = value; +} diff --git a/src/SharpYaml/Serialization/YamlPathTracker.cs b/src/SharpYaml/Serialization/YamlPathTracker.cs index 08bb5c7..1c57c98 100644 --- a/src/SharpYaml/Serialization/YamlPathTracker.cs +++ b/src/SharpYaml/Serialization/YamlPathTracker.cs @@ -7,7 +7,7 @@ namespace SharpYaml.Serialization; /// -/// Tracks the document path of the reader by observing its tokens. Only created when unmapped member reporting is enabled. +/// Tracks the document path of the reader by observing its tokens. Only created when a reporting session exists. /// /// /// Completion of a value is applied lazily on the next token so that, while the reader is positioned on a value, @@ -35,14 +35,14 @@ private struct Frame private Frame[] _frames = new Frame[8]; private int _count; - public YamlPathTracker(YamlUnmappedMemberSession session, string? prefix) + public YamlPathTracker(YamlReportingSession session, string? prefix) { Session = session; _prefix = prefix; } /// Gets the session shared by this tracker's reader and the readers created for buffered nodes. - public YamlUnmappedMemberSession Session { get; } + public YamlReportingSession Session { get; } public void OnToken(YamlTokenType tokenType, string? scalarValue, Mark start) { diff --git a/src/SharpYaml/Serialization/YamlReader.cs b/src/SharpYaml/Serialization/YamlReader.cs index d0d344d..b9d3791 100644 --- a/src/SharpYaml/Serialization/YamlReader.cs +++ b/src/SharpYaml/Serialization/YamlReader.cs @@ -40,7 +40,7 @@ public static YamlReader Create(string yaml, YamlSerializerOptions? options = nu var effectiveOptions = options ?? YamlSerializerOptions.Default; var parser = SharpYaml.Parser.CreateParser(new StringReader(yaml), effectiveOptions.EffectiveMaxDepth, effectiveOptions.SourceName); var referenceReader = effectiveOptions.ReferenceHandling != YamlReferenceHandling.None ? new YamlReferenceReader() : null; - var session = YamlUnmappedMemberSession.Create(effectiveOptions); + var session = YamlReportingSession.Create(effectiveOptions); var tracker = session is null ? null : new YamlPathTracker(session, prefix: null); return new YamlReader(new YamlReaderState(parser, referenceReader, effectiveOptions.SourceName, tracker, discriminatorPropertyName: null), effectiveOptions); } @@ -57,7 +57,7 @@ public static YamlReader Create(TextReader reader, YamlSerializerOptions? option var effectiveOptions = options ?? YamlSerializerOptions.Default; var parser = SharpYaml.Parser.CreateParser(reader, effectiveOptions.EffectiveMaxDepth, effectiveOptions.SourceName); var referenceReader = effectiveOptions.ReferenceHandling != YamlReferenceHandling.None ? new YamlReferenceReader() : null; - var session = YamlUnmappedMemberSession.Create(effectiveOptions); + var session = YamlReportingSession.Create(effectiveOptions); var tracker = session is null ? null : new YamlPathTracker(session, prefix: null); return new YamlReader(new YamlReaderState(parser, referenceReader, effectiveOptions.SourceName, tracker, discriminatorPropertyName: null), effectiveOptions); } @@ -114,7 +114,7 @@ public bool IsDiscriminatorMember(string memberName) /// or is set. /// /// This is intended for converters and generated code, which should only call the unmapped member methods when it is . - public bool IsReportingUnmappedMembers => _state.Tracker is not null; + public bool IsReportingUnmappedMembers => _state.Tracker is { Session.IsReportingUnmappedMembers: true }; /// /// Starts reporting an unmapped member. Call this while the reader is positioned on the value of the unmapped key. @@ -130,8 +130,7 @@ public bool IsDiscriminatorMember(string memberName) /// , , or is . public YamlUnmappedMember? BeginUnmappedMember(Type declaringType, string[] knownMemberNames, string memberName, object? instance) { - var tracker = _state.Tracker; - if (tracker is null) + if (_state.Tracker is not { Session.IsReportingUnmappedMembers: true } tracker) { return null; } @@ -183,7 +182,90 @@ public void CompleteExtensionDataMember(YamlUnmappedMember? member, object? valu session.Report(member); } - internal void CompleteUnmappedMembers() => _state.Tracker?.Session.Complete(); + /// + /// Runs the configured finalizers, then throws when values were recovered under + /// . Called by the serializer once a read completed. + /// + internal void CompleteReports() => _state.Tracker?.Session.Complete(); + + /// + /// Gets whether the read must fail at its end because values were recovered under . + /// + internal bool FailsAtEnd => _state.Tracker is { Session.FailsAtEnd: true }; + + /// + /// Folds , which ended the read, into the listing the values + /// recovered so far. Call this only when is . + /// + /// The exception that ended the read. + internal YamlInvalidValuesException CreateFatalException(YamlException fatal) + => _state.Tracker is { } tracker ? tracker.Session.CreateException(fatal) : throw new InvalidOperationException("No invalid value was recovered."); + + /// + /// Gets whether , thrown while reading a member value, can be recovered from: the configured + /// is not , the + /// exception does not come from the parser, and the reader is still on the token the read began on, so nothing of the + /// value was consumed. + /// + /// The exception the converter threw. + /// The captured before the read. + /// The captured before the read. + /// when the value can be skipped and reported; otherwise . + /// Has no side effects, so it can serve as an exception filter. + internal bool CanRecoverInvalidValue(YamlException exception, YamlTokenType tokenType, Mark start) + => Options.InvalidValueHandling != YamlInvalidValueHandling.FailFast + && exception is not (SyntaxErrorException or SemanticErrorException) + && _state.TokenType == tokenType + && _state.Start.Index == start.Index; + + /// + /// Skips the value the reader is positioned on and reports it as invalid. Call this after + /// returned . The value is captured as a only when a callback or + /// finalizer consumes it. + /// + /// The exception the converter threw. + /// The CLR type being deserialized. + /// The instance being populated, or when it does not exist yet. + /// The YAML key of the member or constructor parameter. + /// The CLR type the value was meant for. + internal void RecoverInvalidValue(YamlException exception, Type declaringType, object? instance, string name, Type targetType) + { + if (_state.Tracker is not { } tracker) + { + Skip(); + return; + } + + var session = tracker.Session; + var value = new YamlInvalidValue(SourceName, declaringType, instance, name, tracker.GetPath(), tracker.GetCurrentKeyStart(), Start, targetType, exception); + if (session.IsReportingInvalidValues) + { + value.SetValue(Converters.YamlModelNodeConverter.ReadUnmappedNode(this, session.Anchors)); + } + else + { + Skip(); + } + + session.Report(value); + } + + /// + /// Creates a located at the current token, for a converter to throw when it rejects a value. + /// + /// The message describing why the value is rejected. + /// An exception carrying , and . + /// + /// Thrown before any of the value is consumed, the exception is recoverable under + /// modes other than : + /// the serializer skips the value and reports it instead of failing. + /// + /// is . + public YamlException CreateException(string message) + { + ArgumentGuard.ThrowIfNull(message); + return new YamlException(SourceName, Start, End, message); + } /// /// Gets the current token type. diff --git a/src/SharpYaml/Serialization/YamlReportingSession.cs b/src/SharpYaml/Serialization/YamlReportingSession.cs new file mode 100644 index 0000000..eb2ed8c --- /dev/null +++ b/src/SharpYaml/Serialization/YamlReportingSession.cs @@ -0,0 +1,123 @@ +// // Copyright (c) Alexandre Mutel. All rights reserved. +// // Licensed under the MIT license. +// // See LICENSE.txt file in the project root for full license information. + +using System; +using System.Collections.Generic; + +namespace SharpYaml.Serialization; + +/// +/// Per-deserialization reporting state. Created only when a report is consumed, and shared between a reader and the +/// readers it creates for buffered (polymorphic) nodes. +/// +internal sealed class YamlReportingSession +{ + private readonly Action? _callback; + private readonly Action>? _finalizer; + private readonly Action? _invalidValueCallback; + private readonly Action>? _invalidValuesFinalizer; + private List? _members; + private List? _invalidValues; + + private YamlReportingSession(YamlSerializerOptions options) + { + _callback = options.UnmappedMemberCallback; + _finalizer = options.UnmappedMembersFinalizer; + _invalidValueCallback = options.InvalidValueCallback; + _invalidValuesFinalizer = options.InvalidValuesFinalizer; + InvalidValueHandling = options.InvalidValueHandling; + } + + /// Whether an unmapped member callback or finalizer is configured. + public bool IsReportingUnmappedMembers => _callback is not null || _finalizer is not null; + + /// Whether an invalid value callback or finalizer is configured, that is whether a skipped value is worth capturing. + public bool IsReportingInvalidValues => _invalidValueCallback is not null || _invalidValuesFinalizer is not null; + + /// The configured handling of invalid values. + public YamlInvalidValueHandling InvalidValueHandling { get; } + + /// Anchors defined by the nodes reported so far, so that later reported nodes can alias them. + public Dictionary Anchors { get; } = new(); + + /// The path of the node most recently buffered by the reader; used to seed the path of the re-parse. + public string? BufferedPath { get; set; } + + /// + /// Creates a session when a report is consumed: an unmapped member delegate is set, invalid values fail at the end, + /// or they are skipped and a delegate is set. Otherwise returns so that the default costs nothing. + /// + public static YamlReportingSession? Create(YamlSerializerOptions options) + { + var reportsUnmappedMembers = options.UnmappedMemberCallback is not null || options.UnmappedMembersFinalizer is not null; + var reportsInvalidValues = options.InvalidValueHandling switch + { + YamlInvalidValueHandling.FailAtEnd => true, + YamlInvalidValueHandling.Skip => options.InvalidValueCallback is not null || options.InvalidValuesFinalizer is not null, + _ => false, + }; + + return reportsUnmappedMembers || reportsInvalidValues ? new YamlReportingSession(options) : null; + } + + public void Report(YamlUnmappedMember member) + { + _callback?.Invoke(member); + if (_finalizer is not null) + { + (_members ??= new List()).Add(member); + } + } + + public void Report(YamlInvalidValue value) + { + _invalidValueCallback?.Invoke(value); + if (_invalidValuesFinalizer is not null || InvalidValueHandling == YamlInvalidValueHandling.FailAtEnd) + { + (_invalidValues ??= new List()).Add(value); + } + } + + /// Whether invalid values were recovered that make the deserialization fail at its end. + public bool FailsAtEnd => InvalidValueHandling == YamlInvalidValueHandling.FailAtEnd && _invalidValues is { Count: > 0 }; + + /// + /// Creates the exception for a deserialization that ended with after at least one value was + /// recovered under . Finalizers do not run, because the read did not complete. + /// + public YamlInvalidValuesException CreateException(YamlException fatal) + { + var invalidValues = _invalidValues ?? throw new InvalidOperationException("No invalid value was recovered."); + _invalidValues = null; + return new YamlInvalidValuesException(invalidValues, fatal); + } + + /// + /// Runs the finalizers once the read completed, then throws when invalid values were recovered under + /// . + /// + /// At least one value was recovered and the handling is . + public void Complete() + { + var members = _members; + if (_finalizer is not null && members is { Count: > 0 }) + { + _members = null; + _finalizer(members); + } + + var invalidValues = _invalidValues; + if (invalidValues is not { Count: > 0 }) + { + return; + } + + _invalidValues = null; + _invalidValuesFinalizer?.Invoke(invalidValues); + if (InvalidValueHandling == YamlInvalidValueHandling.FailAtEnd) + { + throw new YamlInvalidValuesException(invalidValues); + } + } +} diff --git a/src/SharpYaml/Serialization/YamlSerializerContext.cs b/src/SharpYaml/Serialization/YamlSerializerContext.cs index c99e5c0..90e4460 100644 --- a/src/SharpYaml/Serialization/YamlSerializerContext.cs +++ b/src/SharpYaml/Serialization/YamlSerializerContext.cs @@ -100,4 +100,48 @@ public YamlSerializerOptions CreateOptions(Func public abstract YamlTypeInfo? GetTypeInfo(Type type, YamlSerializerOptions options); + + /// + /// Gets whether , thrown while reading a member value, can be recovered from under + /// : the handling is not , + /// the exception does not come from the parser, and is still on the token the read began on. + /// + /// The reader the value is read from. + /// The exception the converter threw. + /// The captured before the read. + /// The captured before the read. + /// when the value can be skipped and reported; otherwise . + /// Intended for generated code, which uses it as an exception filter. It has no side effects. + /// or is . + protected static bool CanRecoverInvalidValue(YamlReader reader, YamlException exception, YamlTokenType tokenType, Mark start) + { + ArgumentGuard.ThrowIfNull(reader); + ArgumentGuard.ThrowIfNull(exception); + return reader.CanRecoverInvalidValue(exception, tokenType, start); + } + + /// + /// Skips the value is positioned on and reports it as invalid. Call this after + /// returned . + /// + /// The reader the value is read from. + /// The exception the converter threw. + /// The CLR type being deserialized. + /// The instance being populated, or when it does not exist yet. + /// The YAML key of the member or constructor parameter. + /// The CLR type the value was meant for. + /// Intended for generated code. + /// + /// , , , + /// or is . + /// + protected static void RecoverInvalidValue(YamlReader reader, YamlException exception, Type declaringType, object? instance, string name, Type targetType) + { + ArgumentGuard.ThrowIfNull(reader); + ArgumentGuard.ThrowIfNull(exception); + ArgumentGuard.ThrowIfNull(declaringType); + ArgumentGuard.ThrowIfNull(name); + ArgumentGuard.ThrowIfNull(targetType); + reader.RecoverInvalidValue(exception, declaringType, instance, name, targetType); + } } diff --git a/src/SharpYaml/Serialization/YamlUnmappedMemberSession.cs b/src/SharpYaml/Serialization/YamlUnmappedMemberSession.cs deleted file mode 100644 index 7d976d9..0000000 --- a/src/SharpYaml/Serialization/YamlUnmappedMemberSession.cs +++ /dev/null @@ -1,59 +0,0 @@ -// // Copyright (c) Alexandre Mutel. All rights reserved. -// // Licensed under the MIT license. -// // See LICENSE.txt file in the project root for full license information. - -using System; -using System.Collections.Generic; - -namespace SharpYaml.Serialization; - -/// -/// Per-deserialization state for unmapped member reporting. Created only when a callback or finalizer is configured, -/// and shared between a reader and the readers it creates for buffered (polymorphic) nodes. -/// -internal sealed class YamlUnmappedMemberSession -{ - private readonly Action? _callback; - private readonly Action>? _finalizer; - private List? _members; - - private YamlUnmappedMemberSession(Action? callback, Action>? finalizer) - { - _callback = callback; - _finalizer = finalizer; - } - - /// Anchors defined by the nodes reported so far, so that later reported nodes can alias them. - public Dictionary Anchors { get; } = new(); - - /// The path of the node most recently buffered by the reader; used to seed the path of the re-parse. - public string? BufferedPath { get; set; } - - public static YamlUnmappedMemberSession? Create(YamlSerializerOptions options) - { - var callback = options.UnmappedMemberCallback; - var finalizer = options.UnmappedMembersFinalizer; - return callback is null && finalizer is null ? null : new YamlUnmappedMemberSession(callback, finalizer); - } - - public void Report(YamlUnmappedMember member) - { - _callback?.Invoke(member); - if (_finalizer is not null) - { - (_members ??= new List()).Add(member); - } - } - - public void Complete() - { - var members = _members; - if (_finalizer is null || members is null || members.Count == 0) - { - return; - } - - _members = null; - _finalizer(members); - } -} diff --git a/src/SharpYaml/YamlException.cs b/src/SharpYaml/YamlException.cs index d980c72..be65586 100644 --- a/src/SharpYaml/YamlException.cs +++ b/src/SharpYaml/YamlException.cs @@ -11,11 +11,19 @@ namespace SharpYaml; /// public class YamlException : Exception { + private readonly string? _reason; + /// /// Gets the optional source name associated with the YAML payload (for example, a file path). /// public string? SourceName { get; } + /// + /// Gets the message without the source name and location that prefixes, for callers + /// that report the location in their own format. For an exception created without a location, this is . + /// + public string Reason => _reason ?? Message; + /// /// Gets the position in the input stream where the event that originated the exception starts. /// @@ -40,6 +48,7 @@ public YamlException() public YamlException(string message) : base(message) { + _reason = message; } /// @@ -72,6 +81,7 @@ public YamlException(string? sourceName, Mark start, Mark end, string message) public YamlException(string? sourceName, Mark start, Mark end, string message, Exception? innerException) : base(FormatMessage(sourceName, start, end, message), innerException) { + _reason = message; SourceName = sourceName; Start = start; End = end; @@ -85,6 +95,7 @@ public YamlException(string? sourceName, Mark start, Mark end, string message, E public YamlException(string message, Exception inner) : base(message, inner) { + _reason = message; } private static string FormatMessage(string? sourceName, Mark start, Mark end, string message) diff --git a/src/SharpYaml/YamlInvalidValueHandling.cs b/src/SharpYaml/YamlInvalidValueHandling.cs new file mode 100644 index 0000000..1e14538 --- /dev/null +++ b/src/SharpYaml/YamlInvalidValueHandling.cs @@ -0,0 +1,33 @@ +// // Copyright (c) Alexandre Mutel. All rights reserved. +// // Licensed under the MIT license. +// // See LICENSE.txt file in the project root for full license information. + +namespace SharpYaml; + +/// +/// Defines what happens when a converter rejects the value of a member or constructor argument during deserialization. +/// +/// +/// A value is recoverable when the converter throws a before consuming any of it, for example +/// through . The member or argument then keeps its default and the value is +/// skipped. Any other exception, and any exception thrown after part of the value was read, fails the deserialization +/// whatever the handling. +/// +public enum YamlInvalidValueHandling +{ + /// + /// Throw at the first invalid value. + /// + FailFast = 0, + + /// + /// Recover from every recoverable invalid value, finish reading, then throw a + /// listing all of them. + /// + FailAtEnd = 1, + + /// + /// Recover from every recoverable invalid value and let the deserialization succeed. + /// + Skip = 2, +} diff --git a/src/SharpYaml/YamlInvalidValuesException.cs b/src/SharpYaml/YamlInvalidValuesException.cs new file mode 100644 index 0000000..b995354 --- /dev/null +++ b/src/SharpYaml/YamlInvalidValuesException.cs @@ -0,0 +1,111 @@ +// // Copyright (c) Alexandre Mutel. All rights reserved. +// // Licensed under the MIT license. +// // See LICENSE.txt file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Text; +using SharpYaml.Serialization; + +namespace SharpYaml; + +/// +/// Exception that is thrown at the end of a deserialization under +/// when at least one value was rejected by a converter and skipped. +/// +/// +/// and are those of the first invalid value. When the +/// deserialization ended with an error that could not be recovered from, that error is the . +/// +public class YamlInvalidValuesException : YamlException +{ + private static readonly YamlInvalidValue[] None = Array.Empty(); + + /// + /// Initializes a new instance of the class. + /// + public YamlInvalidValuesException() + { + InvalidValues = None; + } + + /// + /// Initializes a new instance of the class. + /// + /// The message. + public YamlInvalidValuesException(string message) + : base(message) + { + InvalidValues = None; + } + + /// + /// Initializes a new instance of the class. + /// + /// The message. + /// The inner. + public YamlInvalidValuesException(string message, Exception inner) + : base(message, inner) + { + InvalidValues = None; + } + + /// + /// Initializes a new instance of the class listing the values that were skipped. + /// + /// The values, in document order. At least one is required. + /// is . + /// is empty. + public YamlInvalidValuesException(IReadOnlyList invalidValues) + : this(invalidValues, null) + { + } + + /// + /// Initializes a new instance of the class listing the values that were skipped + /// and the error that ended the deserialization. + /// + /// The values, in document order. At least one is required. + /// The error that ended the deserialization, or when it completed. + /// is . + /// is empty. + public YamlInvalidValuesException(IReadOnlyList invalidValues, Exception? innerException) + : base(First(invalidValues).SourceName, First(invalidValues).Exception.Start, First(invalidValues).Exception.End, FormatMessage(invalidValues, innerException), innerException) + { + InvalidValues = invalidValues; + } + + /// + /// Gets the values that were rejected and skipped, in document order. + /// + public IReadOnlyList InvalidValues { get; } + + private static YamlInvalidValue First(IReadOnlyList invalidValues) + { + ArgumentGuard.ThrowIfNull(invalidValues); + if (invalidValues.Count == 0) + { + throw new ArgumentException("At least one invalid value is required.", nameof(invalidValues)); + } + + return invalidValues[0]; + } + + private static string FormatMessage(IReadOnlyList invalidValues, Exception? innerException) + { + var builder = new StringBuilder(); + builder.Append(invalidValues.Count).Append(invalidValues.Count == 1 ? " invalid value was skipped" : " invalid values were skipped"); + builder.Append(innerException is null ? " while deserializing:" : " before the deserialization failed:"); + foreach (var value in invalidValues) + { + builder.Append('\n').Append(" ").Append(value.Path).Append(": ").Append(value.Message); + } + + if (innerException is not null) + { + builder.Append('\n').Append(" ").Append(innerException.Message); + } + + return builder.ToString(); + } +} diff --git a/src/SharpYaml/YamlSerializer.cs b/src/SharpYaml/YamlSerializer.cs index 4ef03b7..0b88306 100644 --- a/src/SharpYaml/YamlSerializer.cs +++ b/src/SharpYaml/YamlSerializer.cs @@ -1162,12 +1162,17 @@ private static string SerializeCore(YamlTypeInfo typeInfo, T value) { result = typeInfo.ReadAsObject(reader); } + catch (YamlException exception) when (reader.FailsAtEnd) + { + throw reader.CreateFatalException(exception); + } catch (Exception exception) when (IsWrappedReadException(exception)) { - throw CreateReadException(reader, exception); + var readException = CreateReadException(reader, exception); + throw reader.FailsAtEnd ? reader.CreateFatalException(readException) : readException; } - reader.CompleteUnmappedMembers(); + reader.CompleteReports(); return result; } @@ -1187,12 +1192,17 @@ private static string SerializeCore(YamlTypeInfo typeInfo, T value) { result = typeInfo.Read(reader); } + catch (YamlException exception) when (reader.FailsAtEnd) + { + throw reader.CreateFatalException(exception); + } catch (Exception exception) when (IsWrappedReadException(exception)) { - throw CreateReadException(reader, exception); + var readException = CreateReadException(reader, exception); + throw reader.FailsAtEnd ? reader.CreateFatalException(readException) : readException; } - reader.CompleteUnmappedMembers(); + reader.CompleteReports(); return result; } @@ -1212,12 +1222,17 @@ private static string SerializeCore(YamlTypeInfo typeInfo, T value) { result = typeInfo.ReadAsObject(yamlReader); } + catch (YamlException exception) when (yamlReader.FailsAtEnd) + { + throw yamlReader.CreateFatalException(exception); + } catch (Exception exception) when (IsWrappedReadException(exception)) { - throw CreateReadException(yamlReader, exception); + var readException = CreateReadException(yamlReader, exception); + throw yamlReader.FailsAtEnd ? yamlReader.CreateFatalException(readException) : readException; } - yamlReader.CompleteUnmappedMembers(); + yamlReader.CompleteReports(); return result; } @@ -1237,12 +1252,17 @@ private static string SerializeCore(YamlTypeInfo typeInfo, T value) { result = typeInfo.Read(yamlReader); } + catch (YamlException exception) when (yamlReader.FailsAtEnd) + { + throw yamlReader.CreateFatalException(exception); + } catch (Exception exception) when (IsWrappedReadException(exception)) { - throw CreateReadException(yamlReader, exception); + var readException = CreateReadException(yamlReader, exception); + throw yamlReader.FailsAtEnd ? yamlReader.CreateFatalException(readException) : readException; } - yamlReader.CompleteUnmappedMembers(); + yamlReader.CompleteReports(); return result; } diff --git a/src/SharpYaml/YamlSerializerOptions.cs b/src/SharpYaml/YamlSerializerOptions.cs index 748a802..01b0068 100644 --- a/src/SharpYaml/YamlSerializerOptions.cs +++ b/src/SharpYaml/YamlSerializerOptions.cs @@ -119,6 +119,40 @@ public IReadOnlyList Converters /// public Action>? UnmappedMembersFinalizer { get; init; } + /// + /// Gets or sets what happens when a converter rejects the value of a member or constructor argument during deserialization. + /// + /// + /// The default, , throws at the first rejected value. The other modes + /// skip a value its converter rejected before consuming any of it, leave the member or argument at its default and keep + /// reading; see for what is recoverable. This is the counterpart of + /// for values whose key is known. + /// + public YamlInvalidValueHandling InvalidValueHandling { get; init; } = YamlInvalidValueHandling.FailFast; + + /// + /// Gets or sets a callback invoked for every value that was rejected by its converter and skipped during deserialization. + /// + /// + /// The callback runs as the values are encountered, under and + /// alone; under the first + /// rejected value throws and the callback is not invoked. This is the counterpart of . + /// + public Action? InvalidValueCallback { get; init; } + + /// + /// Gets or sets a callback invoked once at the end of a deserialization call with every + /// value that was rejected and skipped, in document order. + /// + /// + /// The callback is only invoked when at least one value was skipped, and runs before the + /// that throws. It may throw to + /// replace that exception, or to fail a call that would let succeed. + /// It is invoked by the YamlSerializer.Deserialize and TryDeserialize methods, not by direct + /// usage. This is the counterpart of . + /// + public Action>? InvalidValuesFinalizer { get; init; } + /// /// Gets or sets the preferred object creation handling for properties and fields during deserialization. ///