Skip to content

Commit dea63fd

Browse files
committed
Remove ObjectExtensions.AsEnumerable/AsArray/AsList
1 parent 9546bcb commit dea63fd

File tree

23 files changed

+43
-58
lines changed

23 files changed

+43
-58
lines changed

src/Examples/DapperExample/TranslationToSql/Builders/SelectStatementBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ private void TrackPrimaryTable(TableAccessorNode tableAccessor)
106106
_selectorsPerTable[tableAccessor] = _selectShape switch
107107
{
108108
SelectShape.Columns => Array.Empty<SelectorNode>(),
109-
SelectShape.Count => new CountSelectorNode(null).AsArray(),
110-
_ => new OneSelectorNode(null).AsArray()
109+
SelectShape.Count => [new CountSelectorNode(null)],
110+
_ => [new OneSelectorNode(null)]
111111
};
112112
}
113113

src/Examples/DapperExample/TranslationToSql/Builders/UpdateClearOneToOneStatementBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public UpdateNode Build(ResourceType resourceType, string setColumnName, string
2929
ColumnNode whereColumn = table.GetColumn(whereColumnName, null, table.Alias);
3030
WhereNode where = GetWhere(whereColumn, whereValue);
3131

32-
return new UpdateNode(table, columnAssignment.AsList(), where);
32+
return new UpdateNode(table, [columnAssignment], where);
3333
}
3434

3535
private WhereNode GetWhere(ColumnNode column, object? value)

src/JsonApiDotNetCore.Annotations/CollectionConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public IReadOnlyCollection<IIdentifiable> ExtractResources(object? value)
7474
HashSet<IIdentifiable> resourceSet => resourceSet,
7575
IReadOnlyCollection<IIdentifiable> resourceCollection => resourceCollection,
7676
IEnumerable<IIdentifiable> resources => resources.ToList(),
77-
IIdentifiable resource => resource.AsArray(),
77+
IIdentifiable resource => [resource],
7878
_ => Array.Empty<IIdentifiable>()
7979
};
8080
}

src/JsonApiDotNetCore.Annotations/ObjectExtensions.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ namespace JsonApiDotNetCore;
44

