Skip to content

Commit

Permalink
Don't use String version of enum parameters. (#1846)
Browse files Browse the repository at this point in the history
Just use the converted value as-is (in case of boolean or Number) enum value.

Closes #1837.
  • Loading branch information
mikereiche committed Oct 11, 2023
1 parent 7283a5c commit fca506f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ private void addAsArray(JsonObject namedValues, String placeholder, Object o) {
Object[] array = (Object[]) o;
JsonArray ja = JsonValue.ja();
for (Object e : array) {
ja.add(String.valueOf(couchbaseConverter.convertForWriteIfNeeded(e)));
ja.add(couchbaseConverter.convertForWriteIfNeeded(e));
}
namedValues.put(placeholder, ja);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonValue;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.couchbase.repository.Collection;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
Expand Down Expand Up @@ -61,6 +62,41 @@ public interface UserRepository extends CouchbaseRepository<User, String> {

List<User> findByIdIsNotNullAndFirstnameEquals(String firstname);

List<User> findByFirstname(@Param("firstName")FirstName firstName );

List<User> findByFirstnameIn(@Param("firstNames")FirstName[] firstNames );

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (firstname = $firstName)")
List<User> queryByFirstnameNamedParameter(@Param("firstName")FirstName firstName );

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (firstname = $1)")
List<User> queryByFirstnamePositionalParameter(@Param("firstName")FirstName firstName );

enum FirstName {
Dave,
William
}

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (jsonNode.myNumber = $myNumber)")
List<User> queryByIntegerEnumNamed(@Param("myNumber")IntEnum myNumber );

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and (jsonNode.myNumber = $1)")
List<User> queryByIntegerEnumPositional(@Param("myNumber")IntEnum myNumber );

enum IntEnum {
One(1),
Two(2),
OneThousand(1000);
Integer value;
IntEnum(Integer i){
value = i;
}
@JsonValue
public Integer getValue(){
return value;
}
}

List<User> findByVersionEqualsAndFirstnameEquals(Long version, String firstname);

@Query("#{#n1ql.selectEntity}|#{#n1ql.filter}|#{#n1ql.bucket}|#{#n1ql.scope}|#{#n1ql.collection}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
import org.springframework.data.couchbase.core.query.N1QLExpression;
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.AirlineRepository;
import org.springframework.data.couchbase.domain.Airline;
import org.springframework.data.couchbase.domain.AirlineRepository;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.AirportJsonValue;
import org.springframework.data.couchbase.domain.AirportJsonValueRepository;
Expand Down Expand Up @@ -741,6 +741,41 @@ public void testTransient() {
userRepository.delete(user);
}

@Test
public void testEnumParameter() {
User user = new User("1", "Dave", "Wilson");
userRepository.save(user);
User user2 = new User("2", "Frank", "Spalding");
userRepository.save(user2);

List<User> foundUsersEquals = userRepository.findByFirstname(UserRepository.FirstName.Dave);
assertEquals(user.getId(), foundUsersEquals.get(0).getId());
assertEquals(1, foundUsersEquals.size());

List<User> foundUsersIn = userRepository.findByFirstnameIn( new UserRepository.FirstName[]{ UserRepository.FirstName.Dave });
assertEquals(user.getId(), foundUsersIn.get(0).getId());
assertEquals(1, foundUsersIn.size());

List<User> namedUsers = userRepository.queryByFirstnameNamedParameter( UserRepository.FirstName.Dave);
assertEquals(user.getId(), namedUsers.get(0).getId());
assertEquals(1, namedUsers.size());

List<User> positionalUsers = userRepository.queryByFirstnamePositionalParameter( UserRepository.FirstName.Dave);
assertEquals(user.getId(), positionalUsers.get(0).getId());
assertEquals(1, positionalUsers.size());

// User objects are initialized with jsonNode.myNumber = 1000
List<User> integerEnumUsersNamed = userRepository.queryByIntegerEnumNamed(UserRepository.IntEnum.OneThousand);
assertEquals(2, integerEnumUsersNamed.size());

// User objects are initialized with jsonNode.myNumber = 1000
List<User> integerEnumUsersPositional = userRepository.queryByIntegerEnumPositional(UserRepository.IntEnum.OneThousand);
assertEquals(2, integerEnumUsersPositional.size());

userRepository.delete(user);
userRepository.delete(user2);
}

@Test
public void testCas() {
User user = new User("1", "Dave", "Wilson");
Expand Down

0 comments on commit fca506f

Please sign in to comment.