Skip to content

Commit

Permalink
Fix #196
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouke van der Maas committed Aug 24, 2018
1 parent a34f8ca commit c916e77
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Master (upcoming 1.8)

## Version 1.7.2

- [**BUGFIX**] Support camel case serialization for relationship names

## Version 1.7.1

- [**BUGFIX**] Support camel case serialization for nested properties in attributes and meta hashes
Expand Down
2 changes: 1 addition & 1 deletion Saule/Serialization/ResourceDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private JToken SingleToFlatStructure(JObject child)
foreach (var rel in child["relationships"] ?? new JArray())
{
var prop = rel as JProperty;
result.Add(prop?.Name.ToPascalCase(), ToFlatStructure(prop?.Value));
result.Add(_propertyNameConverter.ToModelPropertyName(prop?.Name), ToFlatStructure(prop?.Value));
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion Saule/Serialization/ResourceGraphNodeKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ResourceGraphNodeKey(
obj.ThrowIfNull(nameof(obj));
resource.ThrowIfNull(nameof(resource));

Type = resource.ResourceType.ToDashed();
Type = resource.ResourceType;
if (obj != null)
{
Id = obj.GetValueOfProperty(resource.IdProperty)?.ToString();
Expand Down
2 changes: 1 addition & 1 deletion Saule/Serialization/ResourceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private JObject SerializeRelationships(ResourceGraphNode node)
item["data"] = data;
}

response[kv.Key] = item;
response[_propertyNameConverter.ToJsonPropertyName(kv.Key)] = item;
}

return response;
Expand Down
52 changes: 51 additions & 1 deletion Tests/Integration/PropertyNameConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Saule.Http;
using Saule.Serialization;
using Tests.Helpers;
Expand All @@ -8,6 +12,30 @@ namespace Tests.Integration
{
public class PropertyNameConverterTests
{
[Fact(DisplayName = "Deserializes complex models correctly")]
public async Task DeserializeWorks()
{
using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration
{
PropertyNameConverter = new CamelCasePropertyNameConverter()
}))
{
var client = server.GetClient();

var getResult = await client.GetJsonResponseAsync("api/people/123");
getResult.Remove("included");

var content = new StringContent(getResult.ToString());
content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
var postResultMessage = await client.PostAsync("api/people/123", content);

var postResult = JObject.Parse(await postResultMessage.Content.ReadAsStringAsync());
postResult.Remove("included");

Assert.True(JToken.DeepEquals(getResult, postResult));
}
}

[Fact(DisplayName = "CamelCase constuctor generates attributes in camelCase")]
public async Task CamelCaseConstructor()
{
Expand All @@ -25,6 +53,28 @@ public async Task CamelCaseConstructor()
}
}

[Fact(DisplayName = "Relationship names are converted correctly")]
public async Task CamelCaseRelationships()
{
using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration
{
PropertyNameConverter = new CamelCasePropertyNameConverter()
}))
{
var client = server.GetClient();

var result = await client.GetJsonResponseAsync("api/people/123");

var relationships = result["data"]["relationships"];

Assert.NotNull(relationships["secretFriends"]);
Assert.NotNull(relationships["familyMembers"]);

Assert.Null(relationships["secret-friends"]);
Assert.Null(relationships["family-members"]);
}
}

[Fact(DisplayName = "Nested properties in attributes are converted correctly")]
public async Task CamelCaseNestedAttrs()
{
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.7.1.{build}
version: 1.7.2.{build}
configuration: Release
skip_tags: true
assembly_info:
Expand Down

0 comments on commit c916e77

Please sign in to comment.