titobrasolin / Drupal7.Services

.NET API to integrate with Drupal 7 via Services 3 module.
MIT License
7 stars 3 forks source link

How to handle empty fields value #15

Closed shafiqhossain closed 8 years ago

shafiqhossain commented 8 years ago

Some of the Drupal fields has empty value. It returns fields like this:

field_fields_text_field : [Object[]]: hashcode { 34181910 }{}
field_fields_boolean : [XmlRpcStruct]: hashcode { 39201736 }
{
    und : [XmlRpcStruct[]]: hashcode { 17271312 }
    {
        1
    }
}
field_fields_term_reference : [Object[]]: hashcode { 21224086 }{}
field_fields_integer : [Object[]]: hashcode { 56799051 }{}
field_fields_entity_reference : [Object[]]: hashcode { 41429416 }{}

Empty fields: field_fields_term_reference, field_fields_text_field, field_fields_integer.. While converting to the node type it gives the following error:

Exception thrown: 'System.InvalidOperationException' in System.Web.Extensions.dll An unhandled exception of type 'System.InvalidOperationException' occurred in System.Web.Extensions.dll Additional information: Type 'Drupal7.Services.BaseClasses.fields_term_reference' is not supported for deserialization of an array.

How to handle this ?

titobrasolin commented 8 years ago

Good point @shafiqhossain : the XMLRPC server returns a struct ("<member><name>field_tags</name><value><struct><member><name>en</name><value><array>...") when the field is filled, an empty array ("<member><name>field_tags</name><value><array><data></data></array></value></member>") when it's not and the JavaScriptSerializer can't handle both.

Unfortunately I don't have a good solution for now, as possible workaround I would maybe define the field as an object:

class Article : BaseNode
{
    public object field_tags { get; set; }
}
shafiqhossain commented 8 years ago

thanks for the suggestion, what i did for now: node.Remove("field_fields_term_reference"); to remove the key which has empty fields. This way conversion giving no error. Not sure if this is the right way or not.

Struggling to update the text field and entity reference field, which is not working for me. Going to post another issue. Thanks