Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom object converter is not applied on complex types #42

Closed
dimoffon opened this issue Nov 17, 2020 · 2 comments
Closed

Custom object converter is not applied on complex types #42

dimoffon opened this issue Nov 17, 2020 · 2 comments
Assignees
Labels
0.5sp Weight 0.5 SP bug Something isn't working

Comments

@dimoffon
Copy link
Contributor

dimoffon commented Nov 17, 2020

If create a custom object converter

private val timestampConverter = new ObjectConverter[Timestamp, StringValue] {
    override def toValue(obj: Timestamp): StringValue = ValueFactory.newString(obj.getTime.toString)
}

and then register it in default simple or complex types mapper (from DefaultMessagePackMapperFactory), then it does not applied for complex types (like Map, List, ArrayList) and ata runtime throws the next exception:

io.tarantool.driver.mappers.MessagePackObjectMapperException: ObjectConverter for type class java.sql.Timestamp is not found
at io.tarantool.driver.mappers.DefaultMessagePackMapper.toValue(DefaultMessagePackMapper.java:61)
at io.tarantool.driver.api.tuple.TarantoolFieldImpl.getEntity(TarantoolFieldImpl.java:50)
at io.tarantool.driver.api.tuple.TarantoolFieldImpl.toMessagePackValue(TarantoolFieldImpl.java:39)
at io.tarantool.driver.mappers.DefaultPackableObjectConverter.toValue(DefaultPackableObjectConverter.java:20)
at io.tarantool.driver.mappers.DefaultPackableObjectConverter.toValue(DefaultPackableObjectConverter.java:11)
at io.tarantool.driver.mappers.DefaultMessagePackMapper.toValue(DefaultMessagePackMapper.java:63)
at io.tarantool.driver.mappers.DefaultListObjectConverter.lambda$toValue$0(DefaultListObjectConverter.java:26)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)

This is due to the fact that complex types mappers also contains their own list of converters for simple types (which is just a copy from the default mapper, see copy constructor of DefaultMessagePackMapper).
As a workaround I've found only to create instance of DefaultMessagePackMapper manually

private val tmpMapper = DefaultMessagePackMapperFactory.getInstance().defaultSimpleTypeMapper()
private val timestampConverter = new ObjectConverter[Timestamp, StringValue] {
  override def toValue(obj: Timestamp): StringValue = ValueFactory.newString(obj.getTime.toString)
}
tmpMapper.registerObjectConverter(timestampConverter)
val mapper = new DefaultMessagePackMapper.Builder(tmpMapper)
  .withDefaultListObjectConverter()
  .withDefaultArrayValueConverter()
  .withDefaultMapObjectConverter()
  .withDefaultMapValueConverter()
  .build();
mapper.registerObjectConverter(new DefaultPackableObjectConverter(mapper))

val config = TarantoolClientConfig.builder()
  .withMessagePackMapper(mapper)

But I suppose that it is still a bug and user should only use DefaultMessagePackMapperFactory in such case.

@akudiyar
Copy link
Collaborator

Repro:

public class CustomMappersTest {
    @Test
    void testCustomTimestampMapper() {
        MessagePackMapper defaultMapper = DefaultMessagePackMapperFactory.getInstance().defaultComplexTypesMapper();
        defaultMapper.registerObjectConverter(
                Timestamp.class, StringValue.class, object -> ValueFactory.newString(String.valueOf(object.getTime())));

        Timestamp ts = Timestamp.from(Instant.now());
        List<Object> tuple = Arrays.asList("sbc", 123, Arrays.asList("abc", ts), ts);
        ArrayValue packedTuple = defaultMapper.toValue(tuple);
        assertEquals(tuple.size(), packedTuple.size());
    }
}

@akudiyar
Copy link
Collaborator

Closed via #62

@akudiyar akudiyar modified the milestones: Q4 2020, Q1 2021 Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.5sp Weight 0.5 SP bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants