diff --git a/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs index 0412472b595..582f2f53343 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,26 @@ 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) }, + { "decimal", typeof(DecimalAttribute) }, + }}); + 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); + Assert.IsType(attributes.Get("decimal").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..7dff95392b8 --- /dev/null +++ b/commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/Attributes/MixedSetAttribute.json @@ -0,0 +1,14 @@ +{ + "id": 1, + "key": "newKey", + "attributes": [ + { + "name": "set-mixed", + "value": [10, 10.3] + }, + { + "name": "decimal", + "value": 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..9ce57fe4cf3 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; 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..4267d6f8587 100644 --- a/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs +++ b/commercetools.Sdk/commercetools.Sdk.Api/Serialization/MapperTypeRetrievers/AttributeTypeRetriever.cs @@ -138,10 +138,22 @@ private Type GetElementTypeForToken(JsonElement element) return tokenType; } + [Obsolete("use GetAttribute(attributeName, element) instead")] 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