Closed petejohanson closed 7 years ago
Yes this is something I am looking for as well. I would like to specify a certain link or embedded resource as an Object or a Collection. Setting a flag(SINGLE_ELEM_ARRAYS) on RepresentationFactory isn't helping much.
May be something like,
representation.withRepresentations(String rel, Collection<ReadableRepresentation> resources)
similarly,
representation.witLinks(String rel, Collection<Link> links)
+1
It's strange that no-one else is struggling with this - have I misinterpreted something in the HAL spec?
We've had to create a fork which we build internally to work around this (I believe we're planning on submitting a PR).
How are you handling this in your fork?
Currently the BaseRepresentation
has embedded resources as:
Multimap<String, ReadableRepresentation> resources = ArrayListMultimap.create();
I'm guessing we really want to stop using a String
to represent the rel, and introduce some form of Relation
object that holds:
String
rel typeboolean
(or enum ) flag for single element ( default to array use )boolean
(or enum ) flag for cardinality.This would actually clean up the custom check for self
links to force them to a single use.
As for the cardinality, this would just add a (potential) safe guard against changing your assumptions, if you use a rel once saying its a singular, we should prevent you reusing it.
For this, whatever Relation
class we use, should be memoized/cached, preventing redeclaration etc.
I have this implemented locally and will be pushing code to git shortly, aiming with a 4.1 release sometime in the near future.
Support for this is now released in the long delayed 5.0.1 release.
Currently, when using halbuilder-json, I can only specify a global setting for whether to serialize single element arrays as arrays or items for _links/_embedded.
For my use case, I would like to be able to have single elements arrays treated as just the item for links, but for a specific embedded resource link relation, always serialize it as an array, even with only element.
As far as API, I could see:
withRepresentation
with a parameter to specify always being an array:repFactory.newRepresentation().withRepresentation('orders', myOrder, true)
repFactory.newRepresentation().setArrayRelationship('orders')
repFactory.withArrayRelationship('orders')
Thoughts?