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

Deserialize java.util.UUID encoded as Base64 and base64Url with or without padding #4393

Merged
merged 6 commits into from
Feb 21, 2024

Conversation

jebl01
Copy link
Contributor

@jebl01 jebl01 commented Feb 19, 2024

Vert.x 4 changed to Base64UrlEncoder as the default for binary columns in database responses (Vert.x serializes database responses into JSON arrays). This becomes a problem when Jackson tries to deserialize into a field of type UUID. The workaround is to provide a custom serializer for UUID or to have byte[] fields in your database entities.

This PR ensures all variants of Base64 is supported for UUIDs "out of the box".

Copy link
Member

@pjfanning pjfanning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a new line to the end of new file?

byte[] stuff = Base64Variants.getDefaultVariant().decode(id);
byte[] stuff = Base64Variants.getDefaultVariant().decode(id
//cater for url safe chars
.replace("-", "+")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this remarkably inefficient way to do this, using dynamically compiled regexp replaced and constructing Strings on the fly? Should be easy enough to change to character-based alternative.

But overall I am not sure how safe it is to start replacing characters here. I'll have to think about this a bit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace(String, String) is actually not regex, and it even delegates to replace(char, char) if both strings are length 1. still should move to the char version though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. I misremembered that then. Should probably move logic to separate helper method regardless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, changed to chars for the replacement now + separated the logic to helper methods.

class UUIDDeserializerTest {
@Test
void testCanDeserializeUUIDFromString() throws IOException {
final UUID uuid = UUID.randomUUID();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using new generated random UUIDs, it'd be better to generate one randomly but include as constant.
This to avoid any possibility of having non-reproducible fails (at expense of not covering many combinations).

It is unfortunate JDK does not offer a way to see generator for random-number variant, for testing (but perhaps they were worried about devs using that in production and end up with collisions), as otherwise we'd use that.
JUG of course can do that but perhaps it's better not to add it as a (test) dependency,.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I can do that 👍

@cowtowncoder
Copy link
Member

@jebl01 Ok, looks good -- I think this makes sense and timing is good as this can make it into 2.17.0-rc1 release, to be released soon (within a week I hope).

One thing I'd need before final review & merge: CLA (if we haven't already gotten one -- apologies if I missed earlier one).
It's from

https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf

(there's alternative CCLA too if preferable)

and is usually easiest to print, fill & sign, scan/photo, email to cla at fasterxml dot com.
Once we get it I can proceed with merging and we can the improvement in.

Thank you once again for contributing this!

@jebl01
Copy link
Contributor Author

jebl01 commented Feb 20, 2024

CLA in the mail 📫

@cowtowncoder cowtowncoder changed the title deserialize UUID encoded as Base64 and base64Url with or without padding Deserialize java.util.UUID encoded as Base64 and base64Url with or without padding Feb 21, 2024
@cowtowncoder
Copy link
Member

Looks good, made some non-functional changes to help merging to master (3.0).

@cowtowncoder cowtowncoder merged commit 28558da into FasterXML:2.17 Feb 21, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants