Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
import org.hisp.dhis.user.UserGroup;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -105,6 +107,28 @@ void testGetObject() {
assertEquals(id, userById.getId());
}

@ParameterizedTest
@ValueSource(strings = {":all", "*", ":simple", "href", "id,href", "id,name,href"})
@DisplayName("Single object GET returns href when requested directly or via an expanding preset")
void testGetObjectWithHref(String fields) {
String id = GET("/users/").content().getList("users", JsonUser.class).get(0).getId();
JsonUser user =
GET("/users/{id}?fields={fields}", id, fields).content(HttpStatus.OK).as(JsonUser.class);
String href = user.getString("href").string();
assertNotNull(href);
assertTrue(href.endsWith("/users/" + id));
}

@ParameterizedTest
@ValueSource(strings = {":owner", ":identifiable", ":nameable", ":persisted", "id,name"})
@DisplayName("Single object GET omits href for presets that do not expand to it")
void testGetObjectWithoutHref(String fields) {
String id = GET("/users/").content().getList("users", JsonUser.class).get(0).getId();
JsonUser user =
GET("/users/{id}?fields={fields}", id, fields).content(HttpStatus.OK).as(JsonUser.class);
assertFalse(user.has("href"));
}

@Test
void testGetObjectProperty() {
// response will look like: { "surname": <name> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,14 @@ private void handleLinksAndAccess(List<T> entityList, List<String> fields, boole

private boolean fieldsContains(String match, List<String> fields) {
for (String field : fields) {
// for now assume href/access if * or preset is requested
if (field.contains(match) || field.equals("*") || field.startsWith(":")) {
// only presets that expand to the href property need link generation;
// href is transient (not owner/persisted) and not part of the fixed
// identifiable/nameable presets, but it is a simple property, so it is
// included by *, :all and :simple
if (field.contains(match)
|| field.equals("*")
|| field.equals(":all")
|| field.equals(":simple")) {
return true;
}
}
Expand Down
Loading