spring-projects / spring-projects/spring-framework

JavaConfig bean resolution fails with generics + Kotlin declaration-site variance

Open
#22,313 10 comments 0 reactions 1 assignee View on GitHub

@sdeleuze is already working on this.

Since Feb 8, 2023.

in: core theme: kotlin type: enhancement
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

Declaration-site variance used in the very common kotlin.Pair class seems to impact bean resolution and currently prevents bean resolution to work as expected by the end user.

Based on an original report on Twitter, I have created a more focused repro project.

Following test works with class Tuple<A, B> but fails with class Tuple<out A, out B>, which is pretty hard to diagnose when the user is using kotlin.Pair which is provided in Kotlin standard library.

@RunWith(SpringRunner::class)
@ContextConfiguration(classes = [Config::class])
class TypeProjectionTest {

  @Inject
  private lateinit var ctx: Container<Tuple<String, String>>

  @Test
  fun testContext() {
  }

}

// Variant 1: works when not using declaration-site variance
//class Tuple<A, B>

// Variant 2: Fails with using declaration-site variance
class Tuple<out A, out B>

private interface Container<T>
private class ContainerTuple : Container<Tuple<String, String>>

@Configuration
private open class Config {

  @Bean
  open fun containerPair() : ContainerTuple {
    return ContainerTuple()
  }

}

@jhoeller Do you think there is something to refine in our bean resolution algorithm to support this kind of Kotlin declaration-site variance?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.