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

Improve displaying of circular dependency error #11299

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,66 @@ class ConstructorCircularDependencyFailureSpec extends Specification {
ApplicationContext context = ApplicationContext.run()

when:"A bean is obtained that has a setter with @Inject"
B b = context.getBean(B)
MyClassB b = context.getBean(MyClassB)

then:"The implementation is injected"
def e = thrown(CircularDependencyException)
e.message.normalize() == '''\
Failed to inject value for field [a] of class: io.micronaut.inject.failures.ConstructorCircularDependencyFailureSpec$B
Failed to inject value for field [propA] of class: io.micronaut.inject.failures.ConstructorCircularDependencyFailureSpec$MyClassB

Message: Circular dependency detected
Path Taken:
new B() --> B.a --> new A([C c]) --> new C([B b])
^ |
| |
| |
+----------------------------------------------+'''
Path Taken:
new MyClassB()
\\---> MyClassB.propA
^ \\---> new MyClassA([MyClassC propC])
| \\---> new MyClassC([MyClassB propB])
| |
+--------------+'''

cleanup:
context.close()
}

static class C {
@Inject C( B b ) {}
void "test another constructor circular dependency failure"() {
given:
ApplicationContext context = ApplicationContext.run(["spec.name": getClass().simpleName])

when:"A bean is obtained that has a setter with @Inject"
context.getBean(MyClassD)

then:"The implementation is injected"
def e = thrown(CircularDependencyException)
e.message.normalize() == '''\
Failed to inject value for field [propA] of class: io.micronaut.inject.failures.ConstructorCircularDependencyFailureSpec$MyClassB

Message: Circular dependency detected
Path Taken:
new MyClassD(MyClassB propB)
\\---> new MyClassD([MyClassB propB])
\\---> MyClassB.propA
^ \\---> new MyClassA([MyClassC propC])
| \\---> new MyClassC([MyClassB propB])
| |
+--------------+'''
}

static class MyClassC {
@Inject
MyClassC(MyClassB propB) {}
}
@Singleton
static class A {
A(C c) {}
static class MyClassA {
MyClassA(MyClassC propC) {}
}

@Singleton
static class MyClassB {
@Inject protected MyClassA propA
}

@Singleton
static class B {
@Inject protected A a
static class MyClassD {
MyClassD(MyClassB propB) {}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2017-2019 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.inject.failures

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Factory
import io.micronaut.context.exceptions.CircularDependencyException
import spock.lang.Specification

import javax.inject.Inject
import javax.inject.Singleton


class FactoryCircularDependencyFailureSpec extends Specification {

void "test circular dependency with factory failure"() {
given:
ApplicationContext context = ApplicationContext.run()

when:"A bean is obtained that has a setter with @Inject"
context.getBean(ElectricalGrid)

then:"The implementation is injected"
def e = thrown(CircularDependencyException)
e.message.normalize() == '''\
Failed to inject value for parameter [stations] of class: io.micronaut.inject.failures.FactoryCircularDependencyFailureSpec$ElectricalGrid

Message: Circular dependency detected
Path Taken:
new ElectricalGrid(List<ElectricStation E> stations)
\\---> new ElectricalGrid([List<ElectricStation E> stations])
^ \\---> ElectricStationFactory.nuclearStation([MeasuringEquipment equipment])
| \\---> MeasuringEquipment.grid
| |
+--------------+'''

cleanup:
context.close()
}

static class ElectricalGrid {
@Inject
ElectricalGrid(List<ElectricStation> stations) {}
}

static class ElectricStation {
ElectricStation() {}
}

@Factory
static class ElectricStationFactory {

@Singleton
ElectricStation solarStation() {
return new ElectricStation()
}

@Singleton
ElectricStation nuclearStation(MeasuringEquipment equipment) {
return new ElectricStation()
}

}

@Singleton
static class MeasuringEquipment {
@Inject protected ElectricalGrid grid
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ class FieldCircularDependencyFailureSpec extends Specification {
Failed to inject value for field [a] of class: io.micronaut.inject.failures.FieldCircularDependencyFailureSpec$B

Message: Circular dependency detected
Path Taken:
new B() --> B.a --> new A([C c]) --> C.b
^ |
| |
| |
+-------------------------------------+'''
Path Taken:
new B()
\\---> B.a
^ \\---> new A([C c])
| \\---> C.b
| |
+--------------+'''
cleanup:
context.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ class PropertyCircularDependencyFailureSpec extends Specification {

then:"The implementation is injected"
CircularDependencyException e = thrown()
def lines = e.message.lines().toList()
lines[0] == 'Failed to inject value for parameter [a] of method [setA] of class: io.micronaut.inject.failures.PropertyCircularDependencyFailureSpec$B'
lines[1] == ''
lines[2] == 'Message: Circular dependency detected'
lines[3] == 'Path Taken: '
lines[4] == 'new B() --> B.setA([A a]) --> A.setB([B b])'
lines[5] == '^ |'
lines[6] == '| |'
lines[7] == '| |'
lines[8] == '+----------------------------------------+'
e.message == '''\
Failed to inject value for parameter [a] of method [setA] of class: io.micronaut.inject.failures.PropertyCircularDependencyFailureSpec$B

Message: Circular dependency detected
Path Taken:
new B()
\\---> B.setA([A a])
^ \\---> A.setB([B b])
| |
+--------+'''

cleanup:
context.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,54 @@
package io.micronaut.inject.failures.ctorcirculardependency

import io.micronaut.context.ApplicationContext
import io.micronaut.context.BeanContext
import io.micronaut.context.DefaultBeanContext
import io.micronaut.context.annotation.Property
import io.micronaut.context.exceptions.CircularDependencyException
import spock.lang.Specification

import jakarta.inject.Singleton

class ConstructorCircularDependencyFailureSpec extends Specification {

void "test simple constructor circular dependency failure"() {
given:
ApplicationContext context = ApplicationContext.run(["spec.name": getClass().simpleName])

when:"A bean is obtained that has a setter with @Inject"
B b = context.getBean(B)
context.getBean(MyClassB)

then:"The implementation is injected"
def e = thrown(CircularDependencyException)
e.message.normalize() == '''\
Failed to inject value for field [propA] of class: io.micronaut.inject.failures.ctorcirculardependency.MyClassB

Message: Circular dependency detected
Path Taken:
new MyClassB()
\\---> MyClassB.propA
^ \\---> new MyClassA([MyClassC propC])
| \\---> new MyClassC([MyClassB propB])
| |
+--------------+'''
}

void "test another constructor circular dependency failure"() {
given:
ApplicationContext context = ApplicationContext.run(["spec.name": getClass().simpleName])

when:"A bean is obtained that has a setter with @Inject"
context.getBean(MyClassD)

then:"The implementation is injected"
def e = thrown(CircularDependencyException)
e.message.normalize() == '''\
Failed to inject value for field [a] of class: io.micronaut.inject.failures.ctorcirculardependency.B
Failed to inject value for field [propA] of class: io.micronaut.inject.failures.ctorcirculardependency.MyClassB

Message: Circular dependency detected
Path Taken:
new B() --> B.a --> new A([C c]) --> new C([B b])
^ |
| |
| |
+----------------------------------------------+'''
Path Taken:
new MyClassD(MyClassB propB)
\\---> new MyClassD([MyClassB propB])
\\---> MyClassB.propA
^ \\---> new MyClassA([MyClassC propC])
| \\---> new MyClassC([MyClassB propB])
| |
+--------------+'''
}

