Skip to content

Fix when getting map of beans with parent scope #502

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

Merged
merged 1 commit into from
Feb 13, 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 @@ -12,7 +12,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;

class BeanScopeBuilderAddTest {
public class BeanScopeBuilderAddTest {

@Test
void withModules_excludingThisOne() {
Expand All @@ -28,7 +28,7 @@ void withModules_excludingThisOne() {
}
}

static class SillyModule implements Module {
public static class SillyModule implements Module {

@Override
public Class<?>[] requires() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.avaje.inject.BeanScope;
import org.example.autonamed.MyAutoB2;
import org.example.coffee.BeanScopeBuilderAddTest;
import org.junit.jupiter.api.Test;

import java.util.Map;
Expand Down Expand Up @@ -31,4 +32,25 @@ void test() {
assertThat(mapWithUnnamed).hasSize(1);
}
}

@Test
void mapParent() {
try (BeanScope parent = BeanScope.builder()
.bean(SomeStore.class, new LocalStore())
.modules(new BeanScopeBuilderAddTest.SillyModule())
.build()) {

try (BeanScope beanScope = BeanScope.builder().parent(parent).build()) {
Map<String, SomeStore> stores = beanScope.map(SomeStore.class);
assertThat(stores).hasSize(3);
}
}
}

static final class LocalStore implements SomeStore {
@Override
public String store() {
return "foo";
}
}
}
11 changes: 9 additions & 2 deletions inject/src/main/java/io/avaje/inject/spi/DBeanMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ Map<String, Object> map(Type type, BeanScope parent) {
if (parent == null) {
return map(type);
}
Map<String, Object> result = parent.map(type);
result.putAll(map(type));
Map<String, Object> parentMap = parent.map(type);
Map<String, Object> localMap = map(type);
if (parentMap.isEmpty()) {
return localMap;
} else if (localMap.isEmpty()) {
return parentMap;
}
Map<String, Object> result = new LinkedHashMap<>(parentMap);
result.putAll(localMap);
return result;
}

Expand Down