From 04f1c7cb5b8511f0d13db3062f737041e14cb7b0 Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Mon, 26 Aug 2024 16:38:52 -0400 Subject: [PATCH] name of dataset type cannot be only digits #10517 --- doc/sphinx-guides/source/api/native-api.rst | 2 +- src/main/java/edu/harvard/iq/dataverse/api/Datasets.java | 4 ++++ .../java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/sphinx-guides/source/api/native-api.rst b/doc/sphinx-guides/source/api/native-api.rst index 31afb042f29..b161fa7e6ce 100644 --- a/doc/sphinx-guides/source/api/native-api.rst +++ b/doc/sphinx-guides/source/api/native-api.rst @@ -3123,7 +3123,7 @@ Add Dataset Type Note: Before you add any types of your own, there should be a single type called "dataset". If you add "software" or "workflow", these types will be sent to DataCite (if you use DataCite). Otherwise, the only functionality you gain currently from adding types is an entry in the "Dataset Type" facet but be advised that if you add a type other than "software" or "workflow", you will need to add your new type to your Bundle.properties file for it to appear in Title Case rather than lower case in the "Dataset Type" facet. -With all that said, we'll add a "software" type in the example below. This API endpoint is superuser only. +With all that said, we'll add a "software" type in the example below. This API endpoint is superuser only. The "name" of a type cannot be only digits. .. code-block:: bash diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java index 2ebfd4aa308..034ba4536a1 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java @@ -5139,6 +5139,10 @@ public Response addDatasetType(@Context ContainerRequestContext crc, String json if (nameIn == null) { return error(BAD_REQUEST, "A name for the dataset type is required"); } + if (StringUtils.isNumeric(nameIn)) { + // getDatasetTypes supports id or name so we don't want a names that looks like an id + return error(BAD_REQUEST, "The name of the type cannot be only digits."); + } try { DatasetType datasetType = new DatasetType(); diff --git a/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java b/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java index 072e4878663..35a354c6beb 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java @@ -217,6 +217,11 @@ public void testAddAndDeleteDatasetType() { badJson.prettyPrint(); badJson.then().assertThat().statusCode(BAD_REQUEST.getStatusCode()); + String numbersOnlyIn = Json.createObjectBuilder().add("name", "12345").build().toString(); + Response numbersOnly = UtilIT.addDatasetType(numbersOnlyIn, apiToken); + numbersOnly.prettyPrint(); + numbersOnly.then().assertThat().statusCode(BAD_REQUEST.getStatusCode()); + String randomName = UUID.randomUUID().toString().substring(0, 8); String jsonIn = Json.createObjectBuilder().add("name", randomName).build().toString();