-
Notifications
You must be signed in to change notification settings - Fork 26
Description
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