Skip to content

Commit 2da9451

Browse files
author
Bart Koelman
committed
Fix cases for AV1130: Return type should be interface to unchangeable collection
1 parent dc7131e commit 2da9451

File tree

24 files changed

+62
-30
lines changed

24 files changed

+62
-30
lines changed

benchmarks/Serialization/SerializationBenchmarkBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public RelationshipLinks GetRelationshipLinks(RelationshipAttribute relationship
245245

246246
private sealed class FakeMetaBuilder : IMetaBuilder
247247
{
248-
public void Add(IReadOnlyDictionary<string, object?> values)
248+
public void Add(IDictionary<string, object?> values)
249249
{
250250
}
251251

src/JsonApiDotNetCore.Annotations/CollectionConverter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,19 @@ private Type ToConcreteCollectionType(Type collectionType)
6666
/// <summary>
6767
/// Returns a collection that contains zero, one or multiple resources, depending on the specified value.
6868
/// </summary>
69-
public ICollection<IIdentifiable> ExtractResources(object? value)
69+
public IReadOnlyCollection<IIdentifiable> ExtractResources(object? value)
7070
{
71-
if (value is ICollection<IIdentifiable> resourceCollection)
71+
if (value is List<IIdentifiable> resourceList)
72+
{
73+
return resourceList;
74+
}
75+
76+
if (value is HashSet<IIdentifiable> resourceSet)
77+
{
78+
return resourceSet;
79+
}
80+
81+
if (value is IReadOnlyCollection<IIdentifiable> resourceCollection)
7282
{
7383
return resourceCollection;
7484
}

src/JsonApiDotNetCore/AtomicOperations/Processors/SetRelationshipProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public SetRelationshipProcessor(ISetRelationshipService<TResource, TId> service)
4040

4141
if (relationship is HasManyAttribute)
4242
{
43-
ICollection<IIdentifiable> rightResources = _collectionConverter.ExtractResources(rightValue);
43+
IReadOnlyCollection<IIdentifiable> rightResources = _collectionConverter.ExtractResources(rightValue);
4444
return rightResources.ToHashSet(IdentifiableComparer.Instance);
4545
}
4646

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Microsoft.EntityFrameworkCore;
77
using Microsoft.Extensions.DependencyInjection;
88

9+
#pragma warning disable AV1130 // Return type in method signature should be an interface to an unchangeable collection
10+
911
namespace JsonApiDotNetCore.Configuration;
1012

1113
[PublicAPI]

src/JsonApiDotNetCore/Queries/Expressions/QueryableHandlerExpression.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public QueryableHandlerExpression(object queryableHandler, StringValues paramete
2121
_parameterValue = parameterValue;
2222
}
2323

24+
#pragma warning disable AV1130 // Return type in method signature should be an interface to an unchangeable collection
2425
public IQueryable<TResource> Apply<TResource>(IQueryable<TResource> query)
2526
where TResource : class, IIdentifiable
27+
#pragma warning restore AV1130 // Return type in method signature should be an interface to an unchangeable collection
2628
{
2729
var handler = (Func<IQueryable<TResource>, StringValues, IQueryable<TResource>>)_queryableHandler;
2830
return handler(query, _parameterValue);

src/JsonApiDotNetCore/Queries/FieldSelection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public sealed class FieldSelection : Dictionary<ResourceType, FieldSelectors>
1414
{
1515
public bool IsEmpty => Values.All(selectors => selectors.IsEmpty);
1616

17-
public ISet<ResourceType> GetResourceTypes()
17+
public IReadOnlySet<ResourceType> GetResourceTypes()
1818
{
1919
return Keys.ToHashSet();
2020
}

src/JsonApiDotNetCore/Queries/Internal/QueryLayerComposer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public QueryLayer ComposeForUpdate<TId>(TId id, ResourceType primaryResourceType
402402
foreach (RelationshipAttribute relationship in _targetedFields.Relationships)
403403
{
404404
object? rightValue = relationship.GetValue(primaryResource);
405-
ICollection<IIdentifiable> rightResourceIds = _collectionConverter.ExtractResources(rightValue);
405+
HashSet<IIdentifiable> rightResourceIds = _collectionConverter.ExtractResources(rightValue).ToHashSet(IdentifiableComparer.Instance);
406406

407407
if (rightResourceIds.Any())
408408
{

src/JsonApiDotNetCore/Queries/Internal/QueryableBuilding/SelectClauseBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private Expression CreateLambdaBodyInitializer(FieldSelection selection, Resourc
7676
private Expression CreateLambdaBodyInitializerForTypeHierarchy(FieldSelection selection, ResourceType baseResourceType,
7777
IEnumerable<IEntityType> concreteEntityTypes, LambdaScope lambdaScope)
7878
{
79-
ISet<ResourceType> resourceTypes = selection.GetResourceTypes();
79+
IReadOnlySet<ResourceType> resourceTypes = selection.GetResourceTypes();
8080
Expression rootCondition = lambdaScope.Accessor;
8181

8282
foreach (IEntityType entityType in concreteEntityTypes)

src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreRepository.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public virtual async Task<int> CountAsync(FilterExpression? filter, Cancellation
105105
}
106106
}
107107

108+
#pragma warning disable AV1130 // Return type in method signature should be an interface to an unchangeable collection
108109
protected virtual IQueryable<TResource> ApplyQueryLayer(QueryLayer queryLayer)
110+
#pragma warning restore AV1130 // Return type in method signature should be an interface to an unchangeable collection
109111
{
110112
_traceWriter.LogMethodStart(new
111113
{
@@ -149,7 +151,9 @@ protected virtual IQueryable<TResource> ApplyQueryLayer(QueryLayer queryLayer)
149151
}
150152
}
151153

154+
#pragma warning disable AV1130 // Return type in method signature should be an interface to an unchangeable collection
152155
protected virtual IQueryable<TResource> GetAll()
156+
#pragma warning restore AV1130 // Return type in method signature should be an interface to an unchangeable collection
153157
{
154158
return _dbContext.Set<TResource>();
155159
}
@@ -221,12 +225,12 @@ public virtual async Task CreateAsync(TResource resourceFromRequest, TResource r
221225

222226
if (relationship is HasManyAttribute hasManyRelationship)
223227
{
224-
HashSet<IIdentifiable> rightResourceIdSet = _collectionConverter.ExtractResources(rightValue).ToHashSet(IdentifiableComparer.Instance);
228+
HashSet<IIdentifiable> rightResourceIds = _collectionConverter.ExtractResources(rightValue).ToHashSet(IdentifiableComparer.Instance);
225229

226-
await _resourceDefinitionAccessor.OnSetToManyRelationshipAsync(leftResource, hasManyRelationship, rightResourceIdSet, writeOperation,
230+
await _resourceDefinitionAccessor.OnSetToManyRelationshipAsync(leftResource, hasManyRelationship, rightResourceIds, writeOperation,
227231
cancellationToken);
228232

229-
return rightResourceIdSet;
233+
return rightResourceIds;
230234
}
231235

232236
return rightValue;
@@ -572,7 +576,7 @@ protected async Task UpdateRelationshipAsync(RelationshipAttribute relationship,
572576
return null;
573577
}
574578

575-
ICollection<IIdentifiable> rightResources = _collectionConverter.ExtractResources(rightValue);
579+
IReadOnlyCollection<IIdentifiable> rightResources = _collectionConverter.ExtractResources(rightValue);
576580
IIdentifiable[] rightResourcesTracked = rightResources.Select(rightResource => _dbContext.GetTrackedOrAttach(rightResource)).ToArray();
577581

578582
return rightValue is IEnumerable

src/JsonApiDotNetCore/Resources/IResourceDefinition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public interface IResourceDefinition<TResource, in TId>
115115
/// <summary>
116116
/// Enables to add JSON:API meta information, specific to this resource.
117117
/// </summary>
118+
#pragma warning disable AV1130 // Return type in method signature should be an interface to an unchangeable collection
118119
IDictionary<string, object?>? GetMeta(TResource resource);
120+
#pragma warning restore AV1130 // Return type in method signature should be an interface to an unchangeable collection
119121

120122
/// <summary>
121123
/// Executes after the original version of the resource has been retrieved from the underlying data store, as part of a write request.

0 commit comments

Comments
 (0)