Skip to content

Commit ac05b8a

Browse files
committed
SerdeContextHolder
1 parent 10361b6 commit ac05b8a

17 files changed

+689
-656
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<properties>
5757
<java-module-name>com.arangodb.springframework</java-module-name>
58-
<arangodb.version>7.6.0</arangodb.version>
58+
<arangodb.version>7.7.0-SNAPSHOT</arangodb.version>
5959

6060
<!-- enforce dependencies convergence -->
6161
<slf4j>2.0.11</slf4j>

src/main/java/com/arangodb/springframework/config/ArangoConfiguration.java

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
*/
44
package com.arangodb.springframework.config;
55

6-
import java.io.IOException;
7-
import java.lang.annotation.Annotation;
8-
import java.util.Collection;
9-
import java.util.Collections;
10-
import java.util.Optional;
11-
import java.util.Set;
12-
6+
import com.arangodb.ArangoDB;
7+
import com.arangodb.ArangoDBException;
138
import com.arangodb.ContentType;
149
import com.arangodb.serde.ArangoSerde;
10+
import com.arangodb.serde.SerdeContext;
1511
import com.arangodb.serde.jackson.JacksonMapperProvider;
12+
import com.arangodb.springframework.annotation.*;
13+
import com.arangodb.springframework.core.ArangoOperations;
14+
import com.arangodb.springframework.core.convert.*;
15+
import com.arangodb.springframework.core.convert.resolver.*;
16+
import com.arangodb.springframework.core.mapping.ArangoMappingContext;
17+
import com.arangodb.springframework.core.mapping.SerdeContextHolder;
18+
import com.arangodb.springframework.core.template.ArangoTemplate;
1619
import com.fasterxml.jackson.core.JsonProcessingException;
1720
import com.fasterxml.jackson.databind.ObjectMapper;
1821
import org.springframework.context.annotation.Bean;
@@ -22,31 +25,11 @@
2225
import org.springframework.data.mapping.model.FieldNamingStrategy;
2326
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
2427

25-
import com.arangodb.ArangoDB;
26-
import com.arangodb.ArangoDBException;
27-
import com.arangodb.springframework.annotation.Document;
28-
import com.arangodb.springframework.annotation.Edge;
29-
import com.arangodb.springframework.annotation.From;
30-
import com.arangodb.springframework.annotation.Ref;
31-
import com.arangodb.springframework.annotation.Relations;
32-
import com.arangodb.springframework.annotation.To;
33-
import com.arangodb.springframework.core.ArangoOperations;
34-
import com.arangodb.springframework.core.convert.ArangoConverter;
35-
import com.arangodb.springframework.core.convert.ArangoCustomConversions;
36-
import com.arangodb.springframework.core.convert.ArangoTypeMapper;
37-
import com.arangodb.springframework.core.convert.DefaultArangoConverter;
38-
import com.arangodb.springframework.core.convert.DefaultArangoTypeMapper;
39-
import com.arangodb.springframework.core.convert.resolver.DocumentFromResolver;
40-
import com.arangodb.springframework.core.convert.resolver.DocumentToResolver;
41-
import com.arangodb.springframework.core.convert.resolver.EdgeFromResolver;
42-
import com.arangodb.springframework.core.convert.resolver.EdgeToResolver;
43-
import com.arangodb.springframework.core.convert.resolver.RefResolver;
44-
import com.arangodb.springframework.core.convert.resolver.ReferenceResolver;
45-
import com.arangodb.springframework.core.convert.resolver.RelationResolver;
46-
import com.arangodb.springframework.core.convert.resolver.RelationsResolver;
47-
import com.arangodb.springframework.core.convert.resolver.ResolverFactory;
48-
import com.arangodb.springframework.core.mapping.ArangoMappingContext;
49-
import com.arangodb.springframework.core.template.ArangoTemplate;
28+
import java.lang.annotation.Annotation;
29+
import java.util.Collection;
30+
import java.util.Collections;
31+
import java.util.Optional;
32+
import java.util.Set;
5033

