Skip to content

Support enum arguments on repository queries. #1112

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

Merged
merged 1 commit into from
Mar 24, 2021
Merged
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 @@ -17,6 +17,7 @@
package org.springframework.data.couchbase.core.convert;

import java.util.Collections;
import java.util.Optional;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
Expand Down Expand Up @@ -100,7 +101,8 @@ public Object convertForWriteIfNeeded(Object value) {

return this.conversions.getCustomWriteTarget(value.getClass()) //
.map(it -> (Object) this.conversionService.convert(value, it)) //
.orElse(value);
.orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,15 +774,8 @@ private void writeSimpleInternal(final Object source, final CouchbaseDocument ta
target.put(key, getPotentiallyConvertedSimpleWrite(source));
}

private Object getPotentiallyConvertedSimpleWrite(final Object value) {
if (value == null) {
return null;
}

Optional<Class<?>> customTarget = conversions.getCustomWriteTarget(value.getClass());

return customTarget.map(it -> (Object) conversionService.convert(value, it))
.orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value);
public Object getPotentiallyConvertedSimpleWrite(final Object value) {
return convertForWriteIfNeeded(value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public interface AirportRepository extends PagingAndSortingRepository<Airport, S
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Airport findByIata(String iata);

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Airport findByIata(Iata iata);

@Query("#{#n1ql.selectEntity} where iata = $1")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airport> getAllByIata(String iata);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.springframework.data.couchbase.domain;

public enum Iata {
vie,
xxx
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.AirportRepository;
import org.springframework.data.couchbase.domain.Iata;
import org.springframework.data.couchbase.domain.Person;
import org.springframework.data.couchbase.domain.PersonRepository;
import org.springframework.data.couchbase.domain.User;
Expand Down Expand Up @@ -167,6 +168,18 @@ void findBySimpleProperty() {
}
}

@Test
void findByEnum() {
Airport vie = null;
try {
vie = new Airport("airports::vie", "vie", "loww");
vie = airportRepository.save(vie);
Airport airport2 = airportRepository.findByIata(Iata.vie);
assertEquals(airport2.getId(), vie.getId());
} finally {
airportRepository.delete(vie);
}
}
@Test
public void testCas() {
User user = new User("1", "Dave", "Wilson");
Expand Down