Browse Source

Polishing contribution

Closes gh-35294
pull/35303/head
rstoyanchev 6 months ago
parent
commit
89ba0fd6df
  1. 3
      spring-web/src/main/java/org/springframework/http/ProblemDetail.java
  2. 2
      spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java
  3. 2
      spring-web/src/main/java/org/springframework/web/ErrorResponseException.java
  4. 12
      spring-web/src/test/java/org/springframework/http/converter/json/ProblemDetailJacksonMixinTests.java
  5. 3
      spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java
  6. 6
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingExceptionHandlingIntegrationTests.java
  7. 3
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java
  8. 6
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java
  9. 2
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java
  10. 3
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java

3
spring-web/src/main/java/org/springframework/http/ProblemDetail.java

@ -102,7 +102,8 @@ public class ProblemDetail implements Serializable { @@ -102,7 +102,8 @@ public class ProblemDetail implements Serializable {
/**
* Setter for the {@link #getType() problem type}.
* <p>By default, this is not set. According to the spec, when not present, its value is assumed to be "about:blank"
* <p>By default, this is not set. According to the spec, when not present,
* the type is assumed to be "about:blank"
* @param type the problem type
*/
public void setType(@Nullable URI type) {

2
spring-web/src/main/java/org/springframework/web/DefaultErrorResponseBuilder.java

@ -85,7 +85,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder { @@ -85,7 +85,7 @@ final class DefaultErrorResponseBuilder implements ErrorResponse.Builder {
}
@Override
public ErrorResponse.Builder type(URI type) {
public ErrorResponse.Builder type(@Nullable URI type) {
this.problemDetail.setType(type);
return this;
}

2
spring-web/src/main/java/org/springframework/web/ErrorResponseException.java

@ -111,7 +111,7 @@ public class ErrorResponseException extends NestedRuntimeException implements Er @@ -111,7 +111,7 @@ public class ErrorResponseException extends NestedRuntimeException implements Er
* Set the {@link ProblemDetail#setType(URI) type} field of the response body.
* @param type the problem type
*/
public void setType(URI type) {
public void setType(@Nullable URI type) {
this.body.setType(type);
}

12
spring-web/src/test/java/org/springframework/http/converter/json/ProblemDetailJacksonMixinTests.java

@ -42,12 +42,11 @@ class ProblemDetailJacksonMixinTests { @@ -42,12 +42,11 @@ class ProblemDetailJacksonMixinTests {
@Test
void writeStatusAndHeaders() throws Exception {
void writeStatusAndHeaders() {
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
testWrite(detail,
"""
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Missing header"
@ -55,14 +54,13 @@ class ProblemDetailJacksonMixinTests { @@ -55,14 +54,13 @@ class ProblemDetailJacksonMixinTests {
}
@Test
void writeCustomProperty() throws Exception {
void writeCustomProperty() {
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Missing header");
detail.setProperty("host", "abc.org");
detail.setProperty("user", null);
testWrite(detail, """
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Missing header",
@ -72,7 +70,7 @@ class ProblemDetailJacksonMixinTests { @@ -72,7 +70,7 @@ class ProblemDetailJacksonMixinTests {
}
@Test
void readCustomProperty() throws Exception {
void readCustomProperty() {
ProblemDetail detail = this.mapper.readValue("""
{
"type": "about:blank",
@ -93,7 +91,7 @@ class ProblemDetailJacksonMixinTests { @@ -93,7 +91,7 @@ class ProblemDetailJacksonMixinTests {
}
@Test
void readCustomPropertyFromXml() throws Exception {
void readCustomPropertyFromXml() {
ObjectMapper xmlMapper = XmlMapper.builder().addMixIn(ProblemDetail.class, ProblemDetailJacksonMixin.class).build();
ProblemDetail detail = xmlMapper.readValue("""
<problem xmlns="urn:ietf:rfc:7807">
@ -111,7 +109,7 @@ class ProblemDetailJacksonMixinTests { @@ -111,7 +109,7 @@ class ProblemDetailJacksonMixinTests {
assertThat(detail.getProperties()).containsEntry("host", "abc.org");
}
private void testWrite(ProblemDetail problemDetail, String expected) throws Exception {
private void testWrite(ProblemDetail problemDetail, String expected) {
String output = this.mapper.writeValueAsString(problemDetail);
JSONAssert.assertEquals(expected, output, false);
}

3
spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java

@ -125,8 +125,7 @@ public class DispatcherHandlerErrorTests { @@ -125,8 +125,7 @@ public class DispatcherHandlerErrorTests {
"detail":"No static resource non-existing.",\
"instance":"\\/resources\\/non-existing",\
"status":404,\
"title":"Not Found",\
"type":"about:blank"}\
"title":"Not Found"}\
""");
}

6
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingExceptionHandlingIntegrationTests.java

@ -125,8 +125,7 @@ class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMap @@ -125,8 +125,7 @@ class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMap
assertThat(ex.getResponseBodyAsString()).isEqualTo("{" +
"\"instance\":\"\\/no-such-handler\"," +
"\"status\":404," +
"\"title\":\"Not Found\"," +
"\"type\":\"about:blank\"}");
"\"title\":\"Not Found\"}");
});
}
@ -142,8 +141,7 @@ class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMap @@ -142,8 +141,7 @@ class RequestMappingExceptionHandlingIntegrationTests extends AbstractRequestMap
"\"detail\":\"Required query parameter 'q' is not present.\"," +
"\"instance\":\"\\/missing-request-parameter\"," +
"\"status\":400," +
"\"title\":\"Bad Request\"," +
"\"type\":\"about:blank\"}");
"\"title\":\"Bad Request\"}");
});
}

3
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java

@ -152,8 +152,7 @@ class ResponseBodyResultHandlerTests { @@ -152,8 +152,7 @@ class ResponseBodyResultHandlerTests {
{\
"status":400,\
"instance":"\\/path",\
"title":"Bad Request",\
"type":"about:blank"\
"title":"Bad Request"\
}""");
}

6
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java

@ -244,8 +244,7 @@ class ResponseEntityResultHandlerTests { @@ -244,8 +244,7 @@ class ResponseEntityResultHandlerTests {
{\
"instance":"\\/path",\
"status":400,\
"title":"Bad Request",\
"type":"about:blank"\
"title":"Bad Request"\
}""");
}
@ -265,8 +264,7 @@ class ResponseEntityResultHandlerTests { @@ -265,8 +264,7 @@ class ResponseEntityResultHandlerTests {
{\
"instance":"\\/path",\
"status":400,\
"title":"Bad Request",\
"type":"about:blank"\
"title":"Bad Request"\
}""");
}

2
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

@ -395,7 +395,6 @@ class RequestResponseBodyMethodProcessorTests { @@ -395,7 +395,6 @@ class RequestResponseBodyMethodProcessorTests {
<status>400</status>
<instance>/path</instance>
<title>Bad Request</title>
<type>about:blank</type>
</problem>""")
.ignoreWhitespace()
.areIdentical();
@ -403,7 +402,6 @@ class RequestResponseBodyMethodProcessorTests { @@ -403,7 +402,6 @@ class RequestResponseBodyMethodProcessorTests {
else {
JSONAssert.assertEquals("""
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"instance": "/path"

3
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java

@ -144,8 +144,7 @@ class ResourceHttpRequestHandlerIntegrationTests { @@ -144,8 +144,7 @@ class ResourceHttpRequestHandlerIntegrationTests {
"detail":"No static resource non-existing.",\
"instance":"\\/cp\\/non-existing",\
"status":404,\
"title":"Not Found",\
"type":"about:blank"\
"title":"Not Found"\
}\
""");
}

Loading…
Cancel
Save