Skip to content

Back compatibility support for object_id_identity #7622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.security.acls.jdbc;

import java.io.Serializable;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
Expand Down Expand Up @@ -45,6 +46,7 @@ class AclClassIdUtils {
GenericConversionService genericConversionService = new GenericConversionService();
genericConversionService.addConverter(String.class, Long.class, new StringToLongConverter());
genericConversionService.addConverter(String.class, UUID.class, new StringToUUIDConverter());
genericConversionService.addConverter(BigInteger.class, Long.class, new BigIntegerToLongConverter());
this.conversionService = genericConversionService;
}

Expand All @@ -62,8 +64,9 @@ class AclClassIdUtils {
* @throws SQLException
*/
Serializable identifierFrom(Serializable identifier, ResultSet resultSet) throws SQLException {
if (isString(identifier) && hasValidClassIdType(resultSet)
&& canConvertFromStringTo(classIdTypeFrom(resultSet))) {
if (isString(identifier)
&& hasValidClassIdType(resultSet)
&& canConvertFromStringTo(classIdTypeFrom(resultSet))) {

identifier = convertFromStringTo((String) identifier, classIdTypeFrom(resultSet));
} else {
Expand Down Expand Up @@ -100,6 +103,10 @@ private <T extends Serializable> Class<T> classIdTypeFrom(String className) {
return targetType;
}

private <S, T> boolean canConvert(Class<S> sourceType, Class<T> targetType) {
return conversionService.canConvert(sourceType, targetType);
}

private <T> boolean canConvertFromStringTo(Class<T> targetType) {
return conversionService.canConvert(String.class, targetType);
}
Expand All @@ -118,7 +125,7 @@ private <T extends Serializable> T convertFromStringTo(String identifier, Class<
*/
private Long convertToLong(Serializable identifier) {
Long idAsLong;
if (canConvertFromStringTo(Long.class)) {
if (canConvert(identifier.getClass(), Long.class)) {
idAsLong = conversionService.convert(identifier, Long.class);
} else {
idAsLong = Long.valueOf(identifier.toString());
Expand Down Expand Up @@ -158,4 +165,16 @@ public UUID convert(String identifierAsString) {
return UUID.fromString(identifierAsString);
}
}

private static class BigIntegerToLongConverter implements Converter<BigInteger, Long> {
@Override
public Long convert(BigInteger identifierAsStringBigInteger) {
if (identifierAsStringBigInteger == null) {
throw new ConversionFailedException(TypeDescriptor.valueOf(BigInteger.class),
TypeDescriptor.valueOf(Long.class), null, null);

}
return identifierAsStringBigInteger.longValue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.core.convert.ConversionService;

import java.io.Serializable;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
Expand All @@ -40,6 +41,7 @@ public class AclClassIdUtilsTest {

private static final Long DEFAULT_IDENTIFIER = 999L;
private static final String DEFAULT_IDENTIFIER_AS_STRING = DEFAULT_IDENTIFIER.toString();
private static final BigInteger DEFAULT_IDENTIFIER_AS_BIGINT = BigInteger.valueOf( DEFAULT_IDENTIFIER );

@Mock
private ResultSet resultSet;
Expand Down Expand Up @@ -74,6 +76,15 @@ public void shouldReturnLongIfClassIdTypeIsNull() throws SQLException {
assertThat(newIdentifier).isEqualTo(DEFAULT_IDENTIFIER);
}

@Test
public void shouldReturnLongIfIdentifierIsBigInteger() throws SQLException {
// when
Serializable newIdentifier = aclClassIdUtils.identifierFrom(DEFAULT_IDENTIFIER_AS_BIGINT, resultSet);

// then
assertThat(newIdentifier).isEqualTo(DEFAULT_IDENTIFIER);
}

@Test
public void shouldReturnLongIfNoClassIdTypeColumn() throws SQLException {
// given
Expand Down