Browse Source

Allow fallback on subclass of ProblemDetail

Closes gh-30533
pull/30619/head
rstoyanchev 3 years ago
parent
commit
454a85978f
  1. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java
  2. 15
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java
  3. 15
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

2
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java

@ -175,7 +175,7 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa @@ -175,7 +175,7 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa
}
// For ProblemDetail, fall back on RFC 7807 format
if (bestMediaType == null && elementType.toClass().equals(ProblemDetail.class)) {
if (bestMediaType == null && ProblemDetail.class.isAssignableFrom(elementType.toClass())) {
bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType), this.problemMediaTypes);
}

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

@ -139,9 +139,9 @@ public class ResponseBodyResultHandlerTests { @@ -139,9 +139,9 @@ public class ResponseBodyResultHandlerTests {
}
private void testProblemDetailMediaType(MockServerWebExchange exchange, MediaType expectedMediaType) {
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
MyProblemDetail problemDetail = new MyProblemDetail(HttpStatus.BAD_REQUEST);
Method method = on(TestRestController.class).returning(ProblemDetail.class).resolveMethod();
Method method = on(TestRestController.class).returning(MyProblemDetail.class).resolveMethod();
HandlerResult result = getHandlerResult(new TestRestController(), problemDetail, method);
this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
@ -196,7 +196,7 @@ public class ResponseBodyResultHandlerTests { @@ -196,7 +196,7 @@ public class ResponseBodyResultHandlerTests {
return null;
}
public ProblemDetail handleToProblemDetail() {
public MyProblemDetail handleToProblemDetail() {
return null;
}
@ -217,4 +217,13 @@ public class ResponseBodyResultHandlerTests { @@ -217,4 +217,13 @@ public class ResponseBodyResultHandlerTests {
}
}
private static class MyProblemDetail extends ProblemDetail {
public MyProblemDetail(HttpStatus status) {
super(status.value());
}
}
}

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
/*
`/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -374,7 +374,7 @@ class RequestResponseBodyMethodProcessorTests { @@ -374,7 +374,7 @@ class RequestResponseBodyMethodProcessorTests {
}
private void testProblemDetailMediaType(String expectedContentType) throws Exception {
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
MyProblemDetail problemDetail = new MyProblemDetail(HttpStatus.BAD_REQUEST);
this.servletRequest.setRequestURI("/path");
@ -805,7 +805,7 @@ class RequestResponseBodyMethodProcessorTests { @@ -805,7 +805,7 @@ class RequestResponseBodyMethodProcessorTests {
}
@SuppressWarnings("ConstantConditions")
ProblemDetail handleAndReturnProblemDetail() {
MyProblemDetail handleAndReturnProblemDetail() {
return null;
}
@ -821,6 +821,15 @@ class RequestResponseBodyMethodProcessorTests { @@ -821,6 +821,15 @@ class RequestResponseBodyMethodProcessorTests {
}
private static class MyProblemDetail extends ProblemDetail {
public MyProblemDetail(HttpStatus status) {
super(status.value());
}
}
private static abstract class MyParameterizedController<DTO extends Identifiable> {
@SuppressWarnings("unused")

Loading…
Cancel
Save