fix(mapper): unwrap reflective spec construction exceptions in toSpec - #189
Merged
Merged
Conversation
SimpleSpecification.newSpec built specs via Constructor.newInstance under @SneakyThrows, which rethrew the InvocationTargetException wrapper verbatim. Constructor-time validation throws (TypeMismatchException from In/Between and IllegalArgumentException from Between) were therefore masked as InvocationTargetException at the SpecMapper.toSpec boundary, so downstream handlers keyed on TypeMismatchException never fired. Catch InvocationTargetException in newSpec and surface the original cause: RuntimeException/Error causes are rethrown unchanged, checked causes are wrapped in IllegalStateException. Add tests that exercise the mapper path (POJO -> SpecMapper.toSpec) for the In/non-Iterable and Between/1-element cases. Closes #177 Co-authored-by: Claude Opus 4.8 <[email protected]>
shihyuho
commented
Jul 16, 2026
shihyuho
left a comment
Member
Author
There was a problem hiding this comment.
APPROVE (advisory) — reviewed all 9 axes, no must-fix
Non-blocking (2)
- FYI · Spec conformance —
Closes #177will auto-close an issue whose third finding (MAINT-04, the CTX_JOIN hidden invariant) this PR deliberately does not address, silently dropping it from tracking. - Nit · Test coverage —
mapper/src/main/java/tw/com/softleader/data/jpa/spec/domain/SimpleSpecification.java:81— the checked-cause wrapping branch (and theErrorbranch) ships with no test, so a regression that re-leaks a rawInvocationTargetExceptionfor a checked cause would keep the suite green despite that behaviour being a stated acceptance criterion.
Coverage & checks
| Axis | Status |
|---|---|
| Correctness | clean — verified the catch is legal and reachable (In/Between throw from inside the constructor, so InvocationTargetException is the wrapper), that getCause() is the right accessor, and that the cause can never be null |
| Spec conformance | 1 finding |
| Scope | clean — every added line traces to COR-09/TEST-07 of the linked issue; both test files read in full at the head SHA, no drive-by edits |
| Convention | clean — read the head commit message: a well-formed conventional commit (69 chars, under the 100-char header limit); CONTRIBUTING.md's "cover your changes with tests" rule is satisfied |
| Security | clean — domainClass comes from the compile-time @Spec annotation literal, not request data; the newInstance call is byte-identical to the deleted lines |
| Readability | clean — flat catch with two instanceof pattern guards, no nesting; the comment explains why rather than restating what; @SneakyThrows confirmed still load-bearing |
| Architecture | clean — newSpec confirmed repo-wide as the single reflective-construction site, so this is the correct chokepoint with no sibling site left un-unwrapped |
| Performance | clean — a try block that completes normally is zero-cost on the JVM, so the success path is unchanged; the catch is O(1) and allocation-free on the rethrow branches |
| Test coverage | 1 finding |
- Verified — all 4 checks green:
commits,pr-title,continuous-integration/jenkins/branch,continuous-integration/jenkins/pr-merge(read viagh pr checks, never run locally) - Out-of-band — grepped the whole repo for
newInstance/InvocationTargetExceptionto confirmnewSpecis the only reflective-construction site · readIn,Betweenand the head commit message at the pinned SHA
🤖 Reviewed by Claude Opus 4.8
(self-review — same account; GitHub records this as event=COMMENT with no badge, so the verdict above is advisory.)
This was referenced Jul 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SimpleSpecification.newSpecbuilt specs viaConstructor.newInstance(...)under@SneakyThrows.Constructor.newInstancewraps any exception thrown inside a spec constructor in ajava.lang.reflect.InvocationTargetException, and@SneakyThrowsrethrew that wrapper verbatim. As a result, the constructor-time validation exceptions —TypeMismatchException(fromIn/Between/ boolean & comparable specs) andIllegalArgumentException(fromBetween) — were masked asInvocationTargetExceptionat theSpecMapper.toSpecboundary. A downstream handler keyed onTypeMismatchException(e.g. to map it to an HTTP 400) never fired; callers got an opaque failure with the real message buried in the cause.This change catches
InvocationTargetExceptioninnewSpecand surfaces the original cause:RuntimeException/Errorcauses are rethrown unchanged (preserving their message), and checked causes are wrapped inIllegalStateExceptionrather than leaked raw.InvocationTargetExceptioncan no longer escapenewSpec.Closes #177
Acceptance criteria
SpecMapper.toSpecon a POJO whose@Spec(In.class)field holds a non-Iterablevalue throwsTypeMismatchException(notInvocationTargetException) with the same message the direct constructor produces.SpecMapper.toSpecon a POJO whose@Spec(Between.class)field is bound to a 1-element list throwsIllegalArgumentExceptionwith the constructor's message (@Between expected exact 2 elements...).InvocationTargetExceptionnever propagates out ofSimpleSpecification.newSpec; aRuntimeExceptioncause is rethrown unchanged, a checked cause is wrapped (IllegalStateException), not leaked raw.SpecMapper.toSpecpath (constructing a query POJO and mapping it), not the direct spec constructor, for the In/non-Iterable and Between/1-element cases, asserting the surfaced exception type and message.typeMismatchtests remain green.mvn testpasses.Out of scope (per brief): MAINT-04 (
CTX_JOINhidden-invariant) is deferred pending a maintainer design decision; spec constructor exception types and anystarter/HTTP-layer handling are unchanged.Testing
mvn testacross all modules — BUILD SUCCESS. New coverage:InTest.typeMismatchThroughMapper(In on aStringfield →TypeMismatchException) andBetweenTest.oneElementThroughMapper(Between bound to a 1-element list →IllegalArgumentException), both driving the realSpecMapper.toSpecpath.