Skip to content

Commit

Permalink
added ability to skip null properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nrktkt committed Jan 21, 2016
1 parent 626cba0 commit 6f84f99
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>black.door</groupId>
<artifactId>hate</artifactId>
<version>v1r0t2</version>
<version>v1r0t3</version>

<dependencies>
<dependency>
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/black/door/hate/HalRepresentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public static class HalRepresentationBuilder{
private Map<String, HalRepresentation> embedded;
private Map<String, List<HalRepresentation>> multiEmbedded;
private Map<String, Object> properties;
private boolean ignoreNullProperties = false;

public HalRepresentationBuilder() {
links = new HashMap<>();
Expand All @@ -140,8 +141,21 @@ public HalRepresentationBuilder() {
properties = new HashMap<>();
}

/**
* Causes any properties with null values added to this builder after this call to be ignored.
* Properties with null values added before this call will still be included.
* Null properties are included by default.
* @param active
* @return this builder
*/
public HalRepresentationBuilder ignoreNullProperties(boolean active){
this.ignoreNullProperties = active;
return this;
}

public HalRepresentationBuilder addProperty(String name, Object prop){
properties.put(name, prop);
if(!ignoreNullProperties || prop != null)
properties.put(name, prop);
return this;
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/java/black/door/hate/HalRepresentationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ public void testNulls() throws Exception{
System.out.println(orderz.serialize());
}

@Test
public void testIgnoreNullProp(){
HalRepresentation rep = HalRepresentation.builder()
.ignoreNullProperties(true)
.addProperty("thing", "value")
.addProperty("nullthing", null)
.build();
assertFalse(rep.getProperties().containsKey("nullthing"));
assertTrue(rep.getProperties().containsKey("thing"));

HalRepresentation rep2 = HalRepresentation.builder()
.addProperty("thing", "value")
.addProperty("nullthing", null)
.build();
assertTrue(rep2.getProperties().containsKey("nullthing"));
assertTrue(rep2.getProperties().containsKey("thing"));
}


@Test
public void testSerialize() throws Exception {
val basket1 = new Basket(98712);
Expand Down

0 comments on commit 6f84f99

Please sign in to comment.