{
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;)
GNUNET_JSONAPI_resource_delete (res);
res = res_next;
}
+
+ if (NULL != doc->meta)
+ json_decref (doc->meta);
GNUNET_free (doc);
doc = NULL;
}
*/
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,
*/
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,
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,
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;
}
/**
#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 ()
{
obj = GNUNET_JSONAPI_document_new ();
res = GNUNET_JSONAPI_resource_new ("bar",
"1");
-
+
GNUNET_assert (GNUNET_OK ==
GNUNET_JSONAPI_resource_add_attr (res,
"foo",
return 1;
if (0 != test_document ())
return 1;
+ if (0 != test_document_error ())
+ return 1;
return 0;
}