From 2af27d899fd9e52ca2c6c7ad28cae2c039f75149 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 5 Jul 2022 13:53:00 +0200 Subject: [PATCH] Trim string input in Converters where whitespace is irrelevant Closes gh-28756 --- .../core/convert/support/StringToCharsetConverter.java | 7 ++++++- .../core/convert/support/StringToCurrencyConverter.java | 7 ++++++- .../core/convert/support/StringToTimeZoneConverter.java | 6 +++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharsetConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharsetConverter.java index 728ea71f5d3..dd35f2f4458 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharsetConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCharsetConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,17 +19,22 @@ package org.springframework.core.convert.support; import java.nio.charset.Charset; import org.springframework.core.convert.converter.Converter; +import org.springframework.util.StringUtils; /** * Convert a String to a {@link Charset}. * * @author Stephane Nicoll + * @author Sam Brannen * @since 4.2 */ class StringToCharsetConverter implements Converter { @Override public Charset convert(String source) { + if (StringUtils.hasText(source)) { + source = source.trim(); + } return Charset.forName(source); } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCurrencyConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCurrencyConverter.java index 33897e3c530..2c3167cda1b 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCurrencyConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCurrencyConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,17 +19,22 @@ package org.springframework.core.convert.support; import java.util.Currency; import org.springframework.core.convert.converter.Converter; +import org.springframework.util.StringUtils; /** * Convert a String to a {@link Currency}. * * @author Stephane Nicoll + * @author Sam Brannen * @since 4.2 */ class StringToCurrencyConverter implements Converter { @Override public Currency convert(String source) { + if (StringUtils.hasText(source)) { + source = source.trim(); + } return Currency.getInstance(source); } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToTimeZoneConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToTimeZoneConverter.java index fd7404fc1e1..d86ee744d93 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToTimeZoneConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToTimeZoneConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,12 +25,16 @@ import org.springframework.util.StringUtils; * Convert a String to a {@link TimeZone}. * * @author Stephane Nicoll + * @author Sam Brannen * @since 4.2 */ class StringToTimeZoneConverter implements Converter { @Override public TimeZone convert(String source) { + if (StringUtils.hasText(source)) { + source = source.trim(); + } return StringUtils.parseTimeZoneString(source); }