Skip to content

Commit

Permalink
Allow array properties to have a null default value
Browse files Browse the repository at this point in the history
Closes #156.
  • Loading branch information
joelittlejohn committed Mar 5, 2014
1 parent e1e3ab7 commit b92b336
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema c

String fieldType = field.type().fullName();

if (fieldType.startsWith(List.class.getName())) {
if (defaultPresent && !field.type().isPrimitive() && node.isNull()) {
field.init(JExpr._null());

} else if (fieldType.startsWith(List.class.getName())) {
field.init(getDefaultList(field.type(), node));

} else if (fieldType.startsWith(Set.class.getName())) {
Expand All @@ -98,10 +101,6 @@ public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema c

private JExpression getDefaultValue(JType fieldType, JsonNode node) {

if (!fieldType.isPrimitive() && node.isNull()) {
return JExpr._null();
}

fieldType = fieldType.unboxify();

if (fieldType.fullName().equals(String.class.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.jsonschema2pojo.integration;

import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static org.hamcrest.Matchers.*;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -155,6 +155,17 @@ public void simplePropertyCanHaveNullDefaultValue() throws NoSuchMethodException
assertThat(getter.invoke(instance), is(nullValue()));

}

@Test
public void arrayPropertyCanHaveNullDefaultValue() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {

Object instance = classWithDefaults.newInstance();

Method getter = classWithDefaults.getMethod("getArrayPropertyWithNullDefault");

assertThat(getter.invoke(instance), is(nullValue()));

}

@Test
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
"type" : "integer",
"default" : null
},
"arrayPropertyWithNullDefault" : {
"type" : "array",
"items" : {
"type" : "string"
},
"default" : null
},
"uniqueArrayWithDefault" : {
"type" : "array",
"uniqueItems" : true,
Expand Down

0 comments on commit b92b336

Please sign in to comment.