Skip to content

Commit

Permalink
Merge pull request #210 from BookerSoftwareInc/lower-case-fields
Browse files Browse the repository at this point in the history
Allow sparse fieldsets parameter to be dashed case
  • Loading branch information
joukevandermaas authored Dec 6, 2018
2 parents 9edcbb6 + 7ea5aab commit 34c4dd0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Saule/Queries/Fieldset/FieldsetProperty.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Saule.Queries.Fieldset
using System.Linq;

namespace Saule.Queries.Fieldset
{
/// <summary>
/// Property for fieldset
Expand All @@ -13,7 +15,7 @@ public class FieldsetProperty
public FieldsetProperty(string type, string[] fields)
{
Type = type;
Fields = fields;
Fields = fields.Select(f => f.ToComparablePropertyName()).ToArray();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Saule/Serialization/ResourceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private JObject SerializeAttributes(ResourceGraphNode node, FieldsetProperty fie
{
var attributeHash = node.Resource.Attributes
.Where(a =>
node.SourceObject.IncludesProperty(_propertyNameConverter.ToModelPropertyName(a.InternalName)) && fieldset.Fields.Contains(_propertyNameConverter.ToModelPropertyName(a.InternalName)))
node.SourceObject.IncludesProperty(_propertyNameConverter.ToModelPropertyName(a.InternalName)) && fieldset.Fields.Contains(a.InternalName.ToComparablePropertyName()))
.Select(a =>
new
{
Expand Down
5 changes: 5 additions & 0 deletions Saule/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static string ToCamelCase(this string source)
return string.Join(string.Empty, cased.ToArray());
}

public static string ToComparablePropertyName(this string propertyName)
{
return propertyName.Replace("_", string.Empty).Replace("-", string.Empty).ToUpperInvariant();
}

public static string SubstringToSeperator(this string source, string seperator)
{
var to = source.IndexOf(seperator);
Expand Down
22 changes: 22 additions & 0 deletions Tests/JsonApiSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ public void UsesQueryFieldsetExpressions()
Assert.Null(result["data"][0]["attributes"]["number-of-employees"]);
}

[Theory(DisplayName = "Uses query fieldset expressions if specified with various input string cases.")]
[InlineData("?fields[corporation]=name,NumberOfEmployees")]
[InlineData("?fields[corporation]=name,numberofemployees")]
[InlineData("?fields[corporation]=name,NUMBEROFEMPLOYEES")]
[InlineData("?fields[corporation]=name,numberOfEmployees")]
[InlineData("?fields[corporation]=name,number-of-employees")]
[InlineData("?fields[corporation]=name,number_of_employees")]
public void UsesQueryFieldsetExpressionsFieldsFormatCases(string query)
{
var target = new JsonApiSerializer<CompanyResource>
{
AllowQuery = true
};
var companies = Get.Companies(1).ToList();
var result = target.Serialize(companies, new Uri(DefaultUrl, query));
_output.WriteLine(result.ToString());

Assert.NotNull(result["data"][0]["attributes"]["name"]);
Assert.Null(result["data"][0]["attributes"]["location"]);
Assert.NotNull(result["data"][0]["attributes"]["number-of-employees"]);
}

[Fact(DisplayName = "Returns no fields if requested field is not part of that model")]
public void ReturnEmptyModelForUnknownFieldsetExpressions()
{
Expand Down

0 comments on commit 34c4dd0

Please sign in to comment.