Skip to content

Field(name=foo) annotation work like Field(foo) and Field(value=foo). #1186

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
Aug 16, 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 @@ -45,6 +45,7 @@ public class BasicCouchbasePersistentProperty extends AnnotationBasedPersistentP
implements CouchbasePersistentProperty {

private final FieldNamingStrategy fieldNamingStrategy;
private String fieldName;

/**
* Create a new instance of the BasicCouchbasePersistentProperty class.
Expand Down Expand Up @@ -75,25 +76,32 @@ protected Association<CouchbasePersistentProperty> createAssociation() {
*/
@Override
public String getFieldName() {
if (fieldName != null) {
return fieldName;
}
Field annotationField = getField().getAnnotation(Field.class);

if (annotationField != null && StringUtils.hasText(annotationField.value())) {
return annotationField.value();
if (annotationField != null) {
if (StringUtils.hasText(annotationField.value())) {
return fieldName = annotationField.value();
} else if (StringUtils.hasText(annotationField.name())) {
return fieldName = annotationField.name();
}
}
JsonProperty annotation = getField().getAnnotation(JsonProperty.class);

if (annotation != null && StringUtils.hasText(annotation.value())) {
return annotation.value();
return fieldName = annotation.value();
}

String fieldName = fieldNamingStrategy.getFieldName(this);
String fName = fieldNamingStrategy.getFieldName(this);

if (!StringUtils.hasText(fieldName)) {
if (!StringUtils.hasText(fName)) {
throw new MappingException(String.format("Invalid (null or empty) field name returned for property %s by %s!",
this, fieldNamingStrategy.getClass()));
}

return fieldName;
return fieldName = fName;
}

// DATACOUCH-145: allows SDK's @Id annotation to be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class Person extends AbstractEntity {
@Version private long version;

@Nullable @Field("nickname") private String middlename;
@Nullable @Field(name = "prefix") private String salutation;

private Address address;

Expand Down Expand Up @@ -99,10 +100,18 @@ public String getMiddlename() {
return middlename;
}

public String getSalutation() {
return salutation;
}

public void setMiddlename(String middlename) {
this.middlename = middlename;
}

public void setSalutation(String salutation) {
this.salutation = salutation;
}

public long getVersion() {
return version;
}
Expand Down Expand Up @@ -143,15 +152,4 @@ public String toString() {
return sb.toString();
}

static String optional(String name, String obj) {
if (obj != null) {
if (obj != null /*.isPresent() */) {
return (" " + name + ": '" + obj/*.get()*/ + "'\n");
} else {
return " " + name + ": null\n";
}
}
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,7 @@ public interface PersonRepository extends CrudRepository<Person, String> {

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Person> findByMiddlename(String nickName);

@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Person> findBySalutation(String prefix);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.Optional;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -83,6 +83,7 @@
import com.couchbase.client.core.error.IndexFailureException;
import com.couchbase.client.java.env.ClusterEnvironment;
import com.couchbase.client.java.json.JsonArray;
import com.couchbase.client.java.kv.GetResult;
import com.couchbase.client.java.kv.MutationState;
import com.couchbase.client.java.query.QueryOptions;
import com.couchbase.client.java.query.QueryScanConsistency;
Expand Down Expand Up @@ -167,6 +168,27 @@ void annotatedFieldFind() {
}
}

@Test
void annotatedFieldFindName() {
Person person = null;
try {
person = new Person(1, "first", "last");
person.setSalutation("Mrs"); // salutation is stored as prefix
personRepository.save(person);
GetResult result = couchbaseTemplate.getCouchbaseClientFactory().getBucket().defaultCollection()
.get(person.getId().toString());
assertEquals(person.getSalutation(), result.contentAsObject().get("prefix"));
Person person2 = personRepository.findById(person.getId().toString()).get();
assertEquals(person.getSalutation(), person2.getSalutation());
// needs fix from datacouch_1184
//List<Person> persons3 = personRepository.findBySalutation("Mrs");
//assertEquals(1, persons3.size());
//assertEquals(person.getSalutation(), persons3.get(0).getSalutation());
} finally {
personRepository.deleteById(person.getId().toString());
}
}

// "1\" or name=name or name=\"1")
@Test
void findByInjection() {
Expand Down Expand Up @@ -384,7 +406,7 @@ public void testStreamQuery() {
userRepository.save(user1);
userRepository.save(user2);
List<User> users = userRepository.findByLastname("Wilson").collect(Collectors.toList());
assertEquals(2,users.size());
assertEquals(2, users.size());
assertTrue(users.contains(user1));
assertTrue(users.contains(user2));
userRepository.delete(user1);
Expand Down