From 57d7f59951a9f2fc2e30514331e04ba98622793a Mon Sep 17 00:00:00 2001 From: hdavidh Date: Thu, 15 Jun 2023 20:26:34 -0700 Subject: [PATCH 1/6] Move QueryParametersToBodyInterceptor to front of interceptor chain --- .../poet/builder/AsyncClientBuilderClass.java | 60 ++++++++++++------- .../poet/builder/BaseClientBuilderClass.java | 12 ---- .../poet/builder/SyncClientBuilderClass.java | 60 ++++++++++++------- .../test-query-client-builder-class.java | 4 -- 4 files changed, 80 insertions(+), 56 deletions(-) diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java index f8731d7dad07..ce5cc8eac412 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java @@ -18,8 +18,10 @@ import com.squareup.javapoet.ClassName; import com.squareup.javapoet.MethodSpec; import com.squareup.javapoet.ParameterizedTypeName; +import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import java.net.URI; +import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.auth.token.credentials.SdkTokenProvider; @@ -32,6 +34,8 @@ import software.amazon.awssdk.codegen.utils.AuthUtils; import software.amazon.awssdk.core.client.config.SdkClientConfiguration; import software.amazon.awssdk.core.client.config.SdkClientOption; +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; +import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; public class AsyncClientBuilderClass implements ClassSpec { private final IntermediateModel model; @@ -119,26 +123,42 @@ private MethodSpec endpointProviderMethod() { } private MethodSpec buildClientMethod() { - return MethodSpec.methodBuilder("buildClient") - .addAnnotation(Override.class) - .addModifiers(Modifier.PROTECTED, Modifier.FINAL) - .returns(clientInterfaceName) - .addStatement("$T clientConfiguration = super.asyncClientConfiguration()", SdkClientConfiguration.class) - .addStatement("this.validateClientOptions(clientConfiguration)") - .addStatement("$T endpointOverride = null", URI.class) - .addCode("if (clientConfiguration.option($T.ENDPOINT_OVERRIDDEN) != null" - + "&& $T.TRUE.equals(clientConfiguration.option($T.ENDPOINT_OVERRIDDEN))) {" - + "endpointOverride = clientConfiguration.option($T.ENDPOINT);" - + "}", - SdkClientOption.class, Boolean.class, SdkClientOption.class, SdkClientOption.class) - .addStatement("$T serviceClientConfiguration = $T.builder()" - + ".overrideConfiguration(overrideConfiguration())" - + ".region(clientConfiguration.option($T.AWS_REGION))" - + ".endpointOverride(endpointOverride)" - + ".build()", - serviceConfigClassName, serviceConfigClassName, AwsClientOption.class) - .addStatement("return new $T(serviceClientConfiguration, clientConfiguration)", clientClassName) - .build(); + MethodSpec.Builder b = MethodSpec.methodBuilder("buildClient") + .addAnnotation(Override.class) + .addModifiers(Modifier.PROTECTED, Modifier.FINAL) + .returns(clientInterfaceName) + .addStatement("$T clientConfiguration = super.asyncClientConfiguration()", + SdkClientConfiguration.class); + + addQueryParamsToBodyInterceptorIfQueryProtocol(b); + + return b.addStatement("this.validateClientOptions(clientConfiguration)") + .addStatement("$T endpointOverride = null", URI.class) + .addCode("if (clientConfiguration.option($T.ENDPOINT_OVERRIDDEN) != null" + + "&& $T.TRUE.equals(clientConfiguration.option($T.ENDPOINT_OVERRIDDEN))) {" + + "endpointOverride = clientConfiguration.option($T.ENDPOINT);" + + "}", + SdkClientOption.class, Boolean.class, SdkClientOption.class, SdkClientOption.class) + .addStatement("$T serviceClientConfiguration = $T.builder()" + + ".overrideConfiguration(overrideConfiguration())" + + ".region(clientConfiguration.option($T.AWS_REGION))" + + ".endpointOverride(endpointOverride)" + + ".build()", + serviceConfigClassName, serviceConfigClassName, AwsClientOption.class) + .addStatement("return new $T(serviceClientConfiguration, clientConfiguration)", clientClassName) + .build(); + } + + private MethodSpec.Builder addQueryParamsToBodyInterceptorIfQueryProtocol(MethodSpec.Builder b) { + if (model.getMetadata().isQueryProtocol()) { + TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); + b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", + listType, SdkClientOption.class) + .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class) + .addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + + "interceptors).build()", SdkClientOption.class); + } + return b; } private MethodSpec bearerTokenProviderMethod() { diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java index 1be4d730040e..e4763b68e736 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java @@ -28,7 +28,6 @@ import com.squareup.javapoet.TypeSpec; import com.squareup.javapoet.TypeVariableName; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -59,7 +58,6 @@ import software.amazon.awssdk.core.signer.Signer; import software.amazon.awssdk.http.Protocol; import software.amazon.awssdk.http.SdkHttpConfigurationOption; -import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; import software.amazon.awssdk.utils.AttributeMap; import software.amazon.awssdk.utils.CollectionUtils; import software.amazon.awssdk.utils.StringUtils; @@ -288,16 +286,6 @@ private MethodSpec finalizeServiceConfigurationMethod() { builder.addCode("interceptors = $T.mergeLists(interceptors, config.option($T.EXECUTION_INTERCEPTORS));\n", CollectionUtils.class, SdkClientOption.class); - if (model.getMetadata().isQueryProtocol()) { - TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); - builder.addStatement("$T protocolInterceptors = $T.singletonList(new $T())", - listType, - Collections.class, - QueryParametersToBodyInterceptor.class); - builder.addStatement("interceptors = $T.mergeLists(interceptors, protocolInterceptors)", - CollectionUtils.class); - } - if (model.getEndpointOperation().isPresent()) { builder.beginControlFlow("if (!endpointDiscoveryEnabled)") .addStatement("$1T chain = new $1T(config)", DefaultEndpointDiscoveryProviderChain.class) diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java index 036589de04e8..36279aa3a23f 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java @@ -18,8 +18,10 @@ import com.squareup.javapoet.ClassName; import com.squareup.javapoet.MethodSpec; import com.squareup.javapoet.ParameterizedTypeName; +import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import java.net.URI; +import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.auth.token.credentials.SdkTokenProvider; @@ -32,6 +34,8 @@ import software.amazon.awssdk.codegen.utils.AuthUtils; import software.amazon.awssdk.core.client.config.SdkClientConfiguration; import software.amazon.awssdk.core.client.config.SdkClientOption; +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; +import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; public class SyncClientBuilderClass implements ClassSpec { private final IntermediateModel model; @@ -119,26 +123,42 @@ private MethodSpec endpointProviderMethod() { private MethodSpec buildClientMethod() { - return MethodSpec.methodBuilder("buildClient") - .addAnnotation(Override.class) - .addModifiers(Modifier.PROTECTED, Modifier.FINAL) - .returns(clientInterfaceName) - .addStatement("$T clientConfiguration = super.syncClientConfiguration()", SdkClientConfiguration.class) - .addStatement("this.validateClientOptions(clientConfiguration)") - .addStatement("$T endpointOverride = null", URI.class) - .addCode("if (clientConfiguration.option($T.ENDPOINT_OVERRIDDEN) != null" - + "&& $T.TRUE.equals(clientConfiguration.option($T.ENDPOINT_OVERRIDDEN))) {" - + "endpointOverride = clientConfiguration.option($T.ENDPOINT);" - + "}", - SdkClientOption.class, Boolean.class, SdkClientOption.class, SdkClientOption.class) - .addStatement("$T serviceClientConfiguration = $T.builder()" - + ".overrideConfiguration(overrideConfiguration())" - + ".region(clientConfiguration.option($T.AWS_REGION))" - + ".endpointOverride(endpointOverride)" - + ".build()", - serviceConfigClassName, serviceConfigClassName, AwsClientOption.class) - .addStatement("return new $T(serviceClientConfiguration, clientConfiguration)", clientClassName) - .build(); + MethodSpec.Builder b = MethodSpec.methodBuilder("buildClient") + .addAnnotation(Override.class) + .addModifiers(Modifier.PROTECTED, Modifier.FINAL) + .returns(clientInterfaceName) + .addStatement("$T clientConfiguration = super.syncClientConfiguration()", + SdkClientConfiguration.class); + + addQueryParamsToBodyInterceptorIfQueryProtocol(b); + + return b.addStatement("this.validateClientOptions(clientConfiguration)") + .addStatement("$T endpointOverride = null", URI.class) + .addCode("if (clientConfiguration.option($T.ENDPOINT_OVERRIDDEN) != null" + + "&& $T.TRUE.equals(clientConfiguration.option($T.ENDPOINT_OVERRIDDEN))) {" + + "endpointOverride = clientConfiguration.option($T.ENDPOINT);" + + "}", + SdkClientOption.class, Boolean.class, SdkClientOption.class, SdkClientOption.class) + .addStatement("$T serviceClientConfiguration = $T.builder()" + + ".overrideConfiguration(overrideConfiguration())" + + ".region(clientConfiguration.option($T.AWS_REGION))" + + ".endpointOverride(endpointOverride)" + + ".build()", + serviceConfigClassName, serviceConfigClassName, AwsClientOption.class) + .addStatement("return new $T(serviceClientConfiguration, clientConfiguration)", clientClassName) + .build(); + } + + private MethodSpec.Builder addQueryParamsToBodyInterceptorIfQueryProtocol(MethodSpec.Builder b) { + if (model.getMetadata().isQueryProtocol()) { + TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); + b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", + listType, SdkClientOption.class) + .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class) + .addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + + "interceptors).build()", SdkClientOption.class); + } + return b; } private MethodSpec tokenProviderMethodImpl() { diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-client-builder-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-client-builder-class.java index 141b27f6cfe0..e1b5cf7bf055 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-client-builder-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-client-builder-class.java @@ -1,7 +1,6 @@ package software.amazon.awssdk.services.query; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -17,7 +16,6 @@ import software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory; import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; import software.amazon.awssdk.core.signer.Signer; -import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; import software.amazon.awssdk.services.query.endpoints.QueryClientContextParams; import software.amazon.awssdk.services.query.endpoints.QueryEndpointProvider; import software.amazon.awssdk.services.query.endpoints.internal.QueryEndpointAuthSchemeInterceptor; @@ -64,8 +62,6 @@ protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientCon interceptors = CollectionUtils.mergeLists(endpointInterceptors, interceptors); interceptors = CollectionUtils.mergeLists(interceptors, additionalInterceptors); interceptors = CollectionUtils.mergeLists(interceptors, config.option(SdkClientOption.EXECUTION_INTERCEPTORS)); - List protocolInterceptors = Collections.singletonList(new QueryParametersToBodyInterceptor()); - interceptors = CollectionUtils.mergeLists(interceptors, protocolInterceptors); return config.toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors) .option(SdkClientOption.CLIENT_CONTEXT_PARAMS, clientContextParams.build()).build(); } From f31c32ed2c22fe404fd44d6966a3de68d956b0bb Mon Sep 17 00:00:00 2001 From: hdavidh Date: Fri, 16 Jun 2023 16:56:56 -0700 Subject: [PATCH 2/6] Move customization.config interceptors to front of interceptor chain - for query protocols --- .../poet/builder/AsyncClientBuilderClass.java | 30 ++++-- .../poet/builder/BaseClientBuilderClass.java | 6 +- .../poet/builder/SyncClientBuilderClass.java | 30 ++++-- .../MoveQueryParamsToBodyTest.java | 102 ------------------ 4 files changed, 44 insertions(+), 124 deletions(-) delete mode 100644 test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/protocolquery/MoveQueryParamsToBodyTest.java diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java index ce5cc8eac412..6589116727cf 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java @@ -21,6 +21,7 @@ import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import java.net.URI; +import java.util.Collections; import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -130,7 +131,7 @@ private MethodSpec buildClientMethod() { .addStatement("$T clientConfiguration = super.asyncClientConfiguration()", SdkClientConfiguration.class); - addQueryParamsToBodyInterceptorIfQueryProtocol(b); + addQueryProtocolInterceptors(b); return b.addStatement("this.validateClientOptions(clientConfiguration)") .addStatement("$T endpointOverride = null", URI.class) @@ -149,16 +150,25 @@ private MethodSpec buildClientMethod() { .build(); } - private MethodSpec.Builder addQueryParamsToBodyInterceptorIfQueryProtocol(MethodSpec.Builder b) { - if (model.getMetadata().isQueryProtocol()) { - TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); - b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", - listType, SdkClientOption.class) - .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class) - .addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " - + "interceptors).build()", SdkClientOption.class); + private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { + if (!model.getMetadata().isQueryProtocol()) { + return b; + } + + // queryParamsToBody interceptor + TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); + b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) + .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class); + + // customization.config interceptors + List customInterceptors = model.getCustomizationConfig().getInterceptors(); + Collections.reverse(customInterceptors); + for (String customInterceptor : customInterceptors) { + b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(customInterceptor)); } - return b; + + return b.addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + + "interceptors).build()", SdkClientOption.class); } private MethodSpec bearerTokenProviderMethod() { diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java index e4763b68e736..72d534d5ab99 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java @@ -260,8 +260,10 @@ private MethodSpec finalizeServiceConfigurationMethod() { builtInInterceptors.add(endpointRulesSpecUtils.authSchemesInterceptorName()); builtInInterceptors.add(endpointRulesSpecUtils.requestModifierInterceptorName()); - for (String interceptor : model.getCustomizationConfig().getInterceptors()) { - builtInInterceptors.add(ClassName.bestGuess(interceptor)); + if (!model.getMetadata().isQueryProtocol()) { + for (String interceptor : model.getCustomizationConfig().getInterceptors()) { + builtInInterceptors.add(ClassName.bestGuess(interceptor)); + } } for (ClassName interceptor : builtInInterceptors) { diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java index 36279aa3a23f..0328b2446909 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java @@ -21,6 +21,7 @@ import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import java.net.URI; +import java.util.Collections; import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -130,7 +131,7 @@ private MethodSpec buildClientMethod() { .addStatement("$T clientConfiguration = super.syncClientConfiguration()", SdkClientConfiguration.class); - addQueryParamsToBodyInterceptorIfQueryProtocol(b); + addQueryProtocolInterceptors(b); return b.addStatement("this.validateClientOptions(clientConfiguration)") .addStatement("$T endpointOverride = null", URI.class) @@ -149,16 +150,25 @@ private MethodSpec buildClientMethod() { .build(); } - private MethodSpec.Builder addQueryParamsToBodyInterceptorIfQueryProtocol(MethodSpec.Builder b) { - if (model.getMetadata().isQueryProtocol()) { - TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); - b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", - listType, SdkClientOption.class) - .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class) - .addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " - + "interceptors).build()", SdkClientOption.class); + private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { + if (!model.getMetadata().isQueryProtocol()) { + return b; } - return b; + + // queryParamsToBody interceptor + TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); + b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) + .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class); + + // customization.config interceptors + List customInterceptors = model.getCustomizationConfig().getInterceptors(); + Collections.reverse(customInterceptors); + for (String customInterceptor : customInterceptors) { + b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(customInterceptor)); + } + + return b.addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + + "interceptors).build()", SdkClientOption.class); } private MethodSpec tokenProviderMethodImpl() { diff --git a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/protocolquery/MoveQueryParamsToBodyTest.java b/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/protocolquery/MoveQueryParamsToBodyTest.java deleted file mode 100644 index 7f2b32fa668c..000000000000 --- a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/protocolquery/MoveQueryParamsToBodyTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - -package software.amazon.awssdk.services.protocolquery; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.util.Optional; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; -import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; -import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; -import software.amazon.awssdk.core.exception.SdkClientException; -import software.amazon.awssdk.core.interceptor.Context; -import software.amazon.awssdk.core.interceptor.ExecutionAttributes; -import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; -import software.amazon.awssdk.http.ContentStreamProvider; -import software.amazon.awssdk.http.ExecutableHttpRequest; -import software.amazon.awssdk.http.HttpExecuteRequest; -import software.amazon.awssdk.http.SdkHttpClient; -import software.amazon.awssdk.http.SdkHttpRequest; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.utils.IoUtils; - -public class MoveQueryParamsToBodyTest { - private static final AwsCredentialsProvider CREDENTIALS = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); - - private SdkHttpClient mockHttpClient; - - private ProtocolQueryClient client; - - @BeforeEach - public void setup() throws IOException { - mockHttpClient = mock(SdkHttpClient.class); - ExecutableHttpRequest mockRequest = mock(ExecutableHttpRequest.class); - when(mockRequest.call()).thenThrow(new IOException("IO error!")); - when(mockHttpClient.prepareRequest(any())).thenReturn(mockRequest); - } - - @AfterEach - public void teardown() { - if (client != null) { - client.close(); - } - client = null; - } - - @Test - public void customInterceptor_additionalQueryParamsAdded_paramsAlsoMovedToBody() throws IOException { - client = ProtocolQueryClient.builder() - .overrideConfiguration(o -> o.addExecutionInterceptor(new AdditionalQueryParamInterceptor())) - .region(Region.US_WEST_2) - .credentialsProvider(CREDENTIALS) - .httpClient(mockHttpClient) - .build(); - - ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class); - - assertThatThrownBy(() -> client.membersInQueryParams(r -> r.stringQueryParam("hello"))) - .isInstanceOf(SdkClientException.class) - .hasMessageContaining("IO"); - - verify(mockHttpClient, atLeast(1)).prepareRequest(requestCaptor.capture()); - - ContentStreamProvider requestContent = requestCaptor.getValue().contentStreamProvider().get(); - - String contentString = IoUtils.toUtf8String(requestContent.newStream()); - - assertThat(contentString).contains("CustomParamName=CustomParamValue"); - } - - private static class AdditionalQueryParamInterceptor implements ExecutionInterceptor { - @Override - public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { - return context.httpRequest().toBuilder() - .putRawQueryParameter("CustomParamName", "CustomParamValue") - .build(); - } - } -} From 5e5a4580aa06bcf69e246d843d944e87990cbb64 Mon Sep 17 00:00:00 2001 From: hdavidh Date: Mon, 19 Jun 2023 11:54:01 -0700 Subject: [PATCH 3/6] Refactoring --- .../codegen/poet/builder/AsyncClientBuilderClass.java | 7 +------ .../codegen/poet/builder/SyncClientBuilderClass.java | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java index 6589116727cf..86f0ca942313 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java @@ -155,18 +155,13 @@ private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { return b; } - // queryParamsToBody interceptor TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class); - // customization.config interceptors List customInterceptors = model.getCustomizationConfig().getInterceptors(); Collections.reverse(customInterceptors); - for (String customInterceptor : customInterceptors) { - b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(customInterceptor)); - } - + customInterceptors.forEach(i -> b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(i))); return b.addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + "interceptors).build()", SdkClientOption.class); } diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java index 0328b2446909..dc893244cb8e 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java @@ -155,18 +155,13 @@ private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { return b; } - // queryParamsToBody interceptor TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class); - // customization.config interceptors List customInterceptors = model.getCustomizationConfig().getInterceptors(); Collections.reverse(customInterceptors); - for (String customInterceptor : customInterceptors) { - b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(customInterceptor)); - } - + customInterceptors.forEach(i -> b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(i))); return b.addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + "interceptors).build()", SdkClientOption.class); } From 67f47d5be431228ef6036fa014020f7e469dda6c Mon Sep 17 00:00:00 2001 From: hdavidh Date: Mon, 19 Jun 2023 14:20:09 -0700 Subject: [PATCH 4/6] Add codegen tests --- .../QueryProtocolCustomTestInterceptor.java | 12 +++++ .../poet/builder/BuilderClassTest.java | 10 ++++ ...test-query-async-client-builder-class.java | 54 +++++++++++++++++++ .../test-query-sync-client-builder-class.java | 54 +++++++++++++++++++ .../client/c2j/query/customization.config | 7 ++- 5 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 codegen/src/test/java/software/amazon/awssdk/codegen/internal/QueryProtocolCustomTestInterceptor.java create mode 100644 codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java create mode 100644 codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java diff --git a/codegen/src/test/java/software/amazon/awssdk/codegen/internal/QueryProtocolCustomTestInterceptor.java b/codegen/src/test/java/software/amazon/awssdk/codegen/internal/QueryProtocolCustomTestInterceptor.java new file mode 100644 index 000000000000..6bd5206d9b11 --- /dev/null +++ b/codegen/src/test/java/software/amazon/awssdk/codegen/internal/QueryProtocolCustomTestInterceptor.java @@ -0,0 +1,12 @@ +package software.amazon.awssdk.codegen.internal; + +import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.codegen.poet.builder.BuilderClassTest; + +/** + * Empty no-op test interceptor for query protocols to view generated code in test-query-sync-client-builder-class.java and + * test-query-async-client-builder-class.java and validate in {@link BuilderClassTest}. + */ +@SdkInternalApi +public class QueryProtocolCustomTestInterceptor { +} diff --git a/codegen/src/test/java/software/amazon/awssdk/codegen/poet/builder/BuilderClassTest.java b/codegen/src/test/java/software/amazon/awssdk/codegen/poet/builder/BuilderClassTest.java index b111e47bf3c0..3edafd55dab3 100644 --- a/codegen/src/test/java/software/amazon/awssdk/codegen/poet/builder/BuilderClassTest.java +++ b/codegen/src/test/java/software/amazon/awssdk/codegen/poet/builder/BuilderClassTest.java @@ -58,6 +58,16 @@ public void baseQueryClientBuilderClass() throws Exception { validateQueryGeneration(BaseClientBuilderClass::new, "test-query-client-builder-class.java"); } + @Test + public void syncQueryClientBuilderClass() throws Exception { + validateQueryGeneration(SyncClientBuilderClass::new, "test-query-sync-client-builder-class.java"); + } + + @Test + public void asyncQueryClientBuilderClass() throws Exception { + validateQueryGeneration(AsyncClientBuilderClass::new, "test-query-async-client-builder-class.java"); + } + @Test public void syncClientBuilderInterface() throws Exception { validateGeneration(SyncClientBuilderInterface::new, "test-sync-client-builder-interface.java"); diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java new file mode 100644 index 000000000000..13e3838588fb --- /dev/null +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java @@ -0,0 +1,54 @@ +package software.amazon.awssdk.services.query; + +import java.net.URI; +import java.util.List; +import software.amazon.awssdk.annotations.Generated; +import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.auth.token.credentials.SdkTokenProvider; +import software.amazon.awssdk.awscore.client.config.AwsClientOption; +import software.amazon.awssdk.codegen.internal.QueryProtocolCustomTestInterceptor; +import software.amazon.awssdk.core.client.config.SdkClientConfiguration; +import software.amazon.awssdk.core.client.config.SdkClientOption; +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; +import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; +import software.amazon.awssdk.services.query.endpoints.QueryEndpointProvider; + +/** + * Internal implementation of {@link QueryAsyncClientBuilder}. + */ +@Generated("software.amazon.awssdk:codegen") +@SdkInternalApi +final class DefaultQueryAsyncClientBuilder extends DefaultQueryBaseClientBuilder + implements QueryAsyncClientBuilder { + @Override + public DefaultQueryAsyncClientBuilder endpointProvider(QueryEndpointProvider endpointProvider) { + clientConfiguration.option(SdkClientOption.ENDPOINT_PROVIDER, endpointProvider); + return this; + } + + @Override + public DefaultQueryAsyncClientBuilder tokenProvider(SdkTokenProvider tokenProvider) { + clientConfiguration.option(AwsClientOption.TOKEN_PROVIDER, tokenProvider); + return this; + } + + @Override + protected final QueryAsyncClient buildClient() { + SdkClientConfiguration clientConfiguration = super.asyncClientConfiguration(); + List interceptors = clientConfiguration.option(SdkClientOption.EXECUTION_INTERCEPTORS); + interceptors.add(0, new QueryParametersToBodyInterceptor()); + interceptors.add(0, new QueryProtocolCustomTestInterceptor()); + clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors) + .build(); + this.validateClientOptions(clientConfiguration); + URI endpointOverride = null; + if (clientConfiguration.option(SdkClientOption.ENDPOINT_OVERRIDDEN) != null + && Boolean.TRUE.equals(clientConfiguration.option(SdkClientOption.ENDPOINT_OVERRIDDEN))) { + endpointOverride = clientConfiguration.option(SdkClientOption.ENDPOINT); + } + QueryServiceClientConfiguration serviceClientConfiguration = QueryServiceClientConfiguration.builder() + .overrideConfiguration(overrideConfiguration()).region(clientConfiguration.option(AwsClientOption.AWS_REGION)) + .endpointOverride(endpointOverride).build(); + return new DefaultQueryAsyncClient(serviceClientConfiguration, clientConfiguration); + } +} diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java new file mode 100644 index 000000000000..ce215676634d --- /dev/null +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java @@ -0,0 +1,54 @@ +package software.amazon.awssdk.services.query; + +import java.net.URI; +import java.util.List; +import software.amazon.awssdk.annotations.Generated; +import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.auth.token.credentials.SdkTokenProvider; +import software.amazon.awssdk.awscore.client.config.AwsClientOption; +import software.amazon.awssdk.codegen.internal.QueryProtocolCustomTestInterceptor; +import software.amazon.awssdk.core.client.config.SdkClientConfiguration; +import software.amazon.awssdk.core.client.config.SdkClientOption; +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; +import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; +import software.amazon.awssdk.services.query.endpoints.QueryEndpointProvider; + +/** + * Internal implementation of {@link QueryClientBuilder}. + */ +@Generated("software.amazon.awssdk:codegen") +@SdkInternalApi +final class DefaultQueryClientBuilder extends DefaultQueryBaseClientBuilder implements + QueryClientBuilder { + @Override + public DefaultQueryClientBuilder endpointProvider(QueryEndpointProvider endpointProvider) { + clientConfiguration.option(SdkClientOption.ENDPOINT_PROVIDER, endpointProvider); + return this; + } + + @Override + public DefaultQueryClientBuilder tokenProvider(SdkTokenProvider tokenProvider) { + clientConfiguration.option(AwsClientOption.TOKEN_PROVIDER, tokenProvider); + return this; + } + + @Override + protected final QueryClient buildClient() { + SdkClientConfiguration clientConfiguration = super.syncClientConfiguration(); + List interceptors = clientConfiguration.option(SdkClientOption.EXECUTION_INTERCEPTORS); + interceptors.add(0, new QueryParametersToBodyInterceptor()); + interceptors.add(0, new QueryProtocolCustomTestInterceptor()); + clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors) + .build(); + this.validateClientOptions(clientConfiguration); + URI endpointOverride = null; + if (clientConfiguration.option(SdkClientOption.ENDPOINT_OVERRIDDEN) != null + && Boolean.TRUE.equals(clientConfiguration.option(SdkClientOption.ENDPOINT_OVERRIDDEN))) { + endpointOverride = clientConfiguration.option(SdkClientOption.ENDPOINT); + } + QueryServiceClientConfiguration serviceClientConfiguration = QueryServiceClientConfiguration.builder() + .overrideConfiguration(overrideConfiguration()).region(clientConfiguration.option(AwsClientOption.AWS_REGION)) + .endpointOverride(endpointOverride).build(); + return new DefaultQueryClient(serviceClientConfiguration, clientConfiguration); + } +} diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/query/customization.config b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/query/customization.config index c95b6d2e5f63..18824fa00a30 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/query/customization.config +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/query/customization.config @@ -2,7 +2,10 @@ "authPolicyActions" : { "skip" : true }, - "skipEndpointTests": { + "skipEndpointTests": { "test case 4": "Does not work" - } + }, + "interceptors": [ + "software.amazon.awssdk.codegen.internal.QueryProtocolCustomTestInterceptor" + ] } \ No newline at end of file From 8f7f99de470f8d85eb7e7a53c8a11b1aa448eef2 Mon Sep 17 00:00:00 2001 From: hdavidh Date: Mon, 19 Jun 2023 16:13:36 -0700 Subject: [PATCH 5/6] Refactoring --- .../poet/builder/AsyncClientBuilderClass.java | 19 ++++++++++++++----- .../poet/builder/SyncClientBuilderClass.java | 19 ++++++++++++++----- ...test-query-async-client-builder-class.java | 11 +++++++++-- .../test-query-sync-client-builder-class.java | 11 +++++++++-- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java index 86f0ca942313..8d360b00873c 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java @@ -21,7 +21,8 @@ import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import java.net.URI; -import java.util.Collections; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -37,6 +38,7 @@ import software.amazon.awssdk.core.client.config.SdkClientOption; import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; +import software.amazon.awssdk.utils.CollectionUtils; public class AsyncClientBuilderClass implements ClassSpec { private final IntermediateModel model; @@ -156,12 +158,19 @@ private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { } TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); - b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) - .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class); + + b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", + listType, SdkClientOption.class) + .addStatement("$T queryParamsToBodyInterceptor = new $T<>($T.asList(new $T()))", + listType, ArrayList.class, Arrays.class, QueryParametersToBodyInterceptor.class) + .addStatement("$T customizationInterceptors = new $T<>()", listType, ArrayList.class); List customInterceptors = model.getCustomizationConfig().getInterceptors(); - Collections.reverse(customInterceptors); - customInterceptors.forEach(i -> b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(i))); + customInterceptors.forEach(i -> b.addStatement("customizationInterceptors.add(new $T())", ClassName.bestGuess(i))); + + b.addStatement("interceptors = $T.mergeLists(queryParamsToBodyInterceptor, interceptors)", CollectionUtils.class) + .addStatement("interceptors = $T.mergeLists(customizationInterceptors, interceptors)", CollectionUtils.class); + return b.addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + "interceptors).build()", SdkClientOption.class); } diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java index dc893244cb8e..a96a44d70f58 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java @@ -21,7 +21,8 @@ import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import java.net.URI; -import java.util.Collections; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -37,6 +38,7 @@ import software.amazon.awssdk.core.client.config.SdkClientOption; import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; +import software.amazon.awssdk.utils.CollectionUtils; public class SyncClientBuilderClass implements ClassSpec { private final IntermediateModel model; @@ -156,12 +158,19 @@ private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { } TypeName listType = ParameterizedTypeName.get(List.class, ExecutionInterceptor.class); - b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) - .addStatement("interceptors.add(0, new $T())", QueryParametersToBodyInterceptor.class); + + b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", + listType, SdkClientOption.class) + .addStatement("$T queryParamsToBodyInterceptor = new $T<>($T.asList(new $T()))", + listType, ArrayList.class, Arrays.class, QueryParametersToBodyInterceptor.class) + .addStatement("$T customizationInterceptors = new $T<>()", listType, ArrayList.class); List customInterceptors = model.getCustomizationConfig().getInterceptors(); - Collections.reverse(customInterceptors); - customInterceptors.forEach(i -> b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(i))); + customInterceptors.forEach(i -> b.addStatement("customizationInterceptors.add(new $T())", ClassName.bestGuess(i))); + + b.addStatement("interceptors = $T.mergeLists(queryParamsToBodyInterceptor, interceptors)", CollectionUtils.class) + .addStatement("interceptors = $T.mergeLists(customizationInterceptors, interceptors)", CollectionUtils.class); + return b.addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " + "interceptors).build()", SdkClientOption.class); } diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java index 13e3838588fb..b9096e19c394 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java @@ -1,6 +1,8 @@ package software.amazon.awssdk.services.query; import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -12,6 +14,7 @@ import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; import software.amazon.awssdk.services.query.endpoints.QueryEndpointProvider; +import software.amazon.awssdk.utils.CollectionUtils; /** * Internal implementation of {@link QueryAsyncClientBuilder}. @@ -36,8 +39,12 @@ public DefaultQueryAsyncClientBuilder tokenProvider(SdkTokenProvider tokenProvid protected final QueryAsyncClient buildClient() { SdkClientConfiguration clientConfiguration = super.asyncClientConfiguration(); List interceptors = clientConfiguration.option(SdkClientOption.EXECUTION_INTERCEPTORS); - interceptors.add(0, new QueryParametersToBodyInterceptor()); - interceptors.add(0, new QueryProtocolCustomTestInterceptor()); + List queryParamsToBodyInterceptor = new ArrayList<>( + Arrays.asList(new QueryParametersToBodyInterceptor())); + List customizationInterceptors = new ArrayList<>(); + customizationInterceptors.add(new QueryProtocolCustomTestInterceptor()); + interceptors = CollectionUtils.mergeLists(queryParamsToBodyInterceptor, interceptors); + interceptors = CollectionUtils.mergeLists(customizationInterceptors, interceptors); clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors) .build(); this.validateClientOptions(clientConfiguration); diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java index ce215676634d..31ab60055e1c 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java @@ -1,6 +1,8 @@ package software.amazon.awssdk.services.query; import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -12,6 +14,7 @@ import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor; import software.amazon.awssdk.services.query.endpoints.QueryEndpointProvider; +import software.amazon.awssdk.utils.CollectionUtils; /** * Internal implementation of {@link QueryClientBuilder}. @@ -36,8 +39,12 @@ public DefaultQueryClientBuilder tokenProvider(SdkTokenProvider tokenProvider) { protected final QueryClient buildClient() { SdkClientConfiguration clientConfiguration = super.syncClientConfiguration(); List interceptors = clientConfiguration.option(SdkClientOption.EXECUTION_INTERCEPTORS); - interceptors.add(0, new QueryParametersToBodyInterceptor()); - interceptors.add(0, new QueryProtocolCustomTestInterceptor()); + List queryParamsToBodyInterceptor = new ArrayList<>( + Arrays.asList(new QueryParametersToBodyInterceptor())); + List customizationInterceptors = new ArrayList<>(); + customizationInterceptors.add(new QueryProtocolCustomTestInterceptor()); + interceptors = CollectionUtils.mergeLists(queryParamsToBodyInterceptor, interceptors); + interceptors = CollectionUtils.mergeLists(customizationInterceptors, interceptors); clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors) .build(); this.validateClientOptions(clientConfiguration); From a37f419e295310473ea4265fc96d0561cfcddcb7 Mon Sep 17 00:00:00 2001 From: hdavidh Date: Tue, 20 Jun 2023 12:46:49 -0700 Subject: [PATCH 6/6] Refactoring --- .../codegen/poet/builder/AsyncClientBuilderClass.java | 6 +++--- .../awssdk/codegen/poet/builder/SyncClientBuilderClass.java | 6 +++--- .../poet/builder/test-query-async-client-builder-class.java | 6 +++--- .../poet/builder/test-query-sync-client-builder-class.java | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java index 8d360b00873c..bd0e6023d53e 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/AsyncClientBuilderClass.java @@ -22,7 +22,7 @@ import com.squareup.javapoet.TypeSpec; import java.net.URI; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -161,8 +161,8 @@ private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) - .addStatement("$T queryParamsToBodyInterceptor = new $T<>($T.asList(new $T()))", - listType, ArrayList.class, Arrays.class, QueryParametersToBodyInterceptor.class) + .addStatement("$T queryParamsToBodyInterceptor = $T.singletonList(new $T())", + listType, Collections.class, QueryParametersToBodyInterceptor.class) .addStatement("$T customizationInterceptors = new $T<>()", listType, ArrayList.class); List customInterceptors = model.getCustomizationConfig().getInterceptors(); diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java index a96a44d70f58..8b330e76ce1b 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/SyncClientBuilderClass.java @@ -22,7 +22,7 @@ import com.squareup.javapoet.TypeSpec; import java.net.URI; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import javax.lang.model.element.Modifier; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -161,8 +161,8 @@ private MethodSpec.Builder addQueryProtocolInterceptors(MethodSpec.Builder b) { b.addStatement("$T interceptors = clientConfiguration.option($T.EXECUTION_INTERCEPTORS)", listType, SdkClientOption.class) - .addStatement("$T queryParamsToBodyInterceptor = new $T<>($T.asList(new $T()))", - listType, ArrayList.class, Arrays.class, QueryParametersToBodyInterceptor.class) + .addStatement("$T queryParamsToBodyInterceptor = $T.singletonList(new $T())", + listType, Collections.class, QueryParametersToBodyInterceptor.class) .addStatement("$T customizationInterceptors = new $T<>()", listType, ArrayList.class); List customInterceptors = model.getCustomizationConfig().getInterceptors(); diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java index b9096e19c394..f71429db299c 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-async-client-builder-class.java @@ -2,7 +2,7 @@ import java.net.URI; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -39,8 +39,8 @@ public DefaultQueryAsyncClientBuilder tokenProvider(SdkTokenProvider tokenProvid protected final QueryAsyncClient buildClient() { SdkClientConfiguration clientConfiguration = super.asyncClientConfiguration(); List interceptors = clientConfiguration.option(SdkClientOption.EXECUTION_INTERCEPTORS); - List queryParamsToBodyInterceptor = new ArrayList<>( - Arrays.asList(new QueryParametersToBodyInterceptor())); + List queryParamsToBodyInterceptor = Collections + .singletonList(new QueryParametersToBodyInterceptor()); List customizationInterceptors = new ArrayList<>(); customizationInterceptors.add(new QueryProtocolCustomTestInterceptor()); interceptors = CollectionUtils.mergeLists(queryParamsToBodyInterceptor, interceptors); diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java index 31ab60055e1c..56b94d1d3189 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-query-sync-client-builder-class.java @@ -2,7 +2,7 @@ import java.net.URI; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import software.amazon.awssdk.annotations.Generated; import software.amazon.awssdk.annotations.SdkInternalApi; @@ -39,8 +39,8 @@ public DefaultQueryClientBuilder tokenProvider(SdkTokenProvider tokenProvider) { protected final QueryClient buildClient() { SdkClientConfiguration clientConfiguration = super.syncClientConfiguration(); List interceptors = clientConfiguration.option(SdkClientOption.EXECUTION_INTERCEPTORS); - List queryParamsToBodyInterceptor = new ArrayList<>( - Arrays.asList(new QueryParametersToBodyInterceptor())); + List queryParamsToBodyInterceptor = Collections + .singletonList(new QueryParametersToBodyInterceptor()); List customizationInterceptors = new ArrayList<>(); customizationInterceptors.add(new QueryProtocolCustomTestInterceptor()); interceptors = CollectionUtils.mergeLists(queryParamsToBodyInterceptor, interceptors);