From a34f8ca238ea7a57e58e3e3569b1fbefa214c83c Mon Sep 17 00:00:00 2001 From: Jouke van der Maas Date: Mon, 18 Jun 2018 07:55:02 +0200 Subject: [PATCH] Fix #192 --- CHANGELOG.md | 8 +++- .../Serialization/JsonApiContractResolver.cs | 9 ++++- Saule/Serialization/ResourceSerializer.cs | 2 +- .../Integration/PropertyNameConverterTests.cs | 38 +++++++++++++++++++ appveyor.yml | 2 +- 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9463692..6354cea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Saule/Serialization/JsonApiContractResolver.cs b/Saule/Serialization/JsonApiContractResolver.cs index 3064bee..9e07c4e 100644 --- a/Saule/Serialization/JsonApiContractResolver.cs +++ b/Saule/Serialization/JsonApiContractResolver.cs @@ -7,13 +7,20 @@ namespace Saule.Serialization { internal class JsonApiContractResolver : DefaultContractResolver { + private readonly IPropertyNameConverter _nameConverter; + + public JsonApiContractResolver(IPropertyNameConverter nameConverter) + { + _nameConverter = nameConverter; + } + protected override IList 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; diff --git a/Saule/Serialization/ResourceSerializer.cs b/Saule/Serialization/ResourceSerializer.cs index b2f6687..c5f94ad 100644 --- a/Saule/Serialization/ResourceSerializer.cs +++ b/Saule/Serialization/ResourceSerializer.cs @@ -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) diff --git a/Tests/Integration/PropertyNameConverterTests.cs b/Tests/Integration/PropertyNameConverterTests.cs index fb53a5a..303d3ca 100644 --- a/Tests/Integration/PropertyNameConverterTests.cs +++ b/Tests/Integration/PropertyNameConverterTests.cs @@ -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"]); + } + } } } diff --git a/appveyor.yml b/appveyor.yml index a5eecbb..51fb2a6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 1.7.0.{build} +version: 1.7.1.{build} configuration: Release skip_tags: true assembly_info: