diff --git a/Saule/Queries/Fieldset/FieldsetProperty.cs b/Saule/Queries/Fieldset/FieldsetProperty.cs index e3f523b..1999ffa 100644 --- a/Saule/Queries/Fieldset/FieldsetProperty.cs +++ b/Saule/Queries/Fieldset/FieldsetProperty.cs @@ -1,4 +1,6 @@ -namespace Saule.Queries.Fieldset +using System.Linq; + +namespace Saule.Queries.Fieldset { /// /// Property for fieldset @@ -13,7 +15,7 @@ public class FieldsetProperty public FieldsetProperty(string type, string[] fields) { Type = type; - Fields = fields; + Fields = fields.Select(f => f.ToComparablePropertyName()).ToArray(); } /// diff --git a/Saule/Serialization/ResourceSerializer.cs b/Saule/Serialization/ResourceSerializer.cs index 0046edb..8a5873c 100644 --- a/Saule/Serialization/ResourceSerializer.cs +++ b/Saule/Serialization/ResourceSerializer.cs @@ -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 { diff --git a/Saule/StringExtensions.cs b/Saule/StringExtensions.cs index 88b2a2c..80ae54e 100644 --- a/Saule/StringExtensions.cs +++ b/Saule/StringExtensions.cs @@ -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); diff --git a/Tests/JsonApiSerializerTests.cs b/Tests/JsonApiSerializerTests.cs index 1edb5d0..adddd9e 100644 --- a/Tests/JsonApiSerializerTests.cs +++ b/Tests/JsonApiSerializerTests.cs @@ -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 + { + 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() {