I read that the usage of @jakarta.validation.constraints.Pattern
should be used on return types of Charset
. The issue that I am facing is that in my case, I am using generated java files from some gradle tasks that puts the Patterns on all getter methods, no matter the return type. At time of generation it is difficult to determine whether the return type is String or not, so the Pattern annotations are set on all the getter methods.
Now because we are working with a lot of dynamic data, we want to validate these inputs based on a flag validate
.
When validate
is set to true
, I try to trigger validator.validate(view)
which then throws jakarta.validation.UnexpectedTypeException
:
ExampleService.kt:
import jakarta.validation.Validator
import com.example.generated.DEXPDF
// more imports
@Service
class ExampleService(
// ...
private val validator: Validator
)
// ... some code
private fun assertRequestValid(view: DEXPDF) {
val violations = validator.validate(view) // throws UnexpectedTypeException
if (violations.isNotEmpty()) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, violations.joinToString(", "))
}
}
// ... some more code
The generated java looks something like that, this is just a small chunk of the file:
DEXPDF.java:
// some code...
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
@Size(min = 1, max = 7)
@JsonProperty("messageVersion")
@Pattern(regexp = "F\.[1-9][0-9]?\.[1-9]?[0-9]")
public String getMessageVersion() {
return messageVersion;
}
/**
* Sets the value of the messageVersion property.
*
* @param value
* allowed object is
* {@link String }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
public void setMessageVersion(String value) {
this.messageVersion = value;
}
/**
* Gets the value of the messageSender property.
*
* @return
* possible object is
* {@link DEXPDF.MessageSender }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
@Size(min = 1, max = 17)
@JsonProperty("messageSender")
@Pattern(regexp = "[A-Z]{2}[!-~]{1,15}")
public DEXPDF.MessageSender getMessageSender() {
return messageSender;
}
/**
* Sets the value of the messageSender property.
*
* @param value
* allowed object is
* {@link DEXPDF.MessageSender }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
public void setMessageSender(DEXPDF.MessageSender value) {
this.messageSender = value;
}
/**
* Gets the value of the messageRecipient property.
*
* @return
* possible object is
* {@link DEXPDF.MessageRecipient }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
@JsonProperty("messageRecipient")
@Pattern(regexp = "DE0[01][0-9]{4}")
public DEXPDF.MessageRecipient getMessageRecipient() { // throws exception due to unexpected type
return messageRecipient;
}
// more code...
How can I tell the validator to just ignore the unexpected types instead of throwing an exception? If this is not configurable, is there an alternative?
I tried to surround it with a try catch which of course did not help at all. I also tried to create a custom factory by implementing the jakarta.validation.Validator
but at that point I did not know how to continue, there wasn’t a “straightforward” way to configure that.