treat empty/blank @DiscriminatorValue literally

the spec doesn't say it can't be empty, and so I think this
is the most natural interpretation
This commit is contained in:
Gavin King 2024-12-11 12:37:40 +01:00
parent 6cfcee5e4b
commit b8a6097f5b
1 changed files with 13 additions and 13 deletions

View File

@ -1596,27 +1596,27 @@ public class EntityBinder {
public void bindDiscriminatorValue() { public void bindDiscriminatorValue() {
final DiscriminatorValue discriminatorValueAnn = final DiscriminatorValue discriminatorValueAnn =
annotatedClass.getAnnotationUsage( DiscriminatorValue.class, getSourceModelContext() ); annotatedClass.getAnnotationUsage( DiscriminatorValue.class, getSourceModelContext() );
final String discriminatorValue = discriminatorValueAnn != null if ( discriminatorValueAnn == null ) {
? discriminatorValueAnn.value()
: null;
if ( isBlank( discriminatorValue ) ) {
final Value discriminator = persistentClass.getDiscriminator(); final Value discriminator = persistentClass.getDiscriminator();
if ( discriminator == null ) { if ( discriminator == null ) {
persistentClass.setDiscriminatorValue( name ); persistentClass.setDiscriminatorValue( name );
} }
else if ( "character".equals( discriminator.getType().getName() ) ) { else {
switch ( discriminator.getType().getName() ) {
case "character":
throw new AnnotationException( "Entity '" + name throw new AnnotationException( "Entity '" + name
+ "' has a discriminator of character type and must specify its '@DiscriminatorValue'" ); + "' has a discriminator of character type and must specify its '@DiscriminatorValue'" );
} case "integer":
else if ( "integer".equals( discriminator.getType().getName() ) ) { // TODO: pretty nasty, should we just deprecate/disallow this?
persistentClass.setDiscriminatorValue( String.valueOf( name.hashCode() ) ); persistentClass.setDiscriminatorValue( String.valueOf( name.hashCode() ) );
} break;
else { default:
persistentClass.setDiscriminatorValue( name ); //Spec compliant persistentClass.setDiscriminatorValue( name ); //Spec compliant
} }
} }
}
else { else {
persistentClass.setDiscriminatorValue( discriminatorValue ); persistentClass.setDiscriminatorValue( discriminatorValueAnn.value() );
} }
} }