Skip to content

Multi-target against .NET 6 and .NET 8 #1349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove ObjectExtensions.AsEnumerable/AsArray/AsList
  • Loading branch information
bkoelman committed Nov 22, 2023
commit dea63fdba818ed0b6e8dd49c0252363d08d71bba
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ private void TrackPrimaryTable(TableAccessorNode tableAccessor)
_selectorsPerTable[tableAccessor] = _selectShape switch
{
SelectShape.Columns => Array.Empty<SelectorNode>(),
SelectShape.Count => new CountSelectorNode(null).AsArray(),
_ => new OneSelectorNode(null).AsArray()
SelectShape.Count => [new CountSelectorNode(null)],
_ => [new OneSelectorNode(null)]
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public UpdateNode Build(ResourceType resourceType, string setColumnName, string
ColumnNode whereColumn = table.GetColumn(whereColumnName, null, table.Alias);
WhereNode where = GetWhere(whereColumn, whereValue);

return new UpdateNode(table, columnAssignment.AsList(), where);
return new UpdateNode(table, [columnAssignment], where);
}

private WhereNode GetWhere(ColumnNode column, object? value)
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore.Annotations/CollectionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public IReadOnlyCollection<IIdentifiable> ExtractResources(object? value)
HashSet<IIdentifiable> resourceSet => resourceSet,
IReadOnlyCollection<IIdentifiable> resourceCollection => resourceCollection,
IEnumerable<IIdentifiable> resources => resources.ToList(),
IIdentifiable resource => resource.AsArray(),
IIdentifiable resource => [resource],
_ => Array.Empty<IIdentifiable>()
};
}
Expand Down
15 changes: 0 additions & 15 deletions src/JsonApiDotNetCore.Annotations/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ namespace JsonApiDotNetCore;

