HalBuilder / halbuilder-core

HalBuilder Core
38 stars 26 forks source link

Ability for per link relationship single element array setting #18

Closed petejohanson closed 7 years ago

petejohanson commented 10 years ago

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:

  1. An overload of withRepresentation with a parameter to specify always being an array: repFactory.newRepresentation().withRepresentation('orders', myOrder, true)
  2. Settings for a representation instance: repFactory.newRepresentation().setArrayRelationship('orders')
  3. Settings for specific link relations in the RepresentationFactory, e.g: repFactory.withArrayRelationship('orders')

Thoughts?

varmadatla commented 10 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)
mikeycmccarthy commented 10 years ago

+1

mikeycmccarthy commented 9 years ago

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).

talios commented 9 years ago

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:

  1. The String rel type
  2. A boolean (or enum ) flag for single element ( default to array use )
  3. A 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.

talios commented 9 years ago

I have this implemented locally and will be pushing code to git shortly, aiming with a 4.1 release sometime in the near future.

talios commented 7 years ago

Support for this is now released in the long delayed 5.0.1 release.