algolia / algoliasearch-client-java

⚡️ A fully-featured and blazing-fast Java API client to interact with Algolia.
https://www.algolia.com/doc/api-client/getting-started/install/java/
MIT License
47 stars 33 forks source link

feat: annotation support for getters #792

Open mklueh opened 9 months ago

mklueh commented 9 months ago
Q A
Bug fix? no
New feature? yes
BC breaks? no
Related Issue Fix #...
Need Doc update yes/no

Describe your change

This change will let you use @JsonProperty("objectID") annotations on custom Getter methods.

To make things easier, I've dropped the custom reflection logic in favor of Jackson's internal introspection logic. This allows checking for the objectID within property names, getters and annotation values at once.

I've additionally removed the validation for objectIDs entirely for the "getObjectID" method and used an Optional fallback instead.

The tests are within an extra class to not bloat the original tests.

What problem is this fixing?

If you are working with base classes that already come with an internal ID field you want to use as the "Object ID", and you don't want to modify the base class by adding the @JsonProperty("objectID") annotation, you can only do that by using a custom getter in your child class.

The implementation supported only scans on the field name and field annotations, not method annotations.

Additional

I've added

            .disable(
                SerializationFeature
                        .FAIL_ON_EMPTY_BEANS)

Not sure if it has any side effects you do not want. Stumbled accross this case within my tests and imo it won't hurt.