From 97a1837e9a3cfce706bc13b87afaa932d01085ce Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Thu, 30 May 2024 09:51:32 +0200 Subject: [PATCH 1/3] add option to specify attribute type mapping explicit --- .../AttributesTests.cs | 19 +++++++++++++++++++ .../Attributes/MixedSetAttribute.json | 10 ++++++++++ .../SerializationFixture.cs | 8 +++++++- .../ISerializationConfiguration.cs | 5 +++++ .../JsonConverters/AttributeConverter.cs | 5 +++-- .../AttributeTypeRetriever.cs | 11 +++++++++++ .../SerializationConfiguration.cs | 10 +++++----- 7 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json diff --git a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs index 0412472b595..625c37b8733 100644 --- a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs +++ b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using commercetools.Sdk.Api; @@ -21,6 +22,24 @@ public AttributesTests(SerializationFixture serializationFixture) this._serializationFixture = serializationFixture; } + [Fact] + public void MixedSetDeserialization() + { + var services = new ServiceCollection(); + services.UseCommercetoolsApiSerialization(new SerializationConfiguration() { AttributeTypeMap = new Dictionary() + { + { "set-mixed", typeof(SetAttribute) } + }}); + var serviceProvider = services.BuildServiceProvider(); + var serializerService = serviceProvider.GetService(); + var serialized = File.ReadAllText("Resources/Attributes/MixedSetAttribute.json"); + var deserialized = serializerService.Deserialize(serialized); + Assert.NotNull(deserialized); + var attributes = deserialized.Attributes; + Assert.IsType>(attributes.Get("set-mixed").Value); + + } + [Fact] public void AttributesDeserialization() { diff --git a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json new file mode 100644 index 00000000000..b22b9e8a24f --- /dev/null +++ b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json @@ -0,0 +1,10 @@ +{ + "id": 1, + "key": "newKey", + "attributes": [ + { + "name": "set-mixed", + "value": [10, 10.3] + } + ] +} \ No newline at end of file diff --git a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs index 1bf895e8f1d..57770b45bc6 100644 --- a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs +++ b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs @@ -1,5 +1,8 @@ +using System; +using System.Collections.Generic; using commercetools.Base.Serialization; using commercetools.Sdk.Api; +using commercetools.Sdk.Api.Models.Products; using commercetools.Sdk.Api.Serialization; using Microsoft.Extensions.DependencyInjection; @@ -10,7 +13,10 @@ public class SerializationFixture public SerializationFixture() { var services = new ServiceCollection(); - services.UseCommercetoolsApiSerialization(); + services.UseCommercetoolsApiSerialization(new SerializationConfiguration() { AttributeTypeMap = new Dictionary() + { + { "grapes-pct", typeof(SetAttribute) } + }}); var serviceProvider = services.BuildServiceProvider(); this.SerializerService = serviceProvider.GetService(); } diff --git a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/ISerializationConfiguration.cs b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/ISerializationConfiguration.cs index 229ff2bd37e..2fad3575f32 100644 --- a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/ISerializationConfiguration.cs +++ b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/ISerializationConfiguration.cs @@ -1,7 +1,12 @@ +using System; +using System.Collections.Generic; + namespace commercetools.Sdk.Api.Serialization { public interface ISerializationConfiguration { bool DeserializeNumberAttributeAsDecimalOnly { get; set; } + + Dictionary AttributeTypeMap { get; set; } } } \ No newline at end of file diff --git a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/JsonConverters/AttributeConverter.cs b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/JsonConverters/AttributeConverter.cs index d4a3bc99347..4f8a0647f93 100644 --- a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/JsonConverters/AttributeConverter.cs +++ b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/JsonConverters/AttributeConverter.cs @@ -36,8 +36,9 @@ public override IAttribute Read(ref Utf8JsonReader reader, Type typeToConvert, J { var nameProp = rootElement.GetProperty("name"); var valueProp = rootElement.GetProperty("value"); - attribute = _attributeTypeRetriever.GetAttribute(valueProp); - attribute.Name = nameProp.GetString(); + var attributeName = nameProp.GetString(); + attribute = _attributeTypeRetriever.GetAttribute(attributeName, valueProp); + attribute.Name = attributeName; var returnType = attribute is IGenericTypeAttribute attributeValueType ? attributeValueType.GetValueType() : _mapperTypeRetriever.GetTypeForToken(valueProp); diff --git a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs index 78f601e1033..48cadc5ca4a 100644 --- a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs +++ b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs @@ -143,5 +143,16 @@ public IAttribute GetAttribute(JsonElement element) var type = GetTypeForToken(element); return (IAttribute)Activator.CreateInstance(type); } + + public IAttribute GetAttribute(String attributeName, JsonElement element) + { + if (_serializationConfiguration.AttributeTypeMap.TryGetValue(attributeName, out var mappedType)) + { + return (IAttribute)Activator.CreateInstance(mappedType); + } + var type = GetTypeForToken(element); + return (IAttribute)Activator.CreateInstance(type); + } + } } \ No newline at end of file diff --git a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/SerializationConfiguration.cs b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/SerializationConfiguration.cs index 5990dc2d82d..9872d2efd3e 100644 --- a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/SerializationConfiguration.cs +++ b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/SerializationConfiguration.cs @@ -1,13 +1,13 @@ +using System; +using System.Collections.Generic; + namespace commercetools.Sdk.Api.Serialization { public class SerializationConfiguration : ISerializationConfiguration { public static readonly ISerializationConfiguration DefaultConfig = new SerializationConfiguration(); - public bool DeserializeNumberAttributeAsDecimalOnly { get; set; } + public bool DeserializeNumberAttributeAsDecimalOnly { get; set; } = false; - public SerializationConfiguration() - { - this.DeserializeNumberAttributeAsDecimalOnly = false; - } + public Dictionary AttributeTypeMap { get; set; } = new Dictionary(); } } \ No newline at end of file From 497d52062212cd76b2e1a140caf77046faeb1de3 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Thu, 30 May 2024 09:52:36 +0200 Subject: [PATCH 2/3] typo --- .../SerializationFixture.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs index 57770b45bc6..9ce57fe4cf3 100644 --- a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs +++ b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/SerializationFixture.cs @@ -13,10 +13,7 @@ public class SerializationFixture public SerializationFixture() { var services = new ServiceCollection(); - services.UseCommercetoolsApiSerialization(new SerializationConfiguration() { AttributeTypeMap = new Dictionary() - { - { "grapes-pct", typeof(SetAttribute) } - }}); + services.UseCommercetoolsApiSerialization(); var serviceProvider = services.BuildServiceProvider(); this.SerializerService = serviceProvider.GetService(); } From 4b1190c04240761a71ec5a4c0edd62660b1030cf Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Thu, 30 May 2024 10:02:57 +0200 Subject: [PATCH 3/3] test explicit scalar attribute type --- .../commercetools.Api.Serialization.Tests/AttributesTests.cs | 4 +++- .../Resources/Attributes/MixedSetAttribute.json | 4 ++++ .../MapperTypeRetrievers/AttributeTypeRetriever.cs | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs index 625c37b8733..582f2f53343 100644 --- a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs +++ b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs @@ -28,7 +28,8 @@ public void MixedSetDeserialization() var services = new ServiceCollection(); services.UseCommercetoolsApiSerialization(new SerializationConfiguration() { AttributeTypeMap = new Dictionary() { - { "set-mixed", typeof(SetAttribute) } + { "set-mixed", typeof(SetAttribute) }, + { "decimal", typeof(DecimalAttribute) }, }}); var serviceProvider = services.BuildServiceProvider(); var serializerService = serviceProvider.GetService(); @@ -37,6 +38,7 @@ public void MixedSetDeserialization() Assert.NotNull(deserialized); var attributes = deserialized.Attributes; Assert.IsType>(attributes.Get("set-mixed").Value); + Assert.IsType(attributes.Get("decimal").Value); } diff --git a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json index b22b9e8a24f..7dff95392b8 100644 --- a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json +++ b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json @@ -5,6 +5,10 @@ { "name": "set-mixed", "value": [10, 10.3] + }, + { + "name": "decimal", + "value": 10.3 } ] } \ No newline at end of file diff --git a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs index 48cadc5ca4a..4267d6f8587 100644 --- a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs +++ b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs @@ -138,6 +138,7 @@ private Type GetElementTypeForToken(JsonElement element) return tokenType; } + [Obsolete("use GetAttribute(attributeName, element) instead")] public IAttribute GetAttribute(JsonElement element) { var type = GetTypeForToken(element);