diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/TypeRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/TypeRule.java index 228823de6..deff4f8e5 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/TypeRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/TypeRule.java @@ -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); diff --git a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/TypeRuleTest.java b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/TypeRuleTest.java old mode 100644 new mode 100755 index 5821e31dd..577c2acf0 --- a/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/TypeRuleTest.java +++ b/jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/rules/TypeRuleTest.java @@ -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() { diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java index b8bc1bb56..b04e7db25 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/TypeIT.java @@ -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)); diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/type/integerWithLongMaximumAsLong.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/type/integerWithLongMaximumAsLong.json new file mode 100755 index 000000000..9a04a28b9 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/type/integerWithLongMaximumAsLong.json @@ -0,0 +1,9 @@ +{ + "type" : "object", + "properties" : { + "longProperty" : { + "type" : "integer", + "maximum": 2147483648 + } + } +} \ No newline at end of file diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/type/integerWithLongMinimumAsLong.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/type/integerWithLongMinimumAsLong.json new file mode 100755 index 000000000..8da2bfada --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/type/integerWithLongMinimumAsLong.json @@ -0,0 +1,9 @@ +{ + "type" : "object", + "properties" : { + "longProperty" : { + "type" : "integer", + "minimum": -2147483649 + } + } +} \ No newline at end of file