Skip to content

Commit 105dad2

Browse files
Merge pull request #413 from commercetools/devx-551-product-tailoring-attributes
[DEVX-551] add json converter for ProductTailoringAttributes
2 parents ab65f1e + f677363 commit 105dad2

22 files changed

+961
-12
lines changed

commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/ProductTailoringAttributesTests.cs

Lines changed: 443 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{
2+
"id": 1,
3+
"key": "newKey",
4+
"attributes": [
5+
{
6+
"name": "text",
7+
"value": "txt"
8+
},
9+
{
10+
"name": "ltext",
11+
"value": {
12+
"de": "de-txt",
13+
"en": "en-txt"
14+
}
15+
},
16+
{
17+
"name": "enum",
18+
"value": {
19+
"key": "enum-key2",
20+
"label": "enum-label2"
21+
}
22+
},
23+
{
24+
"name": "lenum",
25+
"value": {
26+
"key": "lenum-key2",
27+
"label": {
28+
"de": "enum-label2-de",
29+
"en": "enum-label2-en"
30+
}
31+
}
32+
},
33+
{
34+
"name": "date",
35+
"value": "2021-03-24"
36+
},
37+
{
38+
"name": "time",
39+
"value": "13:23:00.000"
40+
},
41+
{
42+
"name": "datetime",
43+
"value": "2021-03-24T00:45:00.000Z"
44+
},
45+
{
46+
"name": "boolean",
47+
"value": true
48+
},
49+
{
50+
"name": "integer",
51+
"value": 12233344
52+
},
53+
{
54+
"name": "double",
55+
"value": 10.2
56+
},
57+
{
58+
"name": "double-zero",
59+
"value": 13.0
60+
},
61+
{
62+
"name": "reference",
63+
"value": {
64+
"typeId": "product",
65+
"id": "b7334b20-c570-4e1d-a71a-35efd1c1c895"
66+
}
67+
},
68+
{
69+
"name": "money",
70+
"value": {
71+
"type": "centPrecision",
72+
"currencyCode": "EUR",
73+
"centAmount": 234500,
74+
"fractionDigits": 2
75+
}
76+
},
77+
{
78+
"name": "set-text",
79+
"value": ["foo"]
80+
},
81+
{
82+
"name": "set-ltext",
83+
"value": [{ "en": "foo" }]
84+
},
85+
{
86+
"name": "set-integer",
87+
"value": [10]
88+
},
89+
{
90+
"name": "set-double",
91+
"value": [11.2]
92+
},
93+
{
94+
"name": "set-double-zero",
95+
"value": [13.0]
96+
},
97+
{
98+
"name": "set-enum",
99+
"value": [{
100+
"key": "foo",
101+
"label": "foo"
102+
}]
103+
},
104+
{
105+
"name": "set-lenum",
106+
"value": [{
107+
"key": "foo",
108+
"label": {
109+
"en": "foo"
110+
}
111+
}]
112+
},
113+
{
114+
"name": "set-money",
115+
"value": [{
116+
"type": "centPrecision",
117+
"currencyCode": "EUR",
118+
"centAmount": 100
119+
}]
120+
},
121+
{
122+
"name": "set-reference",
123+
"value": [{
124+
"typeId": "product",
125+
"id": "12345"
126+
}]
127+
},
128+
{
129+
"name": "set-date",
130+
"value": ["2020-01-01"]
131+
},
132+
{
133+
"name": "set-datetime",
134+
"value": ["2020-01-01T13:15:00.123Z"]
135+
},
136+
{
137+
"name": "set-time",
138+
"value": ["13:15:00.123"]
139+
},
140+
{
141+
"name": "set-boolean",
142+
"value": [true]
143+
},
144+
{
145+
"name": "set-empty",
146+
"value": []
147+
}
148+
]
149+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": 1,
3+
"key": "newKey",
4+
"attributes": [
5+
{
6+
"name": "set-mixed",
7+
"value": [10, 10.3]
8+
},
9+
{
10+
"name": "decimal",
11+
"value": 10.3
12+
}
13+
]
14+
}

