Skip to content

Commit

Permalink
Merge pull request #118 from bdovaz/fix-IgnoreUnmatchedProperties-maps
Browse files Browse the repository at this point in the history
Fix IgnoreUnmatchedProperties for map types
  • Loading branch information
xoofx authored Feb 13, 2024
2 parents f7960e2 + 5d0236c commit 0aaf634
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
submodules: true
fetch-depth: 0

- name: Install .NET 6.0
- name: Install .NET 7.0
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '7.0.x'

- name: Build, Test, Pack, Publish
shell: bash
Expand Down
24 changes: 21 additions & 3 deletions src/SharpYaml.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void DeserializeScalar()
[Test]
public void DeserializeUnsafeExplicitType()
{
var settings = new SerializerSettings(){ UnsafeAllowDeserializeFromTagTypeName = true };
var settings = new SerializerSettings() { UnsafeAllowDeserializeFromTagTypeName = true };
var serializer = new Serializer(settings);
object result = serializer.Deserialize(YamlFile("explicitType.yaml"), typeof(object));

Expand Down Expand Up @@ -229,8 +229,8 @@ public void DeserializeUnregisterdExplicitDictionary()
{
var serializer = new Serializer();
Assert.Throws<YamlException>(() => serializer.Deserialize(YamlFile("dictionaryExplicit.yaml")));
}
}

[Test]
public void DeserializeListOfDictionaries()
{
Expand Down Expand Up @@ -903,6 +903,24 @@ public void SerializeGenericDictionaryShouldNotThrowTargetException()
});
}

[Test]
public void DeserializeDoesNotThrowWithNonPrimitiveIgnoreUnmatchedProperties()
{
var serializer = new Serializer(new SerializerSettings { IgnoreUnmatchedProperties = true });
var yaml = @"---
Mother:
Name: Name1
Father:
Name: Name2
Dog:
Name: Name3
...";

var family = serializer.Deserialize<Family>(yaml);
Assert.AreEqual("Name1", family.Mother.Name);
Assert.AreEqual("Name2", family.Father.Name);
}

private class OnlyGenericDictionary : IDictionary<string, string>
{
private readonly Dictionary<string, string> _dictionary = new Dictionary<string, string>();
Expand Down
2 changes: 1 addition & 1 deletion src/SharpYaml.Tests/SharpYaml.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<TargetFrameworks>net48;net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/SharpYaml/Serialization/Serializers/ObjectSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ protected virtual void ReadMember(ref ObjectContext objectContext)
if (!TryReadMember(ref objectContext, out var memberScalar, out var memberName))
{
if (!objectContext.Settings.IgnoreUnmatchedProperties)
throw new YamlException(memberScalar.Start, memberScalar.End, $"Unable to deserialize property [{memberName}] not found in type [{objectContext.Descriptor}]");
throw new YamlException(memberScalar.Start, memberScalar.End, $"Unable to deserialize property [{memberName}] not found in type [{objectContext.Descriptor}]");
else
objectContext.Reader.Skip(objectContext.Reader.CurrentDepth);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.100",
"version": "7.0.100",
"rollForward": "latestMinor",
"allowPrerelease": false
}
Expand Down

0 comments on commit 0aaf634

Please sign in to comment.