Skip to content

Commit 74402c3

Browse files
authored
Merge pull request #173 from commercetools/Fix-MappersConverters
Handle Deserialization of SetAttribute of SetAttribute elements
2 parents 68db2de + 65a56a4 commit 74402c3

File tree

8 files changed

+171
-5
lines changed

8 files changed

+171
-5
lines changed

commercetools.Sdk/Tests/commercetools.Sdk.Serialization.Tests/AttributeDeserializationTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,41 @@ public void DeserializeDateTimeAsTextAttribute()
342342
var deserialized = serializerService.Deserialize<ProductVariant>(serialized);
343343
Assert.IsAssignableFrom<Attribute<string>>(deserialized.Attributes[0]);
344344
}
345+
346+
[Fact]
347+
public void DeserializeProductWithSetOfSetAttribute()
348+
{
349+
var serializerService = this.serializationFixture.SerializerService;
350+
var productSerialized = File.ReadAllText("Resources/Attributes/SetOfSetAttribute.json");
351+
352+
var product = serializerService.Deserialize<Product>(productSerialized);
353+
var attr = product.MasterData.Current.MasterVariant.Attributes[0];
354+
Assert.NotNull(attr);
355+
Assert.True(attr.IsSetAttribute<AttributeSet<string>>());
356+
var setOfSetAttr = attr as Attribute<AttributeSet<AttributeSet<string>>>;
357+
Assert.NotNull(setOfSetAttr);
358+
var firstSet = setOfSetAttr.Value.First();
359+
var secondSet = setOfSetAttr.Value.ElementAt(1);
360+
Assert.Equal("firstSet-1",firstSet.FirstOrDefault());
361+
Assert.Equal("secondSet-1",secondSet.FirstOrDefault());
362+
}
363+
[Fact]
364+
public void DeserializeProductWithSetOfSetOfSetAttribute()
365+
{
366+
var serializerService = this.serializationFixture.SerializerService;
367+
var productSerialized = File.ReadAllText("Resources/Attributes/SetOfSetAttribute.json");
368+
369+
var product = serializerService.Deserialize<Product>(productSerialized);
370+
var attr = product.MasterData.Current.MasterVariant.Attributes[1];
371+
Assert.NotNull(attr);
372+
Assert.True(attr.IsSetAttribute<AttributeSet<AttributeSet<string>>>());
373+
var setOfSetAttr = attr as Attribute<AttributeSet<AttributeSet<AttributeSet<string>>>>;
374+
Assert.NotNull(setOfSetAttr);
375+
var set111 = setOfSetAttr.Value.First().First();
376+
var set121 = setOfSetAttr.Value.ElementAt(1).First();
377+
Assert.Equal("set111-1",set111.FirstOrDefault());
378+
Assert.Equal("set121-1",set121.FirstOrDefault());
379+
}
345380

346381
}
347382
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"id": "830d727f-3c15-42d4-887f-1b14e28395ae",
3+
"version": 1,
4+
"lastMessageSequenceNumber": 1,
5+
"createdAt": "2020-02-28T10:57:23.638Z",
6+
"lastModifiedAt": "2020-02-28T10:57:23.638Z",
7+
"lastModifiedBy": {
8+
"clientId": "",
9+
"isPlatformClient": false
10+
},
11+
"createdBy": {
12+
"clientId": "",
13+
"isPlatformClient": false
14+
},
15+
"productType": {
16+
"typeId": "product-type",
17+
"id": "3a0b9586-8ace-4d73-93e0-e8a5085da1f4"
18+
},
19+
"catalogs": [],
20+
"masterData": {
21+
"current": {
22+
"name": {
23+
"en": "Bag “Sofia“ large Gabs"
24+
},
25+
"categories": [],
26+
"categoryOrderHints": {},
27+
"slug": {
28+
"en": "gabs-bag-sofia-large-green"
29+
},
30+
"masterVariant": {
31+
"id": 1,
32+
"sku": "A0E200000001WG3",
33+
"prices": [],
34+
"images": [],
35+
"attributes": [
36+
{
37+
"name": "setOfSet",
38+
"value": [
39+
[
40+
"firstSet-1",
41+
"firstSet-2"
42+
],
43+
[
44+
"secondSet-1",
45+
"secondSet-2"
46+
]
47+
]
48+
},
49+
{
50+
"name": "setOfSetOfSet",
51+
"value": [
52+
[
53+
[
54+
"set111-1",
55+
"set111-2"
56+
],
57+
[
58+
"set112-1",
59+
"set112-2"
60+
]
61+
],
62+
[
63+
[
64+
"set121-1",
65+
"set121-2"
66+
],
67+
[
68+
"set122-1",
69+
"set122-2"
70+
]
71+
]
72+
]
73+
}
74+
],
75+
"assets": []
76+
},
77+
"variants": [],
78+
"searchKeywords": {}
79+
},
80+
"staged": {
81+
"name": {
82+
"en": "Bag “Sofia“ large Gabs"
83+
},
84+
"categories": [],
85+
"categoryOrderHints": {},
86+
"slug": {
87+
"en": "gabs-bag-sofia-large-green"
88+
},
89+
"masterVariant": {
90+
"id": 1,
91+
"sku": "A0E200000001WG3",
92+
"prices": [],
93+
"images": [],
94+
"attributes": [
95+
{
96+
"name": "setOfSet",
97+
"value": [
98+
[
99+
"firstSet-1",
100+
"firstSet-2"
101+
],
102+
[
103+
"secondSet-1",
104+
"secondSet-2"
105+
]
106+
]
107+
}
108+
],
109+
"assets": []
110+
},
111+
"variants": [],
112+
"searchKeywords": {}
113+
},
114+
"published": true,
115+
"hasStagedChanges": false
116+
},
117+
"catalogData": {},
118+
"taxCategory": {
119+
"typeId": "tax-category",
120+
"id": "6cb35c8a-ec1c-470f-951c-af36649cb0af"
121+
},
122+
"lastVariantId": 1
123+
}

