Skip to content

Commit 515bddb

Browse files
authored
throw for invalid usage of IgnoreMember (#521)
1 parent 6e52e62 commit 515bddb

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

docs/serializer-settings.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ public Task ScopedSerializerFluent()
613613
.AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All);
614614
}
615615
```
616-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2233-L2260' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
616+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2248-L2275' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
617617
<!-- endSnippet -->
618618

619619
Result:
@@ -881,7 +881,7 @@ public Task WithObsoleteProp()
881881
return Verify(target);
882882
}
883883
```
884-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2197-L2217' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
884+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2212-L2232' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
885885
<!-- endSnippet -->
886886

887887
Result:
@@ -929,7 +929,7 @@ public Task WithObsoletePropIncludedFluent()
929929
.IncludeObsoletes();
930930
}
931931
```
932-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2168-L2195' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
932+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2183-L2210' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
933933
<!-- endSnippet -->
934934

935935
Or globally:
@@ -939,7 +939,7 @@ Or globally:
939939
```cs
940940
VerifierSettings.IncludeObsoletes();
941941
```
942-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2161-L2165' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
942+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2176-L2180' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
943943
<!-- endSnippet -->
944944

945945
Result:
@@ -1169,7 +1169,7 @@ public Task CustomExceptionPropFluent()
11691169
.IgnoreMembersThatThrow<CustomException>();
11701170
}
11711171
```
1172-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2057-L2076' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
1172+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2072-L2091' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
11731173
<!-- endSnippet -->
11741174

11751175
Or globally:
@@ -1179,7 +1179,7 @@ Or globally:
11791179
```cs
11801180
VerifierSettings.IgnoreMembersThatThrow<CustomException>();
11811181
```
1182-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2050-L2054' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
1182+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2065-L2069' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
11831183
<!-- endSnippet -->
11841184

11851185
Result:

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public Task VerifyJsonJToken()
309309
return VerifyJson(target);
310310
}
311311
```
312-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1981-L2013' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
312+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1996-L2028' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
313313
<!-- endSnippet -->
314314

315315
Results in:

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;msb3277;CS0436</NoWarn>
5-
<Version>16.8.0</Version>
5+
<Version>16.8.1</Version>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<LangVersion>10</LangVersion>
88
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
Type: Exception,
3+
Message:
4+
IgnoreMember<T> can only be used on the type that defines the member.
5+
To ignore specific members for T, create a custom converter.
6+
}

src/Verify.Tests/Serialization/SerializationTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,6 +1919,21 @@ public Task IgnoreMemberByNameFluent()
19191919

19201920
#endregion
19211921

1922+
public class IgnoreTargetBase
1923+
{
1924+
public string Property { get; set; }
1925+
}
1926+
1927+
public class IgnoreTargetSub :
1928+
IgnoreTargetBase
1929+
{
1930+
}
1931+
1932+
[Fact]
1933+
public Task IgnoreMemberSubClass() =>
1934+
Throws(() => VerifierSettings.IgnoreMember<IgnoreTargetSub>(_ => _.Property))
1935+
.IgnoreStackTrack();
1936+
19221937
[Fact]
19231938
public Task IgnoreJTokenByName()
19241939
{

src/Verify/Serialization/SerializationSettings_Ignore.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ public void IgnoreMember<T>(Expression<Func<T, object?>> expression)
2121
where T : notnull
2222
{
2323
var member = expression.FindMember();
24-
IgnoreMember(member.DeclaringType!, member.Name);
24+
var declaringType = member.DeclaringType!;
25+
if (typeof(T) != declaringType)
26+
{
27+
throw new(@"IgnoreMember<T> can only be used on the type that defines the member.
28+
To ignore specific members for T, create a custom converter.");
29+
}
30+
31+
IgnoreMember(declaringType, member.Name);
2532
}
2633

2734
public void IgnoreMembers<T>(params string[] names)

0 commit comments

Comments
 (0)