Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove duplicate DatasetFieldTypes #11066

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Json Printer Bug fix

DatasetFieldTypes in MetadataBlock response that are also a child of another DatasetFieldType were being returned twice. The child DatasetFieldType was included in the "fields" object as well as in the "childFields" of it's parent DatasetFieldType. This fix suppresses the standalone object so only one instance of the DatasetFieldType is returned (in the "childFields" of its parent).
This fix changes the Json output of the API `/api/dataverses/{dataverseAlias}/metadatablocks`

## Backward Incompatible Changes

The Json response of API call `/api/dataverses/{dataverseAlias}/metadatablocks` will no longer include the DatasetFieldTypes in "fields" if they are children of another DatasetFieldType. The child DatasetFieldType will only be included in the "childFields" of it's parent DatasetFieldType.
25 changes: 19 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -642,17 +642,19 @@ public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printO
.add("displayName", metadataBlock.getDisplayName())
.add("displayOnCreate", metadataBlock.isDisplayOnCreate());

Set<DatasetFieldType> datasetFieldTypes;
List<DatasetFieldType> datasetFieldTypesList;

if (ownerDataverse != null) {
datasetFieldTypes = new TreeSet<>(datasetFieldService.findAllInMetadataBlockAndDataverse(
metadataBlock, ownerDataverse, printOnlyDisplayedOnCreateDatasetFieldTypes));
datasetFieldTypesList = datasetFieldService.findAllInMetadataBlockAndDataverse(
metadataBlock, ownerDataverse, printOnlyDisplayedOnCreateDatasetFieldTypes);
} else {
datasetFieldTypes = printOnlyDisplayedOnCreateDatasetFieldTypes
? new TreeSet<>(datasetFieldService.findAllDisplayedOnCreateInMetadataBlock(metadataBlock))
: new TreeSet<>(metadataBlock.getDatasetFieldTypes());
datasetFieldTypesList = printOnlyDisplayedOnCreateDatasetFieldTypes
? datasetFieldService.findAllDisplayedOnCreateInMetadataBlock(metadataBlock)
: metadataBlock.getDatasetFieldTypes();
}

Set<DatasetFieldType> datasetFieldTypes = filterOutDuplicateDatasetFieldTypes(datasetFieldTypesList);

JsonObjectBuilder fieldsBuilder = Json.createObjectBuilder();
for (DatasetFieldType datasetFieldType : datasetFieldTypes) {
fieldsBuilder.add(datasetFieldType.getName(), json(datasetFieldType, ownerDataverse));
Expand All @@ -662,6 +664,17 @@ public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printO
return jsonObjectBuilder;
}

// This will remove datasetFieldTypes that are in the list but also a child of another datasetFieldType in the list
// Prevents duplicate datasetFieldType information from being returned twice
// See: https://github.com/IQSS/dataverse/issues/10472
private static Set<DatasetFieldType> filterOutDuplicateDatasetFieldTypes(List<DatasetFieldType> datasetFieldTypesList) {
// making a copy of the list as to not damage the original when we remove items
List<DatasetFieldType> datasetFieldTypes = new ArrayList<>(datasetFieldTypesList);
// exclude/remove datasetFieldTypes if datasetFieldType exists as a child of another datasetFieldType
datasetFieldTypesList.forEach(dsft -> dsft.getChildDatasetFieldTypes().forEach(c -> datasetFieldTypes.remove(c)));
return new TreeSet<>(datasetFieldTypes);
}

public static JsonArrayBuilder jsonDatasetFieldTypes(List<DatasetFieldType> fields) {
JsonArrayBuilder fieldsJson = Json.createArrayBuilder();
for (DatasetFieldType field : fields) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/DataversesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ public void testListMetadataBlocks() {
.body("data.size()", equalTo(1))
.body("data[0].name", is("citation"))
.body("data[0].fields.title.displayOnCreate", equalTo(true))
.body("data[0].fields.size()", is(28));
.body("data[0].fields.size()", is(10)); // 28 - 18 child duplicates

Response setMetadataBlocksResponse = UtilIT.setMetadataBlocks(dataverseAlias, Json.createArrayBuilder().add("citation").add("astrophysics"), apiToken);
setMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode());
Expand Down Expand Up @@ -1007,11 +1007,11 @@ public void testListMetadataBlocks() {
// Since the included property of notesText is set to false, we should retrieve the total number of fields minus one
int citationMetadataBlockIndex = geospatialMetadataBlockIndex == 0 ? 1 : 0;
listMetadataBlocksResponse.then().assertThat()
.body(String.format("data[%d].fields.size()", citationMetadataBlockIndex), equalTo(79));
.body(String.format("data[%d].fields.size()", citationMetadataBlockIndex), equalTo(34)); // 79 minus 45 child duplicates

// Since the included property of geographicCoverage is set to false, we should retrieve the total number of fields minus one
listMetadataBlocksResponse.then().assertThat()
.body(String.format("data[%d].fields.size()", geospatialMetadataBlockIndex), equalTo(10));
.body(String.format("data[%d].fields.size()", geospatialMetadataBlockIndex), equalTo(6)); // 10 - 4 child duplicates

String actualGeospatialMetadataField1 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.geographicCoverage.name", geospatialMetadataBlockIndex));
String actualGeospatialMetadataField2 = listMetadataBlocksResponse.then().extract().path(String.format("data[%d].fields.country.name", geospatialMetadataBlockIndex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void testListMetadataBlocks() {

// returnDatasetFieldTypes=true
listMetadataBlocksResponse = UtilIT.listMetadataBlocks(false, true);
int expectedNumberOfMetadataFields = 80;
int expectedNumberOfMetadataFields = 35; // 80 - 45 child duplicates;
listMetadataBlocksResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].fields", not(equalTo(null)))
Expand All @@ -51,7 +51,8 @@ void testListMetadataBlocks() {

// onlyDisplayedOnCreate=true and returnDatasetFieldTypes=true
listMetadataBlocksResponse = UtilIT.listMetadataBlocks(true, true);
expectedNumberOfMetadataFields = 28;
listMetadataBlocksResponse.prettyPrint();
expectedNumberOfMetadataFields = 10; // 28 - 18 child duplicates
listMetadataBlocksResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].fields", not(equalTo(null)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.json.JsonString;

import edu.harvard.iq.dataverse.util.BundleUtil;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;

Expand Down Expand Up @@ -268,6 +269,54 @@ public void testDatasetContactWithPrivacy() {

}

@Test
public void testDatasetFieldTypesWithChildren() {
MetadataBlock block = new MetadataBlock();
block.setId(0L);
block.setName("citation");
long id = 0L;
// create datasetFieldTypes
List<DatasetFieldType> datasetFieldTypes = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DatasetFieldType dft = new DatasetFieldType();
dft.setId(id++);
dft.setDisplayOrder(i);
dft.setMetadataBlock(block);
dft.setFieldType(FieldType.TEXT);
dft.setName("subType" + dft.getId());
dft.setTitle(dft.getName());
dft.setChildDatasetFieldTypes(Lists.emptyList());
datasetFieldTypes.add(dft);
}
// add DatasetFieldType as children to another DatasetFieldType to test the suppression of duplicate data
// adding 3 and 4 as children of 2
datasetFieldTypes.get(3).setParentDatasetFieldType(datasetFieldTypes.get(2));
datasetFieldTypes.get(4).setParentDatasetFieldType(datasetFieldTypes.get(2));
datasetFieldTypes.get(2).setChildDatasetFieldTypes(List.of(datasetFieldTypes.get(3), datasetFieldTypes.get(4)));
// adding 6 as child of 9
datasetFieldTypes.get(6).setParentDatasetFieldType(datasetFieldTypes.get(9));
datasetFieldTypes.get(9).setChildDatasetFieldTypes(List.of(datasetFieldTypes.get(6)));

block.setDatasetFieldTypes(datasetFieldTypes);

DatasetFieldServiceBean nullDFServiceBean = null;
JsonPrinter.injectSettingsService(new MockSettingsSvc(), nullDFServiceBean);

JsonObject jsonObject = JsonPrinter.json(block).build();
assertNotNull(jsonObject);

System.out.println("json: " + JsonUtil.prettyPrint(jsonObject.toString()));
assertEquals("subType2 subType3", jsonObject.getJsonObject("fields").getJsonObject("subType2")
.getJsonObject("childFields").getJsonObject("subType3").getString("displayName"));
assertEquals("subType2 subType4", jsonObject.getJsonObject("fields").getJsonObject("subType2")
.getJsonObject("childFields").getJsonObject("subType4").getString("displayName"));
assertEquals("subType9 subType6", jsonObject.getJsonObject("fields").getJsonObject("subType9")
.getJsonObject("childFields").getJsonObject("subType6").getString("displayName"));
assertNull(jsonObject.getJsonObject("fields").getJsonObject("subType3"));
assertNull(jsonObject.getJsonObject("fields").getJsonObject("subType4"));
assertNull(jsonObject.getJsonObject("fields").getJsonObject("subType6"));
}

@Test
public void testDataversePrinter() {
Dataverse dataverse = new Dataverse();
Expand Down
Loading