Skip to content

Commit 0b50fe4

Browse files
committed
Support String to char[] bindings
Update RelaxedConversionService to also support String to char[] conversion. Primarily to support the `password` field in MongoProperties. Fixes gh-1572
1 parent 23ff7c9 commit 0b50fe4

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.mongo;
18+
19+
import org.junit.Test;
20+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
21+
import org.springframework.boot.test.EnvironmentTestUtils;
22+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
import static org.hamcrest.Matchers.equalTo;
26+
import static org.junit.Assert.assertThat;
27+
28+
/**
29+
* Tests for {@link MongoProperties}.
30+
*
31+
* @author Phillip Webb
32+
*/
33+
public class MongoPropertiesTests {
34+
35+
@Test
36+
public void canBindCharArrayPassword() {
37+
// gh-1572
38+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
39+
EnvironmentTestUtils.addEnvironment(context, "spring.data.mongodb.password:word");
40+
context.register(Conf.class);
41+
context.refresh();
42+
MongoProperties properties = context.getBean(MongoProperties.class);
43+
assertThat(properties.getPassword(), equalTo("word".toCharArray()));
44+
}
45+
46+
@Configuration
47+
@EnableConfigurationProperties(MongoProperties.class)
48+
static class Conf {
49+
50+
}
51+
52+
}

spring-boot/src/main/java/org/springframework/boot/bind/RelaxedConversionService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public RelaxedConversionService(ConversionService conversionService) {
4949
this.additionalConverters = new GenericConversionService();
5050
this.additionalConverters
5151
.addConverterFactory(new StringToEnumIgnoringCaseConverterFactory());
52+
this.additionalConverters.addConverter(new StringToCharArrayConverter());
5253
}
5354

5455
@Override
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.bind;
18+
19+
import org.springframework.core.convert.converter.Converter;
20+
21+
/**
22+
* Converts a String to a Char Array.
23+
*
24+
* @author Phillip Webb
25+
*/
26+
class StringToCharArrayConverter implements Converter<String, char[]> {
27+
28+
@Override
29+
public char[] convert(String source) {
30+
return source.toCharArray();
31+
}
32+
33+
}

spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ protected void onRefresh() throws BeansException {
168168
assertTrue("No init", ConfigurationPropertiesWithFactoryBean.factoryBeanInit);
169169
}
170170

171+
@Test
172+
public void configurationPropertiesWithCharArray() throws Exception {
173+
this.context = new AnnotationConfigApplicationContext();
174+
EnvironmentTestUtils.addEnvironment(this.context, "test.chars:word");
175+
this.context.register(PropertyWithCharArray.class);
176+
this.context.refresh();
177+
assertThat(this.context.getBean(PropertyWithCharArray.class).getChars(),
178+
equalTo("word".toCharArray()));
179+
}
180+
171181
@Configuration
172182
@EnableConfigurationProperties
173183
public static class TestConfigurationWithValidatingSetter {
@@ -282,6 +292,23 @@ public String getBar() {
282292

283293
}
284294

295+
@Configuration
296+
@EnableConfigurationProperties
297+
@ConfigurationProperties(prefix = "test")
298+
public static class PropertyWithCharArray {
299+
300+
private char[] chars;
301+
302+
public char[] getChars() {
303+
return this.chars;
304+
}
305+
306+
public void setChars(char[] chars) {
307+
this.chars = chars;
308+
}
309+
310+
}
311+
285312
@Configuration
286313
@EnableConfigurationProperties
287314
@ConfigurationProperties(prefix = "test")

0 commit comments

Comments
 (0)