commercetools.Sdk/commercetools.Sdk.Api/DependencyInjectionSetup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using commercetools.Base.Registration;
1414
using commercetools.Base.Serialization;
1515
using commercetools.Sdk.Api.Client;
16+
using commercetools.Sdk.Api.Models.ProductTailorings;
1617
using commercetools.Sdk.Api.Serialization;
1718
using commercetools.Sdk.Api.Serialization.MapperTypeRetrievers;
1819
using Microsoft.Extensions.Configuration;
@@ -74,8 +75,10 @@ public static void UseCommercetoolsApiSerialization(this IServiceCollection serv
7475
services.AddSingleton(serializationConfiguration ?? SerializationConfiguration.DefaultConfig);
7576
services.AddSingleton<IMapperTypeRetriever<IFieldContainer>>(provider => new FieldMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
7677
services.AddSingleton<IMapperTypeRetriever<IAttribute>>(provider => new AttributeMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
78+
services.AddSingleton<IMapperTypeRetriever<IProductTailoringAttribute>>(provider => new ProductTailoringAttributeMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
7779
services.AddSingleton<AttributeTypeRetriever>(provider => new AttributeTypeRetriever(provider.GetService<ISerializationConfiguration>()));
7880
services.AddSingleton<FieldMapperTypeRetriever>(provider => new FieldMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
81+
services.AddSingleton<ProductTailoringAttributeTypeRetriever>(provider => new ProductTailoringAttributeTypeRetriever(provider.GetService<ISerializationConfiguration>()));
7982
services.AddSingleton<SerializerService>();
8083
services.AddSingleton<IApiSerializerService, SerializerService>();
8184
}

commercetools.Sdk/commercetools.Sdk.Api/Extensions/AttributeExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using commercetools.Sdk.Api.Models.Common;
44
using commercetools.Sdk.Api.Models.Products;
5+
using commercetools.Sdk.Api.Models.ProductTailorings;
56
using commercetools.Sdk.Api.Models.ProductTypes;
67

78
namespace commercetools.Sdk.Api.Extensions
@@ -12,6 +13,11 @@ public static IAttribute Get(this IList<IAttribute> attributes, string name)
1213
{
1314
return attributes.FirstOrDefault(a => a.Name.Equals(name));
1415
}
16+
17+
public static IProductTailoringAttribute Get(this IList<IProductTailoringAttribute> attributes, string name)
18+
{
19+
return attributes.FirstOrDefault(a => a.Name.Equals(name));
20+
}
1521

1622
public static T Get<T>(this IList<IAttribute> attributes, string name) where T : IAttribute
1723
{
@@ -28,6 +34,22 @@ public static T Get<T>(this IList<IAttribute> attributes, string name) where T :
2834
}
2935
return (T)t;
3036
}
37+
38+
public static T Get<T>(this IList<IProductTailoringAttribute> attributes, string name) where T : IProductTailoringAttribute
39+
{
40+
var t = attributes.FirstOrDefault(a => a.Name.Equals(name));
41+
if (t is LongAttribute l && typeof(T).IsAssignableFrom(typeof(DecimalAttribute)))
42+
{
43+
var toD = (DecimalAttribute)l;
44+
return (T)(IAttribute)toD;
45+
}
46+
if (t is DecimalAttribute d && typeof(T).IsAssignableFrom(typeof(LongAttribute)))
47+
{
48+
var toL = (LongAttribute)d;
49+
return (T)(IAttribute)toL;
50+
}
51+
return (T)t;
52+
}
3153

3254
public static bool IsTextAttribute(this IAttribute attribute)
3355
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using commercetools.Base.CustomAttributes;
3+
using commercetools.Sdk.Api.Models.Products;
4+
5+
// ReSharper disable CheckNamespace
6+
namespace commercetools.Sdk.Api.Models.ProductTailorings
7+
{
8+
public partial interface IProductTailoringAttribute : IAttribute
9+
{
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using commercetools.Sdk.Api.Models.Products;
2+
3+
namespace commercetools.Sdk.Api.Models.ProductTailorings
4+
{
5+
6+
public partial class ProductTailoringAttribute : Attribute
7+
{
8+
}
9+
}

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/BooleanAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using commercetools.Sdk.Api.Models.ProductTailorings;
23

34
namespace commercetools.Sdk.Api.Models.Products
45
{
5-
public class BooleanAttribute : Attribute, IGenericAttribute<bool>
6+
public class BooleanAttribute : Attribute, IGenericAttribute<bool>, IProductTailoringAttribute
67
{
78
public Type GetValueType() => typeof(bool);
89

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/DecimalAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using commercetools.Sdk.Api.Models.ProductTailorings;
23

34
namespace commercetools.Sdk.Api.Models.Products
45
{
5-
public class DecimalAttribute : Attribute, IGenericAttribute<decimal>
6+
public class DecimalAttribute : Attribute, IGenericAttribute<decimal>, IProductTailoringAttribute
67
{
78
public Type GetValueType() => typeof(decimal);
89

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/LocalizedEnumAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
2+
using commercetools.Sdk.Api.Models.ProductTailorings;
23
using commercetools.Sdk.Api.Models.ProductTypes;
34

45
namespace commercetools.Sdk.Api.Models.Products
56
{
6-
public class LocalizedEnumAttribute : Attribute, IGenericAttribute<IAttributeLocalizedEnumValue>
7+
public class LocalizedEnumAttribute : Attribute, IGenericAttribute<IAttributeLocalizedEnumValue>, IProductTailoringAttribute
78
{
89
public Type GetValueType() => typeof(IAttributeLocalizedEnumValue);
910

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/LocalizedStringAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using commercetools.Sdk.Api.Models.Common;
3+
using commercetools.Sdk.Api.Models.ProductTailorings;
34

45
namespace commercetools.Sdk.Api.Models.Products
56
{
6-
public class LocalizedStringAttribute : Attribute, IGenericAttribute<LocalizedString>
7+
public class LocalizedStringAttribute : Attribute, IGenericAttribute<LocalizedString>, IProductTailoringAttribute
78
{
89
public Type GetValueType() => typeof(LocalizedString);
910

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/LongAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using commercetools.Sdk.Api.Models.ProductTailorings;
23

34
namespace commercetools.Sdk.Api.Models.Products
45
{
5-
public class LongAttribute : Attribute, IGenericAttribute<long>
6+
public class LongAttribute : Attribute, IGenericAttribute<long>, IProductTailoringAttribute
67
{
78
public Type GetValueType() => typeof(long);
89

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/MoneyAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using commercetools.Sdk.Api.Models.Common;
3+
using commercetools.Sdk.Api.Models.ProductTailorings;
34

45
namespace commercetools.Sdk.Api.Models.Products
56
{
6-
public class MoneyAttribute : Attribute, IGenericAttribute<ITypedMoney>
7+
public class MoneyAttribute : Attribute, IGenericAttribute<ITypedMoney>, IProductTailoringAttribute
78
{
89
public Type GetValueType() => typeof(ITypedMoney);
910

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/PlainEnumAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
2+
using commercetools.Sdk.Api.Models.ProductTailorings;
23
using commercetools.Sdk.Api.Models.ProductTypes;
34

45
namespace commercetools.Sdk.Api.Models.Products
56
{
6-
public class PlainEnumAttribute : Attribute, IGenericAttribute<IAttributePlainEnumValue>
7+
public class PlainEnumAttribute : Attribute, IGenericAttribute<IAttributePlainEnumValue>, IProductTailoringAttribute
78
{
89
public Type GetValueType() => typeof(IAttributePlainEnumValue);
910

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/ReferenceAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using commercetools.Sdk.Api.Models.Common;
3+
using commercetools.Sdk.Api.Models.ProductTailorings;
34

45
namespace commercetools.Sdk.Api.Models.Products
56
{
6-
public class ReferenceAttribute : Attribute, IGenericAttribute<IReference>
7+
public class ReferenceAttribute : Attribute, IGenericAttribute<IReference>, IProductTailoringAttribute
78
{
89
public Type GetValueType() => typeof(IReference);
910

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/SetAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using commercetools.Sdk.Api.Models.ProductTailorings;
34

45
namespace commercetools.Sdk.Api.Models.Products
56
{
6-
public class SetAttribute<T> : Attribute, IGenericAttribute<List<T>>
7+
public class SetAttribute<T> : Attribute, IGenericAttribute<List<T>>, IProductTailoringAttribute
78
{
89
public Type GetValueType() => typeof(List<T>);
910

commercetools.Sdk/commercetools.Sdk.Api/Models/Products/StringAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using commercetools.Sdk.Api.Models.ProductTailorings;
23

34
namespace commercetools.Sdk.Api.Models.Products
45
{
5-
public class StringAttribute : Attribute, IGenericAttribute<string>
6+
public class StringAttribute : Attribute, IGenericAttribute<string>, IProductTailoringAttribute
67
{
78
public Type GetValueType() => typeof(string);
89

commercetools.Sdk/commercetools.Sdk.Api/Serialization/JsonConverters/AttributeConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using commercetools.Sdk.Api.Models.Products;
44
using commercetools.Base.Serialization;
55
using commercetools.Base.Serialization.MapperTypeRetrievers;
6+
using commercetools.Sdk.Api.Models.ProductTailorings;
67
using commercetools.Sdk.Api.Serialization.MapperTypeRetrievers;
78
using Attribute = commercetools.Sdk.Api.Models.Products.Attribute;
89
using Type = System.Type;
@@ -24,7 +25,7 @@ public AttributeConverter(IMapperTypeRetriever<IAttribute> mapperTypeRetriever,
2425

2526
public override bool CanConvert(Type typeToConvert)
2627
{
27-
return typeof(IAttribute).IsAssignableFrom(typeToConvert);
28+
return typeof(IAttribute).IsAssignableFrom(typeToConvert) && !typeof(IProductTailoringAttribute).IsAssignableFrom(typeToConvert);
2829
}
2930

3031
public override IAttribute Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

0 commit comments

Comments
 (0)