Skip to content

Commit d51c22a

Browse files
committed
Consistent processing of empty values and catching of RuntimeExceptions for Formatters
Issue: SPR-14345
1 parent 8432c62 commit d51c22a

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 13 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,12 @@ public void testBindingErrorWithFormatter() {
403403
}
404404

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

583613
@Test
584-
public void testBindingErrorWithCustomStringFormatter() {
614+
public void testBindingErrorWithParseExceptionFromCustomFormatter() {
585615
TestBean tb = new TestBean();
586616
DataBinder binder = new DataBinder(tb);
617+
587618
binder.addCustomFormatter(new Formatter<String>() {
588619
@Override
589620
public String parse(String text, Locale locale) throws ParseException {
@@ -594,12 +625,39 @@ public String print(String object, Locale locale) {
594625
return object;
595626
}
596627
});
628+
597629
MutablePropertyValues pvs = new MutablePropertyValues();
598630
pvs.add("name", "test");
599631

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

605663
@Test
@@ -991,7 +1049,7 @@ public String print(Integer object, Locale locale) {
9911049
}, "age");
9921050

9931051
MutablePropertyValues pvs = new MutablePropertyValues();
994-
pvs.add("age", "");
1052+
pvs.add("age", "x");
9951053
binder.bind(pvs);
9961054

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

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static Object invokeConverter(GenericConverter converter, Object source,
3838
catch (ConversionFailedException ex) {
3939
throw ex;
4040
}
41-
catch (Exception ex) {
41+
catch (Throwable ex) {
4242
throw new ConversionFailedException(sourceType, targetType, source, ex);
4343
}
4444
}

0 commit comments

Comments
 (0)