Skip to content

Commit

Permalink
fix jsonschema maximum tip for issue #1854
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Sep 19, 2023
1 parent 08daae9 commit d7f9463
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public ValidateResult validate(Object value) {

if (maximum != Long.MIN_VALUE) {
if (exclusiveMaximum ? longValue >= maximum : longValue > maximum) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public ValidateResult validate(long longValue) {

if (maximum != Long.MIN_VALUE) {
if (exclusiveMaximum ? longValue >= maximum : longValue > maximum) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, longValue);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, longValue);
}
}

Expand Down Expand Up @@ -211,7 +211,7 @@ public ValidateResult validate(Long value) {

if (maximum != Long.MIN_VALUE) {
if (exclusiveMaximum ? longValue >= maximum : longValue > maximum) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ public ValidateResult validate(Integer value) {

if (maximum != Long.MIN_VALUE) {
if (exclusiveMaximum ? longValue >= maximum : longValue > maximum) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public ValidateResult validate(Object value) {
if (exclusiveMaximum
? maximum.compareTo(decimalValue) <= 0
: maximum.compareTo(decimalValue) < 0) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ public ValidateResult validate(long value) {
if (maximum != null) {
if (maximumLongValue != Long.MIN_VALUE) {
if (exclusiveMaximum ? value >= maximumLongValue : value > maximumLongValue) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
} else {
if (decimalValue == null) {
Expand All @@ -202,7 +202,7 @@ public ValidateResult validate(long value) {
if (exclusiveMaximum
? maximum.compareTo(decimalValue) <= 0
: maximum.compareTo(decimalValue) < 0) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
}
}
Expand Down Expand Up @@ -244,12 +244,12 @@ public ValidateResult validate(double value) {
if (maximum != null) {
if (maximumLongValue != Long.MIN_VALUE) {
if (exclusiveMaximum ? value >= maximumLongValue : value > maximumLongValue) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
} else {
double maximumDoubleValue = maximum.doubleValue();
if (exclusiveMaximum ? value >= maximumDoubleValue : value > maximumDoubleValue) {
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect >= %s, but %s" : "maximum not match, expect >= %s, but %s", maximum, value);
return new ValidateResult(false, exclusiveMaximum ? "exclusiveMaximum not match, expect <= %s, but %s" : "maximum not match, expect <= %s, but %s", maximum, value);
}
}
}
Expand Down
104 changes: 104 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues_1800/Issue1854.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.alibaba.fastjson2.issues_1800;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONSchemaValidException;
import com.alibaba.fastjson2.schema.JSONSchema;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1854 {
@Test
public void test() {
JSONSchema jsonSchema = JSONSchema.of(JSONObject.of("type", "integer", "maximum", 10));
try {
Object o = 11;
jsonSchema.assertValidate(o);
} catch (JSONSchemaValidException e) {
assertEquals("maximum not match, expect <= 10, but 11", e.getMessage());
}
try {
long l = 11L;
jsonSchema.assertValidate(l);
} catch (JSONSchemaValidException e) {
assertEquals("maximum not match, expect <= 10, but 11", e.getMessage());
}
try {
Long l = 11L;
jsonSchema.assertValidate(l);
} catch (JSONSchemaValidException e) {
assertEquals("maximum not match, expect <= 10, but 11", e.getMessage());
}
try {
Integer i = 11;
jsonSchema.assertValidate(i);
} catch (JSONSchemaValidException e) {
assertEquals("maximum not match, expect <= 10, but 11", e.getMessage());
}
jsonSchema = JSONSchema.of(JSONObject.of("type", "number", "maximum", 10));
try {
Object o = 11;
jsonSchema.assertValidate(o);
} catch (JSONSchemaValidException e) {
assertEquals("maximum not match, expect <= 10, but 11", e.getMessage());
}
try {
long l = 11L;
jsonSchema.assertValidate(l);
} catch (JSONSchemaValidException e) {
assertEquals("maximum not match, expect <= 10, but 11", e.getMessage());
}
try {
double d = 11;
jsonSchema.assertValidate(d);
} catch (JSONSchemaValidException e) {
assertEquals("maximum not match, expect <= 10, but 11.0", e.getMessage());
}

jsonSchema = JSONSchema.of(JSONObject.of("type", "integer", "minimum", 10));
try {
Object o = 9;
jsonSchema.assertValidate(o);
} catch (JSONSchemaValidException e) {
assertEquals("minimum not match, expect >= 10, but 9", e.getMessage());
}
try {
long l = 9L;
jsonSchema.assertValidate(l);
} catch (JSONSchemaValidException e) {
assertEquals("minimum not match, expect >= 10, but 9", e.getMessage());
}
try {
Long l = 9L;
jsonSchema.assertValidate(l);
} catch (JSONSchemaValidException e) {
assertEquals("minimum not match, expect >= 10, but 9", e.getMessage());
}
try {
Integer i = 9;
jsonSchema.assertValidate(i);
} catch (JSONSchemaValidException e) {
assertEquals("minimum not match, expect >= 10, but 9", e.getMessage());
}

jsonSchema = JSONSchema.of(JSONObject.of("type", "number", "minimum", 10));
try {
Object o = 9;
jsonSchema.assertValidate(o);
} catch (JSONSchemaValidException e) {
assertEquals("minimum not match, expect >= 10, but 9", e.getMessage());
}
try {
long l = 9L;
jsonSchema.assertValidate(l);
} catch (JSONSchemaValidException e) {
assertEquals("minimum not match, expect >= 10, but 9", e.getMessage());
}
try {
double d = 9;
jsonSchema.assertValidate(d);
} catch (JSONSchemaValidException e) {
assertEquals("minimum not match, expect >= 10, but 9.0", e.getMessage());
}
}
}

0 comments on commit d7f9463

Please sign in to comment.