Skip to content

Commit 05a9dbe

Browse files
author
Bart Koelman
committed
More casing-related updates
1 parent 6969ce3 commit 05a9dbe

File tree

4 files changed

+28
-38
lines changed

4 files changed

+28
-38
lines changed

src/JsonApiDotNetCore/Graph/ResourceNameFormatters/CamelCaseFormatter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ namespace JsonApiDotNetCore.Graph
3232
public sealed class CamelCaseFormatter: BaseResourceNameFormatter
3333
{
3434
/// <inheritdoc/>
35-
public override string ApplyCasingConvention(string properName) => char.ToLowerInvariant(properName[0]) + properName.Substring(1);
35+
public override string ApplyCasingConvention(string properName) => char.ToLower(properName[0]) + properName.Substring(1);
3636
}
3737
}

src/JsonApiDotNetCore/Graph/ResourceNameFormatters/KebabCaseFormatter.cs

+19-12
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,32 @@ public sealed class KebabCaseFormatter : BaseResourceNameFormatter
3636
/// <inheritdoc/>
3737
public override string ApplyCasingConvention(string properName)
3838
{
39+
if (properName.Length == 0)
40+
{
41+
return properName;
42+
}
43+
3944
var chars = properName.ToCharArray();
40-
if (chars.Length > 0)
45+
var builder = new StringBuilder();
46+
47+
for (var i = 0; i < chars.Length; i++)
4148
{
42-
var builder = new StringBuilder();
43-
for (var i = 0; i < chars.Length; i++)
49+
if (char.IsUpper(chars[i]))
4450
{
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+
if (i > 0)
5152
{
52-
builder.Append(chars[i]);
53+
builder.Append('-');
5354
}
55+
56+
builder.Append(char.ToLower(chars[i]));
57+
}
58+
else
59+
{
60+
builder.Append(chars[i]);
5461
}
55-
return builder.ToString();
5662
}
57-
return properName;
63+
64+
return builder.ToString();
5865
}
5966
}
6067
}

src/JsonApiDotNetCore/Middleware/CurrentRequestMiddleware.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private string GetRelationshipId()
8484
private string[] SplitCurrentPath()
8585
{
8686
var path = _httpContext.Request.Path.Value;
87-
var ns = $"/{GetNameSpace()}";
87+
var ns = $"/{_options.Namespace}";
8888
var nonNameSpaced = path.Replace(ns, "");
8989
nonNameSpaced = nonNameSpaced.Trim('/');
9090
var individualComponents = nonNameSpaced.Split('/');
@@ -96,11 +96,11 @@ private string GetBasePath(string resourceName = null)
9696
var r = _httpContext.Request;
9797
if (_options.RelativeLinks)
9898
{
99-
return GetNameSpace();
99+
return _options.Namespace;
100100
}
101-
var ns = GetNameSpace();
101+
102102
var customRoute = GetCustomRoute(r.Path.Value, resourceName);
103-
var toReturn = $"{r.Scheme}://{r.Host}/{ns}";
103+
var toReturn = $"{r.Scheme}://{r.Host}/{_options.Namespace}";
104104
if (customRoute != null)
105105
{
106106
toReturn += $"/{customRoute}";
@@ -110,12 +110,11 @@ private string GetBasePath(string resourceName = null)
110110

111111
private object GetCustomRoute(string path, string resourceName)
112112
{
113-
var ns = GetNameSpace();
114113
var trimmedComponents = path.Trim('/').Split('/').ToList();
115114
var resourceNameIndex = trimmedComponents.FindIndex(c => c == resourceName);
116115
var newComponents = trimmedComponents.Take(resourceNameIndex).ToArray();
117116
var customRoute = string.Join('/', newComponents);
118-
if (customRoute == ns)
117+
if (customRoute == _options.Namespace)
119118
{
120119
return null;
121120
}
@@ -125,15 +124,10 @@ private object GetCustomRoute(string path, string resourceName)
125124
}
126125
}
127126

128-
private string GetNameSpace()
129-
{
130-
return _options.Namespace;
131-
}
132-
133127
private bool PathIsRelationship()
134128
{
135129
var actionName = (string)_routeValues["action"];
136-
return actionName.ToLower().Contains("relationships");
130+
return actionName.ToLowerInvariant().Contains("relationships");
137131
}
138132

139133
private async Task<bool> IsValidAsync()

test/UnitTests/Builders/ContextGraphBuilder_Tests.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void Resources_Without_Names_Specified_Will_Use_Default_Formatter()
6363
public void Resources_Without_Names_Specified_Will_Use_Configured_Formatter()
6464
{
6565
// Arrange
66-
var builder = new ResourceGraphBuilder(new CamelCaseNameFormatter());
66+
var builder = new ResourceGraphBuilder(new CamelCaseFormatter());
6767
builder.AddResource<TestResource>();
6868

6969
// Act
@@ -93,7 +93,7 @@ public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter()
9393
public void Attrs_Without_Names_Specified_Will_Use_Configured_Formatter()
9494
{
9595
// Arrange
96-
var builder = new ResourceGraphBuilder(new CamelCaseNameFormatter());
96+
var builder = new ResourceGraphBuilder(new CamelCaseFormatter());
9797
builder.AddResource<TestResource>();
9898

9999
// Act
@@ -128,16 +128,5 @@ public sealed class TestResource : Identifiable
128128
}
129129

130130
public class RelatedResource : Identifiable { }
131-
132-
public sealed class CamelCaseNameFormatter : IResourceNameFormatter
133-
{
134-
public string ApplyCasingConvention(string properName) => ToCamelCase(properName);
135-
136-
public string FormatPropertyName(PropertyInfo property) => ToCamelCase(property.Name);
137-
138-
public string FormatResourceName(Type resourceType) => ToCamelCase(resourceType.Name.Pluralize());
139-
140-
private string ToCamelCase(string str) => Char.ToLowerInvariant(str[0]) + str.Substring(1);
141-
}
142131
}
143132
}

0 commit comments

Comments
 (0)