Skip to content

Commit 91a72a2

Browse files
authored
Handle bad newlines written by converter (#580)
1 parent cf2faa0 commit 91a72a2

File tree

7 files changed

+68
-25
lines changed

7 files changed

+68
-25
lines changed

docs/serializer-settings.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public Task ScopedSerializerFluent()
474474
.AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All);
475475
}
476476
```
477-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2387-L2414' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
477+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2412-L2439' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
478478
<!-- endSnippet -->
479479

480480
Result:
@@ -602,7 +602,7 @@ public Task IgnoreTypeFluent()
602602
.IgnoreMembersWithType<ToIgnoreStruct>();
603603
}
604604
```
605-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1627-L1732' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
605+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1652-L1757' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
606606
<!-- endSnippet -->
607607

608608
Or globally:
@@ -612,7 +612,7 @@ Or globally:
612612
```cs
613613
VerifierSettings.IgnoreMembersWithType<ToIgnore>();
614614
```
615-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1620-L1624' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretypeglobal' title='Start of snippet'>anchor</a></sup>
615+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1645-L1649' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretypeglobal' title='Start of snippet'>anchor</a></sup>
616616
<!-- endSnippet -->
617617

618618
Result:
@@ -683,7 +683,7 @@ public Task AddIgnoreInstanceFluent()
683683
.IgnoreInstance<Instance>(_ => _.Property == "Ignore");
684684
}
685685
```
686-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1548-L1587' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
686+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1573-L1612' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
687687
<!-- endSnippet -->
688688

689689
Or globally:
@@ -693,7 +693,7 @@ Or globally:
693693
```cs
694694
VerifierSettings.IgnoreInstance<Instance>(_ => _.Property == "Ignore");
695695
```
696-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1541-L1545' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstanceglobal' title='Start of snippet'>anchor</a></sup>
696+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1566-L1570' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstanceglobal' title='Start of snippet'>anchor</a></sup>
697697
<!-- endSnippet -->
698698

699699
Result:
@@ -736,7 +736,7 @@ public Task WithObsoleteProp()
736736
return Verify(target);
737737
}
738738
```
739-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2353-L2373' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
739+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2378-L2398' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
740740
<!-- endSnippet -->
741741

742742
Result:
@@ -784,7 +784,7 @@ public Task WithObsoletePropIncludedFluent()
784784
.IncludeObsoletes();
785785
}
786786
```
787-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2324-L2351' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
787+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2349-L2376' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
788788
<!-- endSnippet -->
789789

790790
Or globally:
@@ -794,7 +794,7 @@ Or globally:
794794
```cs
795795
VerifierSettings.IncludeObsoletes();
796796
```
797-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2317-L2321' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
797+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2342-L2346' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
798798
<!-- endSnippet -->
799799

800800
Result:
@@ -855,7 +855,7 @@ public Task IgnoreMemberByExpressionFluent()
855855
_ => _.PropertyThatThrows);
856856
}
857857
```
858-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1912-L1951' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
858+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1937-L1976' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
859859
<!-- endSnippet -->
860860

861861
Or globally
@@ -870,7 +870,7 @@ VerifierSettings.IgnoreMembers<IgnoreExplicitTarget>(
870870
_ => _.GetOnlyProperty,
871871
_ => _.PropertyThatThrows);
872872
```
873-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1900-L1909' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpressionglobal' title='Start of snippet'>anchor</a></sup>
873+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1925-L1934' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpressionglobal' title='Start of snippet'>anchor</a></sup>
874874
<!-- endSnippet -->
875875

876876
Result:
@@ -944,7 +944,7 @@ public Task IgnoreMemberByNameFluent()
944944
.IgnoreMember<IgnoreExplicitTarget>(_ => _.PropertyThatThrows);
945945
}
946946
```
947-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2004-L2057' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
947+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2029-L2082' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
948948
<!-- endSnippet -->
949949

950950
Or globally:
@@ -964,7 +964,7 @@ VerifierSettings.IgnoreMember<IgnoreExplicitTarget>("Field");
964964
// For a specific type with expression
965965
VerifierSettings.IgnoreMember<IgnoreExplicitTarget>(_ => _.PropertyThatThrows);
966966
```
967-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1987-L2001' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbynameglobal' title='Start of snippet'>anchor</a></sup>
967+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2012-L2026' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbynameglobal' title='Start of snippet'>anchor</a></sup>
968968
<!-- endSnippet -->
969969

970970
Result:
@@ -1011,7 +1011,7 @@ public Task CustomExceptionPropFluent()
10111011
.IgnoreMembersThatThrow<CustomException>();
10121012
}
10131013
```
1014-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2209-L2228' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
1014+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2234-L2253' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
10151015
<!-- endSnippet -->
10161016

10171017
Or globally:
@@ -1021,7 +1021,7 @@ Or globally:
10211021
```cs
10221022
VerifierSettings.IgnoreMembersThatThrow<CustomException>();
10231023
```
1024-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2202-L2206' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
1024+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2227-L2231' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
10251025
<!-- endSnippet -->
10261026