55
internal static class ObjectExtensions
66
{
7-
public static IEnumerable<T> AsEnumerable<T>(this T element)
8-
{
9-
yield return element;
10-
}
11-
12-
public static T[] AsArray<T>(this T element)
13-
{
14-
return [element];
15-
}
16-
17-
public static List<T> AsList<T>(this T element)
18-
{
19-
return [element];
20-
}
21-
227
public static HashSet<T> AsHashSet<T>(this T element)
238
{
249
return [element];

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IServiceCollection AddJsonApi<TDbContext>(this IServiceCollection
3636
Action<ServiceDiscoveryFacade>? discovery = null, Action<ResourceGraphBuilder>? resources = null, IMvcCoreBuilder? mvcBuilder = null)
3737
where TDbContext : DbContext
3838
{
39-
return AddJsonApi(services, options, discovery, resources, mvcBuilder, typeof(TDbContext).AsArray());
39+
return AddJsonApi(services, options, discovery, resources, mvcBuilder, [typeof(TDbContext)]);
4040
}
4141

4242
private static void SetupApplicationBuilder(IServiceCollection services, Action<JsonApiOptions>? configureOptions,

src/JsonApiDotNetCore/Errors/JsonApiException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public JsonApiException(ErrorObject error, Exception? innerException = null)
2626
{
2727
ArgumentGuard.NotNull(error);
2828

29-
Errors = error.AsArray();
29+
Errors = [error];
3030
}
3131

3232
public JsonApiException(IEnumerable<ErrorObject> errors, Exception? innerException = null)

src/JsonApiDotNetCore/Middleware/ExceptionHandler.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,21 @@ protected virtual IReadOnlyList<ErrorObject> CreateErrorResponse(Exception excep
7474
IReadOnlyList<ErrorObject> errors = exception switch
7575
{
7676
JsonApiException jsonApiException => jsonApiException.Errors,
77-
OperationCanceledException => new ErrorObject((HttpStatusCode)499)
78-
{
79-
Title = "Request execution was canceled."
80-
}.AsArray(),
81-
_ => new ErrorObject(HttpStatusCode.InternalServerError)
82-
{
83-
Title = "An unhandled error occurred while processing this request.",
84-
Detail = exception.Message
85-
}.AsArray()
77+
OperationCanceledException =>
78+
[
79+
new ErrorObject((HttpStatusCode)499)
80+
{
81+
Title = "Request execution was canceled."
82+
}
83+
],
84+
_ =>
85+
[
86+
new ErrorObject(HttpStatusCode.InternalServerError)
87+
{
88+
Title = "An unhandled error occurred while processing this request.",
89+
Detail = exception.Message
90+
}
91+
]
8692
};
8793

8894
if (_options.IncludeExceptionStackTraceInErrors && exception is not InvalidModelStateException)

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private static async Task FlushResponseAsync(HttpResponse httpResponse, JsonSeri
205205

206206
var errorDocument = new Document
207207
{
208-
Errors = error.AsList()
208+
Errors = [error]
209209
};
210210

211211
await JsonSerializer.SerializeAsync(httpResponse.Body, errorDocument, serializerOptions);

src/JsonApiDotNetCore/Queries/Parsing/IncludeParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void ParseRelationshipChain(string source, IncludeTreeNode treeRoot)
8686
// that there's currently no way to include Products without Articles. We could add such optional upcast syntax
8787
// in the future, if desired.
8888

89-
ICollection<IncludeTreeNode> children = ParseRelationshipName(source, treeRoot.AsList());
89+
ICollection<IncludeTreeNode> children = ParseRelationshipName(source, [treeRoot]);
9090

9191
while (TokenStack.TryPeek(out Token? nextToken) && nextToken.Kind == TokenKind.Period)
9292
{

src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public QueryLayer ComposeForGetById<TId>(TId id, ResourceType primaryResourceTyp
272272
QueryLayer queryLayer = ComposeFromConstraints(primaryResourceType);
273273
queryLayer.Sort = null;
274274
queryLayer.Pagination = null;
275-
queryLayer.Filter = CreateFilterByIds(id.AsArray(), idAttribute, queryLayer.Filter);
275+
queryLayer.Filter = CreateFilterByIds([id], idAttribute, queryLayer.Filter);
276276

277277
if (fieldSelection == TopFieldSelection.OnlyIdAttribute)
278278
{
@@ -342,7 +342,7 @@ public QueryLayer WrapLayerForSecondaryEndpoint<TId>(QueryLayer secondaryLayer,
342342
return new QueryLayer(primaryResourceType)
343343
{
344344
Include = RewriteIncludeForSecondaryEndpoint(innerInclude, relationship),
345-
Filter = CreateFilterByIds(primaryId.AsArray(), primaryIdAttribute, primaryFilter),
345+
Filter = CreateFilterByIds([primaryId], primaryIdAttribute, primaryFilter),
346346
Selection = primarySelection
347347
};
348348
}
@@ -390,7 +390,7 @@ public QueryLayer ComposeForUpdate<TId>(TId id, ResourceType primaryResourceType
390390
primaryLayer.Include = includeElements.Any() ? new IncludeExpression(includeElements) : IncludeExpression.Empty;
391391
primaryLayer.Sort = null;
392392
primaryLayer.Pagination = null;
393-
primaryLayer.Filter = CreateFilterByIds(id.AsArray(), primaryIdAttribute, primaryLayer.Filter);
393+
primaryLayer.Filter = CreateFilterByIds([id], primaryIdAttribute, primaryLayer.Filter);
394394
primaryLayer.Selection = null;
395395

396396
return primaryLayer;
@@ -449,7 +449,7 @@ public QueryLayer ComposeForHasMany<TId>(HasManyAttribute hasManyRelationship, T
449449
AttrAttribute rightIdAttribute = GetIdAttribute(hasManyRelationship.RightType);
450450
HashSet<object> rightTypedIds = rightResourceIds.Select(resource => resource.GetTypedId()).ToHashSet();
451451

452-
FilterExpression? leftFilter = CreateFilterByIds(leftId.AsArray(), leftIdAttribute, null);
452+
FilterExpression? leftFilter = CreateFilterByIds([leftId], leftIdAttribute, null);
453453
FilterExpression? rightFilter = CreateFilterByIds(rightTypedIds, rightIdAttribute, null);
454454

455455
var secondarySelection = new FieldSelection();

0 commit comments

Comments
 (0)