Skip to content

Commit ae18541

Browse files
author
Bart Koelman
committed
Resolved new warnings
1 parent 6b3402c commit ae18541

File tree

17 files changed

+57
-41
lines changed

17 files changed

+57
-41
lines changed

benchmarks/LinkBuilder/LinkBuilderGetNamespaceFromPathBenchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static void GetNamespaceFromPathUsingReadOnlySpan(string path, string re
6767

6868
if (isAtEnd || hasDelimiterAfterSegment)
6969
{
70-
_ = pathSpan.Slice(0, index).ToString();
70+
_ = pathSpan[..index].ToString();
7171
}
7272
}
7373
}

src/JsonApiDotNetCore/Configuration/JsonApiValidationFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public bool ShouldValidateEntry(ValidationEntry entry, ValidationEntry parentEnt
4141

4242
var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
4343

44-
if (httpContextAccessor.HttpContext.Request.Method == HttpMethods.Patch || request.OperationKind == OperationKind.UpdateResource)
44+
if (httpContextAccessor.HttpContext!.Request.Method == HttpMethods.Patch || request.OperationKind == OperationKind.UpdateResource)
4545
{
4646
var targetedFields = _serviceProvider.GetRequiredService<ITargetedFields>();
4747
return IsFieldTargeted(entry, targetedFields);

src/JsonApiDotNetCore/Middleware/FixedQueryFeature.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public IQueryCollection Query
2727
{
2828
get
2929
{
30-
if (_features.Collection == null)
30+
if (IsFeatureCollectionNull())
3131
{
3232
return _parsedValues ??= QueryCollection.Empty;
3333
}
@@ -49,7 +49,7 @@ public IQueryCollection Query
4949
{
5050
_parsedValues = value;
5151

52-
if (_features.Collection != null)
52+
if (!IsFeatureCollectionNull())
5353
{
5454
if (value == null)
5555
{
@@ -77,5 +77,12 @@ public FixedQueryFeature(IFeatureCollection features)
7777

7878
_features.Initalize(features);
7979
}
80+
81+
private bool IsFeatureCollectionNull()
82+
{
83+
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
84+
// Justification: This code was copied from the ASP.NET sources. A struct instance can be created without calling one of its constructors.
85+
return _features.Collection == null;
86+
}
8087
}
8188
}

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ private static async Task<bool> ValidateContentTypeHeaderAsync(string allowedCon
125125
{
126126
string contentType = httpContext.Request.ContentType;
127127

128+
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
129+
// Justification: Workaround for https://github.com/dotnet/aspnetcore/issues/32097 (fixed in .NET 6)
128130
if (contentType != null && contentType != allowedContentType)
129131
{
130132
await FlushResponseAsync(httpContext.Response, serializerSettings, new Error(HttpStatusCode.UnsupportedMediaType)
@@ -280,9 +282,9 @@ private static string GetCustomRoute(string resourceName, string apiNamespace, H
280282
if (resourceName != null)
281283
{
282284
Endpoint endpoint = httpContext.GetEndpoint();
283-
var routeAttribute = endpoint.Metadata.GetMetadata<RouteAttribute>();
285+
var routeAttribute = endpoint?.Metadata.GetMetadata<RouteAttribute>();
284286

285-
if (routeAttribute != null)
287+
if (routeAttribute != null && httpContext.Request.Path.Value != null)
286288
{
287289
List<string> trimmedComponents = httpContext.Request.Path.Value.Trim('/').Split('/').ToList();
288290
int resourceNameIndex = trimmedComponents.FindIndex(component => component == resourceName);

src/JsonApiDotNetCore/QueryStrings/Internal/RequestQueryStringAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal sealed class RequestQueryStringAccessor : IRequestQueryStringAccessor
77
{
88
private readonly IHttpContextAccessor _httpContextAccessor;
99

10-
public IQueryCollection Query => _httpContextAccessor.HttpContext.Request.Query;
10+
public IQueryCollection Query => _httpContextAccessor.HttpContext!.Request.Query;
1111

1212
public RequestQueryStringAccessor(IHttpContextAccessor httpContextAccessor)
1313
{

src/JsonApiDotNetCore/Serialization/Building/LinkBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ private bool ShouldIncludeTopLevelLink(LinkTypes linkType, ResourceContext resou
102102
private string GetLinkForTopLevelSelf()
103103
{
104104
return _options.UseRelativeLinks
105-
? _httpContextAccessor.HttpContext.Request.GetEncodedPathAndQuery()
106-
: _httpContextAccessor.HttpContext.Request.GetEncodedUrl();
105+
? _httpContextAccessor.HttpContext!.Request.GetEncodedPathAndQuery()
106+
: _httpContextAccessor.HttpContext!.Request.GetEncodedUrl();
107107
}
108108

109109
private void SetPaginationInTopLevelLinks(ResourceContext requestContext, TopLevelLinks links)
@@ -133,7 +133,7 @@ private void SetPaginationInTopLevelLinks(ResourceContext requestContext, TopLev
133133

134134
private string CalculatePageSizeValue(PageSize topPageSize, ResourceContext requestContext)
135135
{
136-
string pageSizeParameterValue = _httpContextAccessor.HttpContext.Request.Query[PageSizeParameterName];
136+
string pageSizeParameterValue = _httpContextAccessor.HttpContext!.Request.Query[PageSizeParameterName];
137137

138138
PageSize newTopPageSize = Equals(topPageSize, _options.DefaultPageSize) ? null : topPageSize;
139139
return ChangeTopPageSize(pageSizeParameterValue, newTopPageSize, requestContext);
@@ -188,7 +188,7 @@ private string GetLinkForPagination(int pageOffset, string pageSizeValue)
188188
{
189189
string queryStringValue = GetQueryStringInPaginationLink(pageOffset, pageSizeValue);
190190

191-
var builder = new UriBuilder(_httpContextAccessor.HttpContext.Request.GetEncodedUrl())
191+
var builder = new UriBuilder(_httpContextAccessor.HttpContext!.Request.GetEncodedUrl())
192192
{
193193
Query = queryStringValue
194194
};
@@ -200,7 +200,7 @@ private string GetLinkForPagination(int pageOffset, string pageSizeValue)
200200
private string GetQueryStringInPaginationLink(int pageOffset, string pageSizeValue)
201201
{
202202
IDictionary<string, string> parameters =
203-
_httpContextAccessor.HttpContext.Request.Query.ToDictionary(pair => pair.Key, pair => pair.Value.ToString());
203+
_httpContextAccessor.HttpContext!.Request.Query.ToDictionary(pair => pair.Key, pair => pair.Value.ToString());
204204

205205
if (pageSizeValue == null)
206206
{
@@ -311,7 +311,7 @@ private IDictionary<string, object> GetRouteValues(string primaryId, string rela
311311
// By default, we copy all route parameters from the *current* endpoint, which helps in case all endpoints have the same
312312
// set of non-standard parameters. There is no way we can know which non-standard parameters a *different* endpoint needs,
313313
// so users must override RenderLinkForAction to supply them, if applicable.
314-
RouteValueDictionary routeValues = _httpContextAccessor.HttpContext.Request.RouteValues;
314+
RouteValueDictionary routeValues = _httpContextAccessor.HttpContext!.Request.RouteValues;
315315

316316
routeValues["id"] = primaryId;
317317
routeValues["relationshipName"] = relationshipName;

src/JsonApiDotNetCore/Serialization/JsonApiReader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
8181
ValidateRequestBody(model, body, context.HttpContext.Request);
8282
}
8383

84+
// ReSharper disable once AssignNullToNotNullAttribute
85+
// Justification: According to JSON:API we must return 200 OK without a body in some cases.
8486
return await InputFormatterResult.SuccessAsync(model);
8587
}
8688

src/JsonApiDotNetCore/Serialization/RequestDeserializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private OperationKind GetOperationKind(AtomicOperationObject operation)
146146
{
147147
case AtomicOperationCode.Add:
148148
{
149-
if (operation.Ref != null && operation.Ref.Relationship == null)
149+
if (operation.Ref is { Relationship: null })
150150
{
151151
throw new JsonApiSerializationException("The 'ref.relationship' element is required.", null,
152152
atomicOperationIndex: AtomicOperationIndex);
@@ -523,14 +523,14 @@ private bool IsCreatingResource()
523523
{
524524
return _request.Kind == EndpointKind.AtomicOperations
525525
? _request.OperationKind == OperationKind.CreateResource
526-
: _request.Kind == EndpointKind.Primary && _httpContextAccessor.HttpContext.Request.Method == HttpMethod.Post.Method;
526+
: _request.Kind == EndpointKind.Primary && _httpContextAccessor.HttpContext!.Request.Method == HttpMethod.Post.Method;
527527
}
528528

529529
private bool IsUpdatingResource()
530530
{
531531
return _request.Kind == EndpointKind.AtomicOperations
532532
? _request.OperationKind == OperationKind.UpdateResource
533-
: _request.Kind == EndpointKind.Primary && _httpContextAccessor.HttpContext.Request.Method == HttpMethod.Patch.Method;
533+
: _request.Kind == EndpointKind.Primary && _httpContextAccessor.HttpContext!.Request.Method == HttpMethod.Patch.Method;
534534
}
535535
}
536536
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ContentNegotiation/ContentTypeHeaderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task Returns_JsonApi_ContentType_header()
3434

3535
// Assert
3636
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
37-
httpResponse.Content.Headers.ContentType.ToString().Should().Be(HeaderConstants.MediaType);
37+
httpResponse.Content.Headers.ContentType.Should().NotBeNull().And.Subject.ToString().Should().Be(HeaderConstants.MediaType);
3838
}
3939

4040
[Fact]
@@ -67,7 +67,7 @@ public async Task Returns_JsonApi_ContentType_header_with_AtomicOperations_exten
6767

6868
// Assert
6969
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
70-
httpResponse.Content.Headers.ContentType.ToString().Should().Be(HeaderConstants.AtomicOperationsMediaType);
70+
httpResponse.Content.Headers.ContentType.Should().NotBeNull().And.Subject.ToString().Should().Be(HeaderConstants.AtomicOperationsMediaType);
7171
}
7272

7373
[Fact]

test/JsonApiDotNetCoreExampleTests/IntegrationTests/Logging/LoggingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public LoggingTests(ExampleIntegrationTestContext<TestableStartup<AuditDbContext
3131
options.ClearProviders();
3232
options.AddProvider(loggerFactory);
3333
options.SetMinimumLevel(LogLevel.Trace);
34-
options.AddFilter((_, __) => true);
34+
options.AddFilter((_, _) => true);
3535
});
3636

3737
testContext.ConfigureServicesBeforeStartup(services =>

0 commit comments

Comments
 (0)