Skip to content

Commit cf9563d

Browse files
authored
Fix when getting map of beans with parent scope (#502)
Prior to this fix if the parent returning an empty map it was unmodifiable and resulted in an error.
1 parent 710174f commit cf9563d

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

inject-test/src/test/java/org/example/coffee/BeanScopeBuilderAddTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import static org.junit.jupiter.api.Assertions.assertThrows;
1313
import static org.mockito.Mockito.verify;
1414

15-
class BeanScopeBuilderAddTest {
15+
public class BeanScopeBuilderAddTest {
1616

1717
@Test
1818
void withModules_excludingThisOne() {
@@ -28,7 +28,7 @@ void withModules_excludingThisOne() {
2828
}
2929
}
3030

31-
static class SillyModule implements Module {
31+
public static class SillyModule implements Module {
3232

3333
@Override
3434
public Class<?>[] requires() {

inject-test/src/test/java/org/example/coffee/qualifier/StoreManagerWithNamedTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.avaje.inject.BeanScope;
44
import org.example.autonamed.MyAutoB2;
5+
import org.example.coffee.BeanScopeBuilderAddTest;
56
import org.junit.jupiter.api.Test;
67

78
import java.util.Map;
@@ -31,4 +32,25 @@ void test() {
3132
assertThat(mapWithUnnamed).hasSize(1);
3233
}
3334
}
35+
36+
@Test
37+
void mapParent() {
38+
try (BeanScope parent = BeanScope.builder()
39+
.bean(SomeStore.class, new LocalStore())
40+
.modules(new BeanScopeBuilderAddTest.SillyModule())
41+
.build()) {
42+
43+
try (BeanScope beanScope = BeanScope.builder().parent(parent).build()) {
44+
Map<String, SomeStore> stores = beanScope.map(SomeStore.class);
45+
assertThat(stores).hasSize(3);
46+
}
47+
}
48+
}
49+
50+
static final class LocalStore implements SomeStore {
51+
@Override
52+
public String store() {
53+
return "foo";
54+
}
55+
}
3456
}

inject/src/main/java/io/avaje/inject/spi/DBeanMap.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ Map<String, Object> map(Type type, BeanScope parent) {
139139
if (parent == null) {
140140
return map(type);
141141
}
142-
Map<String, Object> result = parent.map(type);
143-
result.putAll(map(type));
142+
Map<String, Object> parentMap = parent.map(type);
143+
Map<String, Object> localMap = map(type);
144+
if (parentMap.isEmpty()) {
145+
return localMap;
146+
} else if (localMap.isEmpty()) {
147+
return parentMap;
148+
}
149+
Map<String, Object> result = new LinkedHashMap<>(parentMap);
150+
result.putAll(localMap);
144151
return result;
145152
}
146153

0 commit comments

Comments
 (0)