Skip to content

Fix Avaje HttpClient interface generation error #763

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 3 commits into from
Jan 15, 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
7 changes: 7 additions & 0 deletions inject-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
<version>1.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-client</artifactId>
<version>2.9-RC7</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ void addLifecycleCallbacks(Append writer, String indent) {
var priority = preDestroyPriority == null || preDestroyPriority == 1000 ? "" : ", " + preDestroyPriority;
writer.indent(indent).append(" builder.addPreDestroy($bean::%s%s);", preDestroyMethod.getSimpleName(), priority).eol();
} else if (typeReader.isClosable() && !prototype) {
writer.indent(indent).append(" builder.addPreDestroy($bean);").eol();
writer.indent(indent).append(" builder.addAutoClosable($bean);").eol();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ void builderBuildAddBean(Append writer) {
: "builder.addPostConstruct($bean::%s);";
writer.indent(indent).append(addPostConstruct, initMethod).eol();
}
final var isCloseable = typeReader != null && typeReader.isClosable();

var priority = priority(destroyPriority);
if (notEmpty(destroyMethod)) {
Expand All @@ -302,14 +303,14 @@ void builderBuildAddBean(Append writer) {
: "builder.addPreDestroy(%s%s);";
writer.indent(indent).append(addPreDestroy, addPreDestroy(destroyMethod), priority).eol();

} else if (typeReader != null && typeReader.isClosable()) {
} else if (isCloseable && !priority.isBlank()) {
var addPreDestroy =
multiRegister
? " .forEach($bean -> builder.addPreDestroy($bean::close%s));"
: "builder.addPreDestroy($bean::close%s);";
writer.indent(indent).append(addPreDestroy, priority).eol();

} else if (beanCloseable) {
} else if (isCloseable || beanCloseable) {
var addAutoClosable =
multiRegister
? " .forEach(builder::addAutoClosable);"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ final class TypeExtendsReader {
*/
private boolean closeableClient(TypeElement baseType) {
return ClientPrism.isPresent(baseType)
&& APContext.typeElement("io.avaje.http.client.HttpClient").getInterfaces().stream()
.map(Object::toString)
.anyMatch(AutoCloseable.class.getCanonicalName()::equals);
&& Optional.ofNullable(APContext.typeElement("io.avaje.http.client.HttpClient"))
.map(TypeElement::getInterfaces)
.stream()
.flatMap(List::stream)
.map(Object::toString)
.anyMatch(AutoCloseable.class.getCanonicalName()::equals);
}

private boolean autoProvide() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.avaje.inject.generator.models.valid.lifecycle;

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

@Factory
public class ClientFactory {

@Bean
ClientInterface i() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.avaje.inject.generator.models.valid.lifecycle;

import io.avaje.http.api.Client;

@Client
public interface ClientInterface {}
7 changes: 7 additions & 0 deletions inject-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-client</artifactId>
<version>2.9-RC7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import jakarta.inject.Named;
import org.example.coffee.parent.DesEngi;
import org.example.request.MyHttpClient;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -16,6 +17,19 @@ public class MyFactory {

String methods = "";

@Bean // (autoCloseable = true)
MyHttpClient httpClient() {
return new MyHttpClientGenerated();
}

public static class MyHttpClientGenerated implements MyHttpClient, AutoCloseable {
public boolean closed;
@Override
public void close() throws Exception {
closed = true;
}
}

@Bean
DFact buildDFact() {
return new DFact();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.avaje.inject.xtra.ApplicationScope;
import io.avaje.inject.BeanScope;
import org.example.coffee.parent.DesEngi;
import org.example.request.MyHttpClient;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -22,12 +23,15 @@ public void closableBeans_expect_closedViaPreDestroy() {

final MyFactory.MyClose myClose;
final MyFactory.MyAutoClose myAutoClose;
final MyFactory.MyHttpClientGenerated myHttpClientGenerated;
try (BeanScope context = BeanScope.builder().build()) {
myClose = context.get(MyFactory.MyClose.class);
myAutoClose = context.get(MyFactory.MyAutoClose.class);
myHttpClientGenerated = (MyFactory.MyHttpClientGenerated) context.get(MyHttpClient.class);
}
assertThat(myClose.isClosed()).isTrue();
assertThat(myAutoClose.isClosed()).isTrue();
assertThat(myHttpClientGenerated.closed).isTrue();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.request;

import io.avaje.http.api.Client;

@Client
public interface MyHttpClient {
}
Loading