Skip to content

Commit

Permalink
More casing-related updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolestandifer3 committed Apr 8, 2020
1 parent 9d447b4 commit affb075
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ namespace JsonApiDotNetCore.Graph
public sealed class CamelCaseFormatter: BaseResourceNameFormatter
{
/// <inheritdoc/>
public override string ApplyCasingConvention(string properName) => char.ToLowerInvariant(properName[0]) + properName.Substring(1);
public override string ApplyCasingConvention(string properName) => char.ToLower(properName[0]) + properName.Substring(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,32 @@ public sealed class KebabCaseFormatter : BaseResourceNameFormatter
/// <inheritdoc/>
public override string ApplyCasingConvention(string properName)
{
if (properName.Length == 0)
{
return properName;
}

var chars = properName.ToCharArray();
if (chars.Length > 0)
var builder = new StringBuilder();

for (var i = 0; i < chars.Length; i++)
{
var builder = new StringBuilder();
for (var i = 0; i < chars.Length; i++)
if (char.IsUpper(chars[i]))
{
if (char.IsUpper(chars[i]))
{
var hashedString = i > 0 ? $"-{char.ToLower(chars[i])}" : $"{char.ToLower(chars[i])}";
builder.Append(hashedString);
}
else
if (i > 0)
{
builder.Append(chars[i]);
builder.Append('-');
}

builder.Append(char.ToLower(chars[i]));
}
else
{
builder.Append(chars[i]);
}
return builder.ToString();
}
return properName;

return builder.ToString();
}
}
}
18 changes: 6 additions & 12 deletions src/JsonApiDotNetCore/Middleware/CurrentRequestMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private string GetRelationshipId()
private string[] SplitCurrentPath()
{
var path = _httpContext.Request.Path.Value;
var ns = $"/{GetNameSpace()}";
var ns = $"/{_options.Namespace}";
var nonNameSpaced = path.Replace(ns, "");
nonNameSpaced = nonNameSpaced.Trim('/');
var individualComponents = nonNameSpaced.Split('/');
Expand All @@ -96,11 +96,11 @@ private string GetBasePath(string resourceName = null)
var r = _httpContext.Request;
if (_options.RelativeLinks)
{
return GetNameSpace();
return _options.Namespace;
}
var ns = GetNameSpace();

var customRoute = GetCustomRoute(r.Path.Value, resourceName);
var toReturn = $"{r.Scheme}://{r.Host}/{ns}";
var toReturn = $"{r.Scheme}://{r.Host}/{_options.Namespace}";
if (customRoute != null)
{
toReturn += $"/{customRoute}";
Expand All @@ -110,12 +110,11 @@ private string GetBasePath(string resourceName = null)

private object GetCustomRoute(string path, string resourceName)
{
var ns = GetNameSpace();
var trimmedComponents = path.Trim('/').Split('/').ToList();
var resourceNameIndex = trimmedComponents.FindIndex(c => c == resourceName);
var newComponents = trimmedComponents.Take(resourceNameIndex).ToArray();
var customRoute = string.Join('/', newComponents);
if (customRoute == ns)
if (customRoute == _options.Namespace)
{
return null;
}
Expand All @@ -125,15 +124,10 @@ private object GetCustomRoute(string path, string resourceName)
}
}

private string GetNameSpace()
{
return _options.Namespace;
}

private bool PathIsRelationship()
{
var actionName = (string)_routeValues["action"];
return actionName.ToLower().Contains("relationships");
return actionName.ToLowerInvariant().Contains("relationships");
}

private async Task<bool> IsValidAsync()
Expand Down
15 changes: 2 additions & 13 deletions test/UnitTests/Builders/ContextGraphBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Resources_Without_Names_Specified_Will_Use_Default_Formatter()
public void Resources_Without_Names_Specified_Will_Use_Configured_Formatter()
{
// Arrange
var builder = new ResourceGraphBuilder(new CamelCaseNameFormatter());
var builder = new ResourceGraphBuilder(new CamelCaseFormatter());
builder.AddResource<TestResource>();

// Act
Expand Down Expand Up @@ -93,7 +93,7 @@ public void Attrs_Without_Names_Specified_Will_Use_Default_Formatter()
public void Attrs_Without_Names_Specified_Will_Use_Configured_Formatter()
{
// Arrange
var builder = new ResourceGraphBuilder(new CamelCaseNameFormatter());
var builder = new ResourceGraphBuilder(new CamelCaseFormatter());
builder.AddResource<TestResource>();

// Act
Expand Down Expand Up @@ -128,16 +128,5 @@ public sealed class TestResource : Identifiable
}

public class RelatedResource : Identifiable { }

public sealed class CamelCaseNameFormatter : IResourceNameFormatter
{
public string ApplyCasingConvention(string properName) => ToCamelCase(properName);

public string FormatPropertyName(PropertyInfo property) => ToCamelCase(property.Name);

public string FormatResourceName(Type resourceType) => ToCamelCase(resourceType.Name.Pluralize());

private string ToCamelCase(string str) => Char.ToLowerInvariant(str[0]) + str.Substring(1);
}
}
}

0 comments on commit affb075

Please sign in to comment.