Skip to content

Commit 001e3d5

Browse files
committed
Allow mininum and maximum to be < Integer.MIN_VALUE and >
Integer.MAX_VALUE fixes #1370 Signed-off-by: Aurélien Pupier <apupier@ibm.com>
1 parent 5580bec commit 001e3d5

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,12 +1815,23 @@ protected void processPropertySchemaContainerTypes(CodegenProperty codegenProper
18151815
}
18161816

18171817
private void handleMinMaxValues(Schema propertySchema, CodegenProperty codegenProperty) {
1818-
if (propertySchema.getMinimum() != null) {
1819-
codegenProperty.minimum = String.valueOf(propertySchema.getMinimum().longValue());
1820-
}
1821-
if (propertySchema.getMaximum() != null) {
1822-
codegenProperty.maximum = String.valueOf(propertySchema.getMaximum().longValue());
1823-
}
1818+
if (propertySchema.getMaximum() != null) {
1819+
long maximumLongValue = propertySchema.getMaximum().longValue();
1820+
if (maximumLongValue > Integer.MAX_VALUE || maximumLongValue < Integer.MIN_VALUE) {
1821+
codegenProperty.maximum = String.valueOf(propertySchema.getMaximum().longValue()) + "L";
1822+
} else {
1823+
codegenProperty.maximum = String.valueOf(propertySchema.getMaximum().longValue());
1824+
}
1825+
}
1826+
if (propertySchema.getMinimum() != null) {
1827+
long minimumLongValue = propertySchema.getMinimum().longValue();
1828+
if (minimumLongValue > Integer.MAX_VALUE || minimumLongValue < Integer.MIN_VALUE) {
1829+
codegenProperty.minimum = String.valueOf(propertySchema.getMinimum().longValue()) + "L";
1830+
} else {
1831+
codegenProperty.minimum = String.valueOf(propertySchema.getMinimum().longValue());
1832+
}
1833+
}
1834+
18241835
if (propertySchema.getExclusiveMinimum() != null) {
18251836
codegenProperty.exclusiveMinimum = propertySchema.getExclusiveMinimum();
18261837
}
@@ -2582,8 +2593,26 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
25822593
// validation
25832594
// handle maximum, minimum properly for int/long by removing the trailing ".0"
25842595
if (parameterSchema instanceof IntegerSchema) {
2585-
codegenParameter.maximum = parameterSchema.getMaximum() == null ? null : String.valueOf(parameterSchema.getMaximum().longValue());
2586-
codegenParameter.minimum = parameterSchema.getMinimum() == null ? null : String.valueOf(parameterSchema.getMinimum().longValue());
2596+
if (parameterSchema.getMaximum() == null) {
2597+
codegenParameter.maximum = null;
2598+
} else {
2599+
long maximumLongValue = parameterSchema.getMaximum().longValue();
2600+
if (maximumLongValue > Integer.MAX_VALUE || maximumLongValue < Integer.MIN_VALUE) {
2601+
codegenParameter.maximum = String.valueOf(parameterSchema.getMaximum().longValue()) + "L";
2602+
} else {
2603+
codegenParameter.maximum = String.valueOf(parameterSchema.getMaximum().longValue());
2604+
}
2605+
}
2606+
if (parameterSchema.getMinimum() == null) {
2607+
codegenParameter.minimum = null;
2608+
} else {
2609+
long minimumLongValue = parameterSchema.getMinimum().longValue();
2610+
if (minimumLongValue > Integer.MAX_VALUE || minimumLongValue < Integer.MIN_VALUE) {
2611+
codegenParameter.minimum = String.valueOf(parameterSchema.getMinimum().longValue()) + "L";
2612+
} else {
2613+
codegenParameter.minimum = String.valueOf(parameterSchema.getMinimum().longValue());
2614+
}
2615+
}
25872616
} else {
25882617
codegenParameter.maximum = parameterSchema.getMaximum() == null ? null : String.valueOf(parameterSchema.getMaximum());
25892618
codegenParameter.minimum = parameterSchema.getMinimum() == null ? null : String.valueOf(parameterSchema.getMinimum());

src/test/java/io/swagger/codegen/v3/generators/DefaultCodegenConfigTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testPutAdditionalProperties() throws Exception {
9191

9292
@Test
9393
public void testNumberSchemaMinMax() {
94-
Schema schema = new NumberSchema()
94+
Schema<?> schema = new NumberSchema()
9595
.minimum(BigDecimal.valueOf(50))
9696
.maximum(BigDecimal.valueOf(1000));
9797

@@ -101,6 +101,21 @@ public void testNumberSchemaMinMax() {
101101
Assert.assertEquals(codegenProperty.minimum, "50");
102102
Assert.assertEquals(codegenProperty.maximum, "1000");
103103
}
104+
105+
@Test
106+
public void testNumberSchemaMinMaxForLong() {
107+
Schema<?> schema = new NumberSchema()
108+
.minimum(BigDecimal.valueOf(50))
109+
.maximum(BigDecimal.valueOf(Integer.MAX_VALUE + 1L));
110+
111+
schema.setFormat("int64");
112+
113+
final DefaultCodegenConfig codegen = new P_DefaultCodegenConfig();
114+
CodegenProperty codegenProperty = codegen.fromProperty("test", schema);
115+
116+
Assert.assertEquals(codegenProperty.minimum, "50");
117+
Assert.assertEquals(codegenProperty.maximum, Integer.MAX_VALUE + 1L + "L");
118+
}
104119

105120
@Test
106121
public void testFromOperation_BodyParamsUnique() {

0 commit comments

Comments
 (0)