commercetools.Sdk/commercetools.Sdk.Domain/Products/Attributes/AttributeExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,10 @@ attribute is SetAttribute<NestedAttribute> ||
136136
attribute is Attribute<AttributeSet<NestedAttribute>> ||
137137
attribute is Attribute<AttributeSet<List<Attribute>>>;
138138
}
139+
public static bool IsSetAttribute<T>(this IAttribute attribute)
140+
{
141+
return
142+
attribute is Attribute<AttributeSet<T>>;
143+
}
139144
}
140145
}

commercetools.Sdk/commercetools.Sdk.Serialization/MapperTypeRetrievers/SetMapperTypeRetriever.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public override Type GetTypeForToken(JToken token)
2323
{
2424
if (token.HasValues)
2525
{
26-
Type firstAttributeType = base.GetTypeForToken(token[0]);
26+
var firstToken = token.First;
27+
var firstAttributeType = firstToken.IsSetAttribute()?
28+
this.GetTypeForToken(firstToken)
29+
: base.GetTypeForToken(firstToken);
2730
type = attributeTypes.GetOrAdd(firstAttributeType, t => this.SetType.MakeGenericType(firstAttributeType));
2831
}
2932
else

commercetools.Sdk/commercetools.Sdk.Serialization/Mappers/EnumConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal abstract class EnumConverter<T, S> : ICustomJsonMapper<T>
1111

1212
public bool CanConvert(JToken property)
1313
{
14-
if (property != null && property.HasValues)
14+
if (property != null && property.HasValues && !property.IsArrayToken())
1515
{
1616
var keyProperty = property["key"];
1717
var labelProperty = property["label"];

commercetools.Sdk/commercetools.Sdk.Serialization/Mappers/LocalizedEnumConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected LocalizedEnumConverter()
1616

1717
public bool CanConvert(JToken property)
1818
{
19-
if (property != null && property.HasValues)
19+
if (property != null && property.HasValues && !property.IsArrayToken())
2020
{
2121
var keyProperty = property["key"];
2222
if (keyProperty != null)

commercetools.Sdk/commercetools.Sdk.Serialization/Mappers/MoneyConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal abstract class MoneyConverter<T, S> : ICustomJsonMapper<T>
1111

1212
public bool CanConvert(JToken property)
1313
{
14-
if (property != null && property.HasValues)
14+
if (property != null && property.HasValues && !property.IsArrayToken())
1515
{
1616
var currencyCodeProperty = property["currencyCode"];
1717
if (currencyCodeProperty != null)

commercetools.Sdk/commercetools.Sdk.Serialization/Mappers/ReferenceConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal abstract class ReferenceConverter<T, S> : ICustomJsonMapper<T>
1111

1212
public bool CanConvert(JToken property)
1313
{
14-
if (property != null && property.HasValues)
14+
if (property != null && property.HasValues && !property.IsArrayToken())
1515
{
1616
var typeIdProperty = property["typeId"];
1717
var idProperty = property["id"];

0 commit comments

Comments
 (0)