Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Correct addmetadata item PUT rest endpoint for header 'content-type: …
Browse files Browse the repository at this point in the history
…application/json'
  • Loading branch information
mgrauer committed Dec 10, 2015
1 parent 8c53547 commit e3277af
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
16 changes: 4 additions & 12 deletions core/controllers/components/ApiitemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public function itemSetmultiplemetadata($args)
* @path /item/addmetadata/{id}
* @http PUT
* @param id The id of the item
* @param metadata The metadata list to add or update, should be passed in the request body
* as 'application/json'.
* @param revision (Optional) Item Revision number to set metadata on, defaults to latest revision.
* @return item on success,
* will fail if there are no revisions or the specified revision is not found.
Expand All @@ -190,7 +192,7 @@ public function itemAddmetadata($args)
{
/** @var ApihelperComponent $apihelperComponent */
$apihelperComponent = MidasLoader::loadComponent('Apihelper');
$apihelperComponent->validateParams($args, array('id'));
$apihelperComponent->validateParams($args, array('id', 'metadata'));
$userDao = $apihelperComponent->getUser($args);

$apihelperComponent->requirePolicyScopes(array(MIDAS_API_PERMISSION_SCOPE_WRITE_DATA));
Expand All @@ -204,17 +206,7 @@ public function itemAddmetadata($args)
$revisionNumber = array_key_exists('revision', $args) ? (int) $args['revision'] : null;
$revision = $apihelperComponent->getItemRevision($item, $revisionNumber);

if (!array_key_exists(0, $args)) {
throw new Exception('Missing request body data.', MIDAS_INVALID_PARAMETER);
}
$jsonBody = json_decode($args[0]);
if ($jsonBody === null) {
throw new Exception('Request body data must be valid JSON.', MIDAS_INVALID_PARAMETER);
}
if (!array_key_exists('metadata', $jsonBody)) {
throw new Exception("Request body data missing key 'metadata'.", MIDAS_INVALID_PARAMETER);
}
$metadata = $jsonBody->metadata;
$metadata = $args['metadata'];
foreach ($metadata as $metadatum) {
if (!isset($metadatum->element) || !isset($metadatum->value)) {
throw new Exception("All metadata must have 'element' and 'value' keys.", MIDAS_INVALID_PARAMETER);
Expand Down
8 changes: 6 additions & 2 deletions core/tests/controllers/api/RestCallItemMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public function testItemAddmetadata()
// Write some metadata correctly.
$this->resetAll();
$this->params['useSession'] = 'true';
$this->params[0] = json_encode(array('metadata' => array(
$metadata = json_encode(array('metadata' => array(
array('element' => 'key1', 'value' => 'val1'),
array('element' => 'key2', 'value' => 'val2'),
)));
$this->request->setHeader('Content-Type', 'application/json');
$this->request->setRawBody($metadata);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusOk($resp);

Expand All @@ -104,10 +106,12 @@ public function testItemAddmetadata()
// Update key1, add key3, leave key2 alone.
$this->resetAll();
$this->params['useSession'] = 'true';
$this->params[0] = json_encode(array('metadata' => array(
$metadata = json_encode(array('metadata' => array(
array('element' => 'key1', 'value' => 'newval1'),
array('element' => 'key3', 'value' => 'val3'),
)));
$this->request->setHeader('Content-Type', 'application/json');
$this->request->setRawBody($metadata);
$resp = $this->_callRestApi('PUT', $apiPath, $userDao);
$this->_assertStatusOk($resp);

Expand Down
2 changes: 2 additions & 0 deletions modules/api/controllers/components/ApiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,8 @@ public function itemSetmultiplemetadata($args)
*
* @param token Authentication token
* @param itemid The id of the item
* @param metadata The metadata list to add or update, should be passed in the request body
* as 'application/json'.
* @param revision (Optional) Item Revision number to set metadata on, defaults to latest revision.
* @return item on success,
* will fail if there are no revisions or the specified revision is not found.
Expand Down

0 comments on commit e3277af

Please sign in to comment.