Skip to content

Commit 10e9ad9

Browse files
author
Bart Koelman
committed
Fix cases for AV1553/AV1554: Optional string/collection/task parameter usage
1 parent 2da9451 commit 10e9ad9

File tree

9 files changed

+60
-10
lines changed

9 files changed

+60
-10
lines changed

src/JsonApiDotNetCore.Annotations/ArgumentGuard.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@ public static void NotNull<T>([NoEnumeration] [SysNotNull] T? value, [InvokerPar
1818
}
1919

2020
[AssertionMethod]
21-
public static void NotNullNorEmpty<T>([SysNotNull] IEnumerable<T>? value, [InvokerParameterName] string name, string? collectionName = null)
21+
public static void NotNullNorEmpty<T>([SysNotNull] IEnumerable<T>? value, [InvokerParameterName] string name)
2222
{
2323
NotNull(value, name);
2424

2525
if (!value.Any())
2626
{
27-
throw new ArgumentException($"Must have one or more {collectionName ?? name}.", name);
27+
throw new ArgumentException($"Must have one or more {name}.", name);
28+
}
29+
}
30+
31+
[AssertionMethod]
32+
public static void NotNullNorEmpty<T>([SysNotNull] IEnumerable<T>? value, [InvokerParameterName] string name, string collectionName)
33+
{
34+
NotNull(value, name);
35+
36+
if (!value.Any())
37+
{
38+
throw new ArgumentException($"Must have one or more {collectionName}.", name);
2839
}
2940
}
3041

src/JsonApiDotNetCore.Annotations/Configuration/ResourceType.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ public sealed class ResourceType
8989
/// </remarks>
9090
public LinkTypes RelationshipLinks { get; }
9191

92-
public ResourceType(string publicName, Type clrType, Type identityClrType, IReadOnlyCollection<AttrAttribute>? attributes = null,
93-
IReadOnlyCollection<RelationshipAttribute>? relationships = null, IReadOnlyCollection<EagerLoadAttribute>? eagerLoads = null,
92+
public ResourceType(string publicName, Type clrType, Type identityClrType, LinkTypes topLevelLinks = LinkTypes.NotConfigured,
93+
LinkTypes resourceLinks = LinkTypes.NotConfigured, LinkTypes relationshipLinks = LinkTypes.NotConfigured)
94+
: this(publicName, clrType, identityClrType, null, null, null, topLevelLinks, resourceLinks, relationshipLinks)
95+
{
96+
}
97+
98+
public ResourceType(string publicName, Type clrType, Type identityClrType, IReadOnlyCollection<AttrAttribute>? attributes,
99+
IReadOnlyCollection<RelationshipAttribute>? relationships, IReadOnlyCollection<EagerLoadAttribute>? eagerLoads,
94100
LinkTypes topLevelLinks = LinkTypes.NotConfigured, LinkTypes resourceLinks = LinkTypes.NotConfigured,
95101
LinkTypes relationshipLinks = LinkTypes.NotConfigured)
96102
{

src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ private static bool IsImplicitManyToManyJoinEntity(IEntityType entityType)
175175
/// The name under which the resource is publicly exposed by the API. If nothing is specified, the naming convention is applied on the pluralized CLR
176176
/// type name.
177177
/// </param>
178+
#pragma warning disable AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
178179
public ResourceGraphBuilder Add<TResource, TId>(string? publicName = null)
179180
where TResource : class, IIdentifiable<TId>
181+
#pragma warning restore AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
180182
{
181183
return Add(typeof(TResource), typeof(TId), publicName);
182184
}
@@ -194,7 +196,9 @@ public ResourceGraphBuilder Add<TResource, TId>(string? publicName = null)
194196
/// The name under which the resource is publicly exposed by the API. If nothing is specified, the naming convention is applied on the pluralized CLR
195197
/// type name.
196198
/// </param>
199+
#pragma warning disable AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
197200
public ResourceGraphBuilder Add(Type resourceClrType, Type? idClrType = null, string? publicName = null)
201+
#pragma warning restore AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
198202
{
199203
ArgumentGuard.NotNull(resourceClrType, nameof(resourceClrType));
200204

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ public static class ServiceCollectionExtensions
1818
/// <summary>
1919
/// Configures JsonApiDotNetCore by registering resources manually.
2020
/// </summary>
21+
#pragma warning disable AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
2122
public static IServiceCollection AddJsonApi(this IServiceCollection services, Action<JsonApiOptions>? options = null,
2223
Action<ServiceDiscoveryFacade>? discovery = null, Action<ResourceGraphBuilder>? resources = null, IMvcCoreBuilder? mvcBuilder = null,
2324
ICollection<Type>? dbContextTypes = null)
25+
#pragma warning restore AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
2426
{
2527
ArgumentGuard.NotNull(services, nameof(services));
2628

src/JsonApiDotNetCore/Diagnostics/CascadingCodeTimer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ static CascadingCodeTimer()
3232
}
3333

3434
/// <inheritdoc />
35-
public IDisposable Measure(string name, bool excludeInRelativeCost = false)
35+
public IDisposable Measure(string name)
36+
{
37+
return Measure(name, false);
38+
}
39+
40+
/// <inheritdoc />
41+
public IDisposable Measure(string name, bool excludeInRelativeCost)
3642
{
3743
MeasureScope childScope = CreateChildScope(name, excludeInRelativeCost);
3844
_activeScopeStack.Push(childScope);

src/JsonApiDotNetCore/Diagnostics/DisabledCodeTimer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ private DisabledCodeTimer()
1111
{
1212
}
1313

14-
public IDisposable Measure(string name, bool excludeInRelativeCost = false)
14+
public IDisposable Measure(string name)
15+
{
16+
return this;
17+
}
18+
19+
public IDisposable Measure(string name, bool excludeInRelativeCost)
1520
{
1621
return this;
1722
}

src/JsonApiDotNetCore/Diagnostics/ICodeTimer.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ namespace JsonApiDotNetCore.Diagnostics;
55
/// </summary>
66
public interface ICodeTimer : IDisposable
77
{
8+
/// <summary>
9+
/// Starts recording the duration of a code block, while including this measurement in calculated percentages. Wrap this call in a <c>using</c>
10+
/// statement, so the recording stops when the return value goes out of scope.
11+
/// </summary>
12+
/// <param name="name">
13+
/// Description of what is being recorded.
14+
/// </param>
15+
IDisposable Measure(string name);
16+
817
/// <summary>
918
/// Starts recording the duration of a code block. Wrap this call in a <c>using</c> statement, so the recording stops when the return value goes out of
1019
/// scope.
@@ -13,9 +22,9 @@ public interface ICodeTimer : IDisposable
1322
/// Description of what is being recorded.
1423
/// </param>
1524
/// <param name="excludeInRelativeCost">
16-
/// When set, indicates to exclude this measurement in calculated percentages. <c>false</c> by default.
25+
/// When set, indicates to exclude this measurement in calculated percentages.
1726
/// </param>
18-
IDisposable Measure(string name, bool excludeInRelativeCost = false);
27+
IDisposable Measure(string name, bool excludeInRelativeCost);
1928

2029
/// <summary>
2130
/// Returns intermediate or final results.

src/JsonApiDotNetCore/Queries/Internal/Parsing/Token.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ public sealed class Token
88
public TokenKind Kind { get; }
99
public string? Value { get; }
1010

11-
public Token(TokenKind kind, string? value = null)
11+
public Token(TokenKind kind)
1212
{
1313
Kind = kind;
14+
}
15+
16+
public Token(TokenKind kind, string value)
17+
: this(kind)
18+
{
1419
Value = value;
1520
}
1621

test/JsonApiDotNetCoreTests/UnitTests/Links/LinkInclusionTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public sealed class LinkInclusionTests
5656
public void Applies_cascading_settings_for_top_level_links(LinkTypes linksInResourceType, LinkTypes linksInOptions, LinkTypes expected)
5757
{
5858
// Arrange
59-
var exampleResourceType = new ResourceType(nameof(ExampleResource), typeof(ExampleResource), typeof(int), topLevelLinks: linksInResourceType);
59+
var exampleResourceType = new ResourceType(nameof(ExampleResource), typeof(ExampleResource), typeof(int), linksInResourceType);
6060

6161
var options = new JsonApiOptions
6262
{
@@ -396,6 +396,7 @@ public ResourceType GetResourceTypeForController(Type? controllerType)
396396

397397
private sealed class FakeLinkGenerator : LinkGenerator
398398
{
399+
#pragma warning disable AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
399400
public override string GetPathByAddress<TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values,
400401
RouteValueDictionary? ambientValues = null, PathString? pathBase = null, FragmentString fragment = new(), LinkOptions? options = null)
401402
{
@@ -420,5 +421,6 @@ public override string GetUriByAddress<TAddress>(TAddress address, RouteValueDic
420421
{
421422
throw new NotImplementedException();
422423
}
424+
#pragma warning restore AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
423425
}
424426
}

0 commit comments

Comments
 (0)