Fixed search resource routing when repository name is longer than 'search' keyword - #2600
Open
ruthst00 wants to merge 2 commits into
Open
Fixed search resource routing when repository name is longer than 'search' keyword#2600ruthst00 wants to merge 2 commits into
ruthst00 wants to merge 2 commits into
Conversation
…ame is longer than 'search' keyword.
Override getMappingComparator in RepositoryRestHandlerMapping to add a
tiebreaker that prefers patterns whose last path segment is a literal
(e.g. /{repository}/search) over patterns whose last segment is a path
variable (e.g. /{repository}/{id} or /authors/{id}).
Previously, AntPatternComparator's length-based heuristic caused
GET /authors/search to be routed to the item-resource handler
(/authors/{id}, effective length 10) instead of the search-resource
handler (/{repository}/search, effective length 9), because the literal
repository name 'authors' (7 chars) made the item-resource pattern
longer than the search pattern. This resulted in a 400 BAD_REQUEST
response when 'search' could not be bound as an entity ID.
The fix introduces isLastSegmentLiteral() and compareByLastSegmentLiteralness()
helpers, and wraps the default Spring MVC comparator so that literal
last-segment patterns always beat variable last-segment patterns before
the length-based comparison is applied.
Signed-off-by: ruthes00 <ruthes00@gmail.com>
…header. Signed-off-by: ruthes00 <ruthes00@gmail.com>
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.
Fixes #1853
Root Cause:
GET /authors/searchreturned400 BAD_REQUESTdue to Spring MVC'sAntPatternComparatorusing string length as a tiebreaker. For a repository named "authors" (7 chars), the item-resource pattern/authors/{id}scored length 10 while the search pattern/{repository}/searchscored length 9, causing the wrong handler to win. "search" was then incorrectly treated as an entity ID, producing a 400 error.Fix: Overrode
getMappingComparator()inRepositoryRestHandlerMappingto wrap the default Spring MVC comparator with a SDR-specific tiebreaker. The tiebreaker (compareByLastSegmentLiteralness) checks whether the last path segment of each matched pattern is a literal (e.g.,/search) or a variable (e.g.,/{id}). Patterns with a literal last segment are always ranked higher, ensuring/{repository}/searchalways beats/{repository}/{id}or/authors/{id}regardless of string length.Files changed (3):
RepositoryRestHandlerMapping.java— AddedgetMappingComparator()override withcompareByLastSegmentLiteralness()andisLastSegmentLiteral()helpers; added@author Steve RutherfordRepositoryRestHandlerMappingUnitTests.java— Added 3 unit tests (handler resolution,isLastSegmentLiteraltrue/false cases); added@author Steve RutherfordJpaWebTests.java— Added integration test verifyingGET /authors/searchroutes to the search handler (returns 404 "no searches exposed" instead of 400 "bad request"); added@author Steve Rutherford