Skip to content

Commit

Permalink
Allow nullable types by ignoring "null" in union
Browse files Browse the repository at this point in the history
Closes #390.
  • Loading branch information
joelittlejohn committed Aug 3, 2015
1 parent 15bb26b commit eca441d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.jsonschema2pojo.rules.PrimitiveTypes.*;

import java.util.Iterator;

import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.Schema;

Expand All @@ -28,9 +30,10 @@

/**
* Applies the "type" schema rule.
*
* @see <a
* href="http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1">http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1</a>
*
* @see <a href=
* "http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1">http:/
* /tools.ietf.org/html/draft-zyp-json-schema-03#section-5.1</a>
*/
public class TypeRule implements Rule<JClassContainer, JType> {

Expand Down Expand Up @@ -60,10 +63,10 @@ protected TypeRule(RuleFactory ruleFactory) {
* <li>"type":"number" =&gt; <code>double</code>
* <li>"type":"object" =&gt; Generated type (see {@link ObjectRule})
* {@link java.util.List}, see {@link ArrayRule}
* <li>"type":"string" =&gt; {@link java.lang.String} (or alternative based on
* presence of "format", see {@link FormatRule})
* <li>"type":"string" =&gt; {@link java.lang.String} (or alternative based
* on presence of "format", see {@link FormatRule})
* </ul>
*
*
* @param nodeName
* the name of the node for which this "type" rule applies
* @param node
Expand Down Expand Up @@ -108,7 +111,7 @@ public JType apply(String nodeName, JsonNode node, JClassContainer jClassContain

if (!node.has("javaType") && node.has("format")) {
type = ruleFactory.getFormatRule().apply(nodeName, node.get("format"), type, schema);
} else if(!node.has("javaType") && propertyTypeName.equals("string") && node.has("media")) {
} else if (!node.has("javaType") && propertyTypeName.equals("string") && node.has("media")) {
type = ruleFactory.getMediaRule().apply(nodeName, node.get("media"), type, schema);
}

Expand All @@ -117,10 +120,15 @@ public JType apply(String nodeName, JsonNode node, JClassContainer jClassContain

private String getTypeName(JsonNode node) {
if (node.has("type") && node.get("type").isArray() && node.get("type").size() > 0) {
return node.get("type").get(0).asText();
for (Iterator<JsonNode> typeNames = node.get("type").iterator(); typeNames.hasNext();) {
String typeName = typeNames.next().asText();
if (!typeName.equals("null")) {
return typeName;
}
}
}

if (node.has("type")) {
if (node.has("type") && node.get("type").isTextual()) {
return node.get("type").asText();
}

Expand Down Expand Up @@ -165,8 +173,7 @@ private JType getJavaType(String javaTypeName, JCodeModel owner) {

if (isPrimitive(javaTypeName, owner)) {
return primitiveType(javaTypeName, owner);
}
else {
} else {
return owner.ref(javaTypeName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -106,7 +103,7 @@ public void anyTypeProducesObject() throws NoSuchMethodException {
assertThat(getterMethod.getReturnType().getName(), is("java.lang.Object"));

}

@Test
public void presenceOfPropertiesImpliesTypeObject() throws NoSuchMethodException {

Expand Down Expand Up @@ -164,6 +161,15 @@ public void javaTypeSupportsPrimitiveTypes() throws NoSuchMethodException {

}

@Test
public void correctTypeIsChosenForNullableType() throws NoSuchMethodException {

Method getterMethod = classWithManyTypes.getMethod("getNullableStringProperty");

assertThat(getterMethod.getReturnType().getName(), is("java.lang.String"));

}

@Test
public void javaTypeCanBeUsedForAnyShemaType() throws NoSuchMethodException {

Expand All @@ -188,8 +194,7 @@ public void useLongIntegersParameterCausesIntegersToBecomeLongs() throws ClassNo

@Test
public void useLongIntegersParameterCausesPrimitiveIntsToBecomeLongs() throws ClassNotFoundException, NoSuchMethodException, SecurityException {
File generatedTypesDirectory = generate("/schema/type/integerAsLong.json", "com.example",
config("useLongIntegers", true, "usePrimitives", true));
File generatedTypesDirectory = generate("/schema/type/integerAsLong.json", "com.example", config("useLongIntegers", true, "usePrimitives", true));
Class<?> classWithLongProperty = compile(generatedTypesDirectory).loadClass("com.example.IntegerAsLong");

Method getterMethod = classWithLongProperty.getMethod("getLongProperty");
Expand All @@ -210,8 +215,7 @@ public void useDoubleNumbersFalseCausesNumbersToBecomeFloats() throws ClassNotFo

@Test
public void useDoubleNumbersFalseCausesPrimitiveNumbersToBecomeFloats() throws ClassNotFoundException, NoSuchMethodException, SecurityException {
File generatedTypesDirectory = generate("/schema/type/numberAsFloat.json", "com.example",
config("useDoubleNumbers", false, "usePrimitives", true));
File generatedTypesDirectory = generate("/schema/type/numberAsFloat.json", "com.example", config("useDoubleNumbers", false, "usePrimitives", true));
Class<?> classWithDoubleProperty = compile(generatedTypesDirectory).loadClass("com.example.NumberAsFloat");

Method getterMethod = classWithDoubleProperty.getMethod("getFloatProperty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
"typeWithInterfaces" : {
"type" : "object",
"javaInterfaces" : ["java.io.Serializable", "Cloneable"]
},
"nullableStringProperty" : {
"type" : ["null", "string"]
}
}
}

0 comments on commit eca441d

Please sign in to comment.