micronaut-projects / micronaut-projects/micronaut-validation

@Digits validation fails to raise constraint error for illegal BigDecimal arguments

Open
#682 1 comment 0 reactions 0 assignees View on GitHub
status: awaiting feedback
Dominant language
Java
Stars
9
Forks
8
Avg merge
20h 7m
Merged PRs (30d)
9

Description

### Expected Behavior

Given the following classes, each with a `BigDecimal` property. One has validation constraint on the constructor arg, the other on the field.

```
@Introspected
class Offer {

@Digits(integer = 6, fraction = 2)
BigDecimal price
}

@Introspected
class CounterOffer {

BigDecimal price

CounterOffer(@Digits(integer = 6, fraction = 2) BigDecimal price) {
this.price = price
}
}

```

The following test asserts the validation constraint checks fail, as expected. The test should pass:

```
void "test BigDecimal Digits property validation"() {
when:
Offer offer = new Offer(price: 1234567.890)
def violations = validator.validate(offer)

then:
violations.size() > 0

when:
CounterOffer counterOffer = new CounterOffer(9876543.210)
violations = validator.validate(counterOffer)

then:
violations.size() == 1
}

```

### Actual Behaviour

The first validation check (for `Offer`) passes, where the constraint is annotated on the class field.

The second (for `CounterOffer`) – where the constructor argument rather than class field is annotated with the constraint - fails to raise the same violation as it should, so the illegal constructor argument sneaks past the `Validator`.

```
Condition not satisfied:

violations.size() == 1
| | |
[] 0 false
```

This is apparently an issue not just with constructor arguments, but method arguments in general. The following also fails to raise validation errors for illegal BigDecimal arguments.

```
@SingleResult
Publisher save(
@NotBlank String slug,
@Digits(integer = 6, fraction = 2) BigDecimal price,
@NotNull Duration duration,
@NotBlank String description);
```

I have only tested this with BigDecimal, so it might also be a problem with other `@Digits` compatible types (`BigInteger`, `CharSequence`, etc).

### Steps To Reproduce

_No response_

### Environment Information

_No response_

### Example Application

_No response_

### Version

3.5.0 (also occurs with 3.4.x)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the provided Offer and CounterOffer classes and validator.validate calls. Trace validation for @Digits on constructor and method arguments, then add a regression test covering BigDecimal values and confirm violations are reported for both field and executable-parameter constraints.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.