=?utf-8?q?=5Blibgdata=5D_Bug_662290_=E2=80=94_Can=27t_update_contact_that?= =?utf-8?q?_has_no_full_name?=



commit 2c7d5579bb82658a2df04dd1e209d9d73d5195ee
Author: Philip Withnall <philip tecnocode co uk>
Date:   Thu Oct 20 17:19:25 2011 +0100

    Bug 662290 â Can't update contact that has no full name
    
    Treat the empty string as if it were NULL for GDataGDName:full-name. Add a
    test case which does so too.
    
    This should fix round-trips to Google for contacts which have no full name
    set.
    
    Closes: bgo#662290

 gdata/gd/gdata-gd-name.c |   12 +++++++++++-
 gdata/tests/general.c    |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletions(-)
---
diff --git a/gdata/gd/gdata-gd-name.c b/gdata/gd/gdata-gd-name.c
index 8300f01..7da9993 100644
--- a/gdata/gd/gdata-gd-name.c
+++ b/gdata/gd/gdata-gd-name.c
@@ -326,7 +326,11 @@ get_xml (GDataParsable *parsable, GString *xml_string)
 	OUTPUT_STRING_ELEMENT ("familyName", family_name)
 	OUTPUT_STRING_ELEMENT ("namePrefix", prefix)
 	OUTPUT_STRING_ELEMENT ("nameSuffix", suffix)
-	OUTPUT_STRING_ELEMENT ("fullName", full_name)
+
+	/* We can't guarantee that priv->full_name is non-empty without breaking API. */
+	if (priv->full_name != NULL && *(priv->full_name) != '\0') {
+		gdata_parser_string_append_escaped (xml_string, "<gd:fullName>", priv->full_name, "</gd:fullName>");
+	}
 
 #undef OUTPUT_STRING_ELEMENT
 }
@@ -573,6 +577,12 @@ gdata_gd_name_set_full_name (GDataGDName *self, const gchar *full_name)
 {
 	g_return_if_fail (GDATA_IS_GD_NAME (self));
 
+	/* Coerce empty strings to NULL. Ideally, we should have this as a precondition (as all the other setters in GDataGDName have) but that would
+	 * break API. */
+	if (full_name != NULL && *full_name == '\0') {
+		full_name = NULL;
+	}
+
 	g_free (self->priv->full_name);
 	self->priv->full_name = g_strdup (full_name);
 	g_object_notify (G_OBJECT (self), "full-name");
diff --git a/gdata/tests/general.c b/gdata/tests/general.c
index c0b2e18..826c5c1 100644
--- a/gdata/tests/general.c
+++ b/gdata/tests/general.c
@@ -1995,6 +1995,50 @@ test_gd_name (void)
 }
 
 static void
+test_gd_name_empty_strings (void)
+{
+	GDataGDName *name;
+	GError *error = NULL;
+
+	g_test_bug ("662290");
+
+	/* Test that empty full names get treated as NULL correctly. */
+	name = GDATA_GD_NAME (gdata_parsable_new_from_xml (GDATA_TYPE_GD_NAME,
+		"<gd:name xmlns:gd='http://schemas.google.com/g/2005'>"
+			"<gd:fullName></gd:fullName>"
+		"</gd:name>", -1, &error));
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_GD_NAME (name));
+	g_clear_error (&error);
+
+	/* Check the properties */
+	g_assert_cmpstr (gdata_gd_name_get_given_name (name), ==, NULL);
+	g_assert_cmpstr (gdata_gd_name_get_additional_name (name), ==, NULL);
+	g_assert_cmpstr (gdata_gd_name_get_family_name (name), ==, NULL);
+	g_assert_cmpstr (gdata_gd_name_get_prefix (name), ==, NULL);
+	g_assert_cmpstr (gdata_gd_name_get_suffix (name), ==, NULL);
+	g_assert_cmpstr (gdata_gd_name_get_full_name (name), ==, NULL);
+
+	g_object_unref (name);
+
+	/* Build a name with an empty string full name and check the serialisation */
+	name = gdata_gd_name_new ("Georgey", "Porgey");
+	gdata_gd_name_set_full_name (name, "");
+
+	g_assert_cmpstr (gdata_gd_name_get_full_name (name), ==, NULL);
+
+	/* Check the outputted XML is the same */
+	gdata_test_assert_xml (name,
+		"<?xml version='1.0' encoding='UTF-8'?>"
+		"<gd:name xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>"
+			"<gd:givenName>Georgey</gd:givenName>"
+			"<gd:familyName>Porgey</gd:familyName>"
+		"</gd:name>");
+
+	g_object_unref (name);
+}
+
+static void
 test_gd_organization (void)
 {
 	GDataGDOrganization *org, *org2;
@@ -3746,6 +3790,7 @@ main (int argc, char *argv[])
 	g_test_add_func ("/gd/im_address", test_gd_im_address);
 	g_test_add_func ("/gd/im_address/escaping", test_gd_im_address_escaping);
 	g_test_add_func ("/gd/name", test_gd_name);
+	g_test_add_func ("/gd/name/empty_strings", test_gd_name_empty_strings);
 	g_test_add_func ("/gd/organization", test_gd_organization);
 	g_test_add_func ("/gd/organization/escaping", test_gd_organization_escaping);
 	g_test_add_func ("/gd/phone_number", test_gd_phone_number);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]