Skip to content

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

Open
mitasov-ra opened this issue Jun 20, 2023 · 6 comments
Assignees
Labels
module: database Related to module - database

Comments

@mitasov-ra
Copy link
Contributor

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
Copy link
Contributor Author

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
}

@GoodforGod GoodforGod self-assigned this Jul 10, 2023
@GoodforGod GoodforGod added the module: database Related to module - database label Jul 10, 2023
@GoodforGod
Copy link
Contributor

Can't guarantee 100% scan on all rounds

Advice: Use injection by class:

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

@GoodforGod GoodforGod added the wontfix This will not be worked on label Jul 10, 2023
@mitasov-ra
Copy link
Contributor Author

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 mitasov-ra reopened this Jul 18, 2023
@GoodforGod GoodforGod assigned Squiry and unassigned GoodforGod Jul 18, 2023
@GoodforGod GoodforGod removed the wontfix This will not be worked on label Jul 18, 2023
@Squiry
Copy link
Contributor

Squiry commented Jul 18, 2023

Try put @Component on your repository class.

@mitasov-ra
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
module: database Related to module - database
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants