Skip to content

Commit bdc025c

Browse files
authored
Fix sourcing of BuiltIn values from ServiceConfig (#2180)
1 parent 4dd1783 commit bdc025c

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,12 @@ private MethodSpec finalizeServiceConfigurationMethod() {
276276
builder.addCode("return config.toBuilder()\n");
277277

278278
if (model.getCustomizationConfig().getServiceConfig().hasDualstackProperty()) {
279-
builder.addCode(".option($T.DUALSTACK_ENDPOINT_ENABLED, c.dualstackEnabled())", AwsClientOption.class);
279+
builder.addCode(".option($T.DUALSTACK_ENDPOINT_ENABLED, finalServiceConfig.dualstackEnabled())",
280+
AwsClientOption.class);
280281
}
281282

282283
if (model.getCustomizationConfig().getServiceConfig().hasFipsProperty()) {
283-
builder.addCode(".option($T.FIPS_ENDPOINT_ENABLED, c.fipsModeEnabled())", AwsClientOption.class);
284+
builder.addCode(".option($T.FIPS_ENDPOINT_ENABLED, finalServiceConfig.fipsModeEnabled())", AwsClientOption.class);
284285
}
285286

286287
if (model.getEndpointOperation().isPresent()) {
@@ -297,7 +298,7 @@ private MethodSpec finalizeServiceConfigurationMethod() {
297298
}
298299

299300
if (StringUtils.isNotBlank(clientConfigClassName)) {
300-
builder.addCode(".option($T.SERVICE_CONFIGURATION, c.build())", SdkClientOption.class);
301+
builder.addCode(".option($T.SERVICE_CONFIGURATION, finalServiceConfig)", SdkClientOption.class);
301302
}
302303

303304
if (model.getCustomizationConfig().useGlobalEndpoint()) {
@@ -315,65 +316,71 @@ private MethodSpec finalizeServiceConfigurationMethod() {
315316

316317
private void mergeServiceConfiguration(MethodSpec.Builder builder, String clientConfigClassName) {
317318
ClassName clientConfigClass = ClassName.bestGuess(clientConfigClassName);
318-
builder.addCode("$1T.Builder c = (($1T) config.option($2T.SERVICE_CONFIGURATION)).toBuilder();" +
319-
"c.profileFile(c.profileFile() != null ? c.profileFile() : config.option($2T.PROFILE_FILE));" +
320-
"c.profileName(c.profileName() != null ? c.profileName() : config.option($2T.PROFILE_NAME));",
319+
builder.addCode("$1T.Builder serviceConfigBuilder = (($1T) config.option($2T.SERVICE_CONFIGURATION)).toBuilder();" +
320+
"serviceConfigBuilder.profileFile(serviceConfigBuilder.profileFile() "
321+
+ "!= null ? serviceConfigBuilder.profileFile() : config.option($2T.PROFILE_FILE));" +
322+
"serviceConfigBuilder.profileName(serviceConfigBuilder.profileName() "
323+
+ "!= null ? serviceConfigBuilder.profileName() : config.option($2T.PROFILE_NAME));",
321324
clientConfigClass, SdkClientOption.class);
322325

326+
327+
323328
if (model.getCustomizationConfig().getServiceConfig().hasDualstackProperty()) {
324-
builder.addCode("if (c.dualstackEnabled() != null) {")
329+
builder.addCode("if (serviceConfigBuilder.dualstackEnabled() != null) {")
325330
.addCode(" $T.validState(config.option($T.DUALSTACK_ENDPOINT_ENABLED) == null, \"Dualstack has been "
326331
+ "configured on both $L and the client/global level. Please limit dualstack configuration to "
327332
+ "one location.\");",
328333
Validate.class, AwsClientOption.class, clientConfigClassName)
329334
.addCode("} else {")
330-
.addCode(" c.dualstackEnabled(config.option($T.DUALSTACK_ENDPOINT_ENABLED));", AwsClientOption.class)
335+
.addCode(" serviceConfigBuilder.dualstackEnabled(config.option($T.DUALSTACK_ENDPOINT_ENABLED));",
336+
AwsClientOption.class)
331337
.addCode("}");
332338
}
333339

334340
if (model.getCustomizationConfig().getServiceConfig().hasFipsProperty()) {
335-
builder.addCode("if (c.fipsModeEnabled() != null) {")
341+
builder.addCode("if (serviceConfigBuilder.fipsModeEnabled() != null) {")
336342
.addCode(" $T.validState(config.option($T.FIPS_ENDPOINT_ENABLED) == null, \"Fips has been "
337343
+ "configured on both $L and the client/global level. Please limit fips configuration to "
338344
+ "one location.\");",
339345
Validate.class, AwsClientOption.class, clientConfigClassName)
340346
.addCode("} else {")
341-
.addCode(" c.fipsModeEnabled(config.option($T.FIPS_ENDPOINT_ENABLED));", AwsClientOption.class)
347+
.addCode(" serviceConfigBuilder.fipsModeEnabled(config.option($T.FIPS_ENDPOINT_ENABLED));",
348+
AwsClientOption.class)
342349
.addCode("}");
343350
}
344351

352+
builder.addStatement("$T finalServiceConfig = serviceConfigBuilder.build()", clientConfigClass);
353+
345354
if (model.getCustomizationConfig().getServiceConfig().hasUseArnRegionProperty()) {
346-
builder.addCode(CodeBlock.builder().beginControlFlow("if (c.useArnRegionEnabled() != null)")
347-
.addStatement("clientContextParams.put($T.USE_ARN_REGION, c.useArnRegionEnabled())",
348-
endpointRulesSpecUtils.clientContextParamsName())
349-
.endControlFlow()
350-
.build());
355+
builder.addCode(
356+
CodeBlock.builder()
357+
.addStatement("clientContextParams.put($T.USE_ARN_REGION, finalServiceConfig.useArnRegionEnabled())",
358+
endpointRulesSpecUtils.clientContextParamsName())
359+
.build());
351360
}
352361

353362
if (model.getCustomizationConfig().getServiceConfig().hasMultiRegionEnabledProperty()) {
354-
builder.addCode(CodeBlock.builder().beginControlFlow("if (c.multiRegionEnabled() != null)")
355-
.addStatement("clientContextParams.put($T.DISABLE_MULTI_REGION_ACCESS_POINTS, !c"
356-
+ ".multiRegionEnabled())",
357-
endpointRulesSpecUtils.clientContextParamsName())
358-
.endControlFlow()
359-
.build());
363+
builder.addCode(
364+
CodeBlock.builder()
365+
.addStatement("clientContextParams.put($T.DISABLE_MULTI_REGION_ACCESS_POINTS, !finalServiceConfig"
366+
+ ".multiRegionEnabled())",
367+
endpointRulesSpecUtils.clientContextParamsName())
368+
.build());
360369
}
361370

362371
if (model.getCustomizationConfig().getServiceConfig().hasForcePathTypeEnabledProperty()) {
363-
builder.addCode(CodeBlock.builder().beginControlFlow("if (c.pathStyleAccessEnabled() != null)")
364-
.addStatement("clientContextParams.put($T.FORCE_PATH_STYLE, c"
372+
builder.addCode(CodeBlock.builder()
373+
.addStatement("clientContextParams.put($T.FORCE_PATH_STYLE, finalServiceConfig"
365374
+ ".pathStyleAccessEnabled())",
366375
endpointRulesSpecUtils.clientContextParamsName())
367-
.endControlFlow()
368376
.build());
369377
}
370378

371379
if (model.getCustomizationConfig().getServiceConfig().hasAccelerateModeEnabledProperty()) {
372-
builder.addCode(CodeBlock.builder().beginControlFlow("if (c.accelerateModeEnabled() != null)")
373-
.addStatement("clientContextParams.put($T.ACCELERATE, c"
380+
builder.addCode(CodeBlock.builder()
381+
.addStatement("clientContextParams.put($T.ACCELERATE, finalServiceConfig"
374382
+ ".accelerateModeEnabled())",
375383
endpointRulesSpecUtils.clientContextParamsName())
376-
.endControlFlow()
377384
.build());
378385
}
379386
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/builder/test-client-builder-class.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory;
1616
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
1717
import software.amazon.awssdk.core.signer.Signer;
18+
import software.amazon.awssdk.services.json.rules.JsonClientContextParams;
1819
import software.amazon.awssdk.services.json.rules.JsonEndpointProvider;
1920
import software.amazon.awssdk.services.json.rules.internal.JsonEndpointAuthSchemeInterceptor;
2021
import software.amazon.awssdk.services.json.rules.internal.JsonRequestSetEndpointInterceptor;
@@ -58,29 +59,37 @@ protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientCon
5859
additionalInterceptors.add(new JsonRequestSetEndpointInterceptor());
5960
interceptors = CollectionUtils.mergeLists(interceptors, additionalInterceptors);
6061
interceptors = CollectionUtils.mergeLists(interceptors, config.option(SdkClientOption.EXECUTION_INTERCEPTORS));
61-
ServiceConfiguration.Builder c = ((ServiceConfiguration) config.option(SdkClientOption.SERVICE_CONFIGURATION))
62-
.toBuilder();
63-
c.profileFile(c.profileFile() != null ? c.profileFile() : config.option(SdkClientOption.PROFILE_FILE));
64-
c.profileName(c.profileName() != null ? c.profileName() : config.option(SdkClientOption.PROFILE_NAME));
65-
if (c.dualstackEnabled() != null) {
62+
ServiceConfiguration.Builder serviceConfigBuilder = ((ServiceConfiguration) config
63+
.option(SdkClientOption.SERVICE_CONFIGURATION)).toBuilder();
64+
serviceConfigBuilder.profileFile(serviceConfigBuilder.profileFile() != null ? serviceConfigBuilder.profileFile() : config
65+
.option(SdkClientOption.PROFILE_FILE));
66+
serviceConfigBuilder.profileName(serviceConfigBuilder.profileName() != null ? serviceConfigBuilder.profileName() : config
67+
.option(SdkClientOption.PROFILE_NAME));
68+
if (serviceConfigBuilder.dualstackEnabled() != null) {
6669
Validate.validState(
6770
config.option(AwsClientOption.DUALSTACK_ENDPOINT_ENABLED) == null,
6871
"Dualstack has been configured on both ServiceConfiguration and the client/global level. Please limit dualstack configuration to one location.");
6972
} else {
70-
c.dualstackEnabled(config.option(AwsClientOption.DUALSTACK_ENDPOINT_ENABLED));
73+
serviceConfigBuilder.dualstackEnabled(config.option(AwsClientOption.DUALSTACK_ENDPOINT_ENABLED));
7174
}
72-
if (c.fipsModeEnabled() != null) {
75+
if (serviceConfigBuilder.fipsModeEnabled() != null) {
7376
Validate.validState(
7477
config.option(AwsClientOption.FIPS_ENDPOINT_ENABLED) == null,
7578
"Fips has been configured on both ServiceConfiguration and the client/global level. Please limit fips configuration to one location.");
7679
} else {
77-
c.fipsModeEnabled(config.option(AwsClientOption.FIPS_ENDPOINT_ENABLED));
80+
serviceConfigBuilder.fipsModeEnabled(config.option(AwsClientOption.FIPS_ENDPOINT_ENABLED));
7881
}
79-
return config.toBuilder().option(AwsClientOption.DUALSTACK_ENDPOINT_ENABLED, c.dualstackEnabled())
80-
.option(AwsClientOption.FIPS_ENDPOINT_ENABLED, c.fipsModeEnabled())
82+
ServiceConfiguration finalServiceConfig = serviceConfigBuilder.build();
83+
clientContextParams.put(JsonClientContextParams.USE_ARN_REGION, finalServiceConfig.useArnRegionEnabled());
84+
clientContextParams.put(JsonClientContextParams.DISABLE_MULTI_REGION_ACCESS_POINTS,
85+
!finalServiceConfig.multiRegionEnabled());
86+
clientContextParams.put(JsonClientContextParams.FORCE_PATH_STYLE, finalServiceConfig.pathStyleAccessEnabled());
87+
clientContextParams.put(JsonClientContextParams.ACCELERATE, finalServiceConfig.accelerateModeEnabled());
88+
return config.toBuilder().option(AwsClientOption.DUALSTACK_ENDPOINT_ENABLED, finalServiceConfig.dualstackEnabled())
89+
.option(AwsClientOption.FIPS_ENDPOINT_ENABLED, finalServiceConfig.fipsModeEnabled())
8190
.option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors)
8291
.option(SdkClientOption.RETRY_POLICY, MyServiceRetryPolicy.resolveRetryPolicy(config))
83-
.option(SdkClientOption.SERVICE_CONFIGURATION, c.build()).build();
92+
.option(SdkClientOption.SERVICE_CONFIGURATION, finalServiceConfig).build();
8493
}
8594

8695
private Signer defaultSigner() {

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/c2j/rest-json/customization.config

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
"serviceConfig": {
88
"className": "ServiceConfiguration",
99
"hasDualstackProperty": true,
10-
"hasFipsProperty": true
10+
"hasFipsProperty": true,
11+
"hasUseArnRegionProperty": true,
12+
"hasMultiRegionEnabledProperty": true,
13+
"hasPathStyleAccessEnabledProperty":true,
14+
"hasAccelerateModeEnabledProperty":true
1115
},
1216
"customRetryPolicy": "software.amazon.MyServiceRetryPolicy",
1317
"verifiedSimpleMethods" : ["paginatedOperationWithResultKey"],

0 commit comments

Comments
 (0)