Bug: Property values that contain the '%' character throw exceptions
Closed this issue · 2 comments
I have a standard properties file. One of the property values is '!@#$%^&*()" The PropertiesInvocationHandler::format method throws an exception when it tries to process this value. The PropertiesInvocationHandler::format method calls String.format(...) but assumes the property value is a format String.
In my environment, I changed the PropertiesInvocationHandler::format method to this:
private String format(Method method, String format, Object... args) {
if (isFeatureDisabled(method, PARAMETER_FORMATTING))
// If there are no arguments to format, we can just return.
// This is also helpful when the {@code format} is a property value
// such as '@#$%^&*()" (e.g., a decrypted password) -- the '%' sign
// does not really indicate a placeholder in the format string.
if ( args == null || args.length == 0 )
return format;
try {
// Do this to achieve property expansion
return String.format(format, args);
}
catch ( Exception e ) {
// There's no guarantee that a property value from a config file
// is a legal format string. When formatting doesn't work, let's
// just return the original property value.
return format;
}
}
To the ConfigTest, I added (around line 47):
@Key("password")
@DefaultValue("@#$%^&*()")
String password();
and, at the end of ConfigTest,
/**
* When a property value contains a '%' character but is not a String format,
* we expect the property value to be returned as-is. Under the covers, the String.format
* method is used for property expansion. We want to verify a property value that
* contains a '%' character but is _not_ a format String is, indeed, supported.
*/
@Test
public void whenPropertyValueIsNotValidFormatString_thenPropertyValueShouldRemainIntact() {
SampleConfig config = ConfigFactory.create(SampleConfig.class);
assertEquals ("@#$%^&*()", config.password());
}
Hi.
Please add @DisableFeature({PARAMETER_FORMATTING})
as explained here.
You are right: the parameter formatting is not a good feature and should be dropped, or at least be disabled by default since it generates more issues than benefits.
This was a "feature" which was very easy to implement and is inspired from GWT i18n, but I shouldn't have implemented that...
Sorry for the issue and the late reply.
I was off for long time, and I started coding since few months again. Hope this will give back life to the project.