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

Fix avaje module #3426

Merged
merged 5 commits into from
May 19, 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
52 changes: 50 additions & 2 deletions modules/jooby-avaje-inject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,26 @@
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<groupId>io.jooby</groupId>
<artifactId>jooby-netty</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jackson</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>test</scope>
</dependency>

Expand All @@ -51,4 +69,34 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<phase>test-compile</phase>
</execution>
</executions>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.jooby</groupId>
<artifactId>jooby-apt</artifactId>
</path>
<path>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.util.List;
import java.util.stream.Collectors;

import com.typesafe.config.Config;

import io.avaje.inject.BeanScope;
import io.avaje.inject.BeanScopeBuilder;
import io.jooby.Extension;
Expand Down Expand Up @@ -68,7 +66,7 @@ public void install(Jooby application) throws Exception {
e -> {
final var key = e.getKey();
if (key.getName() == null) {
beanScope.provideDefault(key.getType(), e::getValue);
beanScope.provideDefault(key.getType(), e.getValue()::get);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

} else {
beanScope.bean(key.getName(), key.getType(), e.getValue());
}
Expand All @@ -80,7 +78,6 @@ public void install(Jooby application) throws Exception {
// configuration properties
final var config = environment.getConfig();
beanScope.propertyPlugin(new JoobyPropertyPlugin(config));
beanScope.bean(Config.class, config);

for (var entry : config.entrySet()) {
String name = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.jooby.avaje.inject;

import io.jooby.avaje.inject.app.TestApp;
import io.jooby.test.JoobyTest;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AvajeInjectModuleTest {

static OkHttpClient client = new OkHttpClient();

@JoobyTest(TestApp.class)
public void shouldPropagateJoobyServicesToAvajeBeanScope(String serverPath) throws IOException {
Request request = new Request.Builder().url(serverPath + "ping").build();

try (Response response = client.newCall(request).execute()) {
assertEquals("test", response.body().string());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.jooby.avaje.inject.app;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.typesafe.config.Config;
import io.avaje.inject.InjectModule;
import io.jooby.annotation.GET;
import io.jooby.annotation.Path;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

@Singleton
@Path("")
@InjectModule(requires = {JsonMapper.class, Config.class, String.class})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never use Avaje Inject. Why do we need this line?

Copy link
Contributor Author

@kliushnichenko kliushnichenko May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Avaje Inject, the dependency graph is typically validated when the application compiles. As beans provided by    Jooby Modules are registered at runtime, you must add @InjectModules(requires={String.class, <other Jooby    Module Provided Bean ClassNames>…​}) to inform the avaje processor that these beans are provided at runtime.   

from https://jooby.io/#dependency-injection-avaje-inject-mvc-routes :)

public class Controller {

private final JsonMapper jsonMapper;
private final Config config;
private final String env;

@Inject
public Controller(JsonMapper jsonMapper, Config config, @Named("application.env") String env) {
this.jsonMapper = jsonMapper;
this.config = config;
this.env = env;
}

@GET
@Path("/ping")
public String ping() {
jsonMapper.version();
config.isEmpty();

return this.env;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.jooby.avaje.inject.app;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.typesafe.config.Config;
import io.jooby.Jooby;
import io.jooby.avaje.inject.AvajeInjectModule;
import io.jooby.jackson.JacksonModule;

public class TestApp extends Jooby {

{
install(new JacksonModule());
install(AvajeInjectModule.of());

mvc(Controller.class);

onStarted(() -> {
JsonMapper jsonMapper = require(JsonMapper.class);
Config config = require(Config.class);
jsonMapper.version();
config.isEmpty();
});
}
}
Loading