-
Notifications
You must be signed in to change notification settings - Fork 70
fix(spring): extend hasRestOption fix to properties composer and add golden tests #1348
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
Changes from all commits
fe887d1
689c0c4
90838fe
569efa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import com.google.protobuf.Descriptors.ServiceDescriptor; | ||
| import com.google.protobuf.StructProto; | ||
| import com.google.showcase.grpcrest.v1beta1.EchoGrpcrest; | ||
| import com.google.showcase.v1beta1.WickedOuterClass; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.HashSet; | ||
|
|
@@ -82,4 +83,35 @@ public GapicContext parseShowcaseEcho() { | |
| .setTransport(getTransport()) | ||
| .build(); | ||
| } | ||
|
|
||
| public GapicContext parseShowcaseWicked() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we going to need to add an entry in this class for every Showcase client that we generate?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do need an entry in the test loader for every new test proto as the unit tests need to access the protoc generated Java files. |
||
| FileDescriptor fileDescriptor = WickedOuterClass.getDescriptor(); | ||
| ServiceDescriptor messagingService = fileDescriptor.getServices().get(0); | ||
| assertEquals("Wicked", messagingService.getName()); | ||
|
|
||
| Map<String, Message> messageTypes = Parser.parseMessages(fileDescriptor); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic looks duplicated with the above method's. If this pattern will be repeated for further clients, consider refactoring this common logic out into something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! This might be refactoring to look into for the main branch's Some additional context on this PR: for now the spring branch has diverged from the main branch (after the monorepo migration), so this is just a copied over change from #1236 to re-use this latest addition in main. The next-phase plan would have spring-related test code such as in this PR's https://github.com/googleapis/gapic-generator-java/blob/90838fe105931feb0cca0433e478e81889da1d0e/src/test/java/com/google/api/generator/spring/composer/SpringAutoConfigClassComposerTest.java#L37 switched to call this method from the published gapic-generator-java jar, once the spring generator is migrated to depend on it. |
||
| messageTypes.putAll(Parser.parseMessages(OperationsProto.getDescriptor())); | ||
| messageTypes.putAll(Parser.parseMessages(StructProto.getDescriptor())); | ||
|
|
||
| Map<String, ResourceName> resourceNames = Parser.parseResourceNames(fileDescriptor); | ||
| Set<ResourceName> outputResourceNames = new HashSet<>(); | ||
| List<Service> services = | ||
| Parser.parseService( | ||
| fileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); | ||
|
|
||
| String jsonFilename = "showcase_grpc_service_config.json"; | ||
| Path jsonPath = Paths.get(getTestFilesDirectory(), jsonFilename); | ||
| Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString()); | ||
| assertTrue(configOpt.isPresent()); | ||
| GapicServiceConfig config = configOpt.get(); | ||
|
|
||
| return GapicContext.builder() | ||
| .setMessages(messageTypes) | ||
| .setResourceNames(resourceNames) | ||
| .setServices(services) | ||
| .setServiceConfig(config) | ||
| .setHelperResourceNames(outputResourceNames) | ||
| .setTransport(getTransport()) | ||
| .build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package com.google.showcase.v1beta1.spring; | ||
|
|
||
| import com.google.api.gax.core.CredentialsProvider; | ||
| 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; | ||
| import com.google.cloud.spring.core.util.RetryUtil; | ||
| import com.google.showcase.v1beta1.WickedClient; | ||
| import com.google.showcase.v1beta1.WickedSettings; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| 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; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| // AUTO-GENERATED DOCUMENTATION AND CLASS. | ||
| /** | ||
| * Auto-configuration for {@link WickedClient}. | ||
| * | ||
| * <p>Provides auto-configuration for Spring Boot | ||
| * | ||
| * <p>The default instance has everything set to sensible defaults: | ||
| * | ||
| * <ul> | ||
| * <li>The default transport provider is used. | ||
| * <li>Credentials are acquired automatically through Application Default Credentials. | ||
| * <li>Retries are configured for idempotent methods but not for non-idempotent methods. | ||
| * </ul> | ||
| */ | ||
| @AutoConfiguration | ||
| @AutoConfigureAfter(GcpContextAutoConfiguration.class) | ||
| @ConditionalOnClass(WickedClient.class) | ||
| @ConditionalOnProperty(value = "com.google.showcase.v1beta1.wicked.enabled", matchIfMissing = true) | ||
| @EnableConfigurationProperties(WickedSpringProperties.class) | ||
| public class WickedSpringAutoConfiguration { | ||
| private final WickedSpringProperties clientProperties; | ||
| private final CredentialsProvider credentialsProvider; | ||
| private static final Log LOGGER = LogFactory.getLog(WickedSpringAutoConfiguration.class); | ||
|
|
||
| protected WickedSpringAutoConfiguration( | ||
| WickedSpringProperties clientProperties, CredentialsProvider credentialsProvider) | ||
| throws IOException { | ||
| this.clientProperties = clientProperties; | ||
| if (this.clientProperties.getCredentials().hasKey()) { | ||
| if (LOGGER.isTraceEnabled()) { | ||
| LOGGER.trace("Using credentials from Wicked-specific configuration"); | ||
| } | ||
| this.credentialsProvider = | ||
| ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties)); | ||
| } else { | ||
| this.credentialsProvider = credentialsProvider; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Provides a default transport channel provider bean. The default is gRPC and will default to it | ||
| * unless the useRest option is supported and provided to use HTTP transport instead | ||
| * | ||
| * @return a default transport channel provider. | ||
| */ | ||
| @Bean | ||
| @ConditionalOnMissingBean(name = "defaultWickedTransportChannelProvider") | ||
| public TransportChannelProvider defaultWickedTransportChannelProvider() { | ||
| return WickedSettings.defaultTransportChannelProvider(); | ||
| } | ||
|
|
||
| /** | ||
| * Provides a WickedSettings bean configured to use a DefaultCredentialsProvider and the client | ||
| * library's default transport channel provider (defaultWickedTransportChannelProvider()). It also | ||
| * configures the quota project ID and executor thread count, if provided through properties. | ||
| * | ||
| * <p>Retry settings are also configured from service-level and method-level properties specified | ||
| * in WickedSpringProperties. Method-level properties will take precedence over service-level | ||
| * properties if available, and client library defaults will be used if neither are specified. | ||
| * | ||
| * @param defaultTransportChannelProvider TransportChannelProvider to use in the settings. | ||
| * @return a {@link WickedSettings} bean configured with {@link TransportChannelProvider} bean. | ||
| */ | ||
| @Bean | ||
| @ConditionalOnMissingBean | ||
| public WickedSettings wickedSettings( | ||
| @Qualifier("defaultWickedTransportChannelProvider") | ||
| TransportChannelProvider defaultTransportChannelProvider) | ||
| throws IOException { | ||
| WickedSettings.Builder clientSettingsBuilder = WickedSettings.newBuilder(); | ||
| clientSettingsBuilder | ||
| .setCredentialsProvider(this.credentialsProvider) | ||
| .setTransportChannelProvider(defaultTransportChannelProvider) | ||
| .setHeaderProvider(this.userAgentHeaderProvider()); | ||
| if (this.clientProperties.getQuotaProjectId() != null) { | ||
| clientSettingsBuilder.setQuotaProjectId(this.clientProperties.getQuotaProjectId()); | ||
| if (LOGGER.isTraceEnabled()) { | ||
| LOGGER.trace( | ||
| "Quota project id set to " | ||
| + this.clientProperties.getQuotaProjectId() | ||
| + ", this overrides project id from credentials."); | ||
| } | ||
| } | ||
| if (this.clientProperties.getExecutorThreadCount() != null) { | ||
| ExecutorProvider executorProvider = | ||
| WickedSettings.defaultExecutorProviderBuilder() | ||
| .setExecutorThreadCount(this.clientProperties.getExecutorThreadCount()) | ||
| .build(); | ||
| clientSettingsBuilder.setBackgroundExecutorProvider(executorProvider); | ||
| if (LOGGER.isTraceEnabled()) { | ||
| LOGGER.trace( | ||
| "Background executor thread count is " | ||
| + this.clientProperties.getExecutorThreadCount()); | ||
| } | ||
| } | ||
| Retry serviceRetry = clientProperties.getRetry(); | ||
| if (serviceRetry != null) { | ||
| RetrySettings craftEvilPlanRetrySettings = | ||
| RetryUtil.updateRetrySettings( | ||
| clientSettingsBuilder.craftEvilPlanSettings().getRetrySettings(), serviceRetry); | ||
| clientSettingsBuilder.craftEvilPlanSettings().setRetrySettings(craftEvilPlanRetrySettings); | ||
|
|
||
| if (LOGGER.isTraceEnabled()) { | ||
| LOGGER.trace("Configured service-level retry settings from properties."); | ||
| } | ||
| } | ||
| Retry craftEvilPlanRetry = clientProperties.getCraftEvilPlanRetry(); | ||
| if (craftEvilPlanRetry != null) { | ||
| RetrySettings craftEvilPlanRetrySettings = | ||
| RetryUtil.updateRetrySettings( | ||
| clientSettingsBuilder.craftEvilPlanSettings().getRetrySettings(), craftEvilPlanRetry); | ||
| clientSettingsBuilder.craftEvilPlanSettings().setRetrySettings(craftEvilPlanRetrySettings); | ||
| if (LOGGER.isTraceEnabled()) { | ||
| LOGGER.trace("Configured method-level retry settings for craftEvilPlan from properties."); | ||
| } | ||
| } | ||
| return clientSettingsBuilder.build(); | ||
| } | ||
|
|
||
| /** | ||
| * Provides a WickedClient bean configured with WickedSettings. | ||
| * | ||
| * @param wickedSettings settings to configure an instance of client bean. | ||
| * @return a {@link WickedClient} bean configured with {@link WickedSettings} | ||
| */ | ||
| @Bean | ||
| @ConditionalOnMissingBean | ||
| public WickedClient wickedClient(WickedSettings wickedSettings) throws IOException { | ||
| return WickedClient.create(wickedSettings); | ||
| } | ||
|
|
||
| private HeaderProvider userAgentHeaderProvider() { | ||
| String springLibrary = "spring-autogen-wicked"; | ||
| String version = this.getClass().getPackage().getImplementationVersion(); | ||
| return () -> Collections.singletonMap("user-agent", springLibrary + "/" + version); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this property used in this composer? Why we don't need this logic in #1343?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blakeli0 The changes from #1343 removed the generation of SpringAutoConfiguration code that condition on this property (
if (this.clientProperties.getUseRest())) to set rest-specific defaults. (e.g. this change to TetherSpringAutoconfiguration)The property itself is still there (just ignored by the autoconfig), and will be removed as part of this PR's changes. I realized when updating these tests that we shouldn't be generating the property either, and it was something I had overlooked in #1343's patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for the explanation! It was an unused property hence not causing any compilation issues.
I did a quick search for
restand see that it is mentioned in this comment , we may want to update it to something likeunless the useRest option is supported and provided, as not every client library support rest.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense - I've updated the comment composer correspondingly. Thanks for the suggestion!