void "test multiple optionals do not cause a circular dependency exception"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@

@Requires(property = "spec.name", value = "ConstructorCircularDependencyFailureSpec")
@Singleton
public class A {
public A(C c) {}
public class MyClassA {
public MyClassA(MyClassC propC) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@Requires(property = "spec.name", value = "ConstructorCircularDependencyFailureSpec")
@Singleton
public class B {
public class MyClassB {
@Inject
protected A a;
protected MyClassA propA;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import jakarta.inject.Inject;

public class C {
public class MyClassC {
@Inject
public C(B b ) {}
public MyClassC(MyClassB propB) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.micronaut.inject.failures.ctorcirculardependency;

import io.micronaut.context.annotation.Requires;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Requires(property = "spec.name", value = "ConstructorCircularDependencyFailureSpec")
@Singleton
public class MyClassD {

@Inject
public MyClassD(MyClassB propB) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ class FieldCircularDependencyFailureSpec extends Specification {
Failed to inject value for field [a] of class: io.micronaut.inject.failures.fieldcirculardependency.B

Message: Circular dependency detected
Path Taken:
new B() --> B.a --> new A([C c]) --> C.b
^ |
| |
| |
+-------------------------------------+'''
Path Taken:
new B()
\\---> B.a
^ \\---> new A([C c])
| \\---> C.b
| |
+--------------+'''

cleanup:
context.close()
Expand Down
Loading
Loading