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

Cannot deserialize UUID values #506

Closed
uniquonil opened this issue Jul 23, 2024 · 8 comments
Closed

Cannot deserialize UUID values #506

uniquonil opened this issue Jul 23, 2024 · 8 comments
Labels
Milestone

Comments

@uniquonil
Copy link

Hi.
Using jackson-dataformat-protobuf:2.16.2, though we can serialize java.util.UUID, we CAN'T deserialize.

import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;

import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

public class JacksonProtobufUUID {
    final static class Pojo {
        public UUID id;
    }

    public static void main (String[] args) throws IOException {
        var mapper = new ProtobufMapper();
        var schema = mapper.generateSchemaFor(Pojo.class);

        var pojo = new Pojo();
        pojo.id = UUID.randomUUID();
        System.out.println(pojo.id);

        var serialized = mapper.writer(schema).writeValueAsBytes(pojo);
        System.out.println(Arrays.toString(serialized));

        var deserialized = mapper.reader(schema).readValue(serialized, Pojo.class);
        System.out.println(deserialized.id);
    }
}

This code results below:

993ea091-6eb4-429d-a8f0-540d4b18419e
[10, 16, -103, 62, -96, -111, 110, -76, 66, -99, -88, -16, 84, 13, 75, 24, 65, -98]
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Invalid byte 99 in Unicode text block
 at [Source: (byte[])[18 bytes]; byte offset: #18] (through reference chain: JacksonProtobufUUID$Pojo["id"])
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:402)
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:361)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1937)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:312)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2125)
	at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1603)
	at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1618)
	at JacksonProtobufUUID.main(JacksonProtobufUUID.java:23)
Caused by: com.fasterxml.jackson.core.JsonParseException: Invalid byte 99 in Unicode text block
 at [Source: (byte[])[18 bytes]; byte offset: #18]
	at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2481)
	at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:752)
	at com.fasterxml.jackson.dataformat.protobuf.ProtobufParser._finishShortText(ProtobufParser.java:1944)
	at com.fasterxml.jackson.dataformat.protobuf.ProtobufParser.getValueAsString(ProtobufParser.java:1390)
	at com.fasterxml.jackson.databind.deser.std.FromStringDeserializer.deserialize(FromStringDeserializer.java:148)
	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:310)
	... 6 more

Is this expected behavior?

@cowtowncoder
Copy link
Member

No, sounds like a bug. Looks like UUID value is correctly serialized as 16-byte "raw" value, but deserialization side expects String, tries to decode as such.

Since 2.16 is not the latest version, could try with 2.17.2, but I doubt behavior is different.

@uniquonil
Copy link
Author

Thank you for your responding.
I tried with 2.17.2 and yes, I got same result.
I'll go with workaround such as serializing as string representation or two long values(sigBits) or something.

@cowtowncoder
Copy link
Member

Re-opened since there might be something to do here.

@uniquonil
Copy link
Author

Ah I closed by mistake, sorry.

@cowtowncoder cowtowncoder changed the title [protobuf] Cannot deserialize UUID. Cannot deserialize UUID values Jul 25, 2024
@cowtowncoder cowtowncoder added this to the 2.17.3 milestone Jul 25, 2024
@cowtowncoder
Copy link
Member

Turned out quite simple to fix, via #507; merged in 2.17 (for eventual 2.17.3), 2.18 (2.18.0).

@uniquonil
Copy link
Author

Is there any chance that bug-fix comes down to 2.16?
I cannot upgrade easily because of my using framework's dependency.

@cowtowncoder
Copy link
Member

Technically I could merge back down to 2.16, but it's unlikely I'll be releasing updates to 2.16 branch (all full minor releases take 2-3 hours).

I will backport/cherry-pick the fix tho just in case.

@uniquonil
Copy link
Author

I got it.
Thank you for your quick reaction anyway!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants