Skip to content

Commit f29f88c

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. To support people that have already built apps on top of Lists, include a `toList<D>` method that honors the old contract. Related issues: #493 Related pull requests: #572 Hack
1 parent d5649c0 commit f29f88c

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

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

Lines changed: 22 additions & 6 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

@@ -52,16 +54,17 @@ public ResourceAssemblerSupport(Class<?> controllerClass, Class<D> resourceType)
5254
}
5355

5456
/**
55-
* Converts all given entities into resources.
57+
* Transform a list of {@code T}s into a list of {@link ResourceSupport}s.
58+
*
59+
* @see {@link #toResources(Iterable)} if you need this transformed list rendered as hypermedia
5660
*
57-
* @see #toResource(Object)
58-
* @param entities must not be {@literal null}.
61+
* @param entities
5962
* @return
6063
*/
61-
public List<D> toResources(Iterable<? extends T> entities) {
64+
public List<D> toList(Iterable<? extends T> entities) {
6265

6366
Assert.notNull(entities, "Entities must not be null!");
64-
List<D> result = new ArrayList<D>();
67+
List<D> result = new ArrayList<>();
6568

6669
for (T entity : entities) {
6770
result.add(toResource(entity));
@@ -70,6 +73,19 @@ public List<D> toResources(Iterable<? extends T> entities) {
7073
return result;
7174
}
7275

76+
/**
77+
* Converts all given entities into resources.
78+
*
79+
* @see #toResource(Object)
80+
* @param entities must not be {@literal null}.
81+
* @return
82+
*/
83+
public Resources<D> toResources(Iterable<? extends T> entities) {
84+
85+
Assert.notNull(entities, "Entities must not be null!");
86+
return new Resources<D>(toList(entities));
87+
}
88+
7389
/**
7490
* Creates a new resource with a self link to the given id.
7591
*

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

Lines changed: 32 additions & 2 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.
@@ -28,13 +28,15 @@
2828
import org.springframework.hateoas.Link;
2929
import org.springframework.hateoas.LinkBuilder;
3030
import org.springframework.hateoas.ResourceSupport;
31+
import org.springframework.hateoas.Resources;
3132
import org.springframework.hateoas.TestUtils;
3233
import org.springframework.web.bind.annotation.RequestMapping;
3334

3435
/**
3536
* Unit tests for {@link IdentifiableResourceAssemblerSupport}.
3637
*
3738
* @author Oliver Gierke
39+
* @author Greg Turnquist
3840
*/
3941
public class IdentifiableResourceAssemblerSupportUnitTest extends TestUtils {
4042

@@ -81,6 +83,34 @@ public void unwrapsIdentifyablesForParameters() {
8183
.hasValueSatisfying(it -> assertThat(it.endsWith("/people/id")));
8284
}
8385

86+
/**
87+
* @see #416
88+
*/
89+
@Test
90+
public void convertsEntitiesToListOfResources() {
91+
92+
Person first = new Person();
93+
first.id = 1L;
94+
Person second = new Person();
95+
second.id = 2L;
96+
97+
List<PersonResource> result = assembler.toList(Arrays.asList(first, second));
98+
99+
LinkBuilder builder = linkTo(PersonController.class);
100+
101+
PersonResource firstResource = new PersonResource();
102+
firstResource.add(builder.slash(1L).withSelfRel());
103+
104+
PersonResource secondResource = new PersonResource();
105+
secondResource.add(builder.slash(1L).withSelfRel());
106+
107+
assertThat(result).hasSize(2);
108+
assertThat(result).contains(firstResource, secondResource);
109+
}
110+
111+
/**
112+
* @see #416
113+
*/
84114
@Test
85115
public void convertsEntitiesToResources() {
86116

@@ -89,7 +119,7 @@ public void convertsEntitiesToResources() {
89119
Person second = new Person();
90120
second.id = 2L;
91121

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

94124
LinkBuilder builder = linkTo(PersonController.class);
95125

0 commit comments

Comments
 (0)