Skip to content

Commit

Permalink
Don't replace MappingJackson2HttpMessageConverter
Browse files Browse the repository at this point in the history
Guard against Spring Data REST TypeConstrained Jackson converters
replacing the default MappingJackson2HttpMessageConverter.

Fixes spring-projectsgh-1968
  • Loading branch information
philwebb committed Nov 21, 2014
1 parent bf06295 commit 70a1438
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
*/
public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>> {

private static final List<Class<?>> NON_REPLACING_CONVERTERS;
static {
List<Class<?>> nonReplacingConverters = new ArrayList<Class<?>>();
addClassIfExists(nonReplacingConverters, "org.springframework.hateoas.mvc."
+ "TypeConstrainedMappingJackson2HttpMessageConverter");
NON_REPLACING_CONVERTERS = Collections.unmodifiableList(nonReplacingConverters);
}

private final List<HttpMessageConverter<?>> converters;

/**
Expand Down Expand Up @@ -95,8 +103,7 @@ public HttpMessageConverters(boolean addDefaultConverters,
Iterator<HttpMessageConverter<?>> iterator = processing.iterator();
while (iterator.hasNext()) {
HttpMessageConverter<?> candidate = iterator.next();
if (ClassUtils.isAssignableValue(defaultConverter.getClass(),
candidate)) {
if (isReplacement(defaultConverter, candidate)) {
combined.add(candidate);
iterator.remove();
}
Expand All @@ -109,6 +116,16 @@ public HttpMessageConverters(boolean addDefaultConverters,
this.converters = Collections.unmodifiableList(combined);
}

private boolean isReplacement(HttpMessageConverter<?> defaultConverter,
HttpMessageConverter<?> candidate) {
for (Class<?> nonRelacingConverter : NON_REPLACING_CONVERTERS) {
if (nonRelacingConverter.isInstance(candidate)) {
return false;
}
}
return ClassUtils.isAssignableValue(defaultConverter.getClass(), candidate);
}

/**
* Method that can be used to post-process the {@link HttpMessageConverter} list
* before it is used.
Expand Down Expand Up @@ -165,4 +182,13 @@ public List<HttpMessageConverter<?>> getConverters() {
return this.converters;
}

private static void addClassIfExists(List<Class<?>> list, String className) {
try {
list.add(Class.forName(className));
}
catch (ClassNotFoundException ex) {
// Ignore
}
}

}

0 comments on commit 70a1438

Please sign in to comment.