Skip to content

Commit 9862fbf

Browse files
committed
BeanWrapper does not fall back to String constructor if ConversionService attempt failed before
Issue: SPR-9865
1 parent d782558 commit 9862fbf

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ else if (convertedValue instanceof Map) {
198198
return (T) convertedValue.toString();
199199
}
200200
else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) {
201-
if (!requiredType.isInterface() && !requiredType.isEnum()) {
201+
if (firstAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) {
202202
try {
203203
Constructor strCtor = requiredType.getConstructor(String.class);
204204
return (T) BeanUtils.instantiateClass(strCtor, convertedValue);
@@ -220,7 +220,6 @@ else if (convertedValue instanceof String && !requiredType.isInstance(convertedV
220220
// It's an empty enum identifier: reset the enum value to null.
221221
return null;
222222
}
223-
224223
convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue);
225224
standardConversion = true;
226225
}

org.springframework.context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.format.datetime.joda;
1818

19-
import static org.junit.Assert.assertEquals;
20-
2119
import java.util.ArrayList;
2220
import java.util.Calendar;
2321
import java.util.Date;
@@ -34,6 +32,7 @@
3432
import org.junit.After;
3533
import org.junit.Before;
3634
import org.junit.Test;
35+
3736
import org.springframework.beans.MutablePropertyValues;
3837
import org.springframework.context.i18n.LocaleContextHolder;
3938
import org.springframework.core.convert.support.DefaultConversionService;
@@ -42,6 +41,8 @@
4241
import org.springframework.format.support.FormattingConversionService;
4342
import org.springframework.validation.DataBinder;
4443

44+
import static org.junit.Assert.*;
45+
4546
/**
4647
* @author Keith Donald
4748
* @author Juergen Hoeller
@@ -96,7 +97,7 @@ public void testBindLocalDate() {
9697
@Test
9798
public void testBindLocalDateArray() {
9899
MutablePropertyValues propertyValues = new MutablePropertyValues();
99-
propertyValues.add("localDate", new String[] {"10/31/09"});
100+
propertyValues.add("localDate", new String[]{"10/31/09"});
100101
binder.bind(propertyValues);
101102
assertEquals(0, binder.getBindingResult().getErrorCount());
102103
}
@@ -240,6 +241,24 @@ public void testBindDate() {
240241
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date"));
241242
}
242243

244+
@Test
245+
public void testBindDateWithErrorAvoidingDateConstructor() {
246+
MutablePropertyValues propertyValues = new MutablePropertyValues();
247+
propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
248+
binder.bind(propertyValues);
249+
assertEquals(1, binder.getBindingResult().getErrorCount());
250+
assertEquals("Sat, 12 Aug 1995 13:30:00 GMT", binder.getBindingResult().getFieldValue("date"));
251+
}
252+
253+
@Test
254+
public void testBindDateWithoutErrorFallingBackToDateConstructor() {
255+
DataBinder binder = new DataBinder(new JodaTimeBean());
256+
MutablePropertyValues propertyValues = new MutablePropertyValues();
257+
propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
258+
binder.bind(propertyValues);
259+
assertEquals(0, binder.getBindingResult().getErrorCount());
260+
}
261+
243262
@Test
244263
public void testBindDateAnnotated() {
245264
MutablePropertyValues propertyValues = new MutablePropertyValues();

0 commit comments

Comments
 (0)