Skip to content
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This project does not adhere to [Semantic Versioning](https://semver.org/) and m
### Added

### Changed
- Ignore zero `multipleOf` divisors returned by custom validators, regardless of scale, matching the default conversion. (#1328)
- Display `multipleOf` divisors in error messages using plain notation when it fits within 32 characters or is no longer than `BigDecimal.toString()`, including values returned by custom validators. Check the length before expanding to plain notation to avoid excessive padding for extreme scales. (#1328)
- Ignore non-finite `multipleOf` divisors and dividends on 2.x, matching master's `multipleOf` behavior. This is a fail-open change: values such as `1e400` rounded to infinity by the default reader are now accepted by this constraint instead of causing an exception. A non-finite divisor disables the constraint for all instances; a non-finite dividend skips the check for that instance. Enable `DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS` to validate these decimal values exactly (see #1293). Other numeric keywords on 2.x retain their existing behavior. (#1328)
- `MultipleOfValidator.getDivisor` and `getDividend` now preserve exact integral and BigDecimal values. Custom validators, including overrides calling `super`, receive these exact values; validation uses hook results without further rounding. Integral dividend scales can change (for example, `2.0` becomes `2`), which affects scale-sensitive comparisons in overrides. Mixed integral/Double or Float inputs above 2^53 can produce different results. Precision already lost when parsing floating-point values is tracked separately in #1293. (#1286)
- Extend document-root unknown-keyword suppression to external `$ref` JSON Pointer targets. Keep container and direct-schema caches separate so direct registry loads retain normal diagnostics regardless of registry load order, including rejection by `DisallowUnknownKeywordFactory`. Within one schema, a whole-document reference following a JSON Pointer reference can still reuse the container and suppress root diagnostics, including strict-factory rejection. Apply the container annotation fallback after nullable/discriminator special handling and make the container marker volatile. (#1325)
- Treat unknown document-root fields as annotations when loading a schema by JSON Pointer fragment, while preserving known keyword processing and diagnostics in subschemas. With `DisallowUnknownKeywordFactory`, such fragment loads now succeed instead of throwing on unknown root fields; direct document loads still throw. If the selected schema references the root with `$ref: "#"`, unknown root fields also remain annotations and do not reach the unknown-keyword factory. (#1205)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* {@link KeywordValidator} for multipleOf.
*/
public class MultipleOfValidator extends BaseKeywordValidator implements KeywordValidator {
private static final int MAX_PLAIN_DIVISOR_LENGTH = 32;

private final BigDecimal divisor;
private final BigInteger denominator;
private final long longDivisor;
Expand All @@ -39,10 +41,11 @@ public class MultipleOfValidator extends BaseKeywordValidator implements Keyword
public MultipleOfValidator(SchemaLocation schemaLocation, JsonNode schemaNode,
Schema parentSchema, SchemaContext schemaContext) {
super(KeywordType.MULTIPLE_OF, schemaNode, schemaLocation, parentSchema, schemaContext);
this.divisor = getDivisor(schemaNode);
BigDecimal divisor = getDivisor(schemaNode);
this.divisor = divisor == null || divisor.signum() == 0 ? null : divisor;
this.denominator = this.divisor == null ? null : this.divisor.unscaledValue().abs();
this.longDivisor = integralDivisor(this.divisor);
this.divisorMessage = this.divisor == null ? null : this.divisor.toString();
this.divisorMessage = formatDivisorMessage(this.divisor);
}

public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
Expand Down Expand Up @@ -72,6 +75,26 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
}
}

private static String formatDivisorMessage(BigDecimal value) {
if (value == null) {
return null;
}
long precision = value.precision();
long scale = value.scale();
long length = scale <= 0 ? precision - scale : Math.max(precision + 1, scale + 2);
if (value.signum() < 0) {
length++;
}
if (length <= MAX_PLAIN_DIVISOR_LENGTH) {
return value.toPlainString();
}
String text = value.toString();
if (length > text.length()) {
return text;
}
return text.indexOf('E') < 0 ? text : value.toPlainString();
}

private static long integralDivisor(BigDecimal value) {
if (value == null) {
return 0;
Expand Down Expand Up @@ -128,12 +151,19 @@ private static boolean isMultipleOf(BigDecimal dividend, BigDecimal divisor, Big

/**
* Gets the divisor to use. The default implementation preserves exact values
* for integral and BigDecimal nodes. Double and Float nodes are converted with
* {@link BigDecimal#valueOf(double)} using their {@code doubleValue()}.
* for integral and BigDecimal nodes. Finite Double and Float nodes are converted
* with {@link BigDecimal#valueOf(double)} using their {@code doubleValue()}.
* Non-finite floating-point nodes return null, so this constraint is ignored
* for those inputs.
* Validation uses the returned value without further rounding, including when
* this method is overridden.
* A null or zero return value causes this constraint to be ignored, including
* when this method is overridden.
* The default conversion removes trailing zeros unless doing so would overflow the scale.
* Error messages preserve the string representation of the returned value.
* Error messages use the returned value's plain notation when it fits within
* 32 characters or is no longer than its {@link BigDecimal#toString()}
* representation. Otherwise, that representation is used.
* This also applies to values returned by overrides.
*
* @param schemaNode the schema node
* @return the divisor or null if the input is not correct
Expand All @@ -145,7 +175,7 @@ protected BigDecimal getDivisor(JsonNode schemaNode) {
return value.signum() == 0 ? null : normalizeDivisor(value);
}
double divisor = schemaNode.doubleValue();
if (divisor != 0) {
if (divisor != 0 && Double.isFinite(divisor)) {
// convert to BigDecimal since double type is not accurate enough to do the
// division and multiple
return BigDecimal.valueOf(divisor).stripTrailingZeros();
Expand All @@ -156,18 +186,23 @@ protected BigDecimal getDivisor(JsonNode schemaNode) {

/**
* Gets the dividend to use. The default implementation preserves exact values
* for integral and BigDecimal nodes. Double and Float nodes are converted with
* {@link BigDecimal#valueOf(double)} using their {@code doubleValue()}.
* for integral and BigDecimal nodes. Finite Double and Float nodes are converted
* with {@link BigDecimal#valueOf(double)} using their {@code doubleValue()}.
* Non-finite floating-point nodes return null, so this constraint is ignored
* for those inputs.
* Validation uses the returned value without further rounding, including when
* this method is overridden.
* Integral inputs can have a different scale than with the former
* double-based conversion, for example {@code 2} instead of {@code 2.0}.
*
* @param node the node
* @return the dividend or null if the type is incorrect
* @return the dividend or null if the type is incorrect or the floating-point value is non-finite
*/
protected BigDecimal getDividend(JsonNode node) {
if (node.isNumber()) {
if (JsonNodeTypes.isNonFiniteNumber(node)) {
return null;
}
// convert to BigDecimal since double type is not accurate enough to do the
// division and multiple
if (node.isIntegralNumber() || node.isBigDecimal()) {
Expand Down
Loading
Loading