From ada6e9291f37e0af9f0eda27b1b020976417806f Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 3 Oct 2022 13:00:47 -0400 Subject: [PATCH] fix(Spring CodeGen): switch to Spring boot 2.7 style in registering auto-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. --- .../google/api/generator/spring/SpringWriter.java | 15 ++++++++------- .../composer/SpringAutoConfigClassComposer.java | 15 ++++++--------- .../spring/SpringAutoConfigClassComposerTest.java | 4 ++-- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/google/api/generator/spring/SpringWriter.java b/src/main/java/com/google/api/generator/spring/SpringWriter.java index d773eadff4..3df77aa1dd 100644 --- a/src/main/java/com/google/api/generator/spring/SpringWriter.java +++ b/src/main/java/com/google/api/generator/spring/SpringWriter.java @@ -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 @@ -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( diff --git a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java index fe33bbdfb9..aba9ec3c97 100644 --- a/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java +++ b/src/main/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposer.java @@ -396,7 +396,7 @@ private static MethodDefinition createConstructor( private static List createClassAnnotations( Service service, Map 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) @@ -421,10 +421,7 @@ private static List 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")) @@ -1020,11 +1017,11 @@ private static Map 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( @@ -1062,7 +1059,7 @@ private static Map 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); diff --git a/src/test/java/com/google/api/generator/spring/SpringAutoConfigClassComposerTest.java b/src/test/java/com/google/api/generator/spring/SpringAutoConfigClassComposerTest.java index 5bbcf5d805..8fef5ea0a5 100644 --- a/src/test/java/com/google/api/generator/spring/SpringAutoConfigClassComposerTest.java +++ b/src/test/java/com/google/api/generator/spring/SpringAutoConfigClassComposerTest.java @@ -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"