Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Obsolete settings #37

Merged
merged 3 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/mdsource/serializer-settings.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ Result:
snippet: Tests.AddIgnoreInstance.verified.txt


## Obsolete members ignored

Members with an [ObsoleteAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.obsoleteattribute) are ignored:

snippet: WithObsoleteProp

Result:

snippet: Tests.WithObsoleteProp.verified.txt


## Including Obsolete members

Obsolete members can be included using `IncludeObsoletes`:

snippet: WithObsoletePropIncluded

Result:

snippet: Tests.WithObsoletePropIncluded.verified.txt



## Ignore member by expressions

To ignore members of a certain type using an expression:
Expand Down
85 changes: 83 additions & 2 deletions docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Serialization settings can be customized at three levels:
* [Scoped settings](#scoped-settings)
* [Ignoring a type](#ignoring-a-type)
* [Ignoring a instance](#ignoring-a-instance)
* [Obsolete members ignored](#obsolete-members-ignored)
* [Including Obsolete members](#including-obsolete-members)
* [Ignore member by expressions](#ignore-member-by-expressions)
* [Ignore member by name](#ignore-member-by-name)
* [Members that throw](#members-that-throw)<!-- endtoc -->
Expand All @@ -47,7 +49,7 @@ var settings = new JsonSerializerSettings
DefaultValueHandling = DefaultValueHandling.Ignore
};
```
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L152-L161' title='File snippet `defaultserialization` was extracted from'>snippet source</a> | <a href='#snippet-defaultserialization' title='Navigate to start of snippet `defaultserialization`'>anchor</a></sup>
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L153-L162' title='File snippet `defaultserialization` was extracted from'>snippet source</a> | <a href='#snippet-defaultserialization' title='Navigate to start of snippet `defaultserialization`'>anchor</a></sup>
<!-- endsnippet -->


Expand Down Expand Up @@ -144,7 +146,7 @@ var target = new DateTimeTarget

await Verify(target);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L557-L573' title='File snippet `date` was extracted from'>snippet source</a> | <a href='#snippet-date' title='Navigate to start of snippet `date`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L581-L597' title='File snippet `date` was extracted from'>snippet source</a> | <a href='#snippet-date' title='Navigate to start of snippet `date`'>anchor</a></sup>
<!-- endsnippet -->

Results in the following:
Expand Down Expand Up @@ -360,6 +362,85 @@ Result:
<!-- endsnippet -->


## Obsolete members ignored

Members with an [ObsoleteAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.obsoleteattribute) are ignored:

<!-- snippet: WithObsoleteProp -->
<a id='snippet-withobsoleteprop'/></a>
```cs
class WithObsolete
{
[Obsolete]
public string ObsoleteProperty { get; set; }
public string OtherProperty { get; set; }
}

[Fact]
public Task WithObsoleteProp()
{
var target = new WithObsolete
{
ObsoleteProperty = "value1",
OtherProperty = "value2"
};
return Verify(target);
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L384-L404' title='File snippet `withobsoleteprop` was extracted from'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Navigate to start of snippet `withobsoleteprop`'>anchor</a></sup>
<!-- endsnippet -->

Result:

<!-- snippet: Tests.WithObsoleteProp.verified.txt -->
<a id='snippet-Tests.WithObsoleteProp.verified.txt'/></a>
```txt
{
OtherProperty: 'value2'
}
```
<sup><a href='/src/Verify.Tests/Tests.WithObsoleteProp.verified.txt#L1-L3' title='File snippet `Tests.WithObsoleteProp.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.WithObsoleteProp.verified.txt' title='Navigate to start of snippet `Tests.WithObsoleteProp.verified.txt`'>anchor</a></sup>
<!-- endsnippet -->


## Including Obsolete members

Obsolete members can be included using `IncludeObsoletes`:

<!-- snippet: WithObsoletePropIncluded -->
<a id='snippet-withobsoletepropincluded'/></a>
```cs
[Fact]
public Task WithObsoletePropIncluded()
{
var target = new WithObsolete
{
ObsoleteProperty = "value1",
OtherProperty = "value2"
};
var settings = new VerifySettings();
settings.ModifySerialization(_=> { _.IncludeObsoletes(); });
return Verify(target, settings);
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L406-L421' title='File snippet `withobsoletepropincluded` was extracted from'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Navigate to start of snippet `withobsoletepropincluded`'>anchor</a></sup>
<!-- endsnippet -->

Result:

<!-- snippet: Tests.WithObsoletePropIncluded.verified.txt -->
<a id='snippet-Tests.WithObsoletePropIncluded.verified.txt'/></a>
```txt
{
ObsoleteProperty: 'value1',
OtherProperty: 'value2'
}
```
<sup><a href='/src/Verify.Tests/Tests.WithObsoletePropIncluded.verified.txt#L1-L4' title='File snippet `Tests.WithObsoletePropIncluded.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.WithObsoletePropIncluded.verified.txt' title='Navigate to start of snippet `Tests.WithObsoletePropIncluded.verified.txt`'>anchor</a></sup>
<!-- endsnippet -->



## Ignore member by expressions

To ignore members of a certain type using an expression:
Expand Down
4 changes: 3 additions & 1 deletion src/Verify.Tests/Tests.WithObsoleteProp.verified.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
OtherProperty: 'value2'
}
4 changes: 4 additions & 0 deletions src/Verify.Tests/Tests.WithObsoletePropIncluded.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
ObsoleteProperty: 'value1',
OtherProperty: 'value2'
}
42 changes: 33 additions & 9 deletions src/Verify.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,26 +378,50 @@ class WithNotSupportedException
{
public Guid NotImplementedExceptionProperty => throw new NotSupportedException();
}

#pragma warning disable 612

#region WithObsoleteProp

class WithObsolete
{
[Obsolete]
public string ObsoleteProperty { get; set; }
public string OtherProperty { get; set; }
}

[Fact]
public Task WithObsoleteProp()
{
var target = new WithObsolete();
var target = new WithObsolete
{
ObsoleteProperty = "value1",
OtherProperty = "value2"
};
return Verify(target);
}

class WithObsolete
{
Guid obsoleteProperty;
#endregion

[Obsolete]
public Guid ObsoleteProperty
#region WithObsoletePropIncluded

[Fact]
public Task WithObsoletePropIncluded()
{
var target = new WithObsolete
{
get { throw new NotImplementedException(); }
set => obsoleteProperty = value;
}
ObsoleteProperty = "value1",
OtherProperty = "value2"
};
var settings = new VerifySettings();
settings.ModifySerialization(_=> { _.IncludeObsoletes(); });
return Verify(target, settings);
}

#endregion

#pragma warning restore 612

[Fact]
public Task Escaping()
{
Expand Down
12 changes: 9 additions & 3 deletions src/Verify/Serialization/CustomContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CustomContractResolver :
{
bool ignoreEmptyCollections;
bool ignoreFalse;
bool includeObsoletes;
IReadOnlyDictionary<Type, List<string>> ignored;
IReadOnlyList<Type> ignoredTypes;
IReadOnlyList<Func<Exception, bool>> ignoreMembersThatThrow;
Expand All @@ -19,6 +20,7 @@ class CustomContractResolver :
public CustomContractResolver(
bool ignoreEmptyCollections,
bool ignoreFalse,
bool includeObsoletes,
IReadOnlyDictionary<Type, List<string>> ignored,
IReadOnlyList<Type> ignoredTypes,
IReadOnlyList<Func<Exception, bool>> ignoreMembersThatThrow,
Expand All @@ -29,6 +31,7 @@ public CustomContractResolver(
Guard.AgainstNull(ignoreMembersThatThrow, nameof(ignoreMembersThatThrow));
this.ignoreEmptyCollections = ignoreEmptyCollections;
this.ignoreFalse = ignoreFalse;
this.includeObsoletes = includeObsoletes;
this.ignored = ignored;
this.ignoredTypes = ignoredTypes;
this.ignoreMembersThatThrow = ignoreMembersThatThrow;
Expand All @@ -52,10 +55,13 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ

property.ConfigureIfBool(member, ignoreFalse);

if (member.GetCustomAttribute<ObsoleteAttribute>(true) != null)
if (!includeObsoletes)
{
property.Ignored = true;
return property;
if (member.GetCustomAttribute<ObsoleteAttribute>(true) != null)
{
property.Ignored = true;
return property;
}
}

if (ignoredTypes.Any(x => x.IsAssignableFrom(property.PropertyType)))
Expand Down
11 changes: 10 additions & 1 deletion src/Verify/Serialization/SerializationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public SerializationSettings Clone()
ignoreMembersWithType = ignoreMembersWithType.Clone(),
ignoredInstances = ignoredInstances.Clone(),
scrubDateTimes = scrubDateTimes,
scrubGuids = scrubGuids
scrubGuids = scrubGuids,
includeObsoletes = includeObsoletes,
};
}

Expand Down Expand Up @@ -164,6 +165,7 @@ public JsonSerializerSettings BuildSettings()
settings.ContractResolver = new CustomContractResolver(
ignoreEmptyCollections,
ignoreFalse,
includeObsoletes,
ignoredMembers,
ignoreMembersWithType,
ignoreMembersThatThrow,
Expand Down Expand Up @@ -224,5 +226,12 @@ internal void RegenSettings()
{
currentSettings = BuildSettings();
}

bool includeObsoletes;

public void IncludeObsoletes()
{
includeObsoletes = true;
}
}
}
1 change: 1 addition & 0 deletions src/Verify/Serialization/VerifySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public void AddExtraSettings(Action<JsonSerializerSettings> action)
serialization.AddExtraSettings(action);
serialization.RegenSettings();
}

}
}