5134
/**
5235
* Defines methods to customize the Java-based configuration for Spring Data
@@ -104,9 +87,15 @@ public byte[] serialize(Object value) {
10487

10588
@Override
10689
public <T> T deserialize(byte[] content, Class<T> clazz) {
90+
return deserialize(content, clazz);
91+
}
92+
93+
@Override
94+
public <T> T deserialize(byte[] content, Class<T> clazz, SerdeContext ctx) {
95+
SerdeContextHolder.INSTANCE.setCtx(ctx);
10796
try {
10897
return converter.read(clazz, om.readTree(content));
109-
} catch (IOException e) {
98+
} catch (Exception e) {
11099
throw new MappingException("Exception while deserializing.", e);
111100
}
112101
}

src/main/java/com/arangodb/springframework/core/convert/ArangoEntityReader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@
2727
/**
2828
* @author Mark Vollmary
2929
* @author Christian Lechner
30-
*
3130
*/
3231
public interface ArangoEntityReader extends EntityReader<Object, JsonNode> {
3332

33+
/**
34+
* Reads the given source into the given type.
35+
*
36+
* @param type they type to convert the given source to.
37+
* @param source the source to create an object of the given type from.
38+
*/
39+
<R> R read(Class<R> type, JsonNode source);
3440
}