10271027
Result:
@@ -1234,7 +1234,7 @@ public Task MemberConverterByExpression()
12341234
return Verify(input);
12351235
}
12361236
```
1237-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1953-L1977' title='Snippet source file'>snippet source</a> | <a href='#snippet-memberconverter' title='Start of snippet'>anchor</a></sup>
1237+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1978-L2002' title='Snippet source file'>snippet source</a> | <a href='#snippet-memberconverter' title='Start of snippet'>anchor</a></sup>
12381238
<!-- endSnippet -->
12391239

12401240

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public Task VerifyJsonJToken()
349349
return VerifyJson(target);
350350
}
351351
```
352-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2133-L2165' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
352+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2158-L2190' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
353353
<!-- endSnippet -->
354354

355355
Results in:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Property1:
2+
3+
4+
A
5+
6+
7+
B
8+
9+
10+
,
11+
Property2:
12+
13+
14+
A
15+
16+
17+
B
18+
19+

src/Verify.Tests/Serialization/SerializationTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,8 @@ public Task TestEnumerableWithExistingConverter()
14691469
{
14701470
"Value"
14711471
};
1472-
return Verify(target).AddExtraSettings(_ => _.Converters.Add(new EnumerableWithExistingConverter()));
1472+
return Verify(target)
1473+
.AddExtraSettings(_ => _.Converters.Add(new EnumerableWithExistingConverter()));
14731474
}
14741475

14751476
class EnumerableWithExistingConverterTarget: List<string>
@@ -1483,6 +1484,30 @@ public override void Write(VerifyJsonWriter writer, EnumerableWithExistingConver
14831484
writer.Serialize("Content");
14841485
}
14851486

1487+
[Fact]
1488+
public Task TestConverterWithBadNewline()
1489+
{
1490+
var target = new ConverterWithBadNewlineTarget();
1491+
return Verify(target)
1492+
.AddExtraSettings(_ => _.Converters.Add(new ConverterWithBadNewline()));
1493+
}
1494+
1495+
class ConverterWithBadNewlineTarget
1496+
{
1497+
}
1498+
1499+
class ConverterWithBadNewline:
1500+
WriteOnlyJsonConverter<ConverterWithBadNewlineTarget>
1501+
{
1502+
public override void Write(VerifyJsonWriter writer, ConverterWithBadNewlineTarget target)
1503+
{
1504+
writer.WritePropertyName("Property1");
1505+
writer.WriteRawValue("\n\r\r\nA\n\r\r\nB\n\r\r\n");
1506+
writer.WritePropertyName("Property2");
1507+
writer.WriteValue("\n\r\r\nA\n\r\r\nB\n\r\r\n");
1508+
}
1509+
}
1510+
14861511
[Fact]
14871512
public Task TestEnumerableWithExistingItemConverter()
14881513
{

src/Verify/Extensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public static List<T> Clone<T>(this List<T> original) =>
1111
return attribute?.Configuration;
1212
}
1313

14+
public static string FixNewlines(this string value) =>
15+
value.Replace("\r\n", "\n")
16+
.Replace('\r', '\n');
17+
1418
public static char? FirstChar(this StringBuilder builder)
1519
{
1620
if (builder.Length > 0)

src/Verify/Serialization/JsonFormatter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static StringBuilder AsJson(object? input, List<ToAppend> appends, Verify
2222
var builder = new StringBuilder();
2323
using var writer = new VerifyJsonWriter(builder, settings, counter);
2424
settings.Serializer.Serialize(writer, input);
25+
builder.FixNewlines();
2526
return builder;
2627
}
2728
}

src/Verify/Serialization/VerifyJsonWriter.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void WriteRawValueWithScrubbers(string? value)
3535
base.WriteRawValue(value);
3636
return;
3737
}
38-
value = ReplaceNewlinesAndScrub(value);
38+
value = ApplyScrubbers.ApplyForPropertyValue(value, settings);
3939
base.WriteRawValue(value);
4040
}
4141

@@ -53,7 +53,7 @@ public override void WriteValue(string? value)
5353
return;
5454
}
5555

56-
value = ReplaceNewlinesAndScrub(value);
56+
value = ApplyScrubbers.ApplyForPropertyValue(value, settings);
5757
if (VerifierSettings.StrictJson)
5858
{
5959
base.WriteValue(value);
@@ -78,12 +78,6 @@ public override void WriteValue(string? value)
7878
WriteRawValue(value);
7979
}
8080

81-
string ReplaceNewlinesAndScrub(string value)
82-
{
83-
value = value.Replace("\r\n", "\n").Replace('\r', '\n');
84-
return ApplyScrubbers.ApplyForPropertyValue(value, settings);
85-
}
86-
8781
public void WriteSingleLineNoScrubbing(string value)
8882
{
8983
if (value is "")

0 commit comments

Comments
 (0)