JOptCommandLinePropertySource selects property names in an inconsistent way [SPR-17639] #22168
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: waiting-for-triage
An issue we've not yet triaged or decided on
chludwig-haufe opened SPR-17639 and commented
I recently had to build a Spring Boot command line application whose application context depends on command line options. I specified the qualified property names (i.e.,
"<prefix>.<prop-name>"
as option alias names using theOptionParser#addAll(List<String>)
method of JOpt's fluent interface. I injected a customizedStandardEnvironment
with aJOptCommandLinePropertySource
based on the parsed command line arguments into theSpringApplication
. To my surprise, however, some of the properties were never bound to the corresponding@ConfigurationProperties
object.I found that this was caused by the implementation of
JOptCommandLinePropertySource#getPropertyNames()
: A comment in the source code claims "only the longest name is used for enumerating". However, the implementation always returns the last element ofOptionSpec#options()
. This is, in general, not the longest option name, becauseOptionSpec#options()
For some options,
JOptCommandLinePropertySource#getPropertyNames()
therefore contained the qualified alias name required for binding the property to the@ConfigurationProperties
object, while for other options it contained the unqualified option alias. Admittedly, the documentation ofJOptCommandLinePropertySource
never specifies which property names are enumerated; but I can't see a scenario where the current behavior is useful.This ticket's reference URL points to a JUnit class that demonstrates this behavior. The test class also includes alternative property name selection strategies that would work better in my scenario. (I wonder why
JOptCommandLinePropertySource#getPropertyNames()
needs to filter the option alias names in the first place.)Affects: 5.0.11, 5.1.3, 5.1.4
Reference URL: https://github.com/chludwig-haufe/JOptCommandLinePropertySource-Issues/blob/master/src/test/java/com/haufe/bugreports/spring/wrongpropertynamedemo/JOptCommandLinePropertySourceTests.java
The text was updated successfully, but these errors were encountered: