Skip to content

Commit

Permalink
fix(Spring CodeGen): switch to Spring boot 2.7 style in registering a…
Browse files Browse the repository at this point in the history
…uto-configurations (#1044)

Spring Boot 2.7 has introduced [changes to how auto-configurations](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#changes-to-auto-configuration) are registered. Both changes are backward compatible, but the older `spring.factories` style [will be removed in Spring Boot 3.0](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0.0-M5-Release-Notes#auto-configuration-registration). 
Also consider that Spring Boot 2.6.x goes out of support by 11-24-2022, switching this code to 2.7's new style.
  • Loading branch information
zhumin8 authored and diegomarquezp committed Oct 12, 2022
1 parent bb0de69 commit 7465171
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
15 changes: 8 additions & 7 deletions src/main/java/com/google/api/generator/spring/SpringWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static CodeGeneratorResponse write(
}

// write spring.factories file
writeSpringFactories(context, jos);
writeAutoConfigRegistration(context, jos);
writeSpringAdditionalMetadataJson(context, jos);

// TODO: metadata and package info not custimized for Spring
Expand Down Expand Up @@ -138,14 +138,15 @@ private static void writeMetadataFile(GapicContext context, String path, JarOutp
}
}

private static void writeSpringFactories(GapicContext context, JarOutputStream jos) {
String path = "src/main/resources/META-INF";
JarEntry jarEntry = new JarEntry(String.format("%s/spring.factories", path));
private static void writeAutoConfigRegistration(GapicContext context, JarOutputStream jos) {
String path = "src/main/resources/META-INF/spring";
JarEntry jarEntry =
new JarEntry(
String.format(
"%s/org.springframework.boot.autoconfigure.AutoConfiguration.imports", path));
try {
jos.putNextEntry(jarEntry);
StringJoiner sb =
new StringJoiner(
",\\\n", "org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\\n", "");
StringJoiner sb = new StringJoiner("\n", "", "");
context
.services()
.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private static MethodDefinition createConstructor(
private static List<AnnotationNode> createClassAnnotations(
Service service, Map<String, TypeNode> types, String libName) {
// @Generated("by gapic-generator-java")
// @Configuration(proxyBeanMethods = false)
// @AutoConfiguration
// @ConditionalOnClass(LanguageServiceClient.class)
// @ConditionalOnProperty(value = "spring.cloud.gcp.language.enabled", matchIfMissing = true)
// @EnableConfigurationProperties(LanguageProperties.class)
Expand All @@ -421,10 +421,7 @@ private static List<AnnotationNode> createClassAnnotations(
// XXX.class
.build();
AnnotationNode configurationNode =
AnnotationNode.builder()
.setType(types.get("Configuration"))
.setDescription("proxyBeanMethods = false") // TODO: change to parameters
.build();
AnnotationNode.builder().setType(types.get("AutoConfiguration")).build();
AnnotationNode enableConfigurationPropertiesNode =
AnnotationNode.builder()
.setType(types.get("EnableConfigurationProperties"))
Expand Down Expand Up @@ -1020,11 +1017,11 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
.setName("Bean")
.setPakkage("org.springframework.context.annotation")
.build());
TypeNode configuration =
TypeNode autoConfiguration =
TypeNode.withReference(
VaporReference.builder()
.setName("Configuration")
.setPakkage("org.springframework.context.annotation")
.setName("AutoConfiguration")
.setPakkage("org.springframework.boot.autoconfigure")
.build());
TypeNode enableConfigurationProperties =
TypeNode.withReference(
Expand Down Expand Up @@ -1062,7 +1059,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
typeMap.put("ServiceSettings", serviceSettings);
typeMap.put("ServiceSettingsBuilder", serviceSettingsBuilder);
typeMap.put("Bean", bean);
typeMap.put("Configuration", configuration);
typeMap.put("AutoConfiguration", autoConfiguration);
typeMap.put("EnableConfigurationProperties", enableConfigurationProperties);
typeMap.put("ConditionalOnMissingBean", conditionalOnMissingBean);
typeMap.put("ConditionalOnProperty", conditionalOnProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ public void generatePropertiesTest() {
+ "import com.google.showcase.v1beta1.EchoSettings;\n"
+ "import java.io.IOException;\n"
+ "import javax.annotation.Generated;\n"
+ "import org.springframework.boot.autoconfigure.AutoConfiguration;\n"
+ "import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;\n"
+ "import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;\n"
+ "import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;\n"
+ "import org.springframework.boot.context.properties.EnableConfigurationProperties;\n"
+ "import org.springframework.context.annotation.Bean;\n"
+ "import org.springframework.context.annotation.Configuration;\n"
+ "\n"
+ "@Generated(\"by gapic-generator-java\")\n"
+ "@Configuration(\"proxyBeanMethods = false\")\n"
+ "@AutoConfiguration\n"
+ "@ConditionalOnClass(\"value = EchoClient.class\")\n"
+ "@ConditionalOnProperty(\n"
+ " \"value = \\\"spring.cloud.gcp.autoconfig.showcase.echo.enabled\\\", matchIfMissing = false\")\n"
Expand Down

0 comments on commit 7465171

Please sign in to comment.