Skip to content

Commit d9da8c5

Browse files
authored
Merge pull request #9213 from IQSS/8944-metadatablocks
#8944 - Extend 'metadatablocks/{block_id}' endpoint JSON output
2 parents 120adc1 + ae83c27 commit d9da8c5

File tree

7 files changed

+83
-7
lines changed

7 files changed

+83
-7
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The API endpoint `/api/metadatablocks/{block_id}` has been extended to include the following fields:
2+
3+
- `controlledVocabularyValues` - All possible values for fields with a controlled vocabulary. For example, the values "Agricultural Sciences", "Arts and Humanities", etc. for the "Subject" field.
4+
- `isControlledVocabulary`: Whether or not this field has a controlled vocabulary.
5+
- `multiple`: Whether or not the field supports multiple values.

doc/sphinx-guides/source/admin/metadatacustomization.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,16 @@ Metadata Block Setup
386386

387387
Now that you understand the TSV format used for metadata blocks, the next step is to attempt to make improvements to existing metadata blocks or create entirely new metadata blocks. For either task, you should have a Dataverse Software development environment set up for testing where you can drop the database frequently while you make edits to TSV files. Once you have tested your TSV files, you should consider making a pull request to contribute your improvement back to the community.
388388

389+
.. _exploring-metadata-blocks:
390+
389391
Exploring Metadata Blocks
390392
~~~~~~~~~~~~~~~~~~~~~~~~~
391393

392-
In addition to studying the TSV files themselves you might find the following highly experimental and subject-to-change API endpoints useful to understand the metadata blocks that have already been loaded into your Dataverse installation:
394+
In addition to studying the TSV files themselves you will probably find the :ref:`metadata-blocks-api` API helpful in getting a structured dump of metadata blocks in JSON format.
395+
396+
There are also a few older, highly experimental, and subject-to-change API endpoints under the "admin" API documented below but the public API above is preferred.
393397

394-
You can get a dump of metadata fields (yes, the output is odd, please open a issue) like this:
398+
You can get a dump of metadata fields like this:
395399

396400
``curl http://localhost:8080/api/admin/datasetfield``
397401

doc/sphinx-guides/source/api/native-api.rst

+29-4
Original file line numberDiff line numberDiff line change
@@ -3007,22 +3007,47 @@ The fully expanded example above (without environment variables) looks like this
30073007
30083008
curl https://demo.dataverse.org/api/info/apiTermsOfUse
30093009
3010+
.. _metadata-blocks-api:
3011+
30103012
Metadata Blocks
30113013
---------------
30123014
3015+
See also :ref:`exploring-metadata-blocks`.
3016+
30133017
Show Info About All Metadata Blocks
30143018
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30153019
3016-
|CORS| Lists brief info about all metadata blocks registered in the system::
3020+
|CORS| Lists brief info about all metadata blocks registered in the system.
3021+
3022+
.. code-block:: bash
3023+
3024+
export SERVER_URL=https://demo.dataverse.org
3025+
3026+
curl $SERVER_URL/api/metadatablocks
3027+
3028+
The fully expanded example above (without environment variables) looks like this:
3029+
3030+
.. code-block:: bash
30173031
3018-
GET http://$SERVER/api/metadatablocks
3032+
curl https://demo.dataverse.org/api/metadatablocks
30193033
30203034
Show Info About Single Metadata Block
30213035
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30223036
3023-
|CORS| Return data about the block whose ``identifier`` is passed. ``identifier`` can either be the block's id, or its name::
3037+
|CORS| Return data about the block whose ``identifier`` is passed, including allowed controlled vocabulary values. ``identifier`` can either be the block's database id, or its name (i.e. "citation").
3038+
3039+
.. code-block:: bash
3040+
3041+
export SERVER_URL=https://demo.dataverse.org
3042+
export IDENTIFIER=citation
3043+
3044+
curl $SERVER_URL/api/metadatablocks/$IDENTIFIER
3045+
3046+
The fully expanded example above (without environment variables) looks like this:
3047+
3048+
.. code-block:: bash
30243049
3025-
GET http://$SERVER/api/metadatablocks/$identifier
3050+
curl https://demo.dataverse.org/api/metadatablocks/citation
30263051
30273052
.. _Notifications:
30283053

src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java

+11
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ public static JsonObjectBuilder json(DatasetFieldType fld) {
550550
fieldsBld.add("type", fld.getFieldType().toString());
551551
fieldsBld.add("watermark", fld.getWatermark());
552552
fieldsBld.add("description", fld.getDescription());
553+
fieldsBld.add("multiple", fld.isAllowMultiples());
554+
fieldsBld.add("isControlledVocabulary", fld.isControlledVocabulary());
555+
if (fld.isControlledVocabulary()) {
556+
// If the field has a controlled vocabulary,
557+
// add all values to the resulting JSON
558+
JsonArrayBuilder jab = Json.createArrayBuilder();
559+
for (ControlledVocabularyValue cvv : fld.getControlledVocabularyValues()) {
560+
jab.add(cvv.getStrValue());
561+
}
562+
fieldsBld.add("controlledVocabularyValues", jab);
563+
}
553564
if (!fld.getChildDatasetFieldTypes().isEmpty()) {
554565
JsonObjectBuilder subFieldsBld = jsonObjectBuilder();
555566
for (DatasetFieldType subFld : fld.getChildDatasetFieldTypes()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package edu.harvard.iq.dataverse.api;
2+
3+
import com.jayway.restassured.RestAssured;
4+
import com.jayway.restassured.response.Response;
5+
import static javax.ws.rs.core.Response.Status.OK;
6+
import org.hamcrest.CoreMatchers;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
public class MetadataBlocksIT {
11+
12+
@BeforeClass
13+
public static void setUpClass() {
14+
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();
15+
}
16+
17+
@Test
18+
public void testGetCitationBlock() {
19+
Response getCitationBlock = UtilIT.getMetadataBlock("citation");
20+
getCitationBlock.prettyPrint();
21+
getCitationBlock.then().assertThat()
22+
.statusCode(OK.getStatusCode())
23+
.body("data.fields.subject.controlledVocabularyValues[0]", CoreMatchers.is("Agricultural Sciences"));
24+
}
25+
26+
}

src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java

+5
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ static Response setMetadataBlocks(String dataverseAlias, JsonArrayBuilder blocks
567567
.post("/api/dataverses/" + dataverseAlias + "/metadatablocks");
568568
}
569569

570+
static Response getMetadataBlock(String block) {
571+
return given()
572+
.get("/api/metadatablocks/" + block);
573+
}
574+
570575
static private String getDatasetXml(String title, String author, String description) {
571576
String nullLicense = null;
572577
String nullRights = null;

tests/integration-tests.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DataversesIT,DatasetsIT,SwordIT,AdminIT,BuiltinUsersIT,UsersIT,UtilIT,ConfirmEmailIT,FileMetadataIT,FilesIT,SearchIT,InReviewWorkflowIT,HarvestingServerIT,HarvestingClientsIT,MoveIT,MakeDataCountApiIT,FileTypeDetectionIT,EditDDIIT,ExternalToolsIT,AccessIT,DuplicateFilesIT,DownloadFilesIT,LinkIT,DeleteUsersIT,DeactivateUsersIT,AuxiliaryFilesIT,InvalidCharactersIT,LicensesIT,NotificationsIT,BagIT
1+
DataversesIT,DatasetsIT,SwordIT,AdminIT,BuiltinUsersIT,UsersIT,UtilIT,ConfirmEmailIT,FileMetadataIT,FilesIT,SearchIT,InReviewWorkflowIT,HarvestingServerIT,HarvestingClientsIT,MoveIT,MakeDataCountApiIT,FileTypeDetectionIT,EditDDIIT,ExternalToolsIT,AccessIT,DuplicateFilesIT,DownloadFilesIT,LinkIT,DeleteUsersIT,DeactivateUsersIT,AuxiliaryFilesIT,InvalidCharactersIT,LicensesIT,NotificationsIT,BagIT,MetadataBlocksIT

0 commit comments

Comments
 (0)