From cd730a9e15ba6a54fe9886865bb5bdc879adac5a Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Sat, 30 Jul 2016 11:53:56 -0700 Subject: [PATCH] improved testing types before coersion --- .../validators/NumericValidator.java | 28 ++++++++++++++++--- .../test/examples/ExampleBuilderTest.java | 20 +++++++++++++ .../test/validators/NumericValidatorTest.java | 10 +++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/swagger/inflector/validators/NumericValidator.java b/src/main/java/io/swagger/inflector/validators/NumericValidator.java index 04b20910..8c2d842c 100644 --- a/src/main/java/io/swagger/inflector/validators/NumericValidator.java +++ b/src/main/java/io/swagger/inflector/validators/NumericValidator.java @@ -27,8 +27,18 @@ public void validate(Object o, Parameter parameter, Iterator chain) t }; if(ap.getMaximum() != null) { double max = ap.getMaximum(); + Double value; + try { + value = Double.parseDouble(o.toString()); + } + catch (NumberFormatException e) { + throw new ValidationException() + .message(new ValidationMessage() + .code(ValidationError.INVALID_FORMAT) + .message(parameter.getIn() + " parameter `" + parameter.getName() + " is not a compatible number")); + } if(ap.isExclusiveMaximum() != null && ap.isExclusiveMaximum()) { - if(Double.parseDouble(o.toString()) >= max) { + if(value >= max) { throw new ValidationException() .message(new ValidationMessage() .code(ValidationError.VALUE_OVER_MAXIMUM) @@ -36,7 +46,7 @@ public void validate(Object o, Parameter parameter, Iterator chain) t } } else { - if(Double.parseDouble(o.toString()) > max) { + if(value > max) { throw new ValidationException() .message(new ValidationMessage() .code(ValidationError.VALUE_OVER_MAXIMUM) @@ -46,8 +56,18 @@ public void validate(Object o, Parameter parameter, Iterator chain) t } if(ap.getMinimum() != null) { double min = ap.getMinimum(); + Double value; + try { + value = Double.parseDouble(o.toString()); + } + catch (NumberFormatException e) { + throw new ValidationException() + .message(new ValidationMessage() + .code(ValidationError.INVALID_FORMAT) + .message(parameter.getIn() + " parameter `" + parameter.getName() + " is not a compatible number")); + } if(ap.isExclusiveMinimum() != null && ap.isExclusiveMinimum()) { - if(Double.parseDouble(o.toString()) <= min) { + if(value <= min) { throw new ValidationException() .message(new ValidationMessage() .code(ValidationError.VALUE_UNDER_MINIMUM) @@ -55,7 +75,7 @@ public void validate(Object o, Parameter parameter, Iterator chain) t } } else { - if(Double.parseDouble(o.toString()) < min) { + if(value < min) { throw new ValidationException() .message(new ValidationMessage() .code(ValidationError.VALUE_UNDER_MINIMUM) diff --git a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java index 47bc33e3..5bebd173 100644 --- a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java @@ -303,6 +303,26 @@ public void testIssue133() throws Exception { "}"); } + @Test + public void testIssue127() throws Exception { + IntegerProperty integerProperty = new IntegerProperty(); + integerProperty.setFormat(null); + integerProperty.setExample(new Long(4321)); + Model model = new ModelImpl() + .property("unboundedInteger", integerProperty); + + Map definitions = new HashMap<>(); + definitions.put("Address", model); + + Example rep = ExampleBuilder.fromProperty(new RefProperty("Address"), definitions); + + Json.prettyPrint(rep); + assertEquals(Json.pretty(rep), + "{\n" + + " \"unboundedInteger\" : 4321\n" + + "}"); + } + private void assertEqualsIgnoreLineEnding(String actual, String expected) { assertEquals(actual.replace("\n", System.getProperty("line.separator")), expected); } diff --git a/src/test/java/io/swagger/test/validators/NumericValidatorTest.java b/src/test/java/io/swagger/test/validators/NumericValidatorTest.java index 3e0a4a63..1bd34af3 100644 --- a/src/test/java/io/swagger/test/validators/NumericValidatorTest.java +++ b/src/test/java/io/swagger/test/validators/NumericValidatorTest.java @@ -131,6 +131,16 @@ public void testInvalidIntegerExclusiveMaximumEquality() throws Exception { InputConverter.getInstance().validate(new Integer(10), parameter); } + @Test(expectedExceptions = ValidationException.class) + public void testIssue127_b() throws Exception { + QueryParameter parameter = new QueryParameter() + .name("test"); + parameter.setMaximum(10.0); + parameter.setExclusiveMaximum(true); + + InputConverter.getInstance().validate("value 1", parameter); + } + @Test public void testValidIntegerEnum() throws Exception { QueryParameter parameter = new QueryParameter()