kora icon indicating copy to clipboard operation
kora copied to clipboard

@Repository generated components are not recognised by graph if their indirect parents are requested

Open mitasov-ra opened this issue 2 years ago • 6 comments

Assume I have the following class hierarchy:

interface FooRepository { ... }

@Repository
abstract class AbstractFooRepository( ... ) : FooRepository, JdbcRepository { ... }

@Generated
class `$AbstractFooRepository_Impl`( ... ) : AbstractFooRepository( ... ) {
  ...
}

If I then create the new component:

@Component
class FooService(val repo: FooRepository)

I get the following error:

Required dependency type FooRepository was not found and can't be autocreated.

But if I change repo type from FooRepository to AbstractFooRepository everything compiles just fine

mitasov-ra avatar Jun 20 '23 10:06 mitasov-ra

Turned out that checking only requested type for @Repository annotation is the specific of Kora's Repository extension.

The intended way of solving the problem is adding:

@Module
interface FooRepositoryModule {
  fun fooRepository(a: AbstractFooRepository): FooRepository = a
}

mitasov-ra avatar Jun 20 '23 11:06 mitasov-ra

Can't guarantee 100% scan on all rounds

Advice: Use injection by class:

@Module
interface FooRepositoryModule {
  fun fooRepository(a: AbstractFooRepository): FooRepository = a
}

GoodforGod avatar Jul 10 '23 13:07 GoodforGod

In 0.13.1 version the above solution doesn't work anymore. But problem still persist.

I get the error:

Required dependency type FooRepository was not found and can't be autocreated.

but now when I try to fix it with module:

@Module
interface FooRepositoryModule {
  fun fooRepository(a: AbstractFooRepository): FooRepository = a
}

I get the following error:

More than one component matches dependency claim FooRepository tag=[]:
  TargetDependency(claim=DependencyClaim(type=FooRepository, tags=[], claimType=ONE_REQUIRED), component=ResolvedComponent(index=125, declaration=FromExtensionComponent(type=$AbstractFooRepository_Impl ...
  TargetDependency(claim=DependencyClaim(type=FooRepository, tags=[], claimType=ONE_REQUIRED), component=ResolvedComponent(index=126, declaration=FromModuleComponent(type=FooRepository, module=MixedInModule(element=Application) ...

The only remaining fix for this issue that I found is manually instantiating $AbstractFooRepository_Impl:

@Module
interface FooRepositoryModule {
  fun fooRepository(/* ... deps ... */): FooRepository = `$AbstractFooRepository_Impl`(/* ... deps ... */)
}

mitasov-ra avatar Jul 18 '23 07:07 mitasov-ra

Try put @Component on your repository class.

Squiry avatar Jul 18 '23 16:07 Squiry

Tried. Got the error:

ApplicationGraph.kt:1655:11: Cannot create an instance of an abstract class
      component118 = graphDraw.addNode0(map["component118"], 
        arrayOf(),
        { AbstractFooRepository(
          it.get(holder0.component117)
        ) },
        listOf(),
        component117
      )

mitasov-ra avatar Jul 21 '23 08:07 mitasov-ra

Kora version: 0.13.3

Now even my last fix (which btw worked in 0.11.10) to directly instantiate $AbstractFooRepository_Impl does not work.

The following Module:

@Module
interface FooRepositoryModule {
  fun fooRepository(/* ... deps ... */): FooRepository = `$AbstractFooRepository_Impl`(/* ... deps ... */)
}

Makes task :kspKotlin run forever.

mitasov-ra avatar Jul 21 '23 08:07 mitasov-ra