Skip to content
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
3 changes: 3 additions & 0 deletions doc/release-notes/12446-fix-guestbook-response-parsing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Bug ##

Guestbook response parsing now allows a textarea value to be a string along with an array.
4 changes: 4 additions & 0 deletions scripts/api/data/guestbook-test-response.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"id": @QID3,
"value": "Yellow"
},
{
"id": @QID4,
"value": "Text area with a string instead of an array"
}
]
}
Expand Down
7 changes: 7 additions & 0 deletions scripts/api/data/guestbook-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
"displayOrder": 3
}
]
},
{
"question": "Address",
"required": false,
"displayOrder": 3,
"type": "textarea",
"hidden": false
}
]
}
11 changes: 9 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,15 @@ public GuestbookResponse parseGuestbookResponse(JsonObject obj, GuestbookRespons
throw new JsonParseException(BundleUtil.getStringFromBundle("access.api.requestAccess.failure.guestbookresponseQuestionIdNotFound", List.of(cqId.toString())));
} else if (cq.getQuestionType().equalsIgnoreCase("textarea")) {
String lineFeed = String.valueOf((char) 10);
JsonArray jsonArray = answer.getJsonArray("value");
List<JsonString> lines = jsonArray.getValuesAs(JsonString.class);
List<JsonString> lines = new ArrayList<>();
try {
// Assume it's an array but fall back to string if it isn't
JsonArray jsonArray = answer.getJsonArray("value");
lines = jsonArray.getValuesAs(JsonString.class);
} catch (Exception ex) {
// if not an array try to get a string and add it to the list
lines.add(answer.getJsonString("value"));
}
response = lines.stream().map(JsonString::getString).collect(Collectors.joining(lineFeed));
} else if (cq.getQuestionType().equalsIgnoreCase("options")) {
String option = answer.getString("value");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3874,7 +3874,6 @@ public void testUpdateWithEmptyFieldsAndVersionCheck() throws InterruptedExcepti
@Test
public void testDownloadFileWithGuestbookResponse() throws IOException, JsonParseException {
msgt("testDownloadFileWithGuestbookResponse");
UtilIT.enableSetting(SettingsServiceBean.Key.FilePIDsEnabled);
// Create superuser
Response createUserResponse = UtilIT.createRandomUser();
assertEquals(200, createUserResponse.getStatusCode());
Expand Down Expand Up @@ -3948,6 +3947,8 @@ public void testDownloadFileWithGuestbookResponse() throws IOException, JsonPars
uploadResponse.prettyPrint();
uploadResponse.then().assertThat().statusCode(OK.getStatusCode());
Integer fileId3 = JsonPath.from(uploadResponse.body().asString()).getInt("data.files[0].dataFile.id");

UtilIT.enableSetting(SettingsServiceBean.Key.FilePIDsEnabled);
JsonObjectBuilder json4 = Json.createObjectBuilder().add("description", "my description4").add("directoryLabel", directoryLabel).add("categories", Json.createArrayBuilder().add("Data"));
uploadResponse = UtilIT.uploadFileViaNative(datasetId.toString(), "src/main/webapp/resources/images/Robot-Icon_2.png", json4.build(), ownerApiToken);
uploadResponse.then().assertThat().statusCode(OK.getStatusCode());
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5596,6 +5596,7 @@ public static Guestbook createRandomGuestbook(String ownerAlias, String persiste
gb.getCustomQuestions().get(0).setId(jsonPath.getLong("data.customQuestions[0].id"));
gb.getCustomQuestions().get(1).setId(jsonPath.getLong("data.customQuestions[1].id"));
gb.getCustomQuestions().get(2).setId(jsonPath.getLong("data.customQuestions[2].id"));
gb.getCustomQuestions().get(3).setId(jsonPath.getLong("data.customQuestions[3].id"));

// Add the Guestbook to the Dataset
if (persistentId != null) {
Expand All @@ -5610,11 +5611,14 @@ public static String generateGuestbookResponse(Guestbook gb) throws IOException
String guestbookAsJson = new String(Files.readAllBytes(Paths.get(guestbookJson.getAbsolutePath())));

List<Long> cqIDs = new ArrayList<>();
gb.getCustomQuestions().stream().forEach(cq -> cqIDs.add(cq.getId()));
// Try to match the IDs. This is no easy task as the custom questions are not added to the db in order by id.
// We will use "displayOrder" to help match them up
gb.getCustomQuestions().stream().sorted(Comparator.comparing(CustomQuestion::getDisplayOrder)).forEach(cq -> cqIDs.add(cq.getId()));

return guestbookAsJson.replace("@ID", gb.getId().toString())
.replace("@QID1", cqIDs.get(0).toString())
.replace("@QID2", cqIDs.get(1).toString())
.replace("@QID3", cqIDs.get(2).toString());
.replace("@QID3", cqIDs.get(2).toString())
.replace("@QID4", cqIDs.get(3).toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public class JsonParserTest {
"displayOrder": 3
}
]
},
{
"question": "Address",
"required": false,
"displayOrder": 3,
"type": "textarea",
"hidden": false
}
]
}
Expand Down Expand Up @@ -792,7 +799,7 @@ public void testGuestbook() throws JsonParseException {
Guestbook gb = new Guestbook();
gb = sut.parseGuestbook(jsonObj, gb);
assertEquals(true, gb.isEnabled());
assertEquals(3, gb.getCustomQuestions().size());
assertEquals(4, gb.getCustomQuestions().size());
assertEquals(4, gb.getCustomQuestions().get(2).getCustomQuestionValues().size());
assertEquals("Purple", gb.getCustomQuestions().get(2).getCustomQuestionValues().get(3).getValueString());
assertEquals(3, gb.getCustomQuestions().get(2).getCustomQuestionValues().get(3).getDisplayOrder());
Expand Down Expand Up @@ -827,6 +834,10 @@ public void testGuestbookResponse() throws JsonParseException {
{
"id": 3,
"value": "Yellow"
},
{
"id": 4,
"value": "Text area with a string instead of an array"
}
]
}
Expand All @@ -850,7 +861,7 @@ public void testGuestbookResponse() throws JsonParseException {
guestbookResponse.setGuestbook(gb);
jsonObj = JsonUtil.getJsonObject(guestbookResponseJson);
GuestbookResponse gbr = sut.parseGuestbookResponse(jsonObj, guestbookResponse);
assertTrue(gbr.getCustomQuestionResponses().size() == 3);
assertTrue(gbr.getCustomQuestionResponses().size() == 4);

// Test missing required question response
try {
Expand Down
Loading