src/main/java/com/arangodb/springframework/core/convert/DefaultArangoConverter.java

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public ArangoTypeMapper getTypeMapper() {
116116

117117
@SuppressWarnings("unchecked")
118118
@Override
119-
public <R> R read(final Class<R> type, final JsonNode source) {
119+
public <R> R read(Class<R> type, JsonNode source) {
120120
return (R) readInternal(TypeInformation.of(type), source);
121121
}
122122

@@ -161,8 +161,8 @@ private Object readInternal(final TypeInformation<?> type, final JsonNode source
161161
return readMap(typeToUse, source);
162162
}
163163

164-
if (!source.isArray() && (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class))) {
165-
return readMap(TypeInformation.MAP, source);
164+
if (!source.isArray() && (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class))) {
165+
return readMap(TypeInformation.MAP, source);
166166
}
167167

168168
if (typeToUse.getType().isArray()) {
@@ -173,8 +173,8 @@ private Object readInternal(final TypeInformation<?> type, final JsonNode source
173173
return readCollection(typeToUse, source);
174174
}
175175

176-
if (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class)) {
177-
return readCollection(TypeInformation.COLLECTION, source);
176+
if (TypeInformation.OBJECT.equals(typeToUse) || rawTypeToUse.equals(Object.class)) {
177+
return readCollection(TypeInformation.COLLECTION, source);
178178
}
179179

180180
ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(rawTypeToUse);
@@ -184,7 +184,8 @@ private Object readInternal(final TypeInformation<?> type, final JsonNode source
184184
private Object readEntity(
185185
final TypeInformation<?> type,
186186
final JsonNode source,
187-
final ArangoPersistentEntity<?> entity) {
187+
final ArangoPersistentEntity<?> entity
188+
) {
188189

189190
if (!source.isObject()) {
190191
throw new MappingException(
@@ -222,7 +223,8 @@ private void readProperty(
222223
final String parentId,
223224
final PersistentPropertyAccessor<?> accessor,
224225
final JsonNode source,
225-
final ArangoPersistentProperty property) {
226+
final ArangoPersistentProperty property
227+
) {
226228

227229
Object propertyValue = readPropertyValue(entity, parentId, source, property);
228230
if (propertyValue != null || !property.getType().isPrimitive()) {
@@ -234,7 +236,8 @@ private Object readPropertyValue(
234236
final ArangoPersistentEntity<?> entity,
235237
final String parentId,
236238
final JsonNode source,
237-
final ArangoPersistentProperty property) {
239+
final ArangoPersistentProperty property
240+
) {
238241

239242
Optional<Ref> ref = property.getRef();
240243
if (ref.isPresent()) {
@@ -329,13 +332,14 @@ private Object readArray(final TypeInformation<?> type, final JsonNode source) {
329332
private Optional<Object> readReference(
330333
final JsonNode source,
331334
final ArangoPersistentProperty property,
332-
final Annotation annotation) {
335+
final Annotation annotation
336+
) {
333337

334-
if (source.isMissingNode() || source.isNull()) {
335-
return Optional.empty();
336-
}
338+
if (source.isMissingNode() || source.isNull()) {
339+
return Optional.empty();
340+
}
337341

338-
Optional<ReferenceResolver<Annotation>> resolver = resolverFactory.getReferenceResolver(annotation);
342+
Optional<ReferenceResolver<Annotation>> resolver = resolverFactory.getReferenceResolver(annotation);
339343

340344
if (resolver.isEmpty()) {
341345
return Optional.empty();
@@ -349,9 +353,9 @@ private Optional<Object> readReference(
349353

350354
return resolver.map(res -> res.resolveMultiple(ids, property.getTypeInformation(), annotation));
351355
} else if (source.isObject()) {
352-
// source contains target
353-
return Optional.of(readInternal(property.getTypeInformation(), source));
354-
} else {
356+
// source contains target
357+
return Optional.of(readInternal(property.getTypeInformation(), source));
358+
} else {
355359
if (!source.isTextual()) {
356360
throw new MappingException(
357361
String.format("A reference must be of type String, but got type %s!", source.getNodeType()));
@@ -366,7 +370,8 @@ private <A extends Annotation> Optional<Object> readRelation(
366370
final String parentId,
367371
final JsonNode source,
368372
final ArangoPersistentProperty property,
369-
final A annotation) {
373+
final A annotation
374+
) {
370375

371376
if (source.isNull()) {
372377
return Optional.empty();
@@ -446,7 +451,7 @@ private Object readSimple(final Class<?> type, final JsonNode source) {
446451
Enum<?> e = Enum.valueOf((Class<? extends Enum>) type, value);
447452
return e;
448453
} else if (byte[].class.isAssignableFrom(type)) {
449-
return Base64.getDecoder().decode(value);
454+
return Base64.getDecoder().decode(value);
450455
} else if (java.sql.Date.class.isAssignableFrom(type)) {
451456
return new java.sql.Date(parseDate(value).getTime());
452457
} else if (Timestamp.class.isAssignableFrom(type)) {
@@ -505,7 +510,8 @@ private DBDocumentEntity readDBDocumentEntity(final JsonNode source) {
505510

506511
private ParameterValueProvider<ArangoPersistentProperty> getParameterProvider(
507512
final ArangoPersistentEntity<?> entity,
508-
final JsonNode source) {
513+
final JsonNode source
514+
) {
509515

510516
PropertyValueProvider<ArangoPersistentProperty> provider = new ArangoPropertyValueProvider(entity, source);
511517
return new PersistentEntityParameterValueProvider<>(entity, provider, null);
@@ -778,16 +784,16 @@ private Optional<String> getRefId(final Object source, final ArangoPersistentEnt
778784
}
779785

780786
return Optional.ofNullable(entity.getIdentifierAccessor(source).getIdentifier())
781-
.map(key -> {
782-
if (annotation != null) {
783-
return resolverFactory.getReferenceResolver(annotation)
784-
.map(resolver -> resolver.write(source, entity, convertId(key), annotation))
785-
.orElseThrow(() -> new IllegalArgumentException("Missing reference resolver for " + annotation));
786-
} else {
787-
return MetadataUtils.createIdFromCollectionAndKey(entity.getCollection(), convertId(key));
788-
}
789-
})
790-
.or(() -> Optional.ofNullable((String) entity.getArangoIdAccessor(source).getIdentifier()));
787+
.map(key -> {
788+
if (annotation != null) {
789+
return resolverFactory.getReferenceResolver(annotation)
790+
.map(resolver -> resolver.write(source, entity, convertId(key)))
791+
.orElseThrow(() -> new IllegalArgumentException("Missing reference resolver for " + annotation));
792+
} else {
793+
return MetadataUtils.createIdFromCollectionAndKey(entity.getCollection(), convertId(key));
794+
}
795+
})
796+
.or(() -> Optional.ofNullable((String) entity.getArangoIdAccessor(source).getIdentifier()));
791797
}
792798

793799
private static Collection<?> asCollection(final Object source) {

0 commit comments

Comments
 (0)