Skip to content

Commit 4a48748

Browse files
DavideDmarko-bekhta
authored andcommitted
HV-1591 Log a warning for improper use of Valid annotation
Signed-off-by: marko-bekhta <[email protected]>
1 parent f6b7388 commit 4a48748

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

engine/src/main/java/org/hibernate/validator/internal/metadata/aggregated/CascadingMetaDataBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ public CascadingMetaData build(ValueExtractorManager valueExtractorManager, Obje
224224
Set<ValueExtractorDescriptor> containerDetectionValueExtractorCandidates = valueExtractorManager.getResolver()
225225
.getValueExtractorCandidatesForContainerDetectionOfGlobalCascadedValidation( enclosingType );
226226
if ( !containerDetectionValueExtractorCandidates.isEmpty() ) {
227+
// Using @Valid on a container is deprecated at the moment. You are supposed to apply the annotation on the type argument(s).
228+
LOG.deprecatedUseOfValidOnContainer( ReflectionHelper.getClassFromType( enclosingType ), context );
229+
227230
if ( containerDetectionValueExtractorCandidates.size() > 1 ) {
228231
throw LOG.getUnableToGetMostSpecificValueExtractorDueToSeveralMaximallySpecificValueExtractorsDeclaredException(
229232
ReflectionHelper.getClassFromType( enclosingType ),
@@ -259,6 +262,7 @@ public CascadingMetaData build(ValueExtractorManager valueExtractorManager, Obje
259262
// and they will be used at runtime to check if any of those could be applied to a runtime type and if PotentiallyContainerCascadingMetaData
260263
// should be promoted to ContainerCascadingMetaData or not.
261264
if ( !potentialValueExtractorCandidates.isEmpty() ) {
265+
LOG.potentiallyDeprecatedUseOfValidOnContainer( ReflectionHelper.getClassFromType( enclosingType ), context );
262266
return PotentiallyContainerCascadingMetaData.of( this, potentialValueExtractorCandidates, context );
263267
}
264268

engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,11 @@ ConstraintDefinitionException getConstraintValidatorDefinitionConstraintMismatch
952952
void resourceBundleNotPreLoaded(Locale localeToPreload);
953953

954954
@LogMessage(level = WARN)
955-
@Message(id = 271, value = "Using `@Valid` on a container (%1$s) is deprecated. You should apply the annotation on the type argument(s). Field name: %2$s")
955+
@Message(id = 271, value = "Using `@Valid` on a container (%1$s) is deprecated. You should apply the annotation on the type argument(s). Affected element: %2$s")
956956
void deprecatedUseOfValidOnContainer(@FormatWith(ClassObjectFormatter.class) Class<?> valueType, Object context);
957+
958+
@LogMessage(level = WARN)
959+
@Message(id = 272,
960+
value = "Using `@Valid` on a container is deprecated. You should apply the annotation on the type argument(s). (%1$s) can potentially be a container at runtime. Affected element: %2$s")
961+
void potentiallyDeprecatedUseOfValidOnContainer(@FormatWith(ClassObjectFormatter.class) Class<?> valueType, Object context);
957962
}

0 commit comments

Comments
 (0)