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(Spring CodeGen): switch to Spring boot 2.7 style in registering auto-configurations #1044

Merged
merged 3 commits into from
Oct 3, 2022
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
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