Skip to content

Commit

Permalink
Return 415 on bad request body content-type
Browse files Browse the repository at this point in the history
Issue: SPR-10982
  • Loading branch information
rstoyanchev committed Nov 5, 2013
1 parent 4b38dc8 commit 2f13e05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
Expand Down Expand Up @@ -115,10 +116,17 @@ protected <T> Object readWithMessageConverters(NativeWebRequest webRequest,
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage,
MethodParameter methodParam, Type targetType) throws IOException, HttpMediaTypeNotSupportedException {

MediaType contentType = inputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
MediaType contentType;
try {
contentType = inputMessage.getHeaders().getContentType();
}
catch (InvalidMediaTypeException ex) {
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
}

if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
}

Class<?> contextClass = methodParam.getDeclaringClass();
Map<TypeVariable, Type> map = GenericTypeResolver.getTypeVariableMap(contextClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public void resolveArgumentNoContentType() throws Exception {
}
}

@Test(expected = HttpMediaTypeNotSupportedException.class)
public void resolveArgumentInvalidContentType() throws Exception {
this.servletRequest.setContentType("bad");
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
}

@Test
public void resolveArgumentNotRequiredNoContent() throws Exception {
servletRequest.setContent(null);
Expand Down

0 comments on commit 2f13e05

Please sign in to comment.