Skip to content

Commit 4688cec

Browse files
authored
Fix generation of credentialType(bearer) for non-priority bearer operations (#6201)
* Fix generation of credentialType(bearer) for non-priority bearer operations * Add changelog
1 parent ecb18c1 commit 4688cec

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Fix generation of credentialType(bearer) for non-priority bearer operations"
6+
}

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ProtocolSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ default String discoveredEndpoint(OperationModel opModel) {
121121

122122
default CodeBlock credentialType(OperationModel opModel, IntermediateModel model) {
123123

124-
if (AuthUtils.isOpBearerAuth(model, opModel)) {
124+
if (AuthUtils.isOpBearerAuthPreferred(model, opModel)) {
125125
return CodeBlock.of(".credentialType($T.TOKEN)\n", CredentialType.class);
126126
} else {
127127
return CodeBlock.of("");

codegen/src/main/java/software/amazon/awssdk/codegen/utils/AuthUtils.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ public static boolean usesAwsAuth(IntermediateModel model) {
5858
}
5959

6060
/**
61-
* Returns {@code true} if the operation should use bearer auth.
61+
* Returns {@code true} if and only if the operation should use bearer auth as the first preferred auth scheme.
6262
*/
63-
public static boolean isOpBearerAuth(IntermediateModel model, OperationModel opModel) {
64-
if (opModel.getAuthType() == AuthType.BEARER) {
65-
return true;
66-
}
67-
return isServiceBearerAuth(model) && hasNoAuthType(opModel);
63+
public static boolean isOpBearerAuthPreferred(IntermediateModel model, OperationModel opModel) {
64+
return opModel.getAuthType() == AuthType.BEARER // single modeled auth on operation is bearer
65+
// auth array, first auth type is bearer
66+
|| (opModel.getAuth() != null && !opModel.getAuth().isEmpty() && opModel.getAuth().get(0) == AuthType.BEARER)
67+
// service is only bearer and operation doesn't override
68+
|| (model.getMetadata().getAuthType() == AuthType.BEARER && hasNoAuthType(opModel))
69+
// service is only bearer first and operation doesn't override
70+
|| (model.getMetadata().getAuth() != null && !model.getMetadata().getAuth().isEmpty()
71+
&& model.getMetadata().getAuth().get(0) == AuthType.BEARER && hasNoAuthType(opModel));
6872
}
6973

7074
private static boolean isServiceBearerAuth(IntermediateModel model) {

codegen/src/test/java/software/amazon/awssdk/codegen/utils/AuthUtilsTest.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,30 @@ private static Stream<Arguments> awsAuthServiceValues() {
7979

8080
@ParameterizedTest
8181
@MethodSource("opValues")
82-
public void testIfOperationIsBearerAuth(AuthType serviceAuthType, AuthType opAuthType, Boolean expectedResult) {
83-
IntermediateModel model = modelWith(serviceAuthType);
84-
OperationModel opModel = opModelWith(opAuthType);
85-
assertThat(AuthUtils.isOpBearerAuth(model, opModel)).isEqualTo(expectedResult);
82+
public void testIfOperationIsBearerAuthPreferred(AuthType serviceAuthType, List<AuthType> serviceAuth,
83+
AuthType opAuthType, List<AuthType> opAuth,
84+
Boolean expectedResult) {
85+
IntermediateModel model = modelWith(serviceAuthType, serviceAuth);
86+
OperationModel opModel = opModelWith(opAuthType, opAuth);
87+
assertThat(AuthUtils.isOpBearerAuthPreferred(model, opModel)).isEqualTo(expectedResult);
8688
}
8789

8890
private static Stream<Arguments> opValues() {
89-
return Stream.of(Arguments.of(AuthType.BEARER, AuthType.BEARER, true),
90-
Arguments.of(AuthType.BEARER, AuthType.S3V4, false),
91-
Arguments.of(AuthType.BEARER, AuthType.NONE, true),
92-
Arguments.of(AuthType.BEARER, null, true),
93-
Arguments.of(AuthType.S3V4, AuthType.BEARER, true),
94-
Arguments.of(AuthType.S3V4, AuthType.S3V4, false),
95-
Arguments.of(AuthType.S3V4, AuthType.NONE, false),
96-
Arguments.of(AuthType.S3V4, null, false));
91+
return Stream.of(
92+
Arguments.of(AuthType.BEARER, null, AuthType.BEARER, null, true),
93+
Arguments.of(AuthType.BEARER, null, AuthType.S3V4, null, false),
94+
Arguments.of(AuthType.BEARER, null, AuthType.NONE, null, true),
95+
Arguments.of(AuthType.BEARER, null, null, null, true),
96+
Arguments.of(AuthType.S3V4, null, AuthType.BEARER, null, true),
97+
Arguments.of(AuthType.S3V4, null, AuthType.S3V4, null, false),
98+
Arguments.of(AuthType.S3V4, null, AuthType.NONE, null, false),
99+
Arguments.of(AuthType.S3V4, null, null, null, false),
100+
Arguments.of(AuthType.S3V4, Arrays.asList(AuthType.S3V4, AuthType.BEARER), AuthType.S3V4, null, false),
101+
Arguments.of(AuthType.S3V4, null, AuthType.S3V4, Arrays.asList(AuthType.S3V4, AuthType.BEARER), false),
102+
Arguments.of(AuthType.S3V4, Arrays.asList(AuthType.BEARER, AuthType.S3V4), null, null, true),
103+
Arguments.of(AuthType.S3V4, Arrays.asList(AuthType.BEARER, AuthType.S3V4), AuthType.S3V4, null, false),
104+
Arguments.of(AuthType.S3V4, null, AuthType.S3V4, Arrays.asList(AuthType.BEARER, AuthType.S3V4), true)
105+
);
97106
}
98107

99108
private static OperationModel opModelWith(AuthType authType) {
@@ -102,6 +111,12 @@ private static OperationModel opModelWith(AuthType authType) {
102111
return opModel;
103112
}
104113

114+
private static OperationModel opModelWith(AuthType authType, List<AuthType> auth) {
115+
OperationModel opModel = opModelWith(authType);
116+
opModel.setAuth(auth);
117+
return opModel;
118+
}
119+
105120
private static IntermediateModel modelWith(AuthType authType) {
106121
IntermediateModel model = new IntermediateModel();
107122
Metadata metadata = new Metadata();

0 commit comments

Comments
 (0)