Skip to content

Commit af0414f

Browse files
committed
Fix CA1859: Use concrete types when possible for improved performance
1 parent a5337ac commit af0414f

File tree

17 files changed

+64
-71
lines changed

17 files changed

+64
-71
lines changed

src/JsonApiDotNetCore.OpenApi.Client.NSwag/JsonApiClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public IDisposable WithPartialAttributeSerialization<TRequestDocument, TAttribut
2929
{
3030
ArgumentGuard.NotNull(requestDocument);
3131

32-
var attributeNames = new HashSet<string>();
32+
HashSet<string> attributeNames = [];
3333

3434
foreach (Expression<Func<TAttributesObject, object?>> selector in alwaysIncludedAttributeSelectors)
3535
{
@@ -96,10 +96,10 @@ public void Dispose()
9696
/// </summary>
9797
private sealed class AlwaysIncludedAttributes
9898
{
99-
private readonly ISet<string> _propertyNames;
99+
private readonly HashSet<string> _propertyNames;
100100
private readonly Type _attributesObjectType;
101101

102-
public AlwaysIncludedAttributes(ISet<string> propertyNames, Type attributesObjectType)
102+
public AlwaysIncludedAttributes(HashSet<string> propertyNames, Type attributesObjectType)
103103
{
104104
ArgumentGuard.NotNull(propertyNames);
105105
ArgumentGuard.NotNull(attributesObjectType);

src/JsonApiDotNetCore.OpenApi.Swashbuckle/ConfigureSwaggerGenOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void Configure(SwaggerGenOptions options)
6363
options.DocumentFilter<UnusedComponentSchemaCleaner>();
6464
}
6565

66-
private IEnumerable<Type> SelectDerivedTypes(Type baseType)
66+
private List<Type> SelectDerivedTypes(Type baseType)
6767
{
6868
if (baseType == typeof(ResourceData))
6969
{
@@ -103,7 +103,7 @@ private List<Type> GetConstructedTypesForAtomicOperation()
103103
return derivedTypes;
104104
}
105105

106-
private static IList<string> GetOpenApiOperationTags(ApiDescription description, IControllerResourceMapping controllerResourceMapping)
106+
private static List<string> GetOpenApiOperationTags(ApiDescription description, IControllerResourceMapping controllerResourceMapping)
107107
{
108108
MethodInfo actionMethod = description.ActionDescriptor.GetActionMethod();
109109
ResourceType? resourceType = controllerResourceMapping.GetResourceTypeForController(actionMethod.ReflectedType);

src/JsonApiDotNetCore.OpenApi.Swashbuckle/IncludeDependencyScanner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public IReadOnlySet<ResourceType> GetReachableRelatedTypes(ResourceType resource
1212
{
1313
ArgumentGuard.NotNull(resourceType);
1414

15-
var resourceTypesFound = new HashSet<ResourceType>();
15+
HashSet<ResourceType> resourceTypesFound = [];
1616
AddTypesFromRelationships(resourceType.Relationships, resourceTypesFound);
1717
return resourceTypesFound;
1818
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiActionDescriptorCollectionProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public JsonApiActionDescriptorCollectionProvider(IActionDescriptorCollectionProv
3939
private ActionDescriptorCollection GetActionDescriptors()
4040
{
4141
List<ActionDescriptor> newDescriptors = _defaultProvider.ActionDescriptors.Items.ToList();
42-
List<ActionDescriptor> endpoints = newDescriptors.Where(IsVisibleJsonApiEndpoint).ToList();
42+
ActionDescriptor[] endpoints = newDescriptors.Where(IsVisibleJsonApiEndpoint).ToArray();
4343

4444
foreach (ActionDescriptor endpoint in endpoints)
4545
{
@@ -69,7 +69,7 @@ internal static bool IsVisibleJsonApiEndpoint(ActionDescriptor descriptor)
6969
return descriptor is ControllerActionDescriptor controllerAction && controllerAction.Properties.ContainsKey(typeof(ApiDescriptionActionData));
7070
}
7171

72-
private static IEnumerable<ActionDescriptor> AddJsonApiMetadataToAction(ActionDescriptor endpoint, IJsonApiEndpointMetadata? jsonApiEndpointMetadata)
72+
private static List<ActionDescriptor> AddJsonApiMetadataToAction(ActionDescriptor endpoint, IJsonApiEndpointMetadata? jsonApiEndpointMetadata)
7373
{
7474
switch (jsonApiEndpointMetadata)
7575
{
@@ -133,10 +133,10 @@ private static bool ProducesJsonApiResponseDocument(ActionDescriptor endpoint)
133133
contentType is HeaderConstants.MediaType or HeaderConstants.AtomicOperationsMediaType or HeaderConstants.RelaxedAtomicOperationsMediaType);
134134
}
135135

136-
private static IEnumerable<ActionDescriptor> Expand(ActionDescriptor genericEndpoint, NonPrimaryEndpointMetadata metadata,
136+
private static List<ActionDescriptor> Expand(ActionDescriptor genericEndpoint, NonPrimaryEndpointMetadata metadata,
137137
Action<ActionDescriptor, Type, string> expansionCallback)
138138
{
139-
var expansion = new List<ActionDescriptor>();
139+
List<ActionDescriptor> expansion = [];
140140

141141
foreach ((string relationshipName, Type documentType) in metadata.DocumentTypesByRelationshipName)
142142
{

src/JsonApiDotNetCore.OpenApi.Swashbuckle/JsonApiSchemaIdSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal sealed class JsonApiSchemaIdSelector
2727
private const string RelationshipIdentifierSchemaIdTemplate = "[ResourceName] [RelationshipName] Relationship Identifier";
2828
private const string RelationshipNameSchemaIdTemplate = "[ResourceName] [RelationshipName] Relationship Name";
2929

30-
private static readonly IDictionary<Type, string> SchemaTypeToTemplateMap = new Dictionary<Type, string>
30+
private static readonly Dictionary<Type, string> SchemaTypeToTemplateMap = new()
3131
{
3232
[typeof(CreateResourceRequestDocument<>)] = "Create [ResourceName] Request Document",
3333
[typeof(UpdateResourceRequestDocument<>)] = "Update [ResourceName] Request Document",

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiEndpointConvention.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void SetResponseMetadata(ActionModel action, JsonApiEndpoint endpoint, R
144144
}
145145
}
146146

147-
private static IEnumerable<HttpStatusCode> GetSuccessStatusCodesForEndpoint(JsonApiEndpoint endpoint)
147+
private static HttpStatusCode[] GetSuccessStatusCodesForEndpoint(JsonApiEndpoint endpoint)
148148
{
149149
return endpoint switch
150150
{
@@ -176,7 +176,7 @@ private static IEnumerable<HttpStatusCode> GetSuccessStatusCodesForEndpoint(Json
176176
};
177177
}
178178

179-
private IEnumerable<HttpStatusCode> GetErrorStatusCodesForEndpoint(JsonApiEndpoint endpoint, ResourceType? resourceType)
179+
private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpoint endpoint, ResourceType? resourceType)
180180
{
181181
// Condition doesn't apply to atomic operations, because Forbidden is also used when an operation is not accessible.
182182
ClientIdGenerationMode clientIdGeneration = resourceType?.ClientIdGeneration ?? _options.ClientIdGeneration;
@@ -212,12 +212,12 @@ private IEnumerable<HttpStatusCode> GetErrorStatusCodesForEndpoint(JsonApiEndpoi
212212
HttpStatusCode.UnprocessableEntity
213213
],
214214
JsonApiEndpoint.DeleteResource => [HttpStatusCode.NotFound],
215-
JsonApiEndpoint.PostRelationship or JsonApiEndpoint.PatchRelationship or JsonApiEndpoint.DeleteRelationship => new[]
216-
{
215+
JsonApiEndpoint.PostRelationship or JsonApiEndpoint.PatchRelationship or JsonApiEndpoint.DeleteRelationship =>
216+
[
217217
HttpStatusCode.BadRequest,
218218
HttpStatusCode.NotFound,
219219
HttpStatusCode.Conflict
220-
},
220+
],
221221
JsonApiEndpoint.PostOperations =>
222222
[
223223
HttpStatusCode.BadRequest,

src/JsonApiDotNetCore.OpenApi.Swashbuckle/OpenApiOperationIdSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal sealed class OpenApiOperationIdSelector
1919
private const string RelationshipIdTemplate = $"{SecondaryResourceIdTemplate} Relationship";
2020
private const string AtomicOperationsIdTemplate = "[Method] Operations";
2121

22-
private static readonly IDictionary<Type, string> SchemaOpenTypeToOpenApiOperationIdTemplateMap = new Dictionary<Type, string>
22+
private static readonly Dictionary<Type, string> SchemaOpenTypeToOpenApiOperationIdTemplateMap = new()
2323
{
2424
[typeof(ResourceCollectionResponseDocument<>)] = ResourceCollectionIdTemplate,
2525
[typeof(PrimaryResourceResponseDocument<>)] = ResourceIdTemplate,

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SchemaGenerators/JsonApiSchemaGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle.SchemaGenerators;
1111
internal sealed class JsonApiSchemaGenerator : ISchemaGenerator
1212
{
1313
private readonly ResourceIdSchemaGenerator _resourceIdSchemaGenerator;
14-
private readonly ICollection<BodySchemaGenerator> _bodySchemaGenerators;
14+
private readonly BodySchemaGenerator[] _bodySchemaGenerators;
1515

1616
public JsonApiSchemaGenerator(ResourceIdSchemaGenerator resourceIdSchemaGenerator, IEnumerable<BodySchemaGenerator> bodySchemaGenerators)
1717
{
1818
ArgumentGuard.NotNull(resourceIdSchemaGenerator);
1919
ArgumentGuard.NotNull(bodySchemaGenerators);
2020

2121
_resourceIdSchemaGenerator = resourceIdSchemaGenerator;
22-
_bodySchemaGenerators = bodySchemaGenerators.ToArray();
22+
_bodySchemaGenerators = bodySchemaGenerators as BodySchemaGenerator[] ?? bodySchemaGenerators.ToArray();
2323
}
2424

2525
public OpenApiSchema GenerateSchema(Type modelType, SchemaRepository schemaRepository, MemberInfo? memberInfo = null, ParameterInfo? parameterInfo = null,

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/EndpointOrderingFilter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
2424
ArgumentGuard.NotNull(swaggerDoc);
2525
ArgumentGuard.NotNull(context);
2626

27-
List<KeyValuePair<string, OpenApiPathItem>> endpointsInOrder = swaggerDoc.Paths.OrderBy(GetPrimaryResourcePublicName)
28-
.ThenBy(GetRelationshipName).ThenBy(IsSecondaryEndpoint).ToList();
27+
KeyValuePair<string, OpenApiPathItem>[] endpointsInOrder = swaggerDoc.Paths.OrderBy(GetPrimaryResourcePublicName)
28+
.ThenBy(GetRelationshipName).ThenBy(IsSecondaryEndpoint).ToArray();
2929

3030
swaggerDoc.Paths.Clear();
3131

src/JsonApiDotNetCore.OpenApi.Swashbuckle/SwaggerComponents/JsonApiDataContractResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ private static DataContract OrderPropertiesInDataContract(DataContract dataContr
8181
return dataContract;
8282
}
8383

84-
private IList<DataProperty> GetDataPropertiesThatExistInResourceClrType(Type resourceClrType, DataContract dataContract)
84+
private List<DataProperty> GetDataPropertiesThatExistInResourceClrType(Type resourceClrType, DataContract dataContract)
8585
{
8686
ResourceType resourceType = _resourceGraph.GetResourceType(resourceClrType);
87-
var dataProperties = new List<DataProperty>();
87+
List<DataProperty> dataProperties = [];
8888

8989
foreach (DataProperty property in dataContract.ObjectProperties)
9090
{

0 commit comments

Comments
 (0)