Skip to content

Commit 3b8a3d1

Browse files
committed
#416 - Alter ResourceAssemblerSupport.toResources to return Resources
To avoid confusion about what to return on a Spring MVC endpoint, change toResources to not return `List<D>`, but instead `Resources<D>`. This clearly ensures when used to construct Spring MVC endpoints, will return a type Spring HATEOAS will properly marshal. Related issues: #493 Related pull requests: #572
1 parent d5649c0 commit 3b8a3d1

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,15 @@
2323
import org.springframework.beans.BeanUtils;
2424
import org.springframework.hateoas.ResourceAssembler;
2525
import org.springframework.hateoas.ResourceSupport;
26+
import org.springframework.hateoas.Resources;
2627
import org.springframework.util.Assert;
2728

2829
/**
2930
* Base class to implement {@link ResourceAssembler}s. Will automate {@link ResourceSupport} instance creation and make
3031
* sure a self-link is always added.
3132
*
3233
* @author Oliver Gierke
34+
* @author Greg Turnquist
3335
*/
3436
public abstract class ResourceAssemblerSupport<T, D extends ResourceSupport> implements ResourceAssembler<T, D> {
3537

@@ -53,12 +55,12 @@ public ResourceAssemblerSupport(Class<?> controllerClass, Class<D> resourceType)
5355

5456
/**
5557
* Converts all given entities into resources.
56-
*
58+
*
5759
* @see #toResource(Object)
5860
* @param entities must not be {@literal null}.
5961
* @return
6062
*/
61-
public List<D> toResources(Iterable<? extends T> entities) {
63+
public Resources<D> toResources(Iterable<? extends T> entities) {
6264

6365
Assert.notNull(entities, "Entities must not be null!");
6466
List<D> result = new ArrayList<D>();
@@ -67,7 +69,7 @@ public List<D> toResources(Iterable<? extends T> entities) {
6769
result.add(toResource(entity));
6870
}
6971

70-
return result;
72+
return new Resources<D>(result);
7173
}
7274

7375
/**

src/test/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupportUnitTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
2020

2121
import java.util.Arrays;
22-
import java.util.List;
2322
import java.util.Optional;
2423

2524
import org.junit.Before;
@@ -28,13 +27,15 @@
2827
import org.springframework.hateoas.Link;
2928
import org.springframework.hateoas.LinkBuilder;
3029
import org.springframework.hateoas.ResourceSupport;
30+
import org.springframework.hateoas.Resources;
3131
import org.springframework.hateoas.TestUtils;
3232
import org.springframework.web.bind.annotation.RequestMapping;
3333

3434
/**
3535
* Unit tests for {@link IdentifiableResourceAssemblerSupport}.
3636
*
3737
* @author Oliver Gierke
38+
* @author Greg Turnquist
3839
*/
3940
public class IdentifiableResourceAssemblerSupportUnitTest extends TestUtils {
4041

@@ -81,6 +82,9 @@ public void unwrapsIdentifyablesForParameters() {
8182
.hasValueSatisfying(it -> assertThat(it.endsWith("/people/id")));
8283
}
8384

85+
/**
86+
* @see #416
87+
*/
8488
@Test
8589
public void convertsEntitiesToResources() {
8690

@@ -89,7 +93,7 @@ public void convertsEntitiesToResources() {
8993
Person second = new Person();
9094
second.id = 2L;
9195

92-
List<PersonResource> result = assembler.toResources(Arrays.asList(first, second));
96+
Resources<PersonResource> result = assembler.toResources(Arrays.asList(first, second));
9397

9498
LinkBuilder builder = linkTo(PersonController.class);
9599

0 commit comments

Comments
 (0)