Skip to content

Commit

Permalink
Polishing ProblemDetail support
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jun 24, 2022
1 parent 16c43c2 commit 45ee791
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@

/**
* Representation of an RFC 7807 problem detail, including all RFC-defined
* fields. For an extended response with more fields, create a subclass that
* exposes the additional fields.
* properties.
*
* <p>For an extended response, create a subclass with additional properties.
* A subclass can use the {@link ProblemDetail#ProblemDetail(ProblemDetail)}
* copy constructor to extend an existing {@code ProblemDetail} instance.
*
* @author Rossen Stoyanchev
* @since 6.0
Expand Down Expand Up @@ -63,8 +66,8 @@ protected ProblemDetail(int rawStatusCode) {
}

/**
* Copy constructor that could be used from a subclass to re-create a
* {@code ProblemDetail} in order to extend it with more fields.
* Copy constructor that a subclass can use to re-create and extend a
* {@code ProblemDetail} with additional properties.
*/
protected ProblemDetail(ProblemDetail other) {
this.type = other.type;
Expand All @@ -75,7 +78,7 @@ protected ProblemDetail(ProblemDetail other) {
}

/**
* For deserialization.
* No-arg constructor, for deserialization.
*/
protected ProblemDetail() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,11 @@ public static <T> ResponseEntity<T> of(Optional<T> body) {

/**
* Create a builder for a {@code ResponseEntity} with the given
* {@link ProblemDetail} as the body, also matching to its
* {@link ProblemDetail#getStatus() status}. An {@code @ExceptionHandler}
* method can use to add response headers, or otherwise it can return
* {@code ProblemDetail}.
* {@link ProblemDetail} as the body, and its
* {@link ProblemDetail#getStatus() status} as the status.
* <p>Note that {@code ProblemDetail} is supported as a return value from
* controller methods and from {@code @ExceptionHandler} methods. The method
* here is convenient to also add response headers.
* @param body the details for an HTTP error response
* @return the created builder
* @since 6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* <p>The exception can be used as is, or it can be extended as a more specific
* exception that populates the {@link ProblemDetail#setType(URI) type} or
* {@link ProblemDetail#setDetail(String) detail} fields, or potentially adds
* other non-standard fields.
* other non-standard properties.
*
* @author Rossen Stoyanchev
* @since 6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
throw ex;
}

// Fall back on RFC 7807 format for ProblemDetail
// For ProblemDetail, fall back on RFC 7807 format
if (bestMediaType == null && elementType.toClass().equals(ProblemDetail.class)) {
bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType), this.problemMediaTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ else if (returnValue instanceof ErrorResponse response) {
httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getStatusCode());
}
else if (returnValue instanceof ProblemDetail detail) {
httpEntity = new ResponseEntity<>(returnValue, HttpHeaders.EMPTY, detail.getStatus());
httpEntity = ResponseEntity.of(detail).build();
}
else if (returnValue instanceof HttpHeaders) {
httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter
List<MediaType> compatibleMediaTypes = new ArrayList<>();
determineCompatibleMediaTypes(acceptableTypes, producibleTypes, compatibleMediaTypes);

// Fall back on RFC 7807 format for ProblemDetail
// For ProblemDetail, fall back on RFC 7807 format
if (compatibleMediaTypes.isEmpty() && ProblemDetail.class.isAssignableFrom(valueType)) {
determineCompatibleMediaTypes(this.problemMediaTypes, producibleTypes, compatibleMediaTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getStatusCode());
}
else if (returnValue instanceof ProblemDetail detail) {
httpEntity = new ResponseEntity<>(returnValue, HttpHeaders.EMPTY, detail.getStatus());
httpEntity = ResponseEntity.of(detail).build();
}
else {
Assert.isInstanceOf(HttpEntity.class, returnValue);
Expand Down

0 comments on commit 45ee791

Please sign in to comment.