Skip to content

Commit

Permalink
Update contribution
Browse files Browse the repository at this point in the history
Closes gh-30300
  • Loading branch information
rstoyanchev committed Jan 4, 2024
1 parent a3532bf commit e0d6b69
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
* {@code @RestController} or {@code RestControllerAdvice} class.
*
* @author Rossen Stoyanchev
* @author Yanming Zhou
* @since 6.0
* @see ErrorResponseException
*/
Expand Down Expand Up @@ -143,14 +142,6 @@ default ProblemDetail updateAndGetBody(@Nullable MessageSource messageSource, Lo
if (detail != null) {
getBody().setDetail(detail);
}
else {
// detail from ResponseStatusException reason may be message code
detail = getBody().getDetail();
if (detail != null) {
detail = messageSource.getMessage(detail, null, detail, locale);
getBody().setDetail(detail);
}
}
String title = messageSource.getMessage(getTitleMessageCode(), null, null, locale);
if (title != null) {
getBody().setTitle(title);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-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.
Expand All @@ -16,6 +16,9 @@

package org.springframework.web.server;

import java.util.Locale;

import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
Expand Down Expand Up @@ -127,6 +130,23 @@ public HttpHeaders getResponseHeaders() {
return HttpHeaders.EMPTY;
}

@Override
public ProblemDetail updateAndGetBody(@Nullable MessageSource messageSource, Locale locale) {
super.updateAndGetBody(messageSource, locale);

// The reason may be a code (consistent with ResponseStatusExceptionResolver)

if (messageSource != null && getReason() != null && getReason().equals(getBody().getDetail())) {
Object[] arguments = getDetailMessageArguments(messageSource, locale);
String resolved = messageSource.getMessage(getReason(), arguments, null, locale);
if (resolved != null) {
getBody().setDetail(resolved);
}
}

return getBody();
}

@Override
public String getMessage() {
return getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : "");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-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.
Expand All @@ -25,6 +25,7 @@

import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
Expand All @@ -51,6 +52,7 @@
import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.server.MissingRequestValueException;
import org.springframework.web.server.NotAcceptableStatusException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerErrorException;
import org.springframework.web.server.UnsatisfiedRequestParameterException;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
Expand Down Expand Up @@ -415,6 +417,28 @@ void methodNotAllowedExceptionWithoutSupportedMethods() {
assertThat(ex.getHeaders()).isEmpty();
}

@Test // gh-30300
void responseStatusException() {

Locale locale = Locale.UK;
LocaleContextHolder.setLocale(locale);

try {
String reason = "bad.request";
String message = "Breaking Bad Request";
StaticMessageSource messageSource = new StaticMessageSource();
messageSource.addMessage(reason, locale, message);

ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, reason);

ProblemDetail problemDetail = ex.updateAndGetBody(messageSource, locale);
assertThat(problemDetail.getDetail()).isEqualTo(message);
}
finally {
LocaleContextHolder.resetLocaleContext();
}
}

private void assertStatus(ErrorResponse ex, HttpStatus status) {
ProblemDetail body = ex.getBody();
assertThat(ex.getStatusCode()).isEqualTo(status);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-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.
Expand Down Expand Up @@ -201,21 +201,21 @@ public void errorResponseProblemDetailViaMessageSource() {
}
}

@Test
@Test // gh-30300
public void reasonAsDetailShouldBeUpdatedViaMessageSource() {

Locale locale = Locale.UK;
LocaleContextHolder.setLocale(locale);

String code = "bad.request";
String reason = "bad.request";
String message = "Breaking Bad Request";
try {
StaticMessageSource messageSource = new StaticMessageSource();
messageSource.addMessage(code, locale, message);
messageSource.addMessage(reason, locale, message);

this.exceptionHandler.setMessageSource(messageSource);

ResponseEntity<?> entity = testException(new ResponseStatusException(HttpStatus.BAD_REQUEST, code));
ResponseEntity<?> entity = testException(new ResponseStatusException(HttpStatus.BAD_REQUEST, reason));

ProblemDetail body = (ProblemDetail) entity.getBody();
assertThat(body.getDetail()).isEqualTo(message);
Expand Down

0 comments on commit e0d6b69

Please sign in to comment.