Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sparse fieldsets parameter to be dashed case #210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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