Merge branch '3.3.x'

Closes gh-41995
This commit is contained in:
Phillip Webb 2024-08-21 18:24:17 -07:00
commit c3ed545ffd
3 changed files with 47 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.WebProperties.Resources;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
@ -94,6 +95,8 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
private static final ErrorAttributeOptions ONLY_STATUS = ErrorAttributeOptions.of(Include.STATUS);
private static final DefaultErrorAttributes defaultErrorAttributes = new DefaultErrorAttributes();
private final ErrorProperties errorProperties;
/**
@ -121,8 +124,8 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
* @return a {@code Publisher} of the HTTP response
*/
protected Mono<ServerResponse> renderErrorView(ServerRequest request) {
int status = getHttpStatus(getErrorAttributes(request, ONLY_STATUS));
Map<String, Object> errorAttributes = getErrorAttributes(request, MediaType.TEXT_HTML);
int status = getHttpStatus(request, errorAttributes);
ServerResponse.BodyBuilder responseBody = ServerResponse.status(status).contentType(TEXT_HTML_UTF8);
return Flux.just(getData(status).toArray(new String[] {}))
.flatMap((viewName) -> renderErrorView(viewName, responseBody, errorAttributes))
@ -148,8 +151,8 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
* @return a {@code Publisher} of the HTTP response
*/
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
int status = getHttpStatus(getErrorAttributes(request, ONLY_STATUS));
Map<String, Object> errorAttributes = getErrorAttributes(request, MediaType.ALL);
int status = getHttpStatus(request, errorAttributes);
return ServerResponse.status(status)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(errorAttributes));
@ -234,6 +237,11 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
};
}
private int getHttpStatus(ServerRequest request, Map<String, Object> errorAttributes) {
return getHttpStatus(errorAttributes.containsKey("status") ? errorAttributes
: defaultErrorAttributes.getErrorAttributes(request, ONLY_STATUS));
}
/**
* Get the HTTP error status information from the error map.
* @param errorAttributes the current error information

View File

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.web.reactive.error;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import jakarta.validation.Valid;
@ -597,6 +598,21 @@ class DefaultErrorWebExceptionHandlerIntegrationTests {
});
}
@Test
void customErrorAttributesWithoutStatus() {
this.contextRunner.withUserConfiguration(CustomErrorAttributesWithoutStatus.class).run((context) -> {
WebTestClient client = getWebClient(context);
client.get()
.uri("/badRequest")
.exchange()
.expectStatus()
.isBadRequest()
.expectBody()
.jsonPath("status")
.doesNotExist();
});
}
private String getErrorTemplatesLocation() {
String packageName = getClass().getPackage().getName();
return "classpath:/" + packageName.replace('.', '/') + "/templates/";
@ -686,6 +702,7 @@ class DefaultErrorWebExceptionHandlerIntegrationTests {
@Bean
ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
Map<String, Object> errorAttributes = new HashMap<>();
@ -724,4 +741,23 @@ class DefaultErrorWebExceptionHandlerIntegrationTests {
}
@Configuration(proxyBeanMethods = false)
static class CustomErrorAttributesWithoutStatus {
@Bean
ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
Map<String, Object> attributes = new LinkedHashMap<>(super.getErrorAttributes(request, options));
attributes.remove("status");
return attributes;
}
};
}
}
}

View File

@ -173,7 +173,7 @@ public enum TestImage {
/**
* A container image suitable for testing Pulsar.
*/
PULSAR("apachepulsar/pulsar", "3.2.0", () -> PulsarContainer.class,
PULSAR("apachepulsar/pulsar", "3.2.4", () -> PulsarContainer.class,
(container) -> ((PulsarContainer) container).withStartupAttempts(2)
.withStartupTimeout(Duration.ofMinutes(3))),