You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#1590 - Support for explicit type information on CollectionModel.
We now allow explicit definition of a fallback collection element type on CollectionModel to be used in cases of an empty model, so that the RepresentationModelProcessor infrastructure can still reason about the element type and also invoke the processor for empty collection models.
Fixes#1590.
Copy file name to clipboardExpand all lines: src/main/asciidoc/fundamentals.adoc
+13
Original file line number
Diff line number
Diff line change
@@ -195,3 +195,16 @@ Collection<Person> people = Collections.singleton(new Person("Dave", "Matthews")
195
195
CollectionModel<Person> model = CollectionModel.of(people);
196
196
----
197
197
====
198
+
199
+
While an `EntityModel` is constrained to always contain a payload and thus allows to reason about the type arrangement on the sole instance, a ``CollectionModel``'s underlying collection can be empty.
200
+
Due to Java's type erasure, we cannot actually detect that a `CollectionModel<Person> model = CollectionModel.empty()` is actually a `CollectionModel<Person>` because all we see is the runtime instance and an empty collection.
201
+
That missing type information can be added to the model by either adding it to the empty instance on construction via `CollectionModel.empty(Person.class)` or as fallback in case the underlying collection might be empty:
202
+
203
+
====
204
+
[source, java]
205
+
----
206
+
Iterable<Person> people = repository.findAll();
207
+
var model = CollectionModel.of(people).withFallbackType(Person.class);
You wish to add a link so the client can make payment, but don't want to mix details about your `PaymentController` into
550
550
the `OrderController`.
551
-
552
551
Instead of polluting the details of your ordering system, you can write a `RepresentationModelProcessor` like this:
553
552
554
553
====
@@ -590,6 +589,15 @@ This example is quite simple, but you can easily:
590
589
Also, in this example, the `PaymentProcessor` alters the provided `EntityModel<Order>`. You also have the power to
591
590
_replace_ it with another object. Just be advised the API requires the return type to equal the input type.
592
591
592
+
[[server.processors.empty-collections]]
593
+
=== Processing empty collection models
594
+
595
+
To find the right set of ``RepresentationModelProcessor`` instance to invoke for a `RepresentationModel` instance, the invoking infrastructure performs a detailed analysis of the generics declaration of the ``RepresentationModelProcessor``s registered.
596
+
For `CollectionModel` instances, this includes inspecting the elements of the underlying collection, as at runtime, the sole model instance does not expose generics information (due to Java's type erasure).
597
+
That means, by default, `RepresentationModelProcessor` instances are not invoked for empty collection models.
598
+
To still allow the infrastructure to deduce the payload types correctly, you can initialize empty `CollectionModel` instances with an explicit fallback payload type right from the start, or register it by calling `CollectionModel.withFallbackType(…)`.
599
+
See <<fundamentals.collection-model>> for details.
600
+
593
601
[[server.rel-provider]]
594
602
== [[spis.rel-provider]] Using the `LinkRelationProvider` API
0 commit comments