Skip to content

Commit e0a139b

Browse files
author
Bart Koelman
committed
Removed more case-insensitive string comparisons. They are tricky, because they hide potential usages of property names instead of resource names, which only works with the default camel-case convention but breaks on kebab casing.
1 parent ef8d650 commit e0a139b

File tree

13 files changed

+29
-30
lines changed

13 files changed

+29
-30
lines changed

src/JsonApiDotNetCore/Formatters/JsonApiInputFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public bool CanRead(InputFormatterContext context)
1515

1616
var contentTypeString = context.HttpContext.Request.ContentType;
1717

18-
return contentTypeString == Constants.ContentType;
18+
return contentTypeString == HeaderConstants.ContentType;
1919
}
2020

2121
public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)

src/JsonApiDotNetCore/Formatters/JsonApiOutputFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public bool CanWriteResult(OutputFormatterCanWriteContext context)
1515

1616
var contentTypeString = context.HttpContext.Request.ContentType;
1717

18-
return string.IsNullOrEmpty(contentTypeString) || contentTypeString == Constants.ContentType;
18+
return string.IsNullOrEmpty(contentTypeString) || contentTypeString == HeaderConstants.ContentType;
1919
}
2020
public async Task WriteAsync(OutputFormatterWriteContext context)
2121
{

src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task WriteAsync(OutputFormatterWriteContext context)
5050
}
5151
else
5252
{
53-
response.ContentType = Constants.ContentType;
53+
response.ContentType = HeaderConstants.ContentType;
5454
try
5555
{
5656
responseContent = SerializeResponse(context.Object, (HttpStatusCode)response.StatusCode);

src/JsonApiDotNetCore/Internal/Contracts/IResourceContextProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IResourceContextProvider
1616
/// <summary>
1717
/// Get the resource metadata by the DbSet property name
1818
/// </summary>
19-
ResourceContext GetResourceContext(string exposedResourceName);
19+
ResourceContext GetResourceContext(string resourceName);
2020

2121
/// <summary>
2222
/// Get the resource metadata by the resource type

src/JsonApiDotNetCore/Internal/Constants.cs renamed to src/JsonApiDotNetCore/Internal/HeaderConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace JsonApiDotNetCore.Internal
22
{
3-
public static class Constants
3+
public static class HeaderConstants
44
{
55
public const string AcceptHeader = "Accept";
66
public const string ContentType = "application/vnd.api+json";

src/JsonApiDotNetCore/Internal/ResourceGraph.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ public class ResourceGraph : IResourceGraph
1515
internal List<ValidationResult> ValidationResults { get; }
1616
private List<ResourceContext> Resources { get; }
1717

18-
public ResourceGraph(List<ResourceContext> entities, List<ValidationResult> validationResults = null)
18+
public ResourceGraph(List<ResourceContext> resources, List<ValidationResult> validationResults = null)
1919
{
20-
Resources = entities;
20+
Resources = resources;
2121
ValidationResults = validationResults;
2222
}
2323

2424
/// <inheritdoc />
2525
public ResourceContext[] GetResourceContexts() => Resources.ToArray();
2626
/// <inheritdoc />
27-
public ResourceContext GetResourceContext(string entityName)
28-
=> Resources.SingleOrDefault(e => string.Equals(e.ResourceName, entityName, StringComparison.OrdinalIgnoreCase));
27+
public ResourceContext GetResourceContext(string resourceName)
28+
=> Resources.SingleOrDefault(e => e.ResourceName == resourceName);
2929
/// <inheritdoc />
30-
public ResourceContext GetResourceContext(Type entityType)
31-
=> Resources.SingleOrDefault(e => e.ResourceType == entityType);
30+
public ResourceContext GetResourceContext(Type resourceType)
31+
=> Resources.SingleOrDefault(e => e.ResourceType == resourceType);
3232
/// <inheritdoc />
3333
public ResourceContext GetResourceContext<TResource>() where TResource : class, IIdentifiable
3434
=> GetResourceContext(typeof(TResource));

src/JsonApiDotNetCore/Middleware/CurrentRequestMiddleware.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private static async Task<bool> IsValidContentTypeHeaderAsync(HttpContext contex
149149
await FlushResponseAsync(context, new Error(HttpStatusCode.UnsupportedMediaType)
150150
{
151151
Title = "The specified Content-Type header value is not supported.",
152-
Detail = $"Please specify '{Constants.ContentType}' for the Content-Type header value."
152+
Detail = $"Please specify '{HeaderConstants.ContentType}' for the Content-Type header value."
153153
});
154154

155155
return false;
@@ -159,7 +159,7 @@ private static async Task<bool> IsValidContentTypeHeaderAsync(HttpContext contex
159159

160160
private static async Task<bool> IsValidAcceptHeaderAsync(HttpContext context)
161161
{
162-
if (context.Request.Headers.TryGetValue(Constants.AcceptHeader, out StringValues acceptHeaders) == false)
162+
if (context.Request.Headers.TryGetValue(HeaderConstants.AcceptHeader, out StringValues acceptHeaders) == false)
163163
return true;
164164

165165
foreach (var acceptHeader in acceptHeaders)
@@ -172,7 +172,7 @@ private static async Task<bool> IsValidAcceptHeaderAsync(HttpContext context)
172172
await FlushResponseAsync(context, new Error(HttpStatusCode.NotAcceptable)
173173
{
174174
Title = "The specified Accept header value is not supported.",
175-
Detail = $"Please specify '{Constants.ContentType}' for the Accept header value."
175+
Detail = $"Please specify '{HeaderConstants.ContentType}' for the Accept header value."
176176
});
177177
return false;
178178
}
@@ -184,19 +184,19 @@ private static bool ContainsMediaTypeParameters(string mediaType)
184184
var incomingMediaTypeSpan = mediaType.AsSpan();
185185

186186
// if the content type is not application/vnd.api+json then continue on
187-
if (incomingMediaTypeSpan.Length < Constants.ContentType.Length)
187+
if (incomingMediaTypeSpan.Length < HeaderConstants.ContentType.Length)
188188
{
189189
return false;
190190
}
191191

192-
var incomingContentType = incomingMediaTypeSpan.Slice(0, Constants.ContentType.Length);
193-
if (incomingContentType.SequenceEqual(Constants.ContentType.AsSpan()) == false)
192+
var incomingContentType = incomingMediaTypeSpan.Slice(0, HeaderConstants.ContentType.Length);
193+
if (incomingContentType.SequenceEqual(HeaderConstants.ContentType.AsSpan()) == false)
194194
return false;
195195

196196
// anything appended to "application/vnd.api+json;" will be considered a media type param
197197
return (
198-
incomingMediaTypeSpan.Length >= Constants.ContentType.Length + 2
199-
&& incomingMediaTypeSpan[Constants.ContentType.Length] == ';'
198+
incomingMediaTypeSpan.Length >= HeaderConstants.ContentType.Length + 2
199+
&& incomingMediaTypeSpan[HeaderConstants.ContentType.Length] == ';'
200200
);
201201
}
202202

src/JsonApiDotNetCore/Middleware/DefaultTypeMatchFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void OnActionExecuting(ActionExecutingContext context)
4141

4242
private bool IsJsonApiRequest(HttpRequest request)
4343
{
44-
return (request.ContentType?.Equals(Constants.ContentType, StringComparison.OrdinalIgnoreCase) == true);
44+
return request.ContentType == HeaderConstants.ContentType;
4545
}
4646

4747
public void OnActionExecuted(ActionExecutedContext context) { /* noop */ }

src/JsonApiDotNetCore/Models/Annotation/HasManyThroughAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public HasManyThroughAttribute(string publicName, string internalThroughName, Li
7777
public override object GetValue(object entity)
7878
{
7979
var throughNavigationProperty = entity.GetType()
80-
.GetProperties()
81-
.SingleOrDefault(p => string.Equals(p.Name, InternalThroughName, StringComparison.OrdinalIgnoreCase));
80+
.GetProperties()
81+
.SingleOrDefault(p => p.Name == InternalThroughName);
8282

8383
var throughEntities = throughNavigationProperty.GetValue(entity);
8484

src/JsonApiDotNetCore/QueryParameterServices/Common/QueryParameterParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public virtual void Parse(DisableQueryAttribute disableQueryAttribute)
3333

3434
foreach (var pair in _queryStringAccessor.Query)
3535
{
36-
if (string.IsNullOrWhiteSpace(pair.Value))
36+
if (string.IsNullOrEmpty(pair.Value))
3737
{
3838
throw new InvalidQueryStringParameterException(pair.Key, "Missing query string parameter value.",
3939
$"Missing value for '{pair.Key}' query string parameter.");

src/JsonApiDotNetCore/QueryParameterServices/FilterService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ private List<FilterQuery> GetFilterQueries(string parameterName, StringValues pa
8383
var queries = new List<FilterQuery>();
8484
// InArray case
8585
string op = GetFilterOperation(parameterValue);
86-
if (string.Equals(op, FilterOperation.@in.ToString(), StringComparison.OrdinalIgnoreCase)
87-
|| string.Equals(op, FilterOperation.nin.ToString(), StringComparison.OrdinalIgnoreCase))
86+
if (op == FilterOperation.@in.ToString() || op == FilterOperation.nin.ToString())
8887
{
8988
var (_, filterValue) = ParseFilterOperation(parameterValue);
9089
queries.Add(new FilterQuery(propertyName, filterValue, op));

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/CustomControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public async Task ApiController_attribute_transforms_NotFound_action_result_with
160160
var server = new TestServer(builder);
161161
var client = server.CreateClient();
162162
var request = new HttpRequestMessage(HttpMethod.Patch, route) {Content = new StringContent(content)};
163-
request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.ContentType);
163+
request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.ContentType);
164164

165165
// Act
166166
var response = await client.SendAsync(request);

test/JsonApiDotNetCoreExampleTests/Acceptance/ModelStateValidationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task When_posting_tag_with_invalid_name_it_must_fail()
3636
{
3737
Content = new StringContent(content)
3838
};
39-
request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.ContentType);
39+
request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.ContentType);
4040

4141
var options = (JsonApiOptions)_factory.GetService<IJsonApiOptions>();
4242
options.ValidateModelState = true;
@@ -72,7 +72,7 @@ public async Task When_posting_tag_with_invalid_name_without_model_state_validat
7272
{
7373
Content = new StringContent(content)
7474
};
75-
request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.ContentType);
75+
request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.ContentType);
7676

7777
var options = (JsonApiOptions)_factory.GetService<IJsonApiOptions>();
7878
options.ValidateModelState = false;
@@ -110,7 +110,7 @@ public async Task When_patching_tag_with_invalid_name_it_must_fail()
110110
{
111111
Content = new StringContent(content)
112112
};
113-
request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.ContentType);
113+
request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.ContentType);
114114

115115
var options = (JsonApiOptions)_factory.GetService<IJsonApiOptions>();
116116
options.ValidateModelState = true;
@@ -156,7 +156,7 @@ public async Task When_patching_tag_with_invalid_name_without_model_state_valida
156156
{
157157
Content = new StringContent(content)
158158
};
159-
request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.ContentType);
159+
request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.ContentType);
160160

161161
var options = (JsonApiOptions)_factory.GetService<IJsonApiOptions>();
162162
options.ValidateModelState = false;

0 commit comments

Comments
 (0)