Skip to content

Commit

Permalink
fix(spring): add AutoConfigureAfter annotation (#1208)
Browse files Browse the repository at this point in the history
This adds AutoConfigureAfter(GcpContextAutoConfiguration.class) annotation to enforce configuration order. It also makes two related changes to enable this annotation:
* Switches dependency from spring-cloud-gcp-core to spring-cloud-gcp-autoconfigure, and bumps version to latest
* Fixes ImportWriter to account for types introduced by annotation parameters
  • Loading branch information
emmileaf authored Jan 6, 2023
1 parent 62153dc commit 9b7336d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ SPRING_DEPS = [
"@maven//:org_springframework_boot_spring_boot",
"@maven//:org_springframework_spring_context",
"@maven//:org_springframework_spring_beans",
"@maven//:com_google_cloud_spring_cloud_gcp_autoconfigure",
"@maven//:com_google_cloud_spring_cloud_gcp_core",
"@maven//:commons_logging_commons_logging",
]
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ load("@rules_jvm_external//:defs.bzl", "maven_install")

SPRING_MAVEN_ARTIFACTS = [
"org.springframework.boot:spring-boot-starter:2.7.4",
"com.google.cloud:spring-cloud-gcp-core:3.3.0",
"com.google.cloud:spring-cloud-gcp-autoconfigure:3.4.1",
]

maven_install(
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-core</artifactId>
<version>3.3.0</version>
<artifactId>spring-cloud-gcp-autoconfigure</artifactId>
<version>3.4.1</version>
</dependency>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public void visit(ScopeNode scope) {
@Override
public void visit(AnnotationNode annotation) {
annotation.type().accept(this);
if (annotation.descriptionExprs() != null) {
expressions(annotation.descriptionExprs());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.api.generator.spring.utils.ComposerUtils;
import com.google.api.generator.spring.utils.LoggerUtils;
import com.google.api.generator.spring.utils.Utils;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.common.base.CaseFormat;
Expand All @@ -71,6 +72,7 @@
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -265,6 +267,7 @@ private static MethodDefinition createConstructor(
private static List<AnnotationNode> createClassAnnotations(
Service service, Map<String, TypeNode> types) {
// @AutoConfiguration
// @AutoConfigureAfter(GcpContextAutoConfiguration.class)
// @ConditionalOnClass(LanguageServiceClient.class)
// @ConditionalOnProperty(value =
// "com.google.cloud.language.v1.spring.auto.language-service.enabled", matchIfMissing = true)
Expand Down Expand Up @@ -307,6 +310,16 @@ private static List<AnnotationNode> createClassAnnotations(
.setStaticReferenceType(types.get("ServiceClient"))
.build())
.build();
AnnotationNode autoConfigureAfterNode =
AnnotationNode.builder()
.setType(STATIC_TYPES.get("AutoConfigureAfter"))
.setDescription(
VariableExpr.builder()
.setVariable(
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(STATIC_TYPES.get("GcpContextAutoConfiguration"))
.build())
.build();
AnnotationNode configurationNode =
AnnotationNode.builder().setType(STATIC_TYPES.get("AutoConfiguration")).build();
AnnotationNode enableConfigurationPropertiesNode =
Expand All @@ -322,6 +335,7 @@ private static List<AnnotationNode> createClassAnnotations(

return Arrays.asList(
configurationNode,
autoConfigureAfterNode,
conditionalOnClassNode,
conditionalOnPropertyNode,
enableConfigurationPropertiesNode);
Expand Down Expand Up @@ -958,7 +972,9 @@ private static Map<String, TypeNode> createStaticTypes() {
ConditionalOnMissingBean.class,
EnableConfigurationProperties.class,
CredentialsProvider.class,
GcpContextAutoConfiguration.class,
AutoConfiguration.class,
AutoConfigureAfter.class,
Bean.class,
Qualifier.class,
DefaultCredentialsProvider.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Repeatable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -390,6 +391,43 @@ public void writeVariableExprImports_withAnnotations() {
writerVisitor.write());
}

@Test
public void writeVariableExprImports_annotationsWithDescription() {
Variable variable =
Variable.builder()
.setName("expr")
.setType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class)))
.build();

VariableExpr annotationDescription =
VariableExpr.builder()
.setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(List.class)))
.build();

// Constructs with annotation @Repeatable(List.class)
VariableExpr variableExpr =
VariableExpr.builder()
.setVariable(variable)
.setIsDecl(true)
.setAnnotations(
Arrays.asList(
AnnotationNode.builder()
.setType(
TypeNode.withReference(ConcreteReference.withClazz(Repeatable.class)))
.setDescription(annotationDescription)
.build()))
.build();

variableExpr.accept(writerVisitor);
assertEquals(
LineFormatter.lines(
"import com.google.api.generator.engine.ast.Expr;\n",
"import java.lang.annotation.Repeatable;\n",
"import java.util.List;\n\n"),
writerVisitor.write());
}

@Test
public void writeAnonymousClassExprImports() {
// [Constructing] Function<List<IOException>, MethodDefinition>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.core.Retry;
Expand All @@ -35,6 +36,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -58,6 +60,7 @@ import org.springframework.context.annotation.Bean;
@Generated("by google-cloud-spring-generator")
@BetaApi("Autogenerated Spring autoconfiguration is not yet stable")
@AutoConfiguration
@AutoConfigureAfter(GcpContextAutoConfiguration.class)
@ConditionalOnClass(EchoClient.class)
@ConditionalOnProperty(value = "com.google.showcase.v1beta1.echo.enabled", matchIfMissing = true)
@EnableConfigurationProperties(EchoSpringProperties.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.core.Retry;
Expand All @@ -17,6 +18,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -38,6 +40,7 @@ import org.springframework.context.annotation.Bean;
* </ul>
*/
@AutoConfiguration
@AutoConfigureAfter(GcpContextAutoConfiguration.class)
@ConditionalOnClass(EchoClient.class)
@ConditionalOnProperty(value = "com.google.showcase.v1beta1.echo.enabled", matchIfMissing = true)
@EnableConfigurationProperties(EchoSpringProperties.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.core.Retry;
Expand All @@ -18,6 +19,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -39,6 +41,7 @@ import org.springframework.context.annotation.Bean;
* </ul>
*/
@AutoConfiguration
@AutoConfigureAfter(GcpContextAutoConfiguration.class)
@ConditionalOnClass(EchoClient.class)
@ConditionalOnProperty(value = "com.google.showcase.v1beta1.echo.enabled", matchIfMissing = true)
@EnableConfigurationProperties(EchoSpringProperties.class)
Expand Down

0 comments on commit 9b7336d

Please sign in to comment.