Skip to content

Commit 6969ce3

Browse files
author
Bart Koelman
committed
Inlined casing conversions
1 parent e0a139b commit 6969ce3

File tree

5 files changed

+34
-84
lines changed

5 files changed

+34
-84
lines changed

src/JsonApiDotNetCore/Extensions/StringExtensions.cs

-65
This file was deleted.

src/JsonApiDotNetCore/Graph/ResourceNameFormatters/CamelCaseFormatter.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using str = JsonApiDotNetCore.Extensions.StringExtensions;
2-
31
namespace JsonApiDotNetCore.Graph
42
{
53
/// <summary>
@@ -34,7 +32,6 @@ namespace JsonApiDotNetCore.Graph
3432
public sealed class CamelCaseFormatter: BaseResourceNameFormatter
3533
{
3634
/// <inheritdoc/>
37-
public override string ApplyCasingConvention(string properName) => str.Camelize(properName);
35+
public override string ApplyCasingConvention(string properName) => char.ToLowerInvariant(properName[0]) + properName.Substring(1);
3836
}
3937
}
40-

src/JsonApiDotNetCore/Graph/ResourceNameFormatters/KebabCaseFormatter.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using str = JsonApiDotNetCore.Extensions.StringExtensions;
1+
using System.Text;
22

33
namespace JsonApiDotNetCore.Graph
44
{
@@ -34,6 +34,27 @@ namespace JsonApiDotNetCore.Graph
3434
public sealed class KebabCaseFormatter : BaseResourceNameFormatter
3535
{
3636
/// <inheritdoc/>
37-
public override string ApplyCasingConvention(string properName) => str.Dasherize(properName);
37+
public override string ApplyCasingConvention(string properName)
38+
{
39+
var chars = properName.ToCharArray();
40+
if (chars.Length > 0)
41+
{
42+
var builder = new StringBuilder();
43+
for (var i = 0; i < chars.Length; i++)
44+
{
45+
if (char.IsUpper(chars[i]))
46+
{
47+
var hashedString = i > 0 ? $"-{char.ToLower(chars[i])}" : $"{char.ToLower(chars[i])}";
48+
builder.Append(hashedString);
49+
}
50+
else
51+
{
52+
builder.Append(chars[i]);
53+
}
54+
}
55+
return builder.ToString();
56+
}
57+
return properName;
58+
}
3859
}
3960
}

src/JsonApiDotNetCore/Serialization/Common/ResourceObjectBuilder.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System;
1+
using System;
22
using System.Collections;
3-
using System.Collections.Generic;
3+
using System.Collections.Generic;
44
using System.Linq;
5-
using JsonApiDotNetCore.Extensions;
65
using JsonApiDotNetCore.Internal.Contracts;
76
using JsonApiDotNetCore.Models;
87

@@ -28,7 +27,7 @@ public ResourceObject Build(IIdentifiable entity, IEnumerable<AttrAttribute> att
2827
var resourceContext = _provider.GetResourceContext(entity.GetType());
2928

3029
// populating the top-level "type" and "id" members.
31-
var ro = new ResourceObject { Type = resourceContext.ResourceName, Id = entity.StringId.NullIfEmpty() };
30+
var ro = new ResourceObject { Type = resourceContext.ResourceName, Id = entity.StringId == string.Empty ? null : entity.StringId };
3231

3332
// populating the top-level "attribute" member of a resource object. never include "id" as an attribute
3433
if (attributes != null && (attributes = attributes.Where(attr => attr.PropertyInfo.Name != _identifiablePropertyName)).Any())

test/UnitTests/Builders/LinkBuilderTests.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using JsonApiDotNetCore.Internal.Contracts;
44
using JsonApiDotNetCore.Managers.Contracts;
55
using JsonApiDotNetCore.Models;
6-
using JsonApiDotNetCore.Extensions;
76
using JsonApiDotNetCore.Models.Links;
87
using JsonApiDotNetCoreExample.Models;
98
using Moq;
@@ -50,7 +49,7 @@ public void BuildResourceLinks_GlobalAndResourceConfiguration_ExpectedResult(Lin
5049
{
5150
// Arrange
5251
var config = GetConfiguration(resourceLinks: global);
53-
var primaryResource = GetResourceContext<Article>(resourceLinks: resource);
52+
var primaryResource = GetArticleResourceContext(resourceLinks: resource);
5453
_provider.Setup(m => m.GetResourceContext("articles")).Returns(primaryResource);
5554
var builder = new LinkBuilder(config, GetRequestManager(), null, _provider.Object, _queryStringAccessor);
5655

@@ -98,7 +97,7 @@ public void BuildRelationshipLinks_GlobalResourceAndAttrConfiguration_ExpectedLi
9897
{
9998
// Arrange
10099
var config = GetConfiguration(relationshipLinks: global);
101-
var primaryResource = GetResourceContext<Article>(relationshipLinks: resource);
100+
var primaryResource = GetArticleResourceContext(relationshipLinks: resource);
102101
_provider.Setup(m => m.GetResourceContext(typeof(Article))).Returns(primaryResource);
103102
var builder = new LinkBuilder(config, GetRequestManager(), null, _provider.Object, _queryStringAccessor);
104103
var attr = new HasOneAttribute(links: relationship) { RightType = typeof(Author), PublicRelationshipName = "author" };
@@ -154,7 +153,7 @@ public void BuildTopLevelLinks_GlobalAndResourceConfiguration_ExpectedLinks(Link
154153
{
155154
// Arrange
156155
var config = GetConfiguration(topLevelLinks: global);
157-
var primaryResource = GetResourceContext<Article>(topLevelLinks: resource);
156+
var primaryResource = GetArticleResourceContext(topLevelLinks: resource);
158157
_provider.Setup(m => m.GetResourceContext<Article>()).Returns(primaryResource);
159158

160159
bool useBaseId = expectedSelfLink != _topSelf;
@@ -220,19 +219,18 @@ private IPageService GetPageManager()
220219
mock.Setup(m => m.TotalPages).Returns(3);
221220
mock.Setup(m => m.PageSize).Returns(10);
222221
return mock.Object;
223-
224222
}
225223

226-
private ResourceContext GetResourceContext<TResource>(Link resourceLinks = Link.NotConfigured,
227-
Link topLevelLinks = Link.NotConfigured,
228-
Link relationshipLinks = Link.NotConfigured) where TResource : class, IIdentifiable
224+
private ResourceContext GetArticleResourceContext(Link resourceLinks = Link.NotConfigured,
225+
Link topLevelLinks = Link.NotConfigured,
226+
Link relationshipLinks = Link.NotConfigured)
229227
{
230228
return new ResourceContext
231229
{
232230
ResourceLinks = resourceLinks,
233231
TopLevelLinks = topLevelLinks,
234232
RelationshipLinks = relationshipLinks,
235-
ResourceName = typeof(TResource).Name.Dasherize() + "s"
233+
ResourceName = "articles"
236234
};
237235
}
238236

0 commit comments

Comments
 (0)