-
Notifications
You must be signed in to change notification settings - Fork 910
Move QueryParametersToBodyInterceptor to front of interceptor chain #4109
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 3 commits
57d7f59
f31c32e
5e5a458
67f47d5
8f7f99d
a37f419
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 |
---|---|---|
|
@@ -18,8 +18,11 @@ | |
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.Collections; | ||
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 +35,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 +124,46 @@ 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); | ||
|
||
addQueryProtocolInterceptors(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 addQueryProtocolInterceptors(MethodSpec.Builder b) { | ||
if (!model.getMetadata().isQueryProtocol()) { | ||
return 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); | ||
|
||
List<String> customInterceptors = model.getCustomizationConfig().getInterceptors(); | ||
Collections.reverse(customInterceptors); | ||
customInterceptors.forEach(i -> b.addStatement("interceptors.add(0, new $T())", ClassName.bestGuess(i))); | ||
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 is pretty confusing logic. Can we simplify this to remove the reverse and 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. Simplified to use |
||
return b.addStatement("clientConfiguration = clientConfiguration.toBuilder().option($T.EXECUTION_INTERCEPTORS, " | ||
+ "interceptors).build()", SdkClientOption.class); | ||
} | ||
|
||
private MethodSpec tokenProviderMethodImpl() { | ||
|
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. Why is this test removed? 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. Now that QueryParametersToBodyInterceptor is moved to the front, a custom interceptor like this test interceptor 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. If the test is broken, we should rewrite/fix it; we should continue to have a test to ensure this behavior stays in the client. 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. Discussed offline, we should be okay with this change since technically, you’re allowed to mix and match; you can have some params in the body and some in the URL |
This file was deleted.
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.
Not sure I understand the need for the other changes. Any reason we can't just change the ordering of this statement?
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.
In this
finalizeServiceConfiguration()
the service-specific interceptors are added to theSdkClientConfiguration
asSdkClientOption.EXECUTION_INTERCEPTORS
which are then merged with the global interceptors in SdkDefaultClientBuilder. So changing the ordering here would only move the interceptor to the front of the service-specific interceptors; it would still come after the global interceptors.Alternatively, we could create another SdkClientOption here and merge it in SdkDefaultClientBuilder, but to keep cross-module dependencies down Matt proposed we add it in the
SyncClientBuilderClass
/AsyncClientBuilderClass