HalBuilder / halbuilder-core

HalBuilder Core
38 stars 26 forks source link

ResourceRepresentation::withLinks does not update rels correctly. #43

Closed mg-2014 closed 6 years ago

mg-2014 commented 6 years ago

Only the rel of the last link is actually added to the previously existing rels. The reason seems to be that rels.put() actually returns a new tree on which the rel is added.

Thus the following code

    final TreeMap<String, Rel> updatedRels =
        links
            .map(Links::getRel)
            .foldLeft(
                rels,
                (accum, rel) -> !accum.containsKey(rel) ? rels.put(rel, Rels.natural(rel)) : rels);

should rather be:

    final TreeMap<String, Rel> updatedRels =
        links
            .map(Links::getRel)
            .foldLeft(
                rels,
                (accum, rel) -> !accum.containsKey(rel) ? accum.put(rel, Rels.natural(rel)) : accum);

The issue can be verified by:

    ResourceRepresentation<HashMap<Object, Object>> withLinks =
        ResourceRepresentation.create("/self", HashMap.empty())
            .withLinks(List.of(Links.create("link1", "/link1"), Links.create("link2", "/link2")));

    JsonRepresentationWriter jsonRepresentationWriter = JsonRepresentationWriter.create();
    ByteString representation = jsonRepresentationWriter.print(withLinks);

which will throw an exception.

talios commented 6 years ago

Cheers for that! I'll cut a new release in the morning.