Skip to content

Commit

Permalink
Merge pull request #434 from benjefferies/master
Browse files Browse the repository at this point in the history
Use minimum and maximum value of an integer property to determine java type long
  • Loading branch information
joelittlejohn committed Oct 23, 2015
2 parents 0eda759 + e52fd00 commit bf60a8b
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ private JType unboxIfNecessary(JType type, GenerationConfig config) {
*/
private JType getIntegerType(JCodeModel owner, JsonNode node, GenerationConfig config) {

if (config.isUseLongIntegers()) {
if (config.isUseLongIntegers() ||
(node.has("minimum") && node.get("minimum").isLong()) ||
(node.has("maximum") && node.get("maximum").isLong())) {
return unboxIfNecessary(owner.ref(Long.class), config);
} else {
return unboxIfNecessary(owner.ref(Integer.class), config);
Expand Down
128 changes: 128 additions & 0 deletions jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/TypeRuleTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,134 @@ public void applyGeneratesIntegerUsingJavaTypeLong() {
assertThat(result.fullName(), is("java.lang.Long"));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongPrimitiveWhenMaximumGreaterThanIntegerMax() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("maximum", Integer.MAX_VALUE + 1L);

when(config.isUsePrimitives()).thenReturn(true);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is("long"));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongWhenMaximumGreaterThanIntegerMax() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("maximum", Integer.MAX_VALUE + 1L);

when(config.isUsePrimitives()).thenReturn(false);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is(Long.class.getName()));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongPrimitiveWhenMaximumLessThanIntegerMin() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("maximum", Integer.MIN_VALUE - 1L);

when(config.isUsePrimitives()).thenReturn(true);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is("long"));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongWhenMaximumLessThanIntegerMin() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("maximum", Integer.MIN_VALUE - 1L);

when(config.isUsePrimitives()).thenReturn(false);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is(Long.class.getName()));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongPrimitiveWhenMinimumLessThanIntegerMin() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("minimum", Integer.MIN_VALUE - 1L);

when(config.isUsePrimitives()).thenReturn(true);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is("long"));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongWhenMinimumLessThanIntegerMin() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("minimum", Integer.MIN_VALUE - 1L);

when(config.isUsePrimitives()).thenReturn(false);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is(Long.class.getName()));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongPrimitiveWhenMinimumGreaterThanIntegerMax() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("minimum", Integer.MAX_VALUE + 1L);

when(config.isUsePrimitives()).thenReturn(true);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is("long"));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeLongWhenMinimumGreaterThanIntegerMax() {

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "integer");
objectNode.put("minimum", Integer.MAX_VALUE + 1L);

when(config.isUsePrimitives()).thenReturn(false);

JType result = rule.apply("fooBar", objectNode, jpackage, null);

assertThat(result.fullName(), is(Long.class.getName()));
}

@Test
public void applyGeneratesIntegerUsingJavaTypeBigInteger() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,50 @@ public void javaTypeCanBeUsedForAnyShemaType() throws NoSuchMethodException {

}

@Test
public void maximumGreaterThanIntegerMaxCausesIntegersToBecomeLongs() throws ClassNotFoundException, NoSuchMethodException, SecurityException {
File generatedTypesDirectory = generate("/schema/type/integerWithLongMaximumAsLong.json", "com.example");
Class<?> classWithLongProperty = compile(generatedTypesDirectory).loadClass("com.example.IntegerWithLongMaximumAsLong");

Method getterMethod = classWithLongProperty.getMethod("getLongProperty");

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

}

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

Method getterMethod = classWithLongProperty.getMethod("getLongProperty");

assertThat(getterMethod.getReturnType().getName(), is("long"));

}

@Test
public void minimumLessThanIntegerMinCausesIntegersToBecomeLongs() throws ClassNotFoundException, NoSuchMethodException, SecurityException {
File generatedTypesDirectory = generate("/schema/type/integerWithLongMinimumAsLong.json", "com.example");
Class<?> classWithLongProperty = compile(generatedTypesDirectory).loadClass("com.example.IntegerWithLongMinimumAsLong");

Method getterMethod = classWithLongProperty.getMethod("getLongProperty");

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

}

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

Method getterMethod = classWithLongProperty.getMethod("getLongProperty");

assertThat(getterMethod.getReturnType().getName(), is("long"));

}

@Test
public void useLongIntegersParameterCausesIntegersToBecomeLongs() throws ClassNotFoundException, NoSuchMethodException, SecurityException {
File generatedTypesDirectory = generate("/schema/type/integerAsLong.json", "com.example", config("useLongIntegers", true));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type" : "object",
"properties" : {
"longProperty" : {
"type" : "integer",
"maximum": 2147483648
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type" : "object",
"properties" : {
"longProperty" : {
"type" : "integer",
"minimum": -2147483649
}
}
}

0 comments on commit bf60a8b

Please # to comment.