Skip to content

Commit

Permalink
Concrete MediaType check in StringHttpMessageConverter
Browse files Browse the repository at this point in the history
Closes gh-23287
  • Loading branch information
rstoyanchev committed Jan 7, 2020
1 parent c480a99 commit 9d963ab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -239,7 +239,7 @@ public HttpHeaders getHeaders() {
protected void addDefaultHeaders(HttpHeaders headers, T t, @Nullable MediaType contentType) throws IOException {
if (headers.getContentType() == null) {
MediaType contentTypeToUse = contentType;
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
if (contentType == null || !contentType.isConcrete()) {
contentTypeToUse = getDefaultContentType(t);
}
else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -102,14 +102,14 @@ protected Long getContentLength(String str, @Nullable MediaType contentType) {


@Override
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType mediaType) throws IOException {
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType type) throws IOException {
if (headers.getContentType() == null ) {
if (mediaType != null && mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
if (type != null && type.isConcrete() && type.isCompatibleWith(MediaType.APPLICATION_JSON)) {
// Prevent charset parameter for JSON..
headers.setContentType(mediaType);
headers.setContentType(type);
}
}
super.addDefaultHeaders(headers, s, mediaType);
super.addDefaultHeaders(headers, s, type);
}

@Override
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -131,4 +131,16 @@ public void writeOverrideRequestedContentType() throws IOException {
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}

@Test // gh-24283
public void writeWithWildCardMediaType() throws IOException {
String body = "Hello World";
this.converter.write(body, MediaType.ALL, this.outputMessage);

HttpHeaders headers = this.outputMessage.getHeaders();
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.US_ASCII)).isEqualTo(body);
assertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
assertThat(headers.getContentLength()).isEqualTo(body.getBytes().length);
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
}

}

0 comments on commit 9d963ab

Please sign in to comment.