Skip to content

Commit

Permalink
Add deep copy instead of shallow copy in mappers
Browse files Browse the repository at this point in the history
Closes #166
  • Loading branch information
ArtDu committed Feb 17, 2023
1 parent c60e52b commit a45c752
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]
- Close public access to TarantoolResult*Impl ([#326](https://github.com/tarantool/cartridge-java/issues/326))
- Add deep copy instead of shallow copy in default message pack mapper

## [0.10.1] - 2023-01-13

### Internal and API changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static io.tarantool.driver.mappers.MapperReflectionUtils.getInterfaceParameterClass;

Expand Down Expand Up @@ -55,8 +56,10 @@ public DefaultMessagePackMapper() {
*/
public DefaultMessagePackMapper(DefaultMessagePackMapper mapper) {
this();
this.valueConverters.putAll(mapper.valueConverters);
this.objectConverters.putAll(mapper.objectConverters);
this.valueConverters.putAll(mapper.valueConverters.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> new ArrayList<>(e.getValue()))));
this.objectConverters.putAll(mapper.objectConverters.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> new ArrayList<>(e.getValue()))));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.msgpack.value.impl.ImmutableLongValueImpl;
import org.msgpack.value.impl.ImmutableStringValueImpl;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -186,6 +187,31 @@ void registerObjectConverter() throws MessagePackObjectMapperException {
assertEquals(testValue, mapper.toValue(testTuple).asMapValue().map());
}

@Test
void test_defaultMessagePackMapperCopy_shouldWorkFine() throws MessagePackValueMapperException,
NoSuchFieldException, IllegalAccessException {
DefaultMessagePackMapper mapper = DefaultMessagePackMapperFactory.getInstance().defaultComplexTypesMapper();
MessagePackMapper copiedMapper = mapper.copy();
MessagePackMapper copiedOfMapper = DefaultMessagePackMapperFactory.getInstance().copyOf(mapper);

Field privateField
= DefaultMessagePackMapper.class.getDeclaredField("valueConverters");
// Set the accessibility as true
privateField.setAccessible(true);

assertEquals(1, ((List) ((Map) privateField.get(mapper)).get(ValueType.MAP)).size());
assertEquals(1, ((List) ((Map) privateField.get(copiedMapper)).get(ValueType.MAP)).size());

mapper.registerValueConverter(ValueType.MAP, CustomTuple.class,
(ValueConverter<MapValue, CustomTuple>) v -> null);

assertEquals(2, ((List) ((Map) privateField.get(mapper)).get(ValueType.MAP)).size());
assertEquals(1, ((List) ((Map) privateField.get(copiedMapper)).get(ValueType.MAP)).size());
assertEquals(1, ((List) ((Map) privateField.get(copiedOfMapper)).get(ValueType.MAP)).size());

mapper.copy();
}

//TODO: add this test when will it be resolved https://github.com/tarantool/cartridge-java/issues/118
// @Test
// void should_getObject_returnShort_ifParameterObjectClassIsShort() {
Expand Down

0 comments on commit a45c752

Please sign in to comment.