Skip to content

Commit 7b38d92

Browse files
authored
Allow banning Newtonsoft.Json references (#110)
1 parent 3f5387d commit 7b38d92

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

.github/workflows/semgrep.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
jobs:
1111
call-workflow-semgrep:
1212
permissions:
13+
actions: read
1314
contents: read
1415
security-events: write
1516
uses: workleap/wl-reusable-workflows/.github/workflows/reusable-semgrep-workflow.yml@main

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ If you also have a `Directory.Build.props` file in your solution, you can remove
2929
- Project properties, including deterministic build, strict mode, continuous integration build detection, [faster package restoration](https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#restoring-with-msbuild-static-graph-evaluation), and [faster builds on Visual Studio](https://devblogs.microsoft.com/visualstudio/vs-toolbox-accelerate-your-builds-of-sdk-style-net-projects/), and more.
3030
- .NET analysis rules configuration, including style rules (`IDExxxx`) and code analysis rules (`CAxxxx`). These rules have been manually configured to provide a good balance between quality, performance, security, and build time.
3131
- Banned APIs, such as `DateTime.Now` and `DateTimeOffset.Now` (use their UTC counterparts instead).
32+
- Opt-in banning `Newtonsoft.Json`:
33+
34+
```xml
35+
<PropertyGroup>
36+
<BanNewtonsoftJsonSymbols>true</BanNewtonsoftJsonSymbols>
37+
</PropertyGroup>
38+
```
3239

3340
## What's NOT included
3441

src/build/Workleap.DotNet.CodingStandards.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
<!-- Banned Symbols -->
2222
<PropertyGroup>
2323
<IncludeDefaultBannedSymbols Condition="$(IncludeDefaultBannedSymbols) == ''">true</IncludeDefaultBannedSymbols>
24+
<BanNewtonsoftJsonSymbols Condition="$(BanNewtonsoftJsonSymbols) == ''">false</BanNewtonsoftJsonSymbols>
2425
</PropertyGroup>
2526

2627
<ItemGroup>
2728
<AdditionalFiles Include="$(MSBuildThisFileDirectory)\..\files\BannedSymbols.txt"
2829
Condition="$(IncludeDefaultBannedSymbols) == 'true'"
2930
Visible="false" />
31+
<AdditionalFiles Include="$(MSBuildThisFileDirectory)\..\files\BannedSymbols.Newtonsoft.Json.txt"
32+
Condition="$(BanNewtonsoftJsonSymbols) == 'true'"
33+
Visible="false" />
3034
</ItemGroup>
3135
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
N:Newtonsoft.Json

tests/Workleap.DotNet.CodingStandards.Tests/CodingStandardTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ public async Task BannedSymbolsAreReported()
1717
Assert.True(data.HasWarning("RS0030"));
1818
}
1919

20+
[Fact]
21+
public async Task BannedNewtonsoftJsonSymbolsAreReportedWhenPropertyIsSet()
22+
{
23+
using var project = new ProjectBuilder(fixture, testOutputHelper);
24+
project.AddCsprojFile(
25+
properties: new Dictionary<string, string> { { "BanNewtonsoftJsonSymbols", "true" } },
26+
packageReferences: new Dictionary<string, string> { { "Newtonsoft.Json", "13.0.1" } });
27+
project.AddFile("sample.cs", "_ = Newtonsoft.Json.JsonConvert.SerializeObject(new object());");
28+
var data = await project.BuildAndGetOutput();
29+
Assert.True(data.HasWarning("RS0030"));
30+
}
31+
32+
[Fact]
33+
public async Task BannedNewtonsoftJsonSymbolsAreNotReportedWhenPropertyIsNotSet()
34+
{
35+
using var project = new ProjectBuilder(fixture, testOutputHelper);
36+
project.AddCsprojFile(
37+
packageReferences: new Dictionary<string, string> { { "Newtonsoft.Json", "13.0.1" } });
38+
project.AddFile("sample.cs", "_ = Newtonsoft.Json.JsonConvert.SerializeObject(new object());");
39+
var data = await project.BuildAndGetOutput();
40+
Assert.False(data.HasWarning("RS0030"));
41+
}
42+
2043
[Fact]
2144
public async Task WarningsAsErrorOnGitHubActions()
2245
{

tests/Workleap.DotNet.CodingStandards.Tests/Helpers/ProjectBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void AddCsprojFile(Dictionary<string, string>? properties = null, Diction
5656
{
5757
foreach (var prop in properties)
5858
{
59-
propertyElement.Add(new XElement(prop.Key), prop.Value);
59+
propertyElement.Add(new XElement(prop.Key, prop.Value));
6060
}
6161
}
6262

0 commit comments

Comments
 (0)