Skip to content

Commit

Permalink
MarshallingView explicitly skips BindingResult when searching for a m…
Browse files Browse the repository at this point in the history
…odel object

Just implementing common custom subclass behavior out-of-the-box...

Issue: SPR-11417
  • Loading branch information
jhoeller committed Feb 12, 2014
1 parent 3716a8e commit 6f58491
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Expand Up @@ -25,6 +25,7 @@
import org.springframework.oxm.Marshaller;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractView;

Expand All @@ -38,6 +39,7 @@
* property or have Spring locate the Source object.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
*/
public class MarshallingView extends AbstractView {
Expand Down Expand Up @@ -67,15 +69,15 @@ public MarshallingView() {
*/
public MarshallingView(Marshaller marshaller) {
this();
setMarshaller(marshaller);
Assert.notNull(marshaller, "Marshaller must not be null");
this.marshaller = marshaller;
}


/**
* Sets the {@link Marshaller} to be used by this view.
*/
public void setMarshaller(Marshaller marshaller) {
Assert.notNull(marshaller, "Marshaller must not be null");
this.marshaller = marshaller;
}

Expand Down Expand Up @@ -112,9 +114,10 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
}

/**
* Locates the object to be marshalled. The default implementation first attempts to look
* under the configured {@linkplain #setModelKey(String) model key}, if any, before attempting
* to locate an object of {@linkplain Marshaller#supports(Class) supported type}.
* Locate the object to be marshalled.
* <p>The default implementation first attempts to look under the configured
* {@linkplain #setModelKey(String) model key}, if any, before attempting to
* locate an object of {@linkplain Marshaller#supports(Class) supported type}.
* @param model the model Map
* @return the Object to be marshalled (or {@code null} if none found)
* @throws IllegalStateException if the model object specified by the
Expand All @@ -134,7 +137,8 @@ protected Object locateToBeMarshalled(Map<String, Object> model) throws IllegalS
return obj;
}
for (Object obj : model.values()) {
if (obj != null && this.marshaller.supports(obj.getClass())) {
if (obj != null && (model.size() == 1 || !(obj instanceof BindingResult)) &&
this.marshaller.supports(obj.getClass())) {
return obj;
}
}
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.web.servlet.view.xml;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.transform.stream.StreamResult;

Expand All @@ -26,6 +27,8 @@
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.oxm.Marshaller;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;

import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
Expand All @@ -35,16 +38,18 @@
*/
public class MarshallingViewTests {

private Marshaller marshallerMock;

private MarshallingView view;

private Marshaller marshallerMock;

@Before
public void createView() throws Exception {
marshallerMock = mock(Marshaller.class);
view = new MarshallingView(marshallerMock);
}


@Test
public void getContentType() {
assertEquals("Invalid content type", "application/xml", view.getContentType());
Expand Down Expand Up @@ -159,6 +164,26 @@ public void renderNoModelKey() throws Exception {
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
}

@Test
public void renderNoModelKeyAndBindingResultFirst() throws Exception {
Object toBeMarshalled = new Object();
String modelKey = "key";
Map<String, Object> model = new LinkedHashMap<String, Object>();
model.put(BindingResult.MODEL_KEY_PREFIX + modelKey, new BeanPropertyBindingResult(toBeMarshalled, modelKey));
model.put(modelKey, toBeMarshalled);

MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();

given(marshallerMock.supports(BeanPropertyBindingResult.class)).willReturn(true);
given(marshallerMock.supports(Object.class)).willReturn(true);

view.render(model, request, response);
assertEquals("Invalid content type", "application/xml", response.getContentType());
assertEquals("Invalid content length", 0, response.getContentLength());
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
}

@Test
public void testRenderUnsupportedModel() throws Exception {
Object toBeMarshalled = new Object();
Expand Down

0 comments on commit 6f58491

Please sign in to comment.