Skip to content

Bug fix for @Bean returning Optional not registering extended type #782

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 2 commits into from
Mar 26, 2025
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
@@ -0,0 +1,6 @@
package org.example.myapp.config;

public interface C2Face {

String msg();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example.myapp.config;

public interface CFace extends C2Face {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.example.myapp.config;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
import jakarta.inject.Named;

import java.util.Optional;

@Factory
class CFactory {

@Bean @Named("base")
CFace base() {
return new TheCFace("base");
}

@Bean @Named("optional")
Optional<CFace> optional() {
return Optional.of(new TheCFace("optional"));
}

static final class TheCFace implements CFace {

private final String msg;

TheCFace(String msg) {
this.msg = msg;
}

@Override
public String msg() {
return msg;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.example.myapp.config;

import io.avaje.inject.BeanScope;
import io.avaje.inject.test.TestBeanScope;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class CFactoryTest {

@Test
void optionalWithCascadingInterfaces() {
try (BeanScope scope = TestBeanScope.builder().build()) {
C2Face base = scope.get(C2Face.class, "base");
CFace cface = scope.get(CFace.class, "optional");
C2Face c2face = scope.get(C2Face.class, "optional");
assertThat(base.msg()).isEqualTo("base");
assertThat(cface.msg()).isEqualTo("optional");
assertThat(c2face.msg()).isEqualTo("optional");

List<C2Face> list = scope.list(C2Face.class);
assertThat(list).hasSize(2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ final class MethodReader {
// for multiRegister we desire a qualifier name such that builder.isBeanAbsent() uses it and allows
// other beans of the same type to also register, otherwise it defaults to slightly confusing behaviour
this.name = multiRegister && qualifierName == null ? "multi" : qualifierName;
TypeElement returnElement = multiRegister ? APContext.typeElement(uType.mainType()) : asElement(returnMirror);
TypeElement returnElement =
multiRegister
? APContext.typeElement(returnTypeRaw)
: optionalType ? APContext.typeElement(returnTypeRaw) : asElement(returnMirror);
if (returnElement == null) {
this.typeReader = null;
this.initMethod = initMethod;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.inject.generator.models.valid;

import java.util.Map;
import java.util.Optional;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
Expand All @@ -19,4 +20,10 @@ Map<String, Long> map1() {
Map<String, String> map2(@Named("map1") Map<String, Long> map1) {
return Map.of();
}

@Bean
@Named("optionalA0")
Optional<A0> build() {
return Optional.empty();
}
}