Skip to content

Commit 9a0dd72

Browse files
committed
Fix regression - unable to store and read properties of type Object. (#1883)
Closes #1875.
1 parent 8b87abb commit 9a0dd72

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
614614
return;
615615
}
616616

617-
if (!conversions.isSimpleType(prop.getType())) {
617+
if (!conversions.isSimpleType(propertyObj.getClass())) {
618618
writePropertyInternal(propertyObj, target, prop, accessor);
619619
} else {
620620
writeSimpleInternal(prop, accessor, target, prop.getFieldName());
@@ -813,7 +813,7 @@ private Object readCollection(final TypeInformation<?> targetType, final Couchba
813813
if (dbObjItem instanceof CouchbaseDocument) {
814814
items.add(read(componentType, (CouchbaseDocument) dbObjItem, parent));
815815
} else if (dbObjItem instanceof CouchbaseList) {
816-
items.add(readCollection(componentType, (CouchbaseList) dbObjItem, parent));
816+
items.add(readCollection(componentType != null ? componentType :TypeInformation.of(dbObjItem.getClass()), (CouchbaseList) dbObjItem, parent));
817817
} else {
818818
items.add(getPotentiallyConvertedSimpleRead(dbObjItem, rawComponentType));
819819
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.data.couchbase.domain;
2+
3+
import jakarta.validation.constraints.NotNull;
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.couchbase.core.mapping.Field;
6+
7+
public class MyPerson {
8+
@NotNull
9+
@Id
10+
public String id;
11+
12+
@Field
13+
public Object myObject;
14+
15+
public String toString() {
16+
StringBuffer sb = new StringBuffer();
17+
sb.append("MyPerson:{");
18+
sb.append("id:");
19+
sb.append(id);
20+
sb.append(", myObject:");
21+
sb.append(myObject);
22+
sb.append("}");
23+
return sb.toString();
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.couchbase.domain;
17+
18+
import java.util.List;
19+
import java.util.UUID;
20+
21+
import org.springframework.data.couchbase.repository.CouchbaseRepository;
22+
import org.springframework.data.couchbase.repository.DynamicProxyable;
23+
import org.springframework.data.couchbase.repository.Query;
24+
import org.springframework.data.couchbase.repository.ScanConsistency;
25+
import org.springframework.data.repository.query.Param;
26+
27+
import com.couchbase.client.java.query.QueryScanConsistency;
28+
29+
/**
30+
* @author Michael Reiche
31+
*/
32+
public interface MyPersonRepository extends CouchbaseRepository<MyPerson, String>, DynamicProxyable<PersonRepository> {
33+
34+
}
35+

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.time.Duration;
3939
import java.util.ArrayList;
4040
import java.util.Arrays;
41+
import java.util.Collections;
4142
import java.util.LinkedList;
4243
import java.util.List;
4344
import java.util.Locale;
@@ -80,6 +81,8 @@
8081
import org.springframework.data.couchbase.domain.EJsonCreatorTurbulenceCategory;
8182
import org.springframework.data.couchbase.domain.ETurbulenceCategory;
8283
import org.springframework.data.couchbase.domain.Iata;
84+
import org.springframework.data.couchbase.domain.MyPerson;
85+
import org.springframework.data.couchbase.domain.MyPersonRepository;
8386
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
8487
import org.springframework.data.couchbase.domain.Person;
8588
import org.springframework.data.couchbase.domain.PersonRepository;
@@ -149,6 +152,9 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
149152

150153
@Autowired UserSubmissionRepository userSubmissionRepository;
151154

155+
@Autowired MyPersonRepository myPersonRepository;
156+
157+
152158
@Autowired CouchbaseTemplate couchbaseTemplate;
153159

154160
String scopeName = "_default";
@@ -180,6 +186,22 @@ void shouldSaveAndFindAll() {
180186
}
181187
}
182188

189+
@Test
190+
void findMyPerson() {
191+
MyPerson vie = null;
192+
try {
193+
vie = new MyPerson();
194+
vie.id = "123"; UUID.randomUUID().toString();
195+
vie.myObject = Collections.singletonList("a");
196+
MyPerson p = myPersonRepository.save(vie);
197+
System.err.println(p);
198+
Optional<MyPerson> r = myPersonRepository.findById( p.id);
199+
System.err.println(r.get());
200+
} finally {
201+
try { myPersonRepository.delete(vie); } catch (DataRetrievalFailureException dnfe){}
202+
}
203+
}
204+
183205
@Test
184206
void shouldNotSave() {
185207
Airport vie = new Airport("airports::vie", "vie", "low4");

0 commit comments

Comments
 (0)