Bukimedia / PrestaSharp

CSharp .Net client library for the PrestaShop API via web service
GNU General Public License v3.0
155 stars 152 forks source link

RestSharp DeserializableAs attribute not working as expected #319

Open castillapp opened 6 years ago

castillapp commented 6 years ago

If you want your PrestaSharp Entity Class to have another name than the ones used to build the xml request (ie in a custom class), you should be able to change it using RestSharp class attributes "SerializeAs" and "DeserializeAs" for the serialization and deserialization method.

In fact, SerializeAs works as expected but this does not happen with DeserializeAs.

If you tag an Entities class with DeserializeAs (Name = "yourCustomName") it does not get correctly interpreted by the PrestaSharpDeserializer class.

This happens because in the HandleListDerivative method there's missing code, and it should be something like:

Type t;

if (type.IsGenericType)
{
    t = type.GetGenericArguments()[0];
}
else
{
    t = type.BaseType.GetGenericArguments()[0];
}

var name = t.Name;
var options = t.GetAttribute<DeserializeAsAttribute>();
if (options != null)
{
    name = options.Name ?? name;
}

var list = (IList)Activator.CreateInstance(type);

//Modified version from RestSharp 
var elements = root.Elements(name.AsNamespaced(Namespace));

This way of doing it is already used in the PrestaSharpSerializer and I wonder why it is different in here.

Nevertheless I note that there's a comment that states "Modified version from RestSharp", which I don't know what it is intended to.

Please, note that RestSharp serializable attributes can also be applied to Entities properties. I haven't got the time to look if this is implemented into the PrestaSharp serializable methods yet.