Skip to content

Commit 349c095

Browse files
committed
Provide default fallback implementation of decodeEntity(Object id, byte[] ...) (back to String).
Ensure that if both parsing and close throw, the close exception is suppressed rather than masking the original error. Signed-off-by: Emilien Bevierre <emilien.bevierre@couchbase.com>
1 parent 33354ce commit 349c095

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

src/main/java/org/springframework/data/couchbase/core/ReactiveTemplateSupport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import reactor.core.publisher.Mono;
1919

20+
import java.nio.charset.StandardCharsets;
2021
import java.time.Instant;
2122

2223
import org.springframework.data.couchbase.core.convert.translation.TranslationService;
@@ -36,8 +37,11 @@ public interface ReactiveTemplateSupport {
3637
<T> Mono<T> decodeEntity(Object id, String source, Long cas, Instant expiryTime, Class<T> entityClass, String scope,
3738
String collection, Object txResultHolder, CouchbaseResourceHolder holder);
3839

39-
<T> Mono<T> decodeEntity(Object id, byte[] source, Long cas, Instant expiryTime, Class<T> entityClass, String scope,
40-
String collection, Object txResultHolder, CouchbaseResourceHolder holder);
40+
default <T> Mono<T> decodeEntity(Object id, byte[] source, Long cas, Instant expiryTime, Class<T> entityClass,
41+
String scope, String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
42+
return decodeEntity(id, new String(source, StandardCharsets.UTF_8), cas, expiryTime, entityClass, scope,
43+
collection, txResultHolder, holder);
44+
}
4145

4246
<T> Mono<T> applyResult(T entity, CouchbaseDocument converted, Object id, Long cas,
4347
Object txResultHolder, CouchbaseResourceHolder holder);

src/main/java/org/springframework/data/couchbase/core/TemplateSupport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18+
import java.nio.charset.StandardCharsets;
1819
import java.time.Instant;
1920

2021
import org.springframework.data.couchbase.core.convert.translation.TranslationService;
@@ -32,8 +33,11 @@ public interface TemplateSupport {
3233
<T> T decodeEntity(Object id, String source, Long cas, Instant expiryTime, Class<T> entityClass, String scope,
3334
String collection, Object txResultHolder, CouchbaseResourceHolder holder);
3435

35-
<T> T decodeEntity(Object id, byte[] source, Long cas, Instant expiryTime, Class<T> entityClass, String scope,
36-
String collection, Object txResultHolder, CouchbaseResourceHolder holder);
36+
default <T> T decodeEntity(Object id, byte[] source, Long cas, Instant expiryTime, Class<T> entityClass, String scope,
37+
String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
38+
return decodeEntity(id, new String(source, StandardCharsets.UTF_8), cas, expiryTime, entityClass, scope,
39+
collection, txResultHolder, holder);
40+
}
3741

3842
<T> T applyResult(T entity, CouchbaseDocument converted, Object id, long cas, Object txResultHolder,
3943
CouchbaseResourceHolder holder);

src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ private boolean isEnumOrClass(final Class<?> clazz) {
128128
*/
129129
@Override
130130
public final CouchbaseStorable decode(final String source, final CouchbaseStorable target) {
131-
try {
132-
JsonParser parser = factory.createParser(source);
131+
try (JsonParser parser = factory.createParser(source)) {
133132
return decodeWithParser(parser, target);
134133
} catch (IOException ex) {
135134
throw new RuntimeException("Could not decode JSON", ex);
@@ -146,29 +145,24 @@ public final CouchbaseStorable decode(final String source, final CouchbaseStorab
146145
*/
147146
@Override
148147
public final CouchbaseStorable decode(final byte[] source, final CouchbaseStorable target) {
149-
try {
150-
JsonParser parser = factory.createParser(source);
148+
try (JsonParser parser = factory.createParser(source)) {
151149
return decodeWithParser(parser, target);
152150
} catch (IOException ex) {
153151
throw new RuntimeException("Could not decode JSON", ex);
154152
}
155153
}
156154

157155
private CouchbaseStorable decodeWithParser(final JsonParser parser, final CouchbaseStorable target) throws IOException {
158-
try {
159-
while (parser.nextToken() != null) {
160-
JsonToken currentToken = parser.getCurrentToken();
161-
162-
if (currentToken == JsonToken.START_OBJECT) {
163-
return decodeObject(parser, (CouchbaseDocument) target);
164-
} else if (currentToken == JsonToken.START_ARRAY) {
165-
return decodeArray(parser, new CouchbaseList());
166-
} else {
167-
throw new MappingException("JSON to decode needs to start as array or object!");
168-
}
156+
while (parser.nextToken() != null) {
157+
JsonToken currentToken = parser.getCurrentToken();
158+
159+
if (currentToken == JsonToken.START_OBJECT) {
160+
return decodeObject(parser, (CouchbaseDocument) target);
161+
} else if (currentToken == JsonToken.START_ARRAY) {
162+
return decodeArray(parser, new CouchbaseList());
163+
} else {
164+
throw new MappingException("JSON to decode needs to start as array or object!");
169165
}
170-
} finally {
171-
parser.close();
172166
}
173167
return target;
174168
}

src/main/java/org/springframework/data/couchbase/core/convert/translation/TranslationService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public interface TranslationService {
4848

4949
/**
5050
* Decodes the target format from a byte array into a {@link CouchbaseDocument}.
51-
* This avoids the intermediate String allocation when the source is already available as bytes.
51+
* The default implementation converts the bytes to a String via UTF-8 before decoding.
52+
* Implementations may override this to parse bytes directly and avoid the intermediate String allocation.
5253
*
5354
* @param source the source formatted document as bytes (UTF-8 encoded).
5455
* @param target the target of the populated data.

0 commit comments

Comments
 (0)