Skip to content

Commit

Permalink
Merge pull request #312 from commercetools/mixed_set_fix
Browse files Browse the repository at this point in the history
add option to specify attribute type mapping explicit
  • Loading branch information
kodiakhq[bot] authored May 30, 2024
2 parents dc83729 + 4b1190c commit 08f3f94
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using commercetools.Sdk.Api;
Expand All @@ -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<string, Type>()
{
{ "set-mixed", typeof(SetAttribute<decimal>) },
{ "decimal", typeof(DecimalAttribute) },
}});
var serviceProvider = services.BuildServiceProvider();
var serializerService = serviceProvider.GetService<IApiSerializerService>();
var serialized = File.ReadAllText("Resources/Attributes/MixedSetAttribute.json");
var deserialized = serializerService.Deserialize<ProductVariant>(serialized);
Assert.NotNull(deserialized);
var attributes = deserialized.Attributes;
Assert.IsType<List<decimal>>(attributes.Get("set-mixed").Value);
Assert.IsType<decimal>(attributes.Get("decimal").Value);

}

[Fact]
public void AttributesDeserialization()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": 1,
"key": "newKey",
"attributes": [
{
"name": "set-mixed",
"value": [10, 10.3]
},
{
"name": "decimal",
"value": 10.3
}
]
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.Collections.Generic;

namespace commercetools.Sdk.Api.Serialization
{
public interface ISerializationConfiguration
{
bool DeserializeNumberAttributeAsDecimalOnly { get; set; }

Dictionary<string, Type> AttributeTypeMap { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}
Original file line number Diff line number Diff line change
@@ -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<string, Type> AttributeTypeMap { get; set; } = new Dictionary<string, Type>();
}
}

0 comments on commit 08f3f94

Please sign in to comment.