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

@Json annotations are ignored on Immutables objects #2066

Open
hleibenko-attentive opened this issue Jul 18, 2022 · 4 comments
Open

@Json annotations are ignored on Immutables objects #2066

hleibenko-attentive opened this issue Jul 18, 2022 · 4 comments
Labels
investigation needed stale Stale issue waiting-for-op Waiting for a response from the original poster

Comments

@hleibenko-attentive
Copy link

After some time trying to make Json work with Immutables and debugging Jdbi it looks like if we map an Immutable object to arguments @Json is discarded and because of that the standard Json argument factory doesn't handle the type

@hgschmie
Copy link
Contributor

Hi @hleibenko-attentive,

Thank you for filing an issue with the JDBI project. Do you happen to have a test case or some sample code for us to look at?

@hleibenko-attentive
Copy link
Author

hleibenko-attentive commented Jul 19, 2022

@hgschmie do you have a project template where I can insert example configs?

Summary, if we have an Immutables type like this

@Value.Immutable
public interface Cart {

    static Builder builder() {
        return ImmutableCart.builder();
    }

    @Json //this annotation is not detected because Jdbi reflects on the ImmutableCart
   // workaround is available by using the InjectJdbiJson annotation below
    CartedProduct getProduct();

    interface Builder {
        // this property is processed by BuilderPojoProperties.createProperty so even if a workaround for getProduct is used,
        // this property is unaware of the Json annotation put on the getter above
        Builder product(Product product);

        Cart build();
    }
}

Then this repository doesn't work

public interface CartRepository {

    @SqlUpdate("insert into carts(product) values (:product)")
    //because it can't find an argument factory
    void create(@BindBean Cart cart);

    @SqlQuery("select product from cart limit 1")
    //because it can't find a mapper
    Optional<Cart> findOne();
}

@hleibenko-attentive
Copy link
Author

hleibenko-attentive commented Jul 19, 2022

There is a workaround for update queries / argument mappers using InjectAnnotation from Immutables. But it only allows to set the @JSON annotation on the getter, not on the builder mutator

@InjectAnnotation(type = Json.class, target = ACCESSOR)
public @interface InjectJdbiJson {}

@hgschmie
Copy link
Contributor

Hi @hleibenko-attentive,

I spent a few minutes trying to put a test together but I am not sure. I don't know where "CartedProduct" comes etc. There are some examples for bean mapping in the test classes; it would be very useful to have some example on how you use immutables (and ideally working code with the workaround that breaks when removing it).

@hgschmie hgschmie added stale Stale issue waiting-for-op Waiting for a response from the original poster labels Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigation needed stale Stale issue waiting-for-op Waiting for a response from the original poster
Development

No branches or pull requests

2 participants