Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -742,19 +742,27 @@ private static HttpData convertToJson(ServiceRequestContext ctx,

@Nullable
private static JsonNode getBodyContent(AggregatedHttpRequest request) {
@Nullable
final MediaType contentType = request.contentType();
if (contentType == null || !contentType.isJson()) {
if (request.content().isEmpty()) {
return null;
@Nullable final MediaType contentType = request.contentType();
final HttpData bodyContent = request.content();
final boolean hasBodyContent = !bodyContent.isEmpty();

if (contentType != null && contentType.isJson()) {
if (!hasBodyContent) {
return mapper.createObjectNode();
}

try {
return mapper.readTree(bodyContent.toStringUtf8());
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Failed to parse JSON request.", e);
}
throw new IllegalArgumentException("Missing or invalid content-type in JSON request.");
}
try {
return mapper.readTree(request.contentUtf8());
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Failed to parse JSON request.", e);

if (hasBodyContent) {
throw new IllegalArgumentException("Missing or invalid content-type in JSON request.");
}

return null;
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,15 +917,27 @@ void shouldDenyNonJsonContentType() {
}

@Test
void shouldDenyEmptyJson() {
void shouldAcceptEmptyJson() {
final String emptyJson = "";
final RequestHeaders headers = RequestHeaders.builder()
.method(HttpMethod.POST)
.path("/v1/echo/response_body/repeated")
.contentType(MediaType.JSON)
.build();
final AggregatedHttpResponse response = webClient.execute(headers, emptyJson).aggregate().join();
assertThat(response.status()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(response.status()).isEqualTo(HttpStatus.OK);
}

@Test
void shouldAcceptEmptyContentBody() {
final HttpData emptyContentBody = HttpData.empty();
final RequestHeaders headers = RequestHeaders.builder()
.method(HttpMethod.POST)
.path("/v1/echo/response_body/repeated")
.contentType(MediaType.JSON)
.build();
final AggregatedHttpResponse response = webClient.execute(headers, emptyContentBody).aggregate().join();
assertThat(response.status()).isEqualTo(HttpStatus.OK);
}

@Test
Expand Down
Loading