1
1
/*
2
- /*
3
- * Copyright 2012-2021 the original author or authors
2
+ * Copyright 2012-2022 the original author or authors
4
3
*
5
4
* Licensed under the Apache License, Version 2.0 (the "License");
6
5
* you may not use this file except in compliance with the License.
17
16
18
17
package org .springframework .data .couchbase .core ;
19
18
19
+ import java .util .Map ;
20
+ import java .util .Set ;
21
+
20
22
import org .slf4j .Logger ;
21
23
import org .slf4j .LoggerFactory ;
22
24
import org .springframework .beans .BeansException ;
40
42
import org .springframework .data .mapping .context .MappingContext ;
41
43
import org .springframework .data .mapping .model .ConvertingPropertyAccessor ;
42
44
import org .springframework .util .Assert ;
45
+ import org .springframework .util .ClassUtils ;
43
46
44
47
/**
45
48
* Internal encode/decode support for CouchbaseTemplate.
@@ -84,7 +87,18 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
84
87
public <T > T decodeEntity (String id , String source , long cas , Class <T > entityClass ) {
85
88
final CouchbaseDocument converted = new CouchbaseDocument (id );
86
89
converted .setId (id );
87
- CouchbasePersistentEntity <?> persistentEntity = mappingContext .getRequiredPersistentEntity (entityClass );
90
+ CouchbasePersistentEntity persistentEntity = couldBePersistentEntity (entityClass );
91
+
92
+ if (persistentEntity == null ) { // method could return a Long, Boolean, String etc.
93
+ // QueryExecutionConverters.unwrapWrapperTypes will recursively unwrap until there is nothing left
94
+ // to unwrap. This results in List<String[]> being unwrapped past String[] to String, so this may also be a
95
+ // Collection (or Array) of entityClass. We have no way of knowing - so just assume it is what we are told.
96
+ // if this is a Collection or array, only the first element will be returned.
97
+ Set <Map .Entry <String , Object >> set = ((CouchbaseDocument ) translationService .decode (source , converted ))
98
+ .getContent ().entrySet ();
99
+ return (T ) set .iterator ().next ().getValue ();
100
+ }
101
+
88
102
if (cas != 0 && persistentEntity .getVersionProperty () != null ) {
89
103
converted .put (persistentEntity .getVersionProperty ().getName (), cas );
90
104
}
@@ -98,6 +112,12 @@ public <T> T decodeEntity(String id, String source, long cas, Class<T> entityCla
98
112
N1qlJoinResolver .handleProperties (persistentEntity , accessor , template .reactive (), id );
99
113
return accessor .getBean ();
100
114
}
115
+ CouchbasePersistentEntity couldBePersistentEntity (Class <?> entityClass ) {
116
+ if (ClassUtils .isPrimitiveOrWrapper (entityClass ) || entityClass == String .class ) {
117
+ return null ;
118
+ }
119
+ return mappingContext .getPersistentEntity (entityClass );
120
+ }
101
121
102
122
@ Override
103
123
public Object applyUpdatedCas (final Object entity , CouchbaseDocument converted , final long cas ) {
0 commit comments