Skip to content

Commit f9e8924

Browse files
committed
Consistent processing of empty values and catching of RuntimeExceptions for Formatters
Issue: SPR-14345 (cherry picked from commit d51c22a)
1 parent 367e663 commit f9e8924

File tree

8 files changed

+85
-20
lines changed

8 files changed

+85
-20
lines changed

spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ private Object convertIfNecessary(String propertyName, Object oldValue, Object n
590590
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
591591
throw new ConversionNotSupportedException(pce, requiredType, ex);
592592
}
593-
catch (IllegalArgumentException ex) {
593+
catch (Throwable ex) {
594594
PropertyChangeEvent pce =
595595
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
596596
throw new TypeMismatchException(pce, requiredType, ex);

spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -73,7 +73,7 @@ private <T> T doConvert(Object value, Class<T> requiredType, MethodParameter met
7373
catch (IllegalStateException ex) {
7474
throw new ConversionNotSupportedException(value, requiredType, ex);
7575
}
76-
catch (IllegalArgumentException ex) {
76+
catch (Throwable ex) {
7777
throw new TypeMismatchException(value, requiredType, ex);
7878
}
7979
}

spring-context/src/main/java/org/springframework/format/support/FormatterPropertyEditorAdapter.java

+13-6
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-2016 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.
@@ -23,6 +23,7 @@
2323
import org.springframework.context.i18n.LocaleContextHolder;
2424
import org.springframework.format.Formatter;
2525
import org.springframework.util.Assert;
26+
import org.springframework.util.StringUtils;
2627

2728
/**
2829
* Adapter that bridges between {@link Formatter} and {@link PropertyEditor}.
@@ -60,17 +61,23 @@ public Class<?> getFieldType() {
6061

6162
@Override
6263
public void setAsText(String text) throws IllegalArgumentException {
63-
try {
64-
setValue(this.formatter.parse(text, LocaleContextHolder.getLocale()));
64+
if (StringUtils.hasText(text)) {
65+
try {
66+
setValue(this.formatter.parse(text, LocaleContextHolder.getLocale()));
67+
}
68+
catch (ParseException ex) {
69+
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
70+
}
6571
}
66-
catch (ParseException ex) {
67-
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
72+
else {
73+
setValue(null);
6874
}
6975
}
7076

7177
@Override
7278
public String getAsText() {
73-
return this.formatter.print(getValue(), LocaleContextHolder.getLocale());
79+
Object value = getValue();
80+
return (value != null ? this.formatter.print(value, LocaleContextHolder.getLocale()) : "");
7481
}
7582

7683
}

spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java

+2-2
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-2016 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.
@@ -194,7 +194,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
194194
result = this.parser.parse(text, LocaleContextHolder.getLocale());
195195
}
196196
catch (ParseException ex) {
197-
throw new IllegalArgumentException("Unable to parse '" + text + "'", ex);
197+
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
198198
}
199199
if (result == null) {
200200
throw new IllegalStateException("Parsers are not allowed to return null");

spring-context/src/test/java/org/springframework/validation/DataBinderTests.java

+61-3
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,12 @@ public void testBindingErrorWithFormatter() {
402402
}
403403

404404
@Test
405-
public void testBindingErrorWithStringFormatter() {
405+
public void testBindingErrorWithParseExceptionFromFormatter() {
406406
TestBean tb = new TestBean();
407407
DataBinder binder = new DataBinder(tb);
408408
FormattingConversionService conversionService = new FormattingConversionService();
409409
DefaultConversionService.addDefaultConverters(conversionService);
410+
410411
conversionService.addFormatter(new Formatter<String>() {
411412
@Override
412413
public String parse(String text, Locale locale) throws ParseException {
@@ -417,6 +418,35 @@ public String print(String object, Locale locale) {
417418
return object;
418419
}
419420
});
421+
422+
binder.setConversionService(conversionService);
423+
MutablePropertyValues pvs = new MutablePropertyValues();
424+
pvs.add("name", "test");
425+
426+
binder.bind(pvs);
427+
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
428+
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode());
429+
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
430+
}
431+
432+
@Test
433+
public void testBindingErrorWithRuntimeExceptionFromFormatter() {
434+
TestBean tb = new TestBean();
435+
DataBinder binder = new DataBinder(tb);
436+
FormattingConversionService conversionService = new FormattingConversionService();
437+
DefaultConversionService.addDefaultConverters(conversionService);
438+
439+
conversionService.addFormatter(new Formatter<String>() {
440+
@Override
441+
public String parse(String text, Locale locale) throws ParseException {
442+
throw new RuntimeException(text);
443+
}
444+
@Override
445+
public String print(String object, Locale locale) {
446+
return object;
447+
}
448+
});
449+
420450
binder.setConversionService(conversionService);
421451
MutablePropertyValues pvs = new MutablePropertyValues();
422452
pvs.add("name", "test");
@@ -580,9 +610,10 @@ public void testBindingErrorWithCustomFormatter() {
580610
}
581611

582612
@Test
583-
public void testBindingErrorWithCustomStringFormatter() {
613+
public void testBindingErrorWithParseExceptionFromCustomFormatter() {
584614
TestBean tb = new TestBean();
585615
DataBinder binder = new DataBinder(tb);
616+
586617
binder.addCustomFormatter(new Formatter<String>() {
587618
@Override
588619
public String parse(String text, Locale locale) throws ParseException {
@@ -593,12 +624,39 @@ public String print(String object, Locale locale) {
593624
return object;
594625
}
595626
});
627+
596628
MutablePropertyValues pvs = new MutablePropertyValues();
597629
pvs.add("name", "test");
598630

599631
binder.bind(pvs);
600632
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
601633
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
634+
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode());
635+
}
636+
637+
@Test
638+
public void testBindingErrorWithRuntimeExceptionFromCustomFormatter() {
639+
TestBean tb = new TestBean();
640+
DataBinder binder = new DataBinder(tb);
641+
642+
binder.addCustomFormatter(new Formatter<String>() {
643+
@Override
644+
public String parse(String text, Locale locale) throws ParseException {
645+
throw new RuntimeException(text);
646+
}
647+
@Override
648+
public String print(String object, Locale locale) {
649+
return object;
650+
}
651+
});
652+
653+
MutablePropertyValues pvs = new MutablePropertyValues();
654+
pvs.add("name", "test");
655+
656+
binder.bind(pvs);
657+
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
658+
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
659+
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode());
602660
}
603661

604662
@Test
@@ -990,7 +1048,7 @@ public String print(Integer object, Locale locale) {
9901048
}, "age");
9911049

9921050
MutablePropertyValues pvs = new MutablePropertyValues();
993-
pvs.add("age", "");
1051+
pvs.add("age", "x");
9941052
binder.bind(pvs);
9951053

9961054
assertEquals("argh", binder.getBindingResult().getFieldValue("age"));

spring-core/src/main/java/org/springframework/core/convert/ConversionFailedException.java

+2-2
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-2016 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.
@@ -26,7 +26,7 @@
2626
* @since 3.0
2727
*/
2828
@SuppressWarnings("serial")
29-
public final class ConversionFailedException extends ConversionException {
29+
public class ConversionFailedException extends ConversionException {
3030

3131
private final TypeDescriptor sourceType;
3232

spring-core/src/main/java/org/springframework/core/convert/ConverterNotFoundException.java

+3-3
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-2016 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,15 +25,15 @@
2525
* @since 3.0
2626
*/
2727
@SuppressWarnings("serial")
28-
public final class ConverterNotFoundException extends ConversionException {
28+
public class ConverterNotFoundException extends ConversionException {
2929

3030
private final TypeDescriptor sourceType;
3131

3232
private final TypeDescriptor targetType;
3333

3434

3535
/**
36-
* Creates a new conversion executor not found exception.
36+
* Create a new conversion executor not found exception.
3737
* @param sourceType the source type requested to convert from
3838
* @param targetType the target type requested to convert to
3939
*/

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static Object invokeConverter(GenericConverter converter, Object source,
3737
catch (ConversionFailedException ex) {
3838
throw ex;
3939
}
40-
catch (Exception ex) {
40+
catch (Throwable ex) {
4141
throw new ConversionFailedException(sourceType, targetType, source, ex);
4242
}
4343
}

0 commit comments

Comments
 (0)