Skip to content

Commit

Permalink
Block scalars can be keys too
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardCooke committed Jul 16, 2022
1 parent 38b6ff5 commit a08d558
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
24 changes: 24 additions & 0 deletions YamlDotNet.Test/Serialization/DeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,30 @@ public void NonScalarKeyIsHandledWithTypeDeserialization()
Assert.Equal("baz", item.Value);
}

[Fact]
public void NewLinesInKeys()
{
var yaml = @"? >-
key
a
b
: >-
value
a
b
";
var deserializer = new DeserializerBuilder().Build();
var o = deserializer.Deserialize(yaml, typeof(object));
Assert.IsType<Dictionary<object, object>>(o);
var dictionary = (Dictionary<object, object>)o;
Assert.Equal($"key\na\nb", dictionary.First().Key);
Assert.Equal($"value\na\nb", dictionary.First().Value);
}

public class Test
{
public string Value { get; set; }
Expand Down
6 changes: 2 additions & 4 deletions YamlDotNet/Core/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ private void StaleSimpleKeys()
private void FetchNextToken()
{
// Check if we just started scanning. Fetch STREAM-START then.

if (!streamStartProduced)
{
FetchStreamStart();
Expand Down Expand Up @@ -418,7 +417,6 @@ private void FetchNextToken()
}

// Is it the value indicator?

if (analyzer.Check(':') &&
(flowLevel > 0 || analyzer.IsWhiteBreakOrZero(1)) &&
!(simpleKeyAllowed && flowLevel > 0) &&
Expand Down Expand Up @@ -1464,9 +1462,9 @@ Token ScanTag()

private void FetchBlockScalar(bool isLiteral)
{
// Remove any potential simple keys.
// A block scalar can be a simple key

RemoveSimpleKey();
SaveSimpleKey();

// A simple key may follow a block scalar.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static void DeserializeHelper(Type tKey, Type tValue, IParser parser, Fu
{
if (key == null)
{
throw new ArgumentException("key", "Empty key names are not supported yet.");
throw new ArgumentException("Empty key names are not supported yet.", "key");
}

if (valuePromise == null)
Expand Down

0 comments on commit a08d558

Please sign in to comment.