- add test, bugfix
authorMartin Schanzenbach <mschanzenbach@posteo.de>
Thu, 5 May 2016 10:35:48 +0000 (10:35 +0000)
committerMartin Schanzenbach <mschanzenbach@posteo.de>
Thu, 5 May 2016 10:35:48 +0000 (10:35 +0000)
src/jsonapi/jsonapi_document.c
src/jsonapi/jsonapi_error.c
src/jsonapi/test_jsonapi.c

index b99a7a4fef76c57ecbf0c27618dbccbc29d5406d..faf0bd83109134d8b7e7a0f75229862c96cd9977 100644 (file)
@@ -53,7 +53,20 @@ GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *doc)
 {
   struct GNUNET_JSONAPI_Resource *res;
   struct GNUNET_JSONAPI_Resource *res_next;
-  
+  struct GNUNET_JSONAPI_Error *err;
+  struct GNUNET_JSONAPI_Error *err_next;
+
+
+  for (err = doc->err_list_head;
+       err != NULL;)
+  {
+    err_next = err->next;
+    GNUNET_CONTAINER_DLL_remove (doc->err_list_head,
+                                 doc->err_list_tail,
+                                 err);
+    GNUNET_JSONAPI_error_delete (err);
+    err = err_next;
+  }
 
   for (res = doc->res_list_head;
        res != NULL;)
@@ -65,6 +78,9 @@ GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *doc)
     GNUNET_JSONAPI_resource_delete (res);
     res = res_next;
   }
+
+  if (NULL != doc->meta)
+    json_decref (doc->meta);
   GNUNET_free (doc);
   doc = NULL;
 }
@@ -95,7 +111,7 @@ GNUNET_JSONAPI_document_new ()
  */
 void
 GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
-                                      struct GNUNET_JSONAPI_Error *err)
+                                   struct GNUNET_JSONAPI_Error *err)
 {
   GNUNET_CONTAINER_DLL_insert (doc->err_list_head,
                                doc->err_list_tail,
@@ -113,7 +129,7 @@ GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
  */
 void
 GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *doc,
-                                         struct GNUNET_JSONAPI_Resource *res)
+                                      struct GNUNET_JSONAPI_Resource *res)
 {
   GNUNET_CONTAINER_DLL_insert (doc->res_list_head,
                                doc->res_list_tail,
@@ -310,7 +326,7 @@ GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
       GNUNET_assert (GNUNET_OK ==
                      GNUNET_JSONAPI_error_to_json (error,
                                                    &res_json_tmp));
-      json_array_append (res_json, res_json_tmp);
+      json_array_append_new (res_json, res_json_tmp);
     }
     json_object_set_new (*root_json,
                          GNUNET_JSONAPI_KEY_ERRORS,
index b7fc08d72d1f835e65708b64af3bb066bcb90148..8ce71d26f90a56f48ca0db6b85e999aad03cd5d2 100644 (file)
@@ -149,22 +149,22 @@ GNUNET_JSONAPI_error_new (const char *id,
   struct GNUNET_JSONAPI_Error *error;
   error = GNUNET_new (struct GNUNET_JSONAPI_Error);
 
-  GNUNET_assert (NULL != id);
-  error->id = GNUNET_strdup (id);
-  GNUNET_assert (NULL != status);
-  error->status = GNUNET_strdup (status);
-  GNUNET_assert (NULL != code);
-  error->code = GNUNET_strdup (code);
-  GNUNET_assert (NULL != title);
-  error->title = GNUNET_strdup (title);
-  GNUNET_assert (NULL != detail);
-  error->detail = GNUNET_strdup (detail);
-  GNUNET_assert (NULL != links);
-  error->links = json_deep_copy (links);
-  GNUNET_assert (NULL != source);
-  error->source = json_deep_copy (source);
-  GNUNET_assert (NULL != meta);
-  error->meta = json_deep_copy (meta);
+  if (NULL != id)
+    error->id = GNUNET_strdup (id);
+  if (NULL != status)
+    error->status = GNUNET_strdup (status);
+  if (NULL != code)
+    error->code = GNUNET_strdup (code);
+  if (NULL != title)
+    error->title = GNUNET_strdup (title);
+  if (NULL != detail)
+    error->detail = GNUNET_strdup (detail);
+  if (NULL != links)
+    error->links = json_deep_copy (links);
+  if (NULL != source)
+    error->source = json_deep_copy (source);
+  if (NULL != meta)
+    error->meta = json_deep_copy (meta);
   return error;
 }
 /**
index b5f4d4cba95a2bc6c4c1f204bfc503e6ed227486..379dab9af52ebbebf714b938ddb61ef005c575ab 100644 (file)
 
 #define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}"
 
+#define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
+
+static int
+test_document_error ()
+{
+  struct GNUNET_JSONAPI_Document *obj;
+  struct GNUNET_JSONAPI_Error *error;
+  json_t *doc_json;
+  json_t *data_js;
+  json_error_t err;
+
+  obj = GNUNET_JSONAPI_document_new ();
+  error = GNUNET_JSONAPI_error_new ("1",
+                                    "403",
+                                    "23",
+                                    "Error",
+                                    "Error details",
+                                    NULL,
+                                    NULL,
+                                    NULL);
+
+
+  GNUNET_JSONAPI_document_error_add (obj,
+                                     error);
+
+  GNUNET_assert (GNUNET_OK == 
+                 GNUNET_JSONAPI_document_to_json (obj,
+                                                  &doc_json));
+  data_js = json_loads (TEST_JSONAPI_DOCUMENT_ERR,
+                        JSON_DECODE_ANY,
+                        &err);
+  GNUNET_assert (NULL != data_js);
+  GNUNET_assert (0 != json_equal (data_js, doc_json));
+  GNUNET_JSONAPI_document_delete (obj);
+  json_decref (data_js);
+  json_decref (doc_json);
+  return 0;
+}
+
+
 static int
 test_document ()
 {
@@ -38,7 +78,7 @@ test_document ()
   obj = GNUNET_JSONAPI_document_new ();
   res = GNUNET_JSONAPI_resource_new ("bar",
                                      "1");
-  
+
   GNUNET_assert (GNUNET_OK == 
                  GNUNET_JSONAPI_resource_add_attr (res,
                                                    "foo",
@@ -138,6 +178,8 @@ main(int argc,
     return 1;
   if (0 != test_document ())
     return 1;
+  if (0 != test_document_error ())
+    return 1;
   return 0;
 }