Skip to content

Reject write methods not starting with "set" in Property - #37139

Open
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/property-setter-startswith
Open

Reject write methods not starting with "set" in Property#37139
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/property-setter-startswith

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

org.springframework.core.convert.Property#resolveName() derives a property
name from a write Method when no explicit name is given. It located the
set prefix with String#indexOf, which matches the token anywhere in
the method name.

Problem

A write method whose name merely contains set is silently accepted and
resolves to a meaningless property name derived from whatever follows the
token:

write method current result note
setName(String) name intended behavior
offsetX(String) x not a setter, silently accepted
upset(String) "" (empty) not a setter, empty name
updateName(String) IllegalArgumentException no set token at all

An empty or wrong name then misleads Property#getField() and any consumer of
Property#getName(). Only names with no set token at all were rejected.

Fix

Match the set prefix only at the start of the method name via startsWith.
Write methods that are not setters now consistently throw the existing
IllegalArgumentException("Not a setter method") instead of silently
producing a wrong name.

This was originally bundled into gh-36911 and has been extracted into this
dedicated PR as requested there, since it is a behavior change: write methods
that embed set mid-name were previously accepted and now throw.

Note on impact

A remaining known gap, unchanged by this PR: a write method whose name starts
with set followed by a lowercase letter (for example settle(String))
resolves to the stripped remainder (tle) before and after this change, since
prefix matching alone cannot tell it apart from a setter.

New exceptions surface at Property construction time for write methods
that are not setters (previously offsetX/upset shapes constructed
successfully with a wrong name). Framework-internal call sites are largely
unaffected: spring-beans passes an explicit property name, and SpEL searches
for set-prefixed methods — with one exotic exception: SpEL's Kotlin support
can surface a @JvmName-renamed setter, whose name previously either already
threw (no set token anywhere) or resolved to a coincidental name (set
mid-name) and now consistently throws. gh-37123 passes verified property
names explicitly at those SpEL call sites, which takes them off this code
path entirely.

Tests

PropertyTests covers the intact setName happy path, the previously
rejected updateName shape, and the two newly rejected shapes (offsetX,
upset), asserting the exception message as well.

Note: gh-36911 also adds a PropertyTests class on its branch; whichever PR
lands second will be rebased to merge the two test classes.

Property.resolveName() located the "set" prefix of a write method with
String.indexOf, which matches the token anywhere in the method name. A
write method that merely contains "set" (for example offsetX or upset)
was silently accepted and resolved to a meaningless property name
derived from whatever follows the token, while only names with no
"set" token at all were rejected.

Match the prefix only at the start of the method name via startsWith,
so that write methods that are not setters consistently throw the
existing IllegalArgumentException.

Extracted from the change originally bundled into spring-projectsgh-36911.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 14, 2026
@sbrannen sbrannen self-assigned this Aug 15, 2026
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 15, 2026
@sbrannen sbrannen added this to the 7.1.0-M2 milestone Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants