dropwizard icon indicating copy to clipboard operation
dropwizard copied to clipboard

Can't add a ConfiguredBundle in another ConfiguredBundle without warnings

Open msbit01 opened this issue 10 years ago • 9 comments

I'm using Dropwizard 1.0.0 and am running into an issue where I'm trying to have nested ConfiguredBundles, so users only need to call addBundle for the top-level bundle. The problem is that the top-level bundle cannot call addBundle for its nested bundle using generics without getting warnings. Here is an example:

// Bundle to be nested
public class InnerBundle<T extends Configuration & HasProperty> implements ConfiguredBundle<T> {...}

// Bundle intended to be added by a consumer
public class OuterBundle<T extends Configuration & HasProperty> implements ConfiguredBundle<T> {
    // Add the InnerBundle as a nested bundle
    @Override
    public void initialize(Bootstrap<?> bootstrap) {
        ConfiguredBundle<T> bundle = new InnerBundle<>();
        bootstrap.addBundle(bundle);
    }
}

The call to bootstrap.addBundle(bundle) fails because addBundle takes a ? super T where T is in the context of Bootstrap<T extends Configuration>: https://github.com/dropwizard/dropwizard/blob/v1.0.0/dropwizard-core/src/main/java/io/dropwizard/setup/Bootstrap.java#L147. So it would seem to me that in the call to addBundle, the ConfiguredBundle must have a generic type that is a supertype of Configuration (by the signature of addBundle) but also a subtype of Configuration (by the signature of initialize which takes Bootstrap<?>). In other words, the ConfiguredBundle must have generic type Configuration, which seems very restrictive.

Is this analysis correct and is this the intended behavior?

msbit01 avatar Aug 19 '16 19:08 msbit01

Your analysis is correct, with one clarification (emphasis mine):

in the call to addBundle, the ConfiguredBundle must have a generic type that is a supertype of Configuration (by the signature of addBundle) but also a subtype of Configuration (by the signature of initialize which takes Bootstrap).

The type parameter of the ConfiguredBundle passed to Bootstrap#addBundle must be a subtype of Configuration and a supertype of T extends Configuration. So the ? type must lie within the parental chain between T and Configuration. But in practice, ? would typically be equal to T, as in the cases illustrated by dropwizard-hibernate's tests.

This issue has come up before. That PR, which bounded the wildcard type in ConfiguredBundle#initialize, was merged but then reverted when it was found to break bundles in previous versions of Dropwizard.

In my opinion, this change should have been reconsidered prior to the release of 1.0.0. Given that it's a breaking change, I don't see how we could include it in a release any time soon.

evnm avatar Aug 20 '16 16:08 evnm

That makes sense to me. Is there a way we can prioritize this for the next breaking release so it doesn't slip through the cracks again?

msbit01 avatar Aug 23 '16 19:08 msbit01

We could introduce a 2.0.0 milestone on GitHub. To my knowledge, the topic hasn't even come up yet, since 1.0.0 is still fresh off the presses. So while we can keep track of issues like this, I really can't guess when the next major version bump will be.

evnm avatar Aug 23 '16 19:08 evnm

This might be hacky but what if there was another ConfiguredBundle (let's call it TypedConfiguredBundle) that would have a signature of void initialize(Bootstrap<T> bootstrap);?

That would also imply the need of another bootstrap.addBundle method that takes the TypedConfigurationBundle.

I will give it a try later on and report back, but could this work?

phss avatar Sep 15 '16 10:09 phss

That would work, but would introduce temporary boilerplate that would ideally be removed in 2.x in favor of adding an explicit type to ConfiguredBundle. This would break all applications that rely on this TypedConfiguredBundle and increase the 2.x migration burden.

IMO, this would only be reasonable if the API of the new Bundle subclass is frozen and does not diverge from that of ConfiguredBundle over the course of the 1.x release train. This would make migration to 2.x a one-line change.

evnm avatar Sep 15 '16 23:09 evnm

Hi! I would like to put my 5 cents.

Is there still reason to keep both Bundle and ConfiguredBundle in 2.0.0? As far as I can tell there is no difference between them except signature, so maybe for simplicity it's reasonable to keep only one kind of Bundle with configuration?

alex-shpak avatar Jun 21 '18 12:06 alex-shpak

Maintainers, Do you think my suggestion above is reasonable? I could work on PR solving it.

alex-shpak avatar Sep 21 '18 08:09 alex-shpak

Yeah sure, I'd love to see what you have in mind

nickbabcock avatar Sep 21 '18 13:09 nickbabcock

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 14 days

github-actions[bot] avatar Aug 19 '19 02:08 github-actions[bot]