Skip to content

Commit 0b9a2a8

Browse files
committed
feat(*): add constants for Content-Type
1 parent 3a40d43 commit 0b9a2a8

File tree

12 files changed

+47
-39
lines changed

12 files changed

+47
-39
lines changed

benchmarks/BenchmarkDotNet.Artifacts/results/Benchmarks.Query.QueryParser_Benchmarks-report-github.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ BenchmarkDotNet=v0.10.10, OS=Mac OS X 10.12
44
Processor=Intel Core i5-5257U CPU 2.70GHz (Broadwell), ProcessorCount=4
55
.NET Core SDK=2.0.0
66
[Host] : .NET Core 1.1.4 (Framework 4.6.25714.03), 64bit RyuJIT
7-
Job-OWXJBF : .NET Core 1.1.4 (Framework 4.6.25714.03), 64bit RyuJIT
7+
Job-WKDOLS : .NET Core 1.1.4 (Framework 4.6.25714.03), 64bit RyuJIT
88

99
LaunchCount=3 TargetCount=20 WarmupCount=10
1010

1111
```
12-
| Method | Mean | Error | StdDev | Median |
13-
|--------------- |-------------:|-----------:|------------:|-------------:|
14-
| AscendingSort | 4.451 us | 1.5230 us | 3.2457 us | 3.305 us |
15-
| DescendingSort | 3.287 us | 0.0307 us | 0.0673 us | 3.263 us |
16-
| ComplexQuery | 1,973.029 us | 67.5600 us | 143.9759 us | 1,952.663 us |
12+
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Allocated |
13+
|--------------- |-------------:|-----------:|-----------:|---------:|--------:|----------:|
14+
| AscendingSort | 4.316 us | 1.3773 us | 3.0232 us | 0.5066 | 0.1303 | 1.08 KB |
15+
| DescendingSort | 3.300 us | 0.0314 us | 0.0682 us | 0.5123 | 0.1318 | 1.13 KB |
16+
| ComplexQuery | 2,041.642 us | 41.5631 us | 92.1010 us | 312.5000 | 80.2734 | 648.99 KB |

benchmarks/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace Benchmarks {
66
class Program {
77
static void Main(string[] args) {
8-
BenchmarkRunner.Run<JsonApiDeserializer_Benchmarks>();
9-
BenchmarkRunner.Run<JsonApiSerializer_Benchmarks>();
10-
BenchmarkRunner.Run<QueryParser_Benchmarks>();
8+
var switcher = new BenchmarkSwitcher(new[] {
9+
typeof(JsonApiDeserializer_Benchmarks),
10+
typeof(JsonApiSerializer_Benchmarks),
11+
typeof(QueryParser_Benchmarks)
12+
});
13+
switcher.Run(args);
1114
}
1215
}
1316
}

benchmarks/Query/QueryParser_Benchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using Moq;
1313

1414
namespace Benchmarks.Query {
15-
[MarkdownExporter, SimpleJob(launchCount : 3, warmupCount : 10, targetCount : 20)]
15+
[MarkdownExporter, SimpleJob(launchCount : 3, warmupCount : 10, targetCount : 20), MemoryDiagnoser]
1616
public class QueryParser_Benchmarks {
1717
private readonly BenchmarkFacade _queryParser;
1818

src/JsonApiDotNetCore/Formatters/JsonApiInputFormatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
3+
using JsonApiDotNetCore.Internal;
34
using Microsoft.AspNetCore.Mvc.Formatters;
45
using Microsoft.Extensions.DependencyInjection;
56

@@ -14,7 +15,7 @@ public bool CanRead(InputFormatterContext context)
1415

1516
var contentTypeString = context.HttpContext.Request.ContentType;
1617

17-
return contentTypeString == "application/vnd.api+json";
18+
return contentTypeString == Constants.ContentType;
1819
}
1920

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

src/JsonApiDotNetCore/Formatters/JsonApiOutputFormatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
3+
using JsonApiDotNetCore.Internal;
34
using Microsoft.AspNetCore.Mvc.Formatters;
45
using Microsoft.Extensions.DependencyInjection;
56

@@ -14,7 +15,7 @@ public bool CanWriteResult(OutputFormatterCanWriteContext context)
1415

1516
var contentTypeString = context.HttpContext.Request.ContentType;
1617

17-
return string.IsNullOrEmpty(contentTypeString) || contentTypeString == "application/vnd.api+json";
18+
return string.IsNullOrEmpty(contentTypeString) || contentTypeString == Constants.ContentType;
1819
}
1920

2021
public async Task WriteAsync(OutputFormatterWriteContext context)

src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class JsonApiWriter : IJsonApiWriter
1313
private readonly ILogger<JsonApiWriter> _logger;
1414
private readonly IJsonApiSerializer _serializer;
1515

16-
public JsonApiWriter(IJsonApiSerializer serializer,
16+
public JsonApiWriter(
17+
IJsonApiSerializer serializer,
1718
ILoggerFactory loggerFactory)
1819
{
1920
_serializer = serializer;
@@ -30,7 +31,7 @@ public async Task WriteAsync(OutputFormatterWriteContext context)
3031
var response = context.HttpContext.Response;
3132
using (var writer = context.WriterFactory(response.Body, Encoding.UTF8))
3233
{
33-
response.ContentType = "application/vnd.api+json";
34+
response.ContentType = Constants.ContentType;
3435
string responseContent;
3536
try
3637
{
@@ -55,4 +56,4 @@ private string GetResponseBody(object responseObject)
5556
return _serializer.Serialize(responseObject);
5657
}
5758
}
58-
}
59+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace JsonApiDotNetCore.Internal
2+
{
3+
public static class Constants
4+
{
5+
public const string AcceptHeader = "Accept";
6+
public const string ContentType = "application/vnd.api+json";
7+
}
8+
}

src/JsonApiDotNetCore/Internal/JsonApiExceptionFactory.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ namespace JsonApiDotNetCore.Internal
55
{
66
public static class JsonApiExceptionFactory
77
{
8+
private const string JsonApiException = nameof(JsonApiException);
9+
private const string InvalidCastException = nameof(InvalidCastException);
10+
811
public static JsonApiException GetException(Exception exception)
912
{
1013
var exceptionType = exception.GetType().ToString().Split('.').Last();
1114
switch(exceptionType)
1215
{
13-
case "JsonApiException":
16+
case JsonApiException:
1417
return (JsonApiException)exception;
15-
case "InvalidCastException":
18+
case InvalidCastException:
1619
return new JsonApiException(409, exception.Message);
1720
default:
1821
return new JsonApiException(500, exception.Message, GetExceptionDetail(exception.InnerException));

src/JsonApiDotNetCore/Internal/Query/BaseFilterQuery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ protected FilterOperations GetFilterOperation(string prefix)
88
{
99
if (prefix.Length == 0) return FilterOperations.eq;
1010

11-
if (!Enum.TryParse(prefix, out FilterOperations opertion))
11+
if (Enum.TryParse(prefix, out FilterOperations opertion) == false)
1212
throw new JsonApiException(400, $"Invalid filter prefix '{prefix}'");
1313

1414
return opertion;
1515
}
1616
}
17-
}
17+
}

src/JsonApiDotNetCore/Internal/Query/RelatedAttrFilterQuery.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Linq;
23
using JsonApiDotNetCore.Models;
34
using JsonApiDotNetCore.Services;
@@ -14,7 +15,7 @@ public RelatedAttrFilterQuery(
1415
{
1516
_jsonApiContext = jsonApiCopntext;
1617

17-
var relationshipArray = filterQuery.Key.Split('.');
18+
var relationshipArray = filterQuery.Attribute.Split('.');
1819

1920
var relationship = GetRelationship(relationshipArray[0]);
2021
if (relationship == null)
@@ -36,14 +37,14 @@ public RelatedAttrFilterQuery(
3637
private RelationshipAttribute GetRelationship(string propertyName)
3738
{
3839
return _jsonApiContext.RequestEntity.Relationships
39-
.FirstOrDefault(r => r.InternalRelationshipName.ToLower() == propertyName.ToLower());
40+
.FirstOrDefault(r => string.Equals(r.PublicRelationshipName, propertyName, StringComparison.OrdinalIgnoreCase));
4041
}
4142

4243
private AttrAttribute GetAttribute(RelationshipAttribute relationship, string attribute)
4344
{
4445
var relatedContextExntity = _jsonApiContext.ContextGraph.GetContextEntity(relationship.Type);
4546
return relatedContextExntity.Attributes
46-
.FirstOrDefault(a => a.InternalAttributeName.ToLower() == attribute.ToLower());
47+
.FirstOrDefault(a => string.Equals(a.PublicAttributeName, attribute, StringComparison.OrdinalIgnoreCase));
4748
}
4849
}
49-
}
50+
}

src/JsonApiDotNetCore/Middleware/RequestMiddleware.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using JsonApiDotNetCore.Internal;
23
using Microsoft.AspNetCore.Http;
34
using Microsoft.Extensions.Primitives;
45

@@ -37,7 +38,7 @@ private static bool IsValidContentTypeHeader(HttpContext context)
3738

3839
private static bool IsValidAcceptHeader(HttpContext context)
3940
{
40-
if (context.Request.Headers.TryGetValue("Accept", out StringValues acceptHeaders) == false)
41+
if (context.Request.Headers.TryGetValue(Constants.AcceptHeader, out StringValues acceptHeaders) == false)
4142
return true;
4243

4344
foreach (var acceptHeader in acceptHeaders)
@@ -54,7 +55,7 @@ private static bool IsValidAcceptHeader(HttpContext context)
5455
private static bool ContainsMediaTypeParameters(string mediaType)
5556
{
5657
var mediaTypeArr = mediaType.Split(';');
57-
return (mediaTypeArr[0] == "application/vnd.api+json" && mediaTypeArr.Length == 2);
58+
return (mediaTypeArr[0] == Constants.ContentType && mediaTypeArr.Length == 2);
5859
}
5960

6061
private static void FlushResponse(HttpContext context, int statusCode)

src/JsonApiDotNetCore/Models/DocumentBase.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,8 @@ public class DocumentBase
1515
public Dictionary<string, object> Meta { get; set; }
1616

1717
// http://www.newtonsoft.com/json/help/html/ConditionalProperties.htm
18-
public bool ShouldSerializeIncluded()
19-
{
20-
return (Included != null);
21-
}
22-
23-
public bool ShouldSerializeMeta()
24-
{
25-
return (Meta != null);
26-
}
27-
28-
public bool ShouldSerializeLinks()
29-
{
30-
return (Links != null);
31-
}
18+
public bool ShouldSerializeIncluded() => (Included != null);
19+
public bool ShouldSerializeMeta() => (Meta != null);
20+
public bool ShouldSerializeLinks() => (Links != null);
3221
}
3322
}

0 commit comments

Comments
 (0)