HalBuilder / halbuilder-core

HalBuilder Core
38 stars 26 forks source link

Representations with empty value produce an NPE. #36

Closed mg-2014 closed 7 years ago

mg-2014 commented 7 years ago

Hi,

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

Representations with empty value produce an NPE:

      ResourceRepresentation< ? > resource = ResourceRepresentation.empty()
       .withLink( "foo", "/foo" )
       .withRepresentation( "foo", ResourceRepresentation.create( new HashMap<>() )
          .withLink( "self", "/foo" ) );

results in

Exception in thread "main" java.lang.NullPointerException
    at com.theoryinpractise.halbuilder5.json.JsonRepresentationWriter.renderJsonProperties(JsonRepresentationWriter.java:159)
    at com.theoryinpractise.halbuilder5.json.JsonRepresentationWriter.renderJson(JsonRepresentationWriter.java:104)
    at com.theoryinpractise.halbuilder5.json.JsonRepresentationWriter.write(JsonRepresentationWriter.java:66)
    at com.theoryinpractise.halbuilder5.json.JsonRepresentationWriter.print(JsonRepresentationWriter.java:60)

since the corresponding JsonNode is NULL in renderJsonProperties(). A NULL check before JsonRepresentationWriter.java:159 seems to fix the problem. It's not a big issue though since you can easily work around by using

ResourceRepresentation.create( new HashMap<>() )

instead of

ResourceRepresentation.empty()

but since empty() exists it should not throw an NPE.

Regards, Markus