Skip to content

Commit

Permalink
Merge pull request IQSS#11168 from vera/edit-metadata-api-500-error
Browse files Browse the repository at this point in the history
Fix 500 errors when uploading single controlled vocab or compound field values to editMetadata API
  • Loading branch information
ofahimIQSS authored Jan 27, 2025
2 parents a789342 + 69b9a01 commit 7283dbd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"fields": [
{
"typeName": "targetSampleSize",
"value": {
"targetSampleActualSize": {
"typeName": "targetSampleSizeFormula",
"value": "n = N*X / (X + N – 1)"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typeName": "journalArticleType",
"value": "abstract"
}
10 changes: 8 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -1187,8 +1187,14 @@ private String validateDatasetFieldValues(List<DatasetField> fields) {
if (dsf.getDatasetFieldType().isAllowMultiples() && dsf.getControlledVocabularyValues().isEmpty()
&& dsf.getDatasetFieldCompoundValues().isEmpty() && dsf.getDatasetFieldValues().isEmpty()) {
error.append("Empty multiple value for field: ").append(dsf.getDatasetFieldType().getDisplayName()).append(" ");
} else if (!dsf.getDatasetFieldType().isAllowMultiples() && dsf.getSingleValue().getValue().isEmpty()) {
error.append("Empty value for field: ").append(dsf.getDatasetFieldType().getDisplayName()).append(" ");
} else if (!dsf.getDatasetFieldType().isAllowMultiples()) {
if (dsf.getDatasetFieldType().isControlledVocabulary() && dsf.getSingleControlledVocabularyValue().getStrValue().isEmpty()) {
error.append("Empty cvoc value for field: ").append(dsf.getDatasetFieldType().getDisplayName()).append(" ");
} else if (dsf.getDatasetFieldType().isCompound() && dsf.getDatasetFieldCompoundValues().isEmpty()) {
error.append("Empty compound value for field: ").append(dsf.getDatasetFieldType().getDisplayName()).append(" ");
} else if (!dsf.getDatasetFieldType().isControlledVocabulary() && !dsf.getDatasetFieldType().isCompound() && dsf.getSingleValue().getValue().isEmpty()) {
error.append("Empty value for field: ").append(dsf.getDatasetFieldType().getDisplayName()).append(" ");
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonArrayBuilder;
import jakarta.ws.rs.core.Response.Status;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -368,6 +369,14 @@ public void testAddUpdateDatasetViaNativeAPI() {
createDataverseResponse.prettyPrint();
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);

JsonArrayBuilder metadataBlocks = Json.createArrayBuilder();
metadataBlocks.add("citation");
metadataBlocks.add("journal");
metadataBlocks.add("socialscience");
Response setMetadataBlocksResponse = UtilIT.setMetadataBlocks(dataverseAlias, metadataBlocks, apiToken);
setMetadataBlocksResponse.prettyPrint();
setMetadataBlocksResponse.then().assertThat().statusCode(OK.getStatusCode());

Response createDatasetResponse = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apiToken);
createDatasetResponse.prettyPrint();
Integer datasetId = UtilIT.getDatasetIdFromResponse(createDatasetResponse);
Expand Down Expand Up @@ -411,7 +420,20 @@ public void testAddUpdateDatasetViaNativeAPI() {
String responseString = addSubjectSingleViaNative.prettyPrint();
addSubjectSingleViaNative.then().assertThat()
.statusCode(OK.getStatusCode()).body(containsString("Mathematical Sciences")).body(containsString("Social Sciences"));



String pathToJsonFileSingleCvoc = "doc/sphinx-guides/source/_static/api/dataset-add-single-cvoc-field-metadata.json";
Response addSingleCvocViaNative = UtilIT.updateFieldLevelDatasetMetadataViaNative(datasetPersistentId, pathToJsonFileSingleCvoc, apiToken);
addSingleCvocViaNative.prettyPrint();
addSingleCvocViaNative.then().assertThat()
.statusCode(OK.getStatusCode());


String pathToJsonFileSingleCompound = "doc/sphinx-guides/source/_static/api/dataset-add-single-compound-field-metadata.json";
Response addSingleCompoundViaNative = UtilIT.updateFieldLevelDatasetMetadataViaNative(datasetPersistentId, pathToJsonFileSingleCompound, apiToken);
addSingleCompoundViaNative.prettyPrint();
addSingleCompoundViaNative.then().assertThat()
.statusCode(OK.getStatusCode());


//Trying to blank out required field should fail...
Expand Down

0 comments on commit 7283dbd

Please sign in to comment.