Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detail is not localized when problemdetails enabled #34791

Closed
quaff opened this issue Mar 28, 2023 · 1 comment
Closed

Detail is not localized when problemdetails enabled #34791

quaff opened this issue Mar 28, 2023 · 1 comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid

Comments

@quaff
Copy link
Contributor

quaff commented Mar 28, 2023

@RestController
@SpringBootApplication
public class DemoApplication {

    @RequestMapping("/")
    String home() {
        throw new ResponseStatusException(BAD_REQUEST, "bad.request");
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"spring.mvc.problemdetails.enabled=true"})
class ProblemDetailsTests {

	@Autowired
	private TestRestTemplate testRestTemplate;

	@Autowired
	private MessageSource messageSource;
	@Test
	void detailShouldBeLocalized() {
		ResponseEntity<Map<String, Object>> resp = testRestTemplate.exchange(RequestEntity.method(GET, "/").build(),
				new ParameterizedTypeReference<Map<String, Object>>() {
				});
		assertThat(resp.getBody().get("detail")).isEqualTo(messageSource.getMessage("bad.request",null,null)); // failed
	}

}
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
class ErrorAttributesTests {

	@Autowired
	private TestRestTemplate testRestTemplate;

	@Autowired
	private MessageSource messageSource;
	@Test
	void messageShouldBeLocalized() {
		ResponseEntity<Map<String, Object>> resp = testRestTemplate.exchange(RequestEntity.method(GET, "/").build(),
				new ParameterizedTypeReference<Map<String, Object>>() {
				});
		assertThat(resp.getBody().get("message")).isEqualTo(messageSource.getMessage("bad.request",null,null)); // passed
	}

}

detail should be localized like traditional message.
Here is test project demo.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 28, 2023
@wilkinsona
Copy link
Member

wilkinsona commented Mar 28, 2023

Localisation of problem details is handled by Spring Framework and, therefore, is out of Spring Boot's control. As you have seen it doesn't localise the reason. Instead, it uses the message detail code. I believe that this will work:

throw new ResponseStatusException(BAD_REQUEST, "bad.request") {

	@Override
	public String getDetailMessageCode() {
		return "bad.request";
	}

};

If you think that the localisation support could be improved, please open a Spring Framework issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants