Skip to content

Commit cc32399

Browse files
committed
SPR-6470 - Make filterModel() in MappingJacksonJsonView more lenient
1 parent 541aae1 commit cc32399

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,26 @@ protected void prepareResponse(HttpServletRequest request, HttpServletResponse r
119119
protected void renderMergedOutputModel(Map<String, Object> model,
120120
HttpServletRequest request,
121121
HttpServletResponse response) throws Exception {
122-
model = filterModel(model);
122+
Object value = filterModel(model);
123123
JsonGenerator generator =
124124
objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), encoding);
125125
if (prefixJson) {
126126
generator.writeRaw("{} && ");
127127
}
128-
objectMapper.writeValue(generator, model);
128+
objectMapper.writeValue(generator, value);
129129
}
130130

131131
/**
132-
* Filters out undesired attributes from the given model.
132+
* Filters out undesired attributes from the given model. The return value can be either another {@link Map}, or a
133+
* single value object.
133134
*
134135
* <p>Default implementation removes {@link BindingResult} instances and entries not included in the {@link
135136
* #setRenderedAttributes(Set) renderedAttributes} property.
137+
*
138+
* @param model the model, as passed on to {@link #renderMergedOutputModel}
139+
* @return the object to be rendered
136140
*/
137-
protected Map<String, Object> filterModel(Map<String, Object> model) {
141+
protected Object filterModel(Map<String, Object> model) {
138142
Map<String, Object> result = new HashMap<String, Object>(model.size());
139143
Set<String> renderedAttributes =
140144
!CollectionUtils.isEmpty(this.renderedAttributes) ? this.renderedAttributes : model.keySet();

0 commit comments

Comments
 (0)