Skip to content

Commit 833bef1

Browse files
committed
refactor: apply review feedback
1 parent 135993a commit 833bef1

File tree

1 file changed

+18
-11
lines changed
  • spi/core-spi/src/main/java/org/eclipse/dataspaceconnector/spi/types

1 file changed

+18
-11
lines changed

spi/core-spi/src/main/java/org/eclipse/dataspaceconnector/spi/types/TypeManager.java

+18-11
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,58 @@
3333
* Manages system types and is used to deserialize polymorphic types.
3434
*/
3535
public class TypeManager {
36-
private final ObjectMapper objectMapper;
36+
private final ObjectMapper defaultMapper;
3737

3838
/**
39-
* Concurrent support is not needed since this map is only populated a boot, which is single-threaded
39+
* Concurrent support is not needed since this map is only populated a boot, which is single-threaded.
4040
*/
4141
private final Map<String, ObjectMapper> objectMappers = new HashMap<>();
4242

4343
/**
44-
* Constructor without params.
44+
* Default constructor.
4545
*/
4646
public TypeManager() {
47-
objectMapper = new ObjectMapper();
48-
objectMapper.registerModule(new JavaTimeModule()); // configure ISO 8601 time de/serialization
49-
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); // serialize dates in ISO 8601 format
50-
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
47+
defaultMapper = new ObjectMapper();
48+
defaultMapper.registerModule(new JavaTimeModule()); // configure ISO 8601 time de/serialization
49+
defaultMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); // serialize dates in ISO 8601 format
50+
defaultMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
51+
52+
registerContext("default", defaultMapper);
5153
}
5254

5355
/**
5456
* Returns the object mapper for the default serialization context.
5557
*/
5658
public ObjectMapper getMapper() {
57-
return objectMapper;
59+
return defaultMapper;
5860
}
5961

6062
/**
6163
* Returns the object mapper for the given serialization context, creating one based on the default mapper if required.
6264
*/
6365
@NotNull
6466
public ObjectMapper getMapper(String key) {
65-
return objectMappers.computeIfAbsent(key, k -> objectMapper.copy());
67+
return objectMappers.computeIfAbsent(key, k -> defaultMapper.copy());
68+
}
69+
70+
/**
71+
* Add custom mapper by key to list of object mappers.
72+
*/
73+
public void registerContext(String key, ObjectMapper mapper) {
74+
objectMappers.put(key, mapper);
6675
}
6776

6877
/**
6978
* Registers types with all contexts.
7079
*/
7180
public void registerTypes(Class<?>... type) {
72-
objectMapper.registerSubtypes(type);
7381
objectMappers.values().forEach(m -> m.registerSubtypes(type));
7482
}
7583

7684
/**
7785
* Registers types with all contexts.
7886
*/
7987
public void registerTypes(NamedType... type) {
80-
objectMapper.registerSubtypes(type);
8188
objectMappers.values().forEach(m -> m.registerSubtypes(type));
8289
}
8390

0 commit comments

Comments
 (0)