internal static class ObjectExtensions
{
public static IEnumerable<T> AsEnumerable<T>(this T element)
{
yield return element;
}

public static T[] AsArray<T>(this T element)
{
return [element];
}

public static List<T> AsList<T>(this T element)
{
return [element];
}

public static HashSet<T> AsHashSet<T>(this T element)
{
return [element];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static IServiceCollection AddJsonApi<TDbContext>(this IServiceCollection
Action<ServiceDiscoveryFacade>? discovery = null, Action<ResourceGraphBuilder>? resources = null, IMvcCoreBuilder? mvcBuilder = null)
where TDbContext : DbContext
{
return AddJsonApi(services, options, discovery, resources, mvcBuilder, typeof(TDbContext).AsArray());
return AddJsonApi(services, options, discovery, resources, mvcBuilder, [typeof(TDbContext)]);
}

private static void SetupApplicationBuilder(IServiceCollection services, Action<JsonApiOptions>? configureOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Errors/JsonApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public JsonApiException(ErrorObject error, Exception? innerException = null)
{
ArgumentGuard.NotNull(error);

Errors = error.AsArray();
Errors = [error];
}

public JsonApiException(IEnumerable<ErrorObject> errors, Exception? innerException = null)
Expand Down
24 changes: 15 additions & 9 deletions src/JsonApiDotNetCore/Middleware/ExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,21 @@ protected virtual IReadOnlyList<ErrorObject> CreateErrorResponse(Exception excep
IReadOnlyList<ErrorObject> errors = exception switch
{
JsonApiException jsonApiException => jsonApiException.Errors,
OperationCanceledException => new ErrorObject((HttpStatusCode)499)
{
Title = "Request execution was canceled."
}.AsArray(),
_ => new ErrorObject(HttpStatusCode.InternalServerError)
{
Title = "An unhandled error occurred while processing this request.",
Detail = exception.Message
}.AsArray()
OperationCanceledException =>
[
new ErrorObject((HttpStatusCode)499)
{
Title = "Request execution was canceled."
}
],
_ =>
[
new ErrorObject(HttpStatusCode.InternalServerError)
{
Title = "An unhandled error occurred while processing this request.",
Detail = exception.Message
}
]
};

if (_options.IncludeExceptionStackTraceInErrors && exception is not InvalidModelStateException)
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private static async Task FlushResponseAsync(HttpResponse httpResponse, JsonSeri

var errorDocument = new Document
{
Errors = error.AsList()
Errors = [error]
};

await JsonSerializer.SerializeAsync(httpResponse.Body, errorDocument, serializerOptions);
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Queries/Parsing/IncludeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void ParseRelationshipChain(string source, IncludeTreeNode treeRoot)
// that there's currently no way to include Products without Articles. We could add such optional upcast syntax
// in the future, if desired.

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

while (TokenStack.TryPeek(out Token? nextToken) && nextToken.Kind == TokenKind.Period)
{
Expand Down
8 changes: 4 additions & 4 deletions src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public QueryLayer ComposeForGetById<TId>(TId id, ResourceType primaryResourceTyp
QueryLayer queryLayer = ComposeFromConstraints(primaryResourceType);
queryLayer.Sort = null;
queryLayer.Pagination = null;
queryLayer.Filter = CreateFilterByIds(id.AsArray(), idAttribute, queryLayer.Filter);
queryLayer.Filter = CreateFilterByIds([id], idAttribute, queryLayer.Filter);

if (fieldSelection == TopFieldSelection.OnlyIdAttribute)
{
Expand Down Expand Up @@ -342,7 +342,7 @@ public QueryLayer WrapLayerForSecondaryEndpoint<TId>(QueryLayer secondaryLayer,
return new QueryLayer(primaryResourceType)
{
Include = RewriteIncludeForSecondaryEndpoint(innerInclude, relationship),
Filter = CreateFilterByIds(primaryId.AsArray(), primaryIdAttribute, primaryFilter),
Filter = CreateFilterByIds([primaryId], primaryIdAttribute, primaryFilter),
Selection = primarySelection
};
}
Expand Down Expand Up @@ -390,7 +390,7 @@ public QueryLayer ComposeForUpdate<TId>(TId id, ResourceType primaryResourceType
primaryLayer.Include = includeElements.Any() ? new IncludeExpression(includeElements) : IncludeExpression.Empty;
primaryLayer.Sort = null;
primaryLayer.Pagination = null;
primaryLayer.Filter = CreateFilterByIds(id.AsArray(), primaryIdAttribute, primaryLayer.Filter);
primaryLayer.Filter = CreateFilterByIds([id], primaryIdAttribute, primaryLayer.Filter);
primaryLayer.Selection = null;

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

FilterExpression? leftFilter = CreateFilterByIds(leftId.AsArray(), leftIdAttribute, null);
FilterExpression? leftFilter = CreateFilterByIds([leftId], leftIdAttribute, null);
FilterExpression? rightFilter = CreateFilterByIds(rightTypedIds, rightIdAttribute, null);

var secondarySelection = new FieldSelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ private static Expression IncludeExtensionMethodCall(Expression source, Type ent
{
Expression navigationExpression = Expression.Constant(navigationPropertyPath);

return Expression.Call(typeof(EntityFrameworkQueryableExtensions), "Include", entityType.AsArray(), source, navigationExpression);
return Expression.Call(typeof(EntityFrameworkQueryableExtensions), "Include", [entityType], source, navigationExpression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static Expression TestForNull(Expression expressionToTest, Expression if

private static Expression CopyCollectionExtensionMethodCall(Expression source, string operationName, Type elementType)
{
return Expression.Call(typeof(Enumerable), operationName, elementType.AsArray(), source);
return Expression.Call(typeof(Enumerable), operationName, [elementType], source);
}

private static Expression SelectExtensionMethodCall(Type extensionType, Expression source, Type elementType, Expression selectBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ private static Expression ExtensionMethodCall(Expression source, string operatio
{
Expression constant = value.CreateTupleAccessExpressionForConstant(typeof(int));

return Expression.Call(context.ExtensionType, operationName, context.LambdaScope.Parameter.Type.AsArray(), source, constant);
return Expression.Call(context.ExtensionType, operationName, [context.LambdaScope.Parameter.Type], source, constant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private LambdaExpression GetPredicateLambda(FilterExpression filter, QueryClause

private static Expression WhereExtensionMethodCall(LambdaExpression predicate, QueryClauseBuilderContext context)
{
return Expression.Call(context.ExtensionType, "Where", context.LambdaScope.Parameter.Type.AsArray(), context.Source, predicate);
return Expression.Call(context.ExtensionType, "Where", [context.LambdaScope.Parameter.Type], context.Source, predicate);
}

public override Expression VisitHas(HasExpression expression, QueryClauseBuilderContext context)
Expand Down Expand Up @@ -67,8 +67,8 @@ public override Expression VisitHas(HasExpression expression, QueryClauseBuilder
private static MethodCallExpression AnyExtensionMethodCall(Type elementType, Expression source, Expression? predicate)
{
return predicate != null
? Expression.Call(typeof(Enumerable), "Any", elementType.AsArray(), source, predicate)
: Expression.Call(typeof(Enumerable), "Any", elementType.AsArray(), source);
? Expression.Call(typeof(Enumerable), "Any", [elementType], source, predicate)
: Expression.Call(typeof(Enumerable), "Any", [elementType], source);
}

public override Expression VisitIsType(IsTypeExpression expression, QueryClauseBuilderContext context)
Expand Down Expand Up @@ -125,7 +125,7 @@ public override Expression VisitAny(AnyExpression expression, QueryClauseBuilder

private static Expression ContainsExtensionMethodCall(Expression collection, Expression value)
{
return Expression.Call(typeof(Enumerable), "Contains", value.Type.AsArray(), collection, value);
return Expression.Call(typeof(Enumerable), "Contains", [value.Type], collection, value);
}

public override Expression VisitLogical(LogicalExpression expression, QueryClauseBuilderContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public virtual IReadOnlyCollection<ExpressionInScope> GetConstraints()
? new ExpressionInScope(null, _includeExpression)
: new ExpressionInScope(null, IncludeExpression.Empty);

return expressionInScope.AsArray();
return [expressionInScope];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private SparseFieldSetExpression GetSparseFieldSet(string parameterValue, Resour
public virtual IReadOnlyCollection<ExpressionInScope> GetConstraints()
{
return _sparseFieldTableBuilder.Any()
? new ExpressionInScope(null, new SparseFieldTableExpression(_sparseFieldTableBuilder.ToImmutable())).AsArray()
? [new ExpressionInScope(null, new SparseFieldTableExpression(_sparseFieldTableBuilder.ToImmutable()))]
: Array.Empty<ExpressionInScope>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Document Convert(object? model)
}
else if (model is ErrorObject errorObject)
{
document.Errors = errorObject.AsArray();
document.Errors = [errorObject];
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Immutable;
using System.Reflection;
using JsonApiDotNetCore;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Queries.Expressions;
using JsonApiDotNetCore.Resources;
Expand Down Expand Up @@ -42,7 +41,7 @@ public CarExpressionRewriter(IResourceGraph resourceGraph)
}

string carStringId = (string)rightConstant.TypedValue;
return RewriteFilterOnCarStringIds(leftChain, carStringId.AsEnumerable());
return RewriteFilterOnCarStringIds(leftChain, [carStringId]);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq.Expressions;
using JsonApiDotNetCore;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Queries.Expressions;
using JsonApiDotNetCore.Queries.QueryableBuilding;
Expand Down Expand Up @@ -42,6 +41,6 @@ private LambdaExpression GetSelectorLambda(QueryExpression expression, QueryClau

private static Expression SumExtensionMethodCall(LambdaExpression selector, QueryClauseBuilderContext context)
{
return Expression.Call(context.ExtensionType, "Sum", context.LambdaScope.Parameter.Type.AsArray(), context.Source, selector);
return Expression.Call(context.ExtensionType, "Sum", [context.LambdaScope.Parameter.Type], context.Source, selector);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Net;
using FluentAssertions;
using JsonApiDotNetCore;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -916,7 +915,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
dbContext.WorkItems.Add(existingWorkItem);
await dbContext.SaveChangesAsync();

existingWorkItem.Children = existingWorkItem.AsList();
existingWorkItem.Children = [existingWorkItem];
await dbContext.SaveChangesAsync();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FluentAssertions;
using JetBrains.Annotations;
using JsonApiDotNetCore;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.QueryStrings.FieldChains;
using JsonApiDotNetCore.Resources;
Expand Down Expand Up @@ -47,7 +46,7 @@ public sealed class FieldChainPatternInheritanceMatchTests
public FieldChainPatternInheritanceMatchTests(ITestOutputHelper testOutputHelper)
{
var loggerProvider = new XUnitLoggerProvider(testOutputHelper, null, LogOutputFields.Message);
_loggerFactory = new LoggerFactory(loggerProvider.AsEnumerable());
_loggerFactory = new LoggerFactory([loggerProvider]);

var options = new JsonApiOptions();
_resourceGraph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add<Base, long>().Add<DerivedQ, long>().Add<DerivedV, long>().Build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FluentAssertions;
using JetBrains.Annotations;
using JsonApiDotNetCore;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.QueryStrings.FieldChains;
using JsonApiDotNetCore.Resources;
Expand Down Expand Up @@ -33,7 +32,7 @@ public sealed class FieldChainPatternMatchTests
public FieldChainPatternMatchTests(ITestOutputHelper testOutputHelper)
{
var loggerProvider = new XUnitLoggerProvider(testOutputHelper, null, LogOutputFields.Message);
_loggerFactory = new LoggerFactory(loggerProvider.AsEnumerable());
_loggerFactory = new LoggerFactory([loggerProvider]);

var options = new JsonApiOptions();
IResourceGraph resourceGraph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add<Resource, long>().Build();
Expand Down
3 changes: 1 addition & 2 deletions test/TestBuildingBlocks/TestableDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using JsonApiDotNetCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Logging;
Expand All @@ -21,7 +20,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder builder)
[Conditional("DEBUG")]
private static void WriteSqlStatementsToOutputWindow(DbContextOptionsBuilder builder)
{
builder.LogTo(message => Debug.WriteLine(message), DbLoggerCategory.Database.Name.AsArray(), LogLevel.Information);
builder.LogTo(message => Debug.WriteLine(message), [DbLoggerCategory.Database.Name], LogLevel.Information);
}

protected override void OnModelCreating(ModelBuilder builder)
Expand Down