Skip to content

Commit

Permalink
change priority for attribute type deserialization (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Oct 9, 2023
1 parent b31b781 commit cbfc114
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public static void UseCommercetoolsApiSerialization(this IServiceCollection serv
services.UseRegistration();
services.UseSerialization();
services.AddSingleton(serializationConfiguration ?? SerializationConfiguration.DefaultConfig);
services.AddSingleton<IMapperTypeRetriever<IFieldContainer>, FieldMapperTypeRetriever>();
services.AddSingleton<IMapperTypeRetriever<IAttribute>, AttributeMapperTypeRetriever>();
services.AddSingleton<AttributeTypeRetriever>();
services.AddSingleton<IMapperTypeRetriever<IFieldContainer>>(new FieldMapperTypeRetriever());
services.AddSingleton<IMapperTypeRetriever<IAttribute>>(provider => new AttributeMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<AttributeTypeRetriever>(provider => new AttributeTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<SerializerService>();
services.AddSingleton<IApiSerializerService, SerializerService>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using commercetools.Sdk.Api.Models.Common;
Expand All @@ -11,15 +12,18 @@ namespace commercetools.Sdk.Api.Serialization.MapperTypeRetrievers
{
public class AttributeMapperTypeRetriever : MapperTypeRetriever<IAttribute>
{
private readonly ICultureValidator _cultureValidator;
private readonly ISerializationConfiguration _serializationConfiguration;

public AttributeMapperTypeRetriever(ICultureValidator cultureValidator, ISerializationConfiguration serializationConfiguration = null)
public AttributeMapperTypeRetriever(ISerializationConfiguration serializationConfiguration = null)
{
this._cultureValidator = cultureValidator;
this._serializationConfiguration = serializationConfiguration ?? SerializationConfiguration.DefaultConfig;
}

[Obsolete("use constructor without cultureValidator")]
public AttributeMapperTypeRetriever(ICultureValidator cultureValidator, ISerializationConfiguration serializationConfiguration = null) : this(serializationConfiguration)
{
}

public override Type GetTypeForToken(JsonElement element)
{
Type tokenType;
Expand Down Expand Up @@ -56,12 +60,10 @@ public override Type GetTypeForToken(JsonElement element)
tokenType = typeof(ITypedMoney);
else if (element.IsReferenceElement())
tokenType = typeof(IReference);
else if (element.IsLocalizedStringElement(_cultureValidator))
tokenType = typeof(LocalizedString);
else if (element.IsNestedElement())
tokenType = typeof(IAttribute);
else
tokenType = typeof(object);
tokenType = typeof(LocalizedString);
break;
case JsonValueKind.Array:
var valueKind = element.GetFirstArrayElementValueKind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ namespace commercetools.Sdk.Api.Serialization.MapperTypeRetrievers
{
public class AttributeTypeRetriever
{
private readonly ICultureValidator _cultureValidator;
private readonly ISerializationConfiguration _serializationConfiguration;

public AttributeTypeRetriever(ICultureValidator cultureValidator, ISerializationConfiguration serializationConfiguration = null)
public AttributeTypeRetriever(ISerializationConfiguration serializationConfiguration = null)
{
this._cultureValidator = cultureValidator;
this._serializationConfiguration = serializationConfiguration ?? SerializationConfiguration.DefaultConfig;
}

[Obsolete("use constructor without cultureValidator")]
public AttributeTypeRetriever(ICultureValidator cultureValidator,
ISerializationConfiguration serializationConfiguration = null) : this(serializationConfiguration)
{
}

private Type GetTypeForToken(JsonElement element)
{
Type tokenType;
Expand Down Expand Up @@ -56,10 +60,10 @@ private Type GetTypeForToken(JsonElement element)
tokenType = typeof(MoneyAttribute);
else if (element.IsReferenceElement())
tokenType = typeof(ReferenceAttribute);
else if (element.IsLocalizedStringElement(_cultureValidator))
tokenType = typeof(LocalizedStringAttribute);
else
else if (element.IsNestedElement())
tokenType = typeof(Attribute);
else
tokenType = typeof(LocalizedStringAttribute);
break;
case JsonValueKind.Array:
var valueKind = element.GetFirstArrayElementValueKind();
Expand Down Expand Up @@ -114,12 +118,10 @@ private Type GetElementTypeForToken(JsonElement element)
tokenType = typeof(ITypedMoney);
else if (element.IsReferenceElement())
tokenType = typeof(IReference);
else if (element.IsLocalizedStringElement(_cultureValidator))
tokenType = typeof(LocalizedString);
else if (element.IsNestedElement())
tokenType = typeof(IAttribute);
else
tokenType = typeof(object);
tokenType = typeof(LocalizedString);
break;
case JsonValueKind.Array:
var valueKind = element.GetFirstArrayElementValueKind();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using commercetools.Sdk.Api.Models.Common;
Expand All @@ -10,12 +11,15 @@ namespace commercetools.Sdk.Api.Serialization.MapperTypeRetrievers
{
public class FieldMapperTypeRetriever : MapperTypeRetriever<IFieldContainer>
{
private readonly ICultureValidator _cultureValidator;
public FieldMapperTypeRetriever()
{
}

public FieldMapperTypeRetriever(ICultureValidator cultureValidator)
[Obsolete("use constructor without cultureValidator")]
public FieldMapperTypeRetriever(ICultureValidator cultureValidator) : this()
{
this._cultureValidator = cultureValidator;
}

public override Type GetTypeForToken(JsonElement element)
{
Type tokenType;
Expand All @@ -41,10 +45,8 @@ public override Type GetTypeForToken(JsonElement element)
tokenType = typeof(ITypedMoney);
else if (element.IsReferenceElement())
tokenType = typeof(IReference);
else if (element.IsLocalizedStringElement(_cultureValidator))
tokenType = typeof(LocalizedString);
else
tokenType = typeof(object);
tokenType = typeof(LocalizedString);
break;
case JsonValueKind.Array:
var valueKind = element.GetFirstArrayElementValueKind();
Expand Down

0 comments on commit cbfc114

Please sign in to comment.