Skip to content

delete() crashes with ClassCastException for ID-Types other than String #315

@aburmeis

Description

@aburmeis

With a @Document or @Edge annotated class having a keyType=KeyType.uuid and therefore an @Id annotated attribute of type UUID a call to CrupRepository.delete() (aka SimpleArangoRepository.delete()) will crash resulting in a ClassCastException as the code casts the identifier to String without any need:

	public void delete(final T entity) {
		String id = (String) arangoOperations.getConverter().getMappingContext()
				.getRequiredPersistentEntity(domainClass).getIdentifierAccessor(entity).getRequiredIdentifier();
		arangoOperations.delete(id, defaultDeleteOptions(), domainClass);
	}

Just removing the cast and declaring id as Object should fix the issue:

	public void delete(final T entity) {
		Object id = arangoOperations.getConverter().getMappingContext()
				.getRequiredPersistentEntity(domainClass).getIdentifierAccessor(entity).getRequiredIdentifier();
		arangoOperations.delete(id, defaultDeleteOptions(), domainClass);
	}

Even better also fix the Spring-Data contract of CrupRepository.delete():
Throws: OptimisticLockingFailureException – [...] Also thrown if the entity is assumed to be present but does not exist in the database.

	public void delete(final T entity) {
		Object id = arangoOperations.getConverter().getMappingContext()
				.getRequiredPersistentEntity(domainClass).getIdentifierAccessor(entity).getRequiredIdentifier();
                try {
		    arangoOperations.delete(id, defaultDeleteOptions(), domainClass);
		} catch (DocumentNotFoundException unknown) {
                    throw new OptimisticLockingFailureException("Non persistent entity", unknown)
		}
	}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions