Skip to content

Commit 2af27d8

Browse files
committed
Trim string input in Converters where whitespace is irrelevant
Closes gh-28756
1 parent 0621a8e commit 2af27d8

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/StringToCharsetConverter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,17 +19,22 @@
1919
import java.nio.charset.Charset;
2020

2121
import org.springframework.core.convert.converter.Converter;
22+
import org.springframework.util.StringUtils;
2223

2324
/**
2425
* Convert a String to a {@link Charset}.
2526
*
2627
* @author Stephane Nicoll
28+
* @author Sam Brannen
2729
* @since 4.2
2830
*/
2931
class StringToCharsetConverter implements Converter<String, Charset> {
3032

3133
@Override
3234
public Charset convert(String source) {
35+
if (StringUtils.hasText(source)) {
36+
source = source.trim();
37+
}
3338
return Charset.forName(source);
3439
}
3540

spring-core/src/main/java/org/springframework/core/convert/support/StringToCurrencyConverter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,17 +19,22 @@
1919
import java.util.Currency;
2020

2121
import org.springframework.core.convert.converter.Converter;
22+
import org.springframework.util.StringUtils;
2223

2324
/**
2425
* Convert a String to a {@link Currency}.
2526
*
2627
* @author Stephane Nicoll
28+
* @author Sam Brannen
2729
* @since 4.2
2830
*/
2931
class StringToCurrencyConverter implements Converter<String, Currency> {
3032

3133
@Override
3234
public Currency convert(String source) {
35+
if (StringUtils.hasText(source)) {
36+
source = source.trim();
37+
}
3338
return Currency.getInstance(source);
3439
}
3540

spring-core/src/main/java/org/springframework/core/convert/support/StringToTimeZoneConverter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,12 +25,16 @@
2525
* Convert a String to a {@link TimeZone}.
2626
*
2727
* @author Stephane Nicoll
28+
* @author Sam Brannen
2829
* @since 4.2
2930
*/
3031
class StringToTimeZoneConverter implements Converter<String, TimeZone> {
3132

3233
@Override
3334
public TimeZone convert(String source) {
35+
if (StringUtils.hasText(source)) {
36+
source = source.trim();
37+
}
3438
return StringUtils.parseTimeZoneString(source);
3539
}
3640

0 commit comments

Comments
 (0)