Skip to content

Handle serializing arrays of primitives [DATAREDIS-509] #1086

Closed
@spring-projects-issues

Description

@spring-projects-issues

Maxim Novik opened DATAREDIS-509 and commented

MappingRedisConverter.writeInternal() does a check against properties. If they are "collection like" then there is an additional check for array. However, array is assumed to be an array of objects and is cast to Object[]. That does not work for array of primitives.

if (persistentProperty.isMap()) {
					writeMap(keyspace, propertyStringPath, persistentProperty.getMapValueType(),
							(Map<?, ?>) accessor.getProperty(persistentProperty), sink);
				} else if (persistentProperty.isCollectionLike()) {

					final Object property = accessor.getProperty(persistentProperty);

					if (property == null || Iterable.class.isAssignableFrom(property.getClass())) {

						writeCollection(keyspace, propertyStringPath, (Iterable<?>) property,
								persistentProperty.getTypeInformation().getComponentType(), sink);
					} else if (property.getClass().isArray()) {

						writeCollection(keyspace, propertyStringPath, Arrays.asList((Object[]) property),
								persistentProperty.getTypeInformation().getComponentType(), sink);
					} else {

						throw new RuntimeException("Don't know how to handle " + property.getClass() + " type collection");
					}

If we add the following test to org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java:

@Test
    public void writeHandlesArraysOfPrimitivesProperly() {
        WithArrays source = new WithArrays();
        source.arrayOfPrimitives = new int[] { 1, 2, 3 };
        assertThat(write(source).getBucket(), isBucket().containingUtf8String("arrayOfPrimitives.[0]", "1")
                .containingUtf8String("arrayOfPrimitives.[1]", "2")
                .containingUtf8String("arrayOfPrimitives.[2]", "3"));

    }

, it fails with:

java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;

Affects: 1.7.1 (Hopper SR1)

Referenced from: pull request #196

Backported to: 1.7.2 (Hopper SR2)

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions