Skip to content

Commit 9c2a8bf

Browse files
authored
Merge pull request #11359 from IQSS/11292-update-tabular-tags-with-replace
allow for tag replacement
2 parents 86176f5 + d519a08 commit 9c2a8bf

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### 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+
5+
See also [the guides](https://dataverse-guide--11359.org.readthedocs.build/en/11359/api/native-api.html#updating-file-tabular-tags), #11292, and #11359.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4669,6 +4669,8 @@ Updating File Tabular Tags
46694669
46704670
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.
46714671
4672+
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.
4673+
46724674
The JSON representation of tabular tags (``tags.json``) looks like this::
46734675
46744676
{
@@ -4698,6 +4700,9 @@ The fully expanded example above (without environment variables) looks like this
46984700
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
46994701
"http://demo.dataverse.org/api/files/24/metadata/tabularTags" \
47004702
-H "Content-type:application/json" --upload-file tags.json
4703+
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
4704+
"http://demo.dataverse.org/api/files/24/metadata/tabularTags?replace=true" \
4705+
-H "Content-type:application/json" --upload-file tags.json
47014706
47024707
A curl example using a ``PERSISTENT_ID``
47034708
@@ -4711,6 +4716,9 @@ A curl example using a ``PERSISTENT_ID``
47114716
curl -H "X-Dataverse-key:$API_TOKEN" -X POST \
47124717
"$SERVER_URL/api/files/:persistentId/metadata/tabularTags?persistentId=$PERSISTENT_ID" \
47134718
-H "Content-type:application/json" --upload-file $FILE_PATH
4719+
curl -H "X-Dataverse-key:$API_TOKEN" -X POST \
4720+
"$SERVER_URL/api/files/:persistentId/metadata/tabularTags?persistentId=$PERSISTENT_ID&replace=true" \
4721+
-H "Content-type:application/json" --upload-file $FILE_PATH
47144722
47154723
The fully expanded example above (without environment variables) looks like this:
47164724
@@ -4719,6 +4727,9 @@ The fully expanded example above (without environment variables) looks like this
47194727
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
47204728
"https://demo.dataverse.org/api/files/:persistentId/metadata/tabularTags?persistentId=doi:10.5072/FK2/AAA000" \
47214729
-H "Content-type:application/json" --upload-file tags.json
4730+
curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X POST \
4731+
"https://demo.dataverse.org/api/files/:persistentId/metadata/tabularTags?persistentId=doi:10.5072/FK2/AAA000&replace=true" \
4732+
-H "Content-type:application/json" --upload-file tags.json
47224733
47234734
Note that the specified tabular tags must be valid. The supported tags are:
47244735

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.*;
@@ -947,7 +948,7 @@ public Response setFileCategories(@Context ContainerRequestContext crc, @PathPar
947948
@AuthRequired
948949
@Path("{id}/metadata/tabularTags")
949950
@Produces(MediaType.APPLICATION_JSON)
950-
public Response setFileTabularTags(@Context ContainerRequestContext crc, @PathParam("id") String dataFileId, String jsonBody) {
951+
public Response setFileTabularTags(@Context ContainerRequestContext crc, @PathParam("id") String dataFileId, String jsonBody, @QueryParam("replace") boolean replaceData) {
951952
return response(req -> {
952953
DataFile dataFile = execCommand(new GetDataFileCommand(req, findDataFileOrDie(dataFileId)));
953954
if (!dataFile.isTabularData()) {
@@ -957,6 +958,9 @@ public Response setFileTabularTags(@Context ContainerRequestContext crc, @PathPa
957958
try (StringReader stringReader = new StringReader(jsonBody)) {
958959
jsonObject = Json.createReader(stringReader).readObject();
959960
JsonArray requestedTabularTagsJson = jsonObject.getJsonArray("tabularTags");
961+
if (replaceData) {
962+
dataFile.setTags(Lists.newArrayList());
963+
}
960964
for (JsonValue jsonValue : requestedTabularTagsJson) {
961965
JsonString jsonString = (JsonString) jsonValue;
962966
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;
@@ -2740,6 +2741,21 @@ public void testSetFileTabularTags() throws InterruptedException {
27402741

27412742
setFileTabularTagsResponse = UtilIT.setFileTabularTags(nonTabularFileId, apiToken, List.of(testInvalidTabularTag));
27422743
setFileTabularTagsResponse.then().assertThat().statusCode(BAD_REQUEST.getStatusCode());
2744+
2745+
// Test set with replaceData = true to show that the list is replaced and not added to
2746+
setFileTabularTagsResponse = UtilIT.setFileTabularTags(tabularFileId, apiToken, List.of("Geospatial"), true);
2747+
setFileTabularTagsResponse.then().assertThat().statusCode(OK.getStatusCode());
2748+
getFileDataResponse = UtilIT.getFileData(tabularFileId, apiToken);
2749+
actualTabularTagsCount = getFileDataResponse.jsonPath().getList("data.dataFile.tabularTags").size();
2750+
assertEquals(1, actualTabularTagsCount);
2751+
// Test clear all tags by passing empty list
2752+
setFileTabularTagsResponse = UtilIT.setFileTabularTags(tabularFileId, apiToken, Lists.emptyList(), true);
2753+
setFileTabularTagsResponse.then().assertThat().statusCode(OK.getStatusCode());
2754+
getFileDataResponse = UtilIT.getFileData(tabularFileId, apiToken);
2755+
getFileDataResponse.prettyPrint();
2756+
getFileDataResponse.then().assertThat()
2757+
.body("data.dataFile", not(hasItem("tabularTags")))
2758+
.statusCode(OK.getStatusCode());
27432759
}
27442760

27452761
@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
@@ -4234,17 +4234,21 @@ static Response setFileCategories(String dataFileId, String apiToken, List<Strin
42344234
}
42354235

42364236
static Response setFileTabularTags(String dataFileId, String apiToken, List<String> tabularTags) {
4237+
return setFileTabularTags(dataFileId, apiToken, tabularTags, null);
4238+
}
4239+
static Response setFileTabularTags(String dataFileId, String apiToken, List<String> tabularTags, Boolean replaceData) {
42374240
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
42384241
for (String tabularTag : tabularTags) {
42394242
jsonArrayBuilder.add(tabularTag);
42404243
}
4244+
String replace = replaceData != null ? "?replace=" + replaceData : "";
42414245
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
42424246
jsonObjectBuilder.add("tabularTags", jsonArrayBuilder);
42434247
String jsonString = jsonObjectBuilder.build().toString();
42444248
return given()
42454249
.header(API_TOKEN_HTTP_HEADER, apiToken)
42464250
.body(jsonString)
4247-
.post("/api/files/" + dataFileId + "/metadata/tabularTags");
4251+
.post("/api/files/" + dataFileId + "/metadata/tabularTags" + replace);
42484252
}
42494253

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

0 commit comments

Comments
 (0)