1

I'm trying to add validation to a bean so I can verify its state before persisting it. The class for the bean that requires validation is generated by the openapi-generator-maven-plugin plugin from an OpenAPI contract. As the contract contains restrictions, they Jakarta constraint annotations are present on the generated bean as well. It looks like this (simplified):

public class User {

    @NotNull
    private String name;
}

I added a messages.properties file in the src/main/resources folder with the following content:

jakarta.validation.constraints.NotNull.message=global not null error message

If I then execute the jakarta.validation.Validator.validate(user), the interpolated message matches the configured one. But this configuration applies to all properties that are null, while I only want to apply this to this specific property.

Is there a way I can configure the bean validator to return a specific message through configuration, without having to modify the bean (as it is generated)? I tried to add the following to the messages.properties (suggested by AI):

org.company.User.name.NotNull=Username cannot be null

But the validation framework just seems to ignore this?

All help is appreciated.

1 Answer 1

0

You should set up the message field of the annotation.

@NotNull(message = "{org.company.User.name.NotNull}")
private String name;
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sure this would work, but since the class is generated, I'm not able to modify it (directly)
You could use openapi-generator.tech/docs/file-post-processing or create a custom generator extending that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.