Skip to content

Commit aaf613a

Browse files
committed
allow for tag replacement
1 parent 6ddd86d commit aaf613a

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### File Categories and Tabular Tags can now be replaced
2+
3+
Previously the API Post /files/{id}/metadata/tabularTags could only add new tags to the tabular tags list. Now with the query parameter ?replace=true the list of tags will be replaced.
4+

doc/sphinx-guides/source/api/native-api.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4605,6 +4605,8 @@ Updating File Tabular Tags
46054605
46064606
Updates the tabular tags for an existing tabular file where ``ID`` is the database id of the file to update or ``PERSISTENT_ID`` is the persistent id (DOI or Handle) of the file. Requires a ``jsonString`` expressing the tabular tag names.
46074607
4608+
The list of "tabularTags" will be added to the existing list unless the optional ``replace=true`` query parameter is included. The inclusion of this parameter will cause the pre-existing tags to be deleted and the "tabularTags" to be added. Sending an empty list will remove all of the pre-existing tags.
4609+
46084610
The JSON representation of tabular tags (``tags.json``) looks like this::
46094611
46104612
{
@@ -4634,6 +4636,9 @@ The fully expanded example above (without environment variables) looks like this
46344636
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
46354637
"http://demo.dataverse.org/api/files/24/metadata/tabularTags" \
46364638
-H "Content-type:application/json" --upload-file tags.json
4639+
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
4640+
"http://demo.dataverse.org/api/files/24/metadata/tabularTags?replace=true" \
4641+
-H "Content-type:application/json" --upload-file tags.json
46374642
46384643
A curl example using a ``PERSISTENT_ID``
46394644
@@ -4647,6 +4652,9 @@ A curl example using a ``PERSISTENT_ID``
46474652
curl -H "X-Dataverse-key:$API_TOKEN" -X POST \
46484653
"$SERVER_URL/api/files/:persistentId/metadata/tabularTags?persistentId=$PERSISTENT_ID" \
46494654
-H "Content-type:application/json" --upload-file $FILE_PATH
4655+
curl -H "X-Dataverse-key:$API_TOKEN" -X POST \
4656+
"$SERVER_URL/api/files/:persistentId/metadata/tabularTags?persistentId=$PERSISTENT_ID&replace=true" \
4657+
-H "Content-type:application/json" --upload-file $FILE_PATH
46504658
46514659
The fully expanded example above (without environment variables) looks like this:
46524660
@@ -4655,6 +4663,9 @@ The fully expanded example above (without environment variables) looks like this
46554663
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
46564664
"https://demo.dataverse.org/api/files/:persistentId/metadata/tabularTags?persistentId=doi:10.5072/FK2/AAA000" \
46574665
-H "Content-type:application/json" --upload-file tags.json
4666+
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
4667+
"https://demo.dataverse.org/api/files/:persistentId/metadata/tabularTags?persistentId=doi:10.5072/FK2/AAA000&replace=true" \
4668+
-H "Content-type:application/json" --upload-file tags.json
46584669
46594670
Note that the specified tabular tags must be valid. The supported tags are:
46604671

src/main/java/edu/harvard/iq/dataverse/api/Files.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.harvard.iq.dataverse.api;
22

3+
import com.google.api.client.util.Lists;
34
import com.google.gson.Gson;
45
import com.google.gson.JsonObject;
56
import edu.harvard.iq.dataverse.*;
@@ -918,7 +919,7 @@ public Response setFileCategories(@Context ContainerRequestContext crc, @PathPar
918919
@AuthRequired
919920
@Path("{id}/metadata/tabularTags")
920921
@Produces(MediaType.APPLICATION_JSON)
921-
public Response setFileTabularTags(@Context ContainerRequestContext crc, @PathParam("id") String dataFileId, String jsonBody) {
922+
public Response setFileTabularTags(@Context ContainerRequestContext crc, @PathParam("id") String dataFileId, String jsonBody, @QueryParam("replace") boolean replaceData) {
922923
return response(req -> {
923924
DataFile dataFile = execCommand(new GetDataFileCommand(req, findDataFileOrDie(dataFileId)));
924925
if (!dataFile.isTabularData()) {
@@ -928,6 +929,9 @@ public Response setFileTabularTags(@Context ContainerRequestContext crc, @PathPa
928929
try (StringReader stringReader = new StringReader(jsonBody)) {
929930
jsonObject = Json.createReader(stringReader).readObject();
930931
JsonArray requestedTabularTagsJson = jsonObject.getJsonArray("tabularTags");
932+
if (replaceData) {
933+
dataFile.setTags(Lists.newArrayList());
934+
}
931935
for (JsonValue jsonValue : requestedTabularTagsJson) {
932936
JsonString jsonString = (JsonString) jsonValue;
933937
try {

src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.logging.Logger;
99

1010
import edu.harvard.iq.dataverse.api.auth.ApiKeyAuthMechanism;
11+
import org.assertj.core.util.Lists;
1112
import org.junit.jupiter.api.Test;
1213
import org.junit.jupiter.api.BeforeAll;
1314
import io.restassured.path.json.JsonPath;
@@ -2717,6 +2718,21 @@ public void testSetFileTabularTags() throws InterruptedException {
27172718

27182719
setFileTabularTagsResponse = UtilIT.setFileTabularTags(nonTabularFileId, apiToken, List.of(testInvalidTabularTag));
27192720
setFileTabularTagsResponse.then().assertThat().statusCode(BAD_REQUEST.getStatusCode());
2721+
2722+
// Test set with replaceData = true to show that the list is replaced and not added to
2723+
setFileTabularTagsResponse = UtilIT.setFileTabularTags(tabularFileId, apiToken, List.of("Geospatial"), true);
2724+
setFileTabularTagsResponse.then().assertThat().statusCode(OK.getStatusCode());
2725+
getFileDataResponse = UtilIT.getFileData(tabularFileId, apiToken);
2726+
actualTabularTagsCount = getFileDataResponse.jsonPath().getList("data.dataFile.tabularTags").size();
2727+
assertEquals(1, actualTabularTagsCount);
2728+
// Test clear all tags by passing empty list
2729+
setFileTabularTagsResponse = UtilIT.setFileTabularTags(tabularFileId, apiToken, Lists.emptyList(), true);
2730+
setFileTabularTagsResponse.then().assertThat().statusCode(OK.getStatusCode());
2731+
getFileDataResponse = UtilIT.getFileData(tabularFileId, apiToken);
2732+
getFileDataResponse.prettyPrint();
2733+
getFileDataResponse.then().assertThat()
2734+
.body("data.dataFile", not(hasItem("tabularTags")))
2735+
.statusCode(OK.getStatusCode());
27202736
}
27212737

27222738
@Test

src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4191,17 +4191,21 @@ static Response setFileCategories(String dataFileId, String apiToken, List<Strin
41914191
}
41924192

41934193
static Response setFileTabularTags(String dataFileId, String apiToken, List<String> tabularTags) {
4194+
return setFileTabularTags(dataFileId, apiToken, tabularTags, null);
4195+
}
4196+
static Response setFileTabularTags(String dataFileId, String apiToken, List<String> tabularTags, Boolean replaceData) {
41944197
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
41954198
for (String tabularTag : tabularTags) {
41964199
jsonArrayBuilder.add(tabularTag);
41974200
}
4201+
String replace = replaceData != null ? "?replace=" + replaceData : "";
41984202
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
41994203
jsonObjectBuilder.add("tabularTags", jsonArrayBuilder);
42004204
String jsonString = jsonObjectBuilder.build().toString();
42014205
return given()
42024206
.header(API_TOKEN_HTTP_HEADER, apiToken)
42034207
.body(jsonString)
4204-
.post("/api/files/" + dataFileId + "/metadata/tabularTags");
4208+
.post("/api/files/" + dataFileId + "/metadata/tabularTags" + replace);
42054209
}
42064210

42074211
static Response deleteFileInDataset(Integer fileId, String apiToken) {

0 commit comments

Comments
 (0)