diff --git a/src/main/java/edu/harvard/iq/dataverse/openapi/OpenAPI.java b/src/main/java/edu/harvard/iq/dataverse/openapi/OpenAPI.java index 01071afe6a6..1da9611050d 100644 --- a/src/main/java/edu/harvard/iq/dataverse/openapi/OpenAPI.java +++ b/src/main/java/edu/harvard/iq/dataverse/openapi/OpenAPI.java @@ -5,6 +5,10 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.logging.*; + +import jakarta.json.Json; +import jakarta.json.JsonObject; +import jakarta.json.JsonObjectBuilder; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.*; @@ -59,10 +63,22 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se } else if (YAML_FORMAT.equals(format)){ resp.setContentType(MediaType.TEXT_PLAIN_TYPE.toString()); } else { + List args = Arrays.asList(format); String bundleResponse = BundleUtil.getStringFromBundle("openapi.exception.invalid.format", args); - resp.sendError(Response.Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode(), - bundleResponse); + + JsonObject errorResponse = Json.createObjectBuilder() + .add("status", "ERROR") + .add("code", HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE) + .add("message", bundleResponse) + .build(); + + resp.setContentType(MediaType.APPLICATION_JSON_TYPE.toString()); + resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); + + PrintWriter responseWriter = resp.getWriter(); + responseWriter.println(errorResponse.toString()); + responseWriter.flush(); return; } diff --git a/src/test/java/edu/harvard/iq/dataverse/api/InfoIT.java b/src/test/java/edu/harvard/iq/dataverse/api/InfoIT.java index 23b9b87e393..5e436dd0e98 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/InfoIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/InfoIT.java @@ -81,24 +81,6 @@ public void testGetZipDownloadLimit() { .body("data", notNullValue()); } - @Test - public void testOpenApiDefinition(){ - Response response = given() - .get("/api/info/openapi/jasonInvalid"); - response.prettyPrint(); - response.then().assertThat().statusCode(BAD_REQUEST.getStatusCode()); - - String jsonDefinitionFileContent = UtilIT.getDatasetJson("src/main/resources/edu/harvard/iq/dataverse/openapi/dataverse_openapi.json"); - response = given().get("/api/info/openapi/json"); - response.then().assertThat().statusCode(OK.getStatusCode()) - .body(equalTo(jsonDefinitionFileContent)); - - String yamlFileContent = UtilIT.getDatasetJson("src/main/resources/edu/harvard/iq/dataverse/openapi/dataverse_openapi.yaml"); - response = given().get("/api/info/openapi/yaml"); - response.then().assertThat().statusCode(OK.getStatusCode()) - .body(equalTo(yamlFileContent)); - - } private void testSettingEndpoint(SettingsServiceBean.Key settingKey, String testSettingValue) { String endpoint = "/api/info/settings/" + settingKey;