Skip to content

Commit

Permalink
Fix #192
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouke van der Maas committed Jun 18, 2018
1 parent df52884 commit a34f8ca
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## `master` (upcoming 1.7)
## Master (upcoming 1.8)

## Version 1.7.1

- [**BUGFIX**] Support camel case serialization for nested properties in attributes and meta hashes

## Version 1.7

- [**BUGFIX**] Serialise complex objects correctly (#149 by @laurence79)
- [**BUGFIX**] Do not require Accept header (#151 by @bjornharrtell)
Expand Down
9 changes: 8 additions & 1 deletion Saule/Serialization/JsonApiContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ namespace Saule.Serialization
{
internal class JsonApiContractResolver : DefaultContractResolver
{
private readonly IPropertyNameConverter _nameConverter;

public JsonApiContractResolver(IPropertyNameConverter nameConverter)
{
_nameConverter = nameConverter;
}

protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);

foreach (var property in properties)
{
property.PropertyName = property.PropertyName.ToDashed();
property.PropertyName = _nameConverter.ToJsonPropertyName(property.PropertyName);
}

return properties;
Expand Down
2 changes: 1 addition & 1 deletion Saule/Serialization/ResourceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public JObject Serialize()

public JObject Serialize(JsonSerializer serializer)
{
serializer.ContractResolver = new JsonApiContractResolver();
serializer.ContractResolver = new JsonApiContractResolver(_propertyNameConverter);
_serializer = serializer;

if (_value == null)
Expand Down
38 changes: 38 additions & 0 deletions Tests/Integration/PropertyNameConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,43 @@ public async Task CamelCaseConstructor()
Assert.NotNull(result["data"]["attributes"]["firstName"]);
}
}

[Fact(DisplayName = "Nested properties in attributes are converted correctly")]
public async Task CamelCaseNestedAttrs()
{
using (var server = new NewSetupJsonApiServer(new JsonApiConfiguration
{
PropertyNameConverter = new CamelCasePropertyNameConverter()
}))
{
var client = server.GetClient();

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

var address = result["data"]["attributes"]["address"];

Assert.NotNull(address["streetName"]);
Assert.NotNull(address["zipCode"]);
}
}

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

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

var meta = result["meta"];

Assert.NotNull(meta["numberOfFriends"]);
Assert.NotNull(meta["numberOfFamilyMembers"]);
}
}
}
}
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.0.{build}
version: 1.7.1.{build}
configuration: Release
skip_tags: true
assembly_info:
Expand Down

0 comments on commit a34f8ca

Please sign in to comment.