fix(qrest-crud): correct getId cast, cache Validator, bound list limit - #387
Merged
Conversation
- getId(): use Number.longValue() instead of (long) cast, which threw ClassCastException for Integer-typed entity ids - getAndValidateEntity(): reuse a cached Validator instead of building a ValidatorFactory on every request (and leaking it) - prepareForGet(): clamp ?limit to a configurable maxResults ceiling (default 1000) and fall back to defaults on non-numeric limit/offset
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.
Three correctness/robustness fixes in the
CRUDbase participant (modules/qrest-crud).1.
getId()throwsClassCastExceptionforInteger-typed entity ids((long) id)on anObjectholding anIntegercompiles to a checkcast-to-Longplus unboxing, so it throwsClassCastException: Integer cannot be cast to Longat runtime. Reflection boxes even primitiveintgetters toInteger, so any entity whose id getter returnsint/Integer(rather thanlong/Long) breaks on every POST/PUT/DELETE/GET-by-id, and the exception is swallowed into a generic abort. Fixed with((Number) id).longValue() == 0L.2.
getAndValidateEntity()rebuilds the Bean Validation factory on every requestValidation.buildDefaultValidatorFactory()is expensive and the returnedValidatorFactory(anAutoCloseable) was never closed. Replaced with a cachedstatic final Validator(theValidatoris thread-safe).3. List GET
?limitis unbounded and?limit/?offsetcrash on non-numeric inputInteger.parseIntthrew on non-numeric input, and there was no server-side ceiling onlimit, so a client could request an arbitrarily large page. Added a configurablemaxResultsceiling (default 1000, overridable per endpoint via<property name="maxResults" value="..."/>), clampedlimitto[1, maxResults]andoffsetto>= 0, and made non-numeric input fall back to the existing defaults instead of throwing.Default behavior is unchanged for callers requesting a sane page size; only requests above the ceiling are clamped.
Verification
./gradlew :modules:qrest-crud:compileJavasucceeds. The module has no existing test source set, so no unit tests were added.