Include context path in reactive DefaultErrorAttributes

Closes gh-37269
This commit is contained in:
Moritz Halbritter 2024-01-09 09:54:59 +01:00
parent 2cce123bb5
commit 98609e875d
2 changed files with 11 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -89,7 +89,7 @@ public class DefaultErrorAttributes implements ErrorAttributes {
private Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
Map<String, Object> errorAttributes = new LinkedHashMap<>();
errorAttributes.put("timestamp", new Date());
errorAttributes.put("path", request.path());
errorAttributes.put("path", request.requestPath().value());
Throwable error = getError(request);
MergedAnnotation<ResponseStatus> responseStatusAnnotation = MergedAnnotations
.from(error.getClass(), SearchStrategy.TYPE_HIERARCHY)

View File

@ -225,10 +225,18 @@ class DefaultErrorAttributesTests {
void includePath() {
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND),
ErrorAttributeOptions.defaults());
ErrorAttributeOptions.of(Include.PATH));
assertThat(attributes).containsEntry("path", "/test");
}
@Test
void pathShouldIncludeContext() {
MockServerHttpRequest request = MockServerHttpRequest.get("/context/test").contextPath("/context").build();
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND),
ErrorAttributeOptions.of(Include.PATH));
assertThat(attributes).containsEntry("path", "/context/test");
}
@Test
void excludePath() {
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();