Skip to content

Commit

Permalink
Merge pull request #139 from swagger-api/issue-127
Browse files Browse the repository at this point in the history
address auto-gen integer
  • Loading branch information
fehguy authored Jul 30, 2016
2 parents 53e2b70 + cd730a9 commit 7fa20f8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ public void validate(Object o, Parameter parameter, Iterator<Validator> 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)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " value `" + o + "` is greater than maximum allowed value `" + max + "`"));
}
}
else {
if(Double.parseDouble(o.toString()) > max) {
if(value > max) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.VALUE_OVER_MAXIMUM)
Expand All @@ -46,16 +56,26 @@ public void validate(Object o, Parameter parameter, Iterator<Validator> 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)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " value `" + o + "` is less than minimum allowed value `" + min + "`"));
}
}
else {
if(Double.parseDouble(o.toString()) < min) {
if(value < min) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.VALUE_UNDER_MINIMUM)
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/io/swagger/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Model> 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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/swagger/test/validators/NumericValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7fa20f8

Please sign in to comment.