Properly consider frozen map creation.

We now correctly consider the Frozen annotation when annotating a property to define a frozen<map<…>> type. Previously we only considered frozen key and value components instead of the entire type.

Closes #1148
This commit is contained in:
Mark Paluch
2021-07-26 11:20:18 +02:00
parent 686fd64c8b
commit 74fe0924f3
3 changed files with 29 additions and 2 deletions

View File

@@ -159,8 +159,21 @@ public interface ColumnType {
* @return
*/
static CassandraColumnType mapOf(CassandraColumnType keyType, CassandraColumnType valueType) {
return ColumnType.mapOf(keyType, valueType, false);
}
/**
* Creates a Map {@link CassandraColumnType} given its {@link CassandraColumnType key and value types}.
*
* @param keyType must not be {@literal null}.
* @param valueType must not be {@literal null}.
* @param frozen
* @return
* @since 3.2.4
*/
static CassandraColumnType mapOf(CassandraColumnType keyType, CassandraColumnType valueType, boolean frozen) {
return new DefaultCassandraColumnType(ClassTypeInformation.MAP,
() -> DataTypes.mapOf(keyType.getDataType(), valueType.getDataType()), keyType, valueType);
() -> DataTypes.mapOf(keyType.getDataType(), valueType.getDataType(), frozen), keyType, valueType);
}
/**

View File

@@ -410,7 +410,7 @@ class DefaultColumnTypeResolver implements ColumnTypeResolver {
FrozenIndicator frozenValue = frozen.getFrozen(1);
return ColumnType.mapOf(resolve(typeInformation.getRequiredComponentType(), frozenKey),
resolve(typeInformation.getRequiredMapValueType(), frozenValue));
resolve(typeInformation.getRequiredMapValueType(), frozenValue), frozen.isFrozen());
}
CassandraPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(typeInformation);

View File

@@ -260,6 +260,18 @@ public class ColumnTypeResolverUnitTests {
.describedAs("The element type should be frozen").isTrue();
}
@Test // GH-1148
void frozenMapProperty() {
BasicCassandraPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(Person.class);
DataType dataType = resolver.resolve(entity.getRequiredPersistentProperty("frozenMap")).getDataType();
assertThat(dataType).isInstanceOf(MapType.class);
assertThat(((MapType) dataType).isFrozen()).isTrue();
assertThat(dataType.asCql(true, false)).isEqualTo("frozen<map<text, text>>");
}
@Test // DATACASS-465
void mapPropertyWithFrozenAnnotationOnKey() {
@@ -345,6 +357,8 @@ public class ColumnTypeResolverUnitTests {
@Frozen Set<String> frozenSet;
Set<@Frozen MyUdt> frozenSetContent;
@Frozen Map<String, String> frozenMap;
Map<@Frozen MyUdt, MyUdt> frozenMapKey;
Map<MyUdt, @Frozen MyUdt> frozenMapValue;