Support new default values in WebDataBinder
Prior to this commit, binding a `@ModelAttribute` object as a Controller handler paramater would instantiate the object and set all its properties, fetching data from the request. When no data is available, the WebDataBinder tries to bind default "empty" values: * Boolean.FALSE for boolean types * empty arrays for array types * null by default This commit adds the new default empty values: * empty Collections for Collection types * empty Maps for Map types Rather than using empty implementations provided by `Collections.empty` (which are not mutable), we're using the closest possible target type and real implementations, provided by the `CollectionFactory`. Issue: SPR-13502
This commit is contained in:
@@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,10 +16,15 @@
|
||||
|
||||
package org.springframework.web.bind.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -34,8 +39,6 @@ import org.springframework.web.bind.ServletRequestParameterPropertyValues;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.multipart.support.StringMultipartFileEditor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@@ -126,6 +129,31 @@ public class WebRequestDataBinderTests {
|
||||
assertFalse(target.isPostProcessed());
|
||||
}
|
||||
|
||||
// SPR-13502
|
||||
@Test
|
||||
public void testCollectionFieldsDefault() throws Exception {
|
||||
TestBean target = new TestBean();
|
||||
target.setSomeSet(null);
|
||||
target.setSomeList(null);
|
||||
target.setSomeMap(null);
|
||||
WebRequestDataBinder binder = new WebRequestDataBinder(target);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addParameter("_someSet", "visible");
|
||||
request.addParameter("_someList", "visible");
|
||||
request.addParameter("_someMap", "visible");
|
||||
|
||||
binder.bind(new ServletWebRequest(request));
|
||||
assertThat(target.getSomeSet(), notNullValue());
|
||||
assertThat(target.getSomeSet(), isA(Set.class));
|
||||
|
||||
assertThat(target.getSomeList(), notNullValue());
|
||||
assertThat(target.getSomeList(), isA(List.class));
|
||||
|
||||
assertThat(target.getSomeMap(), notNullValue());
|
||||
assertThat(target.getSomeMap(), isA(Map.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldDefaultPreemptsFieldMarker() throws Exception {
|
||||
TestBean target = new TestBean();
|
||||
|
||||
Reference in New Issue
Block a user