Skip to content

Commit 890ce8b

Browse files
committed
Fix number comparision by parsing the openapi meta schemas
Signed-off-by: Paulo Lopes <[email protected]>
1 parent 06a6f99 commit 890ce8b

File tree

20 files changed

+49
-25
lines changed

20 files changed

+49
-25
lines changed

src/main/java/io/vertx/json/schema/JsonSchema.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ static JsonSchema of(JsonObject json) {
4242
return new io.vertx.json.schema.impl.JsonSchema(json);
4343
}
4444

45+
/**
46+
* Factory method to create a {@link JsonSchema} from a {@link JsonObject}.
47+
* @param id will force the given id as the schema $id.
48+
* @param json a JSON Object.
49+
* @return a wrapper for the input object.
50+
*/
51+
static JsonSchema of(String id, JsonObject json) {
52+
return new io.vertx.json.schema.impl.JsonSchema(
53+
json.copy()
54+
.put("id", id));
55+
}
56+
4557
/**
4658
* Factory method to create a {@link JsonSchema} from a {@link Boolean}.
4759
* @param bool a boolean.

src/main/java/io/vertx/json/schema/impl/Utils.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,52 @@ public static boolean ze(Number instance) {
6161
return instance.doubleValue() == 0.0;
6262
}
6363

64-
public static boolean lt(Number instance, Number value) {
65-
// for big numbers, go slow
66-
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
67-
return toBigDecimal(instance).compareTo(toBigDecimal(value)) < 0;
64+
public static boolean lt(Number instance, Object value) {
65+
if (value instanceof Number) {
66+
// for big numbers, go slow
67+
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
68+
return toBigDecimal(instance).compareTo(toBigDecimal(((Number) value))) < 0;
69+
}
70+
// approx.
71+
return instance.doubleValue() < ((Number) value).doubleValue();
6872
}
69-
// approx.
70-
return instance.doubleValue() < value.doubleValue();
73+
return false;
7174
}
7275

73-
public static boolean lte(Number instance, Number value) {
74-
// for big numbers, go slow
75-
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
76-
return toBigDecimal(instance).compareTo(toBigDecimal(value)) <= 0;
76+
public static boolean lte(Number instance, Object value) {
77+
if (value instanceof Number) {
78+
// for big numbers, go slow
79+
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
80+
return toBigDecimal(instance).compareTo(toBigDecimal(((Number) value))) <= 0;
81+
}
82+
// approx.
83+
return instance.doubleValue() <= ((Number) value).doubleValue();
7784
}
78-
// approx.
79-
return instance.doubleValue() <= value.doubleValue();
85+
return false;
8086
}
8187

82-
public static boolean gt(Number instance, Number value) {
83-
// for big numbers, go slow
84-
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
85-
return toBigDecimal(instance).compareTo(toBigDecimal(value)) > 0;
88+
public static boolean gt(Number instance, Object value) {
89+
if (value instanceof Number) {
90+
// for big numbers, go slow
91+
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
92+
return toBigDecimal(instance).compareTo(toBigDecimal(((Number) value))) > 0;
93+
}
94+
// approx.
95+
return instance.doubleValue() > ((Number) value).doubleValue();
8696
}
87-
// approx.
88-
return instance.doubleValue() > value.doubleValue();
97+
return false;
8998
}
9099

91-
public static boolean gte(Number instance, Number value) {
92-
// for big numbers, go slow
93-
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
94-
return toBigDecimal(instance).compareTo(toBigDecimal(value)) >= 0;
100+
public static boolean gte(Number instance, Object value) {
101+
if (value instanceof Number) {
102+
// for big numbers, go slow
103+
if (instance instanceof BigDecimal || value instanceof BigDecimal || instance instanceof BigInteger || value instanceof BigInteger) {
104+
return toBigDecimal(instance).compareTo(toBigDecimal(((Number) value))) >= 0;
105+
}
106+
// approx.
107+
return instance.doubleValue() >= ((Number) value).doubleValue();
95108
}
96-
// approx.
97-
return instance.doubleValue() >= value.doubleValue();
109+
return false;
98110
}
99111

100112
public static double remainder(Number instance, Number value) {
File renamed without changes.
File renamed without changes.

src/test/resources/json-schema.org/draft/2019-09/meta/applicator renamed to src/main/resources/json-schema.org/draft/2019-09/meta/applicator

File renamed without changes.

src/test/resources/json-schema.org/draft/2019-09/meta/content renamed to src/main/resources/json-schema.org/draft/2019-09/meta/content

File renamed without changes.

src/test/resources/json-schema.org/draft/2019-09/meta/core renamed to src/main/resources/json-schema.org/draft/2019-09/meta/core

File renamed without changes.

src/test/resources/json-schema.org/draft/2019-09/meta/format renamed to src/main/resources/json-schema.org/draft/2019-09/meta/format

File renamed without changes.

src/test/resources/json-schema.org/draft/2019-09/meta/meta-data renamed to src/main/resources/json-schema.org/draft/2019-09/meta/meta-data

File renamed without changes.

src/test/resources/json-schema.org/draft/2019-09/meta/validation renamed to src/main/resources/json-schema.org/draft/2019-09/meta/validation

File renamed without changes.

0 commit comments

Comments
 (0)