Skip to content

Empty array if no embedded representation is added #39

@mg-2014

Description

@mg-2014

Hi,

This issue applies to v5.1.1, 5.1.2-SNAPSHOT (and the current "develop" branch).

There seems to be no way to add an empty array as an _embedded object, e.g. with

      ResourceRepresentation< ? > resource = ResourceRepresentation.empty( "/" )
         .withLink( "items", "/items" )
         .withRepresentation( "items", ResourceRepresentation.empty( "/items" )
            .withRel( Rels.collection( "item" ) ) );

      System.out.println( JsonRepresentationWriter.create( new ObjectMapper() ).print( resource ).utf8() );      

I get the following output:

{
  "_links" : {
    "self" : {
      "href" : "/"
    },
    "items" : {
      "href" : "/items"
    }
  },
  "_embedded" : {
    "items" : {
      "_links" : {
        "self" : {
          "href" : "/items"
        }
      }
    }
  }
}

Since I used .withRel( Rels.collection( "item" ) ) I expected the output to include an empty array:

{
  "_links" : {
    "self" : {
      "href" : "/"
    },
    "items" : {
      "href" : "/items"
    }
  },
  "_embedded" : {
    "items" : {
      "_links" : {
        "self" : {
          "href" : "/items"
        }
      },
      "item" : [ ]
    }
  }
}

The code

      ResourceRepresentation< ? > resource = ResourceRepresentation.empty( "/" )
         .withLink( "items", "/items/1" )
         .withRepresentation( "items", ResourceRepresentation.empty( "/items/1" )
            .withRel( Rels.collection( "item" ) )
            .withRepresentation( "item", ResourceRepresentation.empty() ) );

results in "item": [ { } ] which is not correct.

The code

      ResourceRepresentation< ? > resource = ResourceRepresentation.empty( "/" )
         .withLink( "items", "/items/1" )
         .withRepresentation( "items", ResourceRepresentation.empty( "/items/1" )
            .withRel( Rels.collection( "item" ) )
            .withRepresentation( "item", ResourceRepresentation.create( Collections.EMPTY_LIST ) ) );

results in an Exception:

Exception in thread "main" java.lang.IllegalStateException: Unable to serialise a non Object Node
	at com.theoryinpractise.halbuilder5.json.JsonRepresentationWriter.renderJsonProperties(JsonRepresentationWriter.java:151)
...

The code

      ResourceRepresentation< ? > resource = ResourceRepresentation.empty( "/" )
         .withLink( "items", "/items" )
         .withRepresentation( "items",
            ResourceRepresentation.create( "/items", ImmutableMap.of( "item", Collections.EMPTY_LIST ) ) );            

actually provides the expected result although it seems to be more of a work around to set the value of the ResourceRepresentation to an object { "list": [ ] }. The bad thing of this work around is that we will need a check for an empty list beforehand and if it's empty, we create this representation, else we create the "normal" representation. It might be a good idea to make the initial example with .withRel( Rels.collection( "item" ) ) lead to the same result.

Regards,
Markus

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions