Fix property name resolution for record accessors#36911
Open
junhyeong9812 wants to merge 1 commit into
Open
Conversation
Property.resolveName() located the get/is/set accessor prefix with String.indexOf, which matches the prefix anywhere in the method name. A record component accessor whose name embeds such a prefix (for example budget()) had the wrong portion stripped and resolved to an empty or wrong property name, which in turn caused the component's backing field annotations to be dropped. Detect record component accessors first and otherwise match the get/is/set prefix only at the start of the method name via startsWith. Regular JavaBeans accessors are unaffected. Signed-off-by: junhyeong9812 <pickjog@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.
Overview
org.springframework.core.convert.Property#resolveName()derives a propertyname from a read/write
Method. It locates theget/is/setaccessor prefixwith
String#indexOf, which matches the prefix anywhere in the method name.Problem
For record-style accessors (supported since gh-26029) whose component name
contains such a token, the wrong portion is stripped. Because
isis a verycommon bigram, this is not limited to contrived names — many ordinary record
component names are affected:
budget()/widget()/gadget()""(empty)budget/widget/gadgetissue()/island()sue/landissue/islandhistory()/distance()tory/tancehistory/distancedecision()/visible()ion/ibledecision/visibleAn empty/incorrect name then makes
Property#getField()fail to locate therecord component's backing field, so annotations declared on the component are
silently dropped — for example when a record property is accessed through SpEL's
ReflectivePropertyAccessor, which builds aPropertywithout an explicit name.Before record support the read method always started with
get/is(otherwiseit threw), so
indexOfbehaved likestartsWith. Record support added anindex = 0fallback for plain accessors but did not account for a prefixappearing elsewhere in the name.
Fix
Detect record component accessors first (the declaring class is a record and the
method is one of its record component accessors) and otherwise match the
get/is/setprefix only at the start of the method name viastartsWith.Regular JavaBeans accessors are unaffected.
The setter branch is likewise switched from
indexOf("set")tostartsWith("set"), which is stricter for non-setter write methods passed to theconstructor (these now consistently throw
IllegalArgumentException).Tests
Adds
PropertyTestscovering standard getters, boolean getters and setters;record accessors that embed (
budget) or start with (issue) a prefix; plainrecord accessors; a JavaBeans getter declared on a record; and record components
literally named
get/is.