-
Notifications
You must be signed in to change notification settings - Fork 41.1k
Problems with Spring and Bean Validation interpolation messages #42773
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
Comments
I've only looked at the first branch, and it's hard to know exactly what you're looking at as the The default message in the object errors is resolved by looking up I'm not going to investigate further at this point as I suspect the second and third problems may be a knock-on effect of the misunderstanding that's caused the first. If applying the change suggested above does not help with the second and third problems and you would like us to investigate further, please update them so that there's a test that we can run that precisely reproduces the problem rather than us trying to guess what part of the state in the debugger it is that you consider to be incorrect. |
As I understood you want to interpolate and include validation errors in the I checked your first branch messages.properties NotBlank.person.name=The field {0} must not be blank
Size.person.name=The size of the {0} field must be between {2} and {1}
problemDetail.org.springframework.web.bind.MethodArgumentNotValidException={0}{1} If you would like to support messages_pt_BR.properties NotBlank.person.name=O campo {0} n\u00e3o deve estar em branco
Size.person.name=O tamanho do campo {0} deve estar entre {2} e {1} HTTP Request: POST http://localhost:8080/people
Content-Type: application/json
Accept-Language: pt-BR
{
"name": ""
}
HTTP Response: {
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "O campo name não deve estar em branco, and O tamanho do campo name deve estar entre 1 e 50",
"instance": "/people"
} |
Hi @wilkinsona, thank you for the reply. record Person(@Size(min = 1, max = 10) String name) {
}
@Validated
public class MyService {
void addStudent(@Valid Person person, @Max(2) int degrees) {
// ...
}
} The example does not use the message property and I tried to do that same way, so I got the default Bean Validation message. On the branch spring-doc-with-message I put the Spring code on the message attribute, I got the message but Spring did not interpolate the messages. And on the bean-validation it was worst, using Bean Validation interpolation way Spring Boot broke with an exception. |
Hi @nosan, thank you for the reply. Yes, if I try to use the detail message it will work, but this occurs because Spring finishes the interpolation after validation using the method |
I will update the branch spring-doc-with-message to return the messages without interpolation, this way will be easier to see my point. I need to customize individual validation messages with Spring way (using placeholders like {0}...) or Bean Validation way (using expressions and parameters values like {min}, {max}). |
I have just updated the branch spring-doc-with-message, now it is possible to see that the validation messages were not interpolated appropriately. Result: {
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Invalid request content.",
"instance": "/people",
"errors": {
"Size": "The size of the {0} field must be between {1} and {2}",
"NotBlank": "The field {0} must not be blank"
}
} |
Some time ago, Spring Boot introduced Bean Validation Message Interpolation via The primary goal of this enhancement was to utilize Let’s consider the following example: message.properties NotBlank.person.name=The field name must not be blank
Size.person.name=The size of the name field must be between {min} and {max} Additionally, if you remove the {
"timestamp": "2024-10-17T20:13:29.504+00:00",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [
"Size.person.name",
"Size.name",
"Size.java.lang.String",
"Size"
],
"arguments": [
{
"codes": [
"person.name",
"name"
],
"arguments": null,
"defaultMessage": "name",
"code": "name"
},
50,
1
],
"defaultMessage": "The size of the name field must be between 1 and 50",
"objectName": "person",
"field": "name",
"rejectedValue": "",
"bindingFailure": false,
"code": "Size"
},
{
"codes": [
"NotBlank.person.name",
"NotBlank.name",
"NotBlank.java.lang.String",
"NotBlank"
],
"arguments": [
{
"codes": [
"person.name",
"name"
],
"arguments": null,
"defaultMessage": "name",
"code": "name"
}
],
"defaultMessage": "The field name must not be blank",
"objectName": "person",
"field": "name",
"rejectedValue": "",
"bindingFailure": false,
"code": "NotBlank"
}
],
"path": "/people"
}
As you can see, the interpolation works as expected. However, when you have added The main issue is that the Spring Framework also attempts to resolve Bean Validation's codes using
As you can see, the code
With that in mind, I can suggest the following options:
|
Thanks @nosan! It doesn't look like this is a Spring Boot bug so I'll close the issue. |
@nosan and @wilkinsona I took some time to investigate how things work deeply and understood exactly how Spring works with Bean Validation. It was a misunderstanding on my side believing that Spring would interpolate the messages automatically. I saw that I needed to use one of the Thank you. |
Hi,
I am using:
I am using the default Spring Boot auto-configuration, there is no customization on the project. I have a Controller Advice that is inherent from
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
, and I want to use Problem Details as a response error format in my project.1 - I need to customize my validation messages and interpolate some values. I am trying to follow the Spring Framework documentation. However following the documentation instructions I got the default messages from Bean Validation (in my language pt-BR).
2 - Then I tried to put the Spring codes on the annotations message attribute. I got the messages from the
messages.properties
file but the arguments {0}, {1}, {2}, etc. do not were interpolated by Spring.3 - Finally, I changed the strategy and resolved to use the Bean Validation interpolation format, I got the correct messages, but when Spring Boot tried to resolve the Problem Detail fields I got an unexpected exception.
The following project can be used to simulate the problems: spring-boot-bean-validation-message-interpolation-issue
Public
Three branches simulate the respective problems:
1 - spring-doc
2 - spring-doc-with-message
3 - bean-validation
I read the Spring documentation many times and debugged the project, but I did not find a way to make the project work as expected.
Let me know if I missed some steps to make Bean Validation work with Spring Boot and be able to customize my messages according to the official documentation.
The text was updated successfully, but these errors were encountered: