Consistent processing of empty values and catching of RuntimeExceptions for Formatters
Issue: SPR-14345
This commit is contained in:
@@ -590,7 +590,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
|
|||||||
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
|
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
|
||||||
throw new ConversionNotSupportedException(pce, requiredType, ex);
|
throw new ConversionNotSupportedException(pce, requiredType, ex);
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ex) {
|
catch (Throwable ex) {
|
||||||
PropertyChangeEvent pce =
|
PropertyChangeEvent pce =
|
||||||
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
|
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
|
||||||
throw new TypeMismatchException(pce, requiredType, ex);
|
throw new TypeMismatchException(pce, requiredType, ex);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -73,7 +73,7 @@ public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport
|
|||||||
catch (IllegalStateException ex) {
|
catch (IllegalStateException ex) {
|
||||||
throw new ConversionNotSupportedException(value, requiredType, ex);
|
throw new ConversionNotSupportedException(value, requiredType, ex);
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ex) {
|
catch (Throwable ex) {
|
||||||
throw new TypeMismatchException(value, requiredType, ex);
|
throw new TypeMismatchException(value, requiredType, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -23,6 +23,7 @@ import java.text.ParseException;
|
|||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
import org.springframework.format.Formatter;
|
import org.springframework.format.Formatter;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapter that bridges between {@link Formatter} and {@link PropertyEditor}.
|
* Adapter that bridges between {@link Formatter} and {@link PropertyEditor}.
|
||||||
@@ -60,17 +61,23 @@ public class FormatterPropertyEditorAdapter extends PropertyEditorSupport {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAsText(String text) throws IllegalArgumentException {
|
public void setAsText(String text) throws IllegalArgumentException {
|
||||||
try {
|
if (StringUtils.hasText(text)) {
|
||||||
setValue(this.formatter.parse(text, LocaleContextHolder.getLocale()));
|
try {
|
||||||
|
setValue(this.formatter.parse(text, LocaleContextHolder.getLocale()));
|
||||||
|
}
|
||||||
|
catch (ParseException ex) {
|
||||||
|
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (ParseException ex) {
|
else {
|
||||||
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
|
setValue(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAsText() {
|
public String getAsText() {
|
||||||
return this.formatter.print(getValue(), LocaleContextHolder.getLocale());
|
Object value = getValue();
|
||||||
|
return (value != null ? this.formatter.print(value, LocaleContextHolder.getLocale()) : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -194,7 +194,7 @@ public class FormattingConversionService extends GenericConversionService
|
|||||||
result = this.parser.parse(text, LocaleContextHolder.getLocale());
|
result = this.parser.parse(text, LocaleContextHolder.getLocale());
|
||||||
}
|
}
|
||||||
catch (ParseException ex) {
|
catch (ParseException ex) {
|
||||||
throw new IllegalArgumentException("Unable to parse '" + text + "'", ex);
|
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
|
||||||
}
|
}
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
throw new IllegalStateException("Parsers are not allowed to return null");
|
throw new IllegalStateException("Parsers are not allowed to return null");
|
||||||
|
|||||||
@@ -403,11 +403,12 @@ public class DataBinderTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindingErrorWithStringFormatter() {
|
public void testBindingErrorWithParseExceptionFromFormatter() {
|
||||||
TestBean tb = new TestBean();
|
TestBean tb = new TestBean();
|
||||||
DataBinder binder = new DataBinder(tb);
|
DataBinder binder = new DataBinder(tb);
|
||||||
FormattingConversionService conversionService = new FormattingConversionService();
|
FormattingConversionService conversionService = new FormattingConversionService();
|
||||||
DefaultConversionService.addDefaultConverters(conversionService);
|
DefaultConversionService.addDefaultConverters(conversionService);
|
||||||
|
|
||||||
conversionService.addFormatter(new Formatter<String>() {
|
conversionService.addFormatter(new Formatter<String>() {
|
||||||
@Override
|
@Override
|
||||||
public String parse(String text, Locale locale) throws ParseException {
|
public String parse(String text, Locale locale) throws ParseException {
|
||||||
@@ -418,6 +419,35 @@ public class DataBinderTests {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
binder.setConversionService(conversionService);
|
||||||
|
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||||
|
pvs.add("name", "test");
|
||||||
|
|
||||||
|
binder.bind(pvs);
|
||||||
|
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
|
||||||
|
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode());
|
||||||
|
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindingErrorWithRuntimeExceptionFromFormatter() {
|
||||||
|
TestBean tb = new TestBean();
|
||||||
|
DataBinder binder = new DataBinder(tb);
|
||||||
|
FormattingConversionService conversionService = new FormattingConversionService();
|
||||||
|
DefaultConversionService.addDefaultConverters(conversionService);
|
||||||
|
|
||||||
|
conversionService.addFormatter(new Formatter<String>() {
|
||||||
|
@Override
|
||||||
|
public String parse(String text, Locale locale) throws ParseException {
|
||||||
|
throw new RuntimeException(text);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String print(String object, Locale locale) {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
binder.setConversionService(conversionService);
|
binder.setConversionService(conversionService);
|
||||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||||
pvs.add("name", "test");
|
pvs.add("name", "test");
|
||||||
@@ -581,9 +611,10 @@ public class DataBinderTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindingErrorWithCustomStringFormatter() {
|
public void testBindingErrorWithParseExceptionFromCustomFormatter() {
|
||||||
TestBean tb = new TestBean();
|
TestBean tb = new TestBean();
|
||||||
DataBinder binder = new DataBinder(tb);
|
DataBinder binder = new DataBinder(tb);
|
||||||
|
|
||||||
binder.addCustomFormatter(new Formatter<String>() {
|
binder.addCustomFormatter(new Formatter<String>() {
|
||||||
@Override
|
@Override
|
||||||
public String parse(String text, Locale locale) throws ParseException {
|
public String parse(String text, Locale locale) throws ParseException {
|
||||||
@@ -594,12 +625,39 @@ public class DataBinderTests {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||||
pvs.add("name", "test");
|
pvs.add("name", "test");
|
||||||
|
|
||||||
binder.bind(pvs);
|
binder.bind(pvs);
|
||||||
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
|
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
|
||||||
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
|
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
|
||||||
|
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindingErrorWithRuntimeExceptionFromCustomFormatter() {
|
||||||
|
TestBean tb = new TestBean();
|
||||||
|
DataBinder binder = new DataBinder(tb);
|
||||||
|
|
||||||
|
binder.addCustomFormatter(new Formatter<String>() {
|
||||||
|
@Override
|
||||||
|
public String parse(String text, Locale locale) throws ParseException {
|
||||||
|
throw new RuntimeException(text);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String print(String object, Locale locale) {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||||
|
pvs.add("name", "test");
|
||||||
|
|
||||||
|
binder.bind(pvs);
|
||||||
|
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
|
||||||
|
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
|
||||||
|
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -991,7 +1049,7 @@ public class DataBinderTests {
|
|||||||
}, "age");
|
}, "age");
|
||||||
|
|
||||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||||
pvs.add("age", "");
|
pvs.add("age", "x");
|
||||||
binder.bind(pvs);
|
binder.bind(pvs);
|
||||||
|
|
||||||
assertEquals("argh", binder.getBindingResult().getFieldValue("age"));
|
assertEquals("argh", binder.getBindingResult().getFieldValue("age"));
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -26,7 +26,7 @@ import org.springframework.util.ObjectUtils;
|
|||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public final class ConversionFailedException extends ConversionException {
|
public class ConversionFailedException extends ConversionException {
|
||||||
|
|
||||||
private final TypeDescriptor sourceType;
|
private final TypeDescriptor sourceType;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -25,7 +25,7 @@ package org.springframework.core.convert;
|
|||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public final class ConverterNotFoundException extends ConversionException {
|
public class ConverterNotFoundException extends ConversionException {
|
||||||
|
|
||||||
private final TypeDescriptor sourceType;
|
private final TypeDescriptor sourceType;
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ public final class ConverterNotFoundException extends ConversionException {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new conversion executor not found exception.
|
* Create a new conversion executor not found exception.
|
||||||
* @param sourceType the source type requested to convert from
|
* @param sourceType the source type requested to convert from
|
||||||
* @param targetType the target type requested to convert to
|
* @param targetType the target type requested to convert to
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ abstract class ConversionUtils {
|
|||||||
catch (ConversionFailedException ex) {
|
catch (ConversionFailedException ex) {
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Throwable ex) {
|
||||||
throw new ConversionFailedException(sourceType, targetType, source, ex);
|
throw new ConversionFailedException(sourceType, targetType, source, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user