archaius
archaius copied to clipboard
2.x Custom decoder not properly handled for parsing configuration properties
When creating a custom decoder and using the DefaultAppConfig.builder() pattern the custom decoder is ignored when trying to parse the property value.
Example Decoder
import com.netflix.archaius.DefaultDecoder;
import java.time.LocalDate; // Could be Joda LocalDate
public class LocalDateDecoder extends DefaultDecoder
{
@Override
@SuppressWarnings("unchecked")
public <T> T decode(Class<T> type, String encoded)
{
if (LocalDate.class.isAssignableFrom(type))
{
return (T) LocalDate.parse(encoded);
}
return super.decode(type, encoded);
}
}
Accompanying test case
import com.netflix.archaius.DefaultAppConfig;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDate; // Could be Joda LocalDate
public class TestCustomDecoder {
@Test
public void testCustomDecoder() {
DefaultAppConfig config = DefaultAppConfig.builder()
.withApplicationConfigName("test")
.withDecoder(new LocalDateDecoder())
.build();
config.setProperty("app.date", "2016-01-01");
Assert.assertEquals(LocalDate.parse("2016-01-01"), config.get(LocalDate.class, "app.date", LocalDate.parse("2015-01-01")));
}
}
In order to properly decode properties when given a custom decoder in this paradigm, I believe the only change would be to push down the Decoder from the CascadingCompositeConfig into each registered child configuration.