Skip to content

Commit 38a5776

Browse files
committed
fixup! feat: support @httpApiKeyAuth trait
1 parent f0ee2e5 commit 38a5776

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPlugin.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@ public List<RuntimeClientPlugin> getClientPlugins() {
7272
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
7373
.name("resolveHttpApiKeyAuthConfig")
7474
.build())
75-
.servicePredicate((m, s) -> ServiceIndex.of(m).getEffectiveAuthSchemes(s)
76-
.values().stream()
77-
.filter(c -> c instanceof HttpApiKeyAuthTrait)
78-
.findFirst()
79-
.isPresent()
80-
&& !areAllOptionalAuthOperations(m, s))
75+
.servicePredicate((m, s) -> hasEffectiveHttpApiKeyAuthTrait(m, s))
8176
.build(),
8277

8378
// Add the middleware to operations that use HTTP API key authorization.
@@ -87,19 +82,17 @@ public List<RuntimeClientPlugin> getClientPlugins() {
8782
.name("getHttpApiKeyAuthPlugin")
8883
.build())
8984
.additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap<String, Object>() {{
90-
// It's safe to do getTrait().get() because the operation predicate ensures that the trait
85+
// It's safe to do expectTrait() because the operation predicate ensures that the trait
9186
// exists `in` and `name` are required attributes of the trait, `scheme` is optional.
92-
put("in", s.getTrait(HttpApiKeyAuthTrait.class).get().getIn().toString());
93-
put("name", s.getTrait(HttpApiKeyAuthTrait.class).get().getName());
94-
s.getTrait(HttpApiKeyAuthTrait.class).get().getScheme().ifPresent(scheme ->
87+
put("in", s.expectTrait(HttpApiKeyAuthTrait.class).getIn().toString());
88+
put("name", s.expectTrait(HttpApiKeyAuthTrait.class).getName());
89+
s.expectTrait(HttpApiKeyAuthTrait.class).getScheme().ifPresent(scheme ->
9590
put("scheme", scheme));
9691
}})
97-
.operationPredicate((m, s, o) -> ServiceIndex.of(m).getEffectiveAuthSchemes(s)
98-
.values().stream()
99-
.filter(c -> c instanceof HttpApiKeyAuthTrait)
100-
.findFirst()
101-
.isPresent()
102-
&& !o.hasTrait(OptionalAuthTrait.class))
92+
.operationPredicate((m, s, o) -> ServiceIndex.of(m).getEffectiveAuthSchemes(s, o)
93+
.keySet()
94+
.contains(HttpApiKeyAuthTrait.ID)
95+
&& !o.hasTrait(OptionalAuthTrait.class))
10396
.build()
10497
);
10598
}
@@ -115,7 +108,7 @@ public void writeAdditionalFiles(
115108

116109
// If the service doesn't use HTTP API keys, we don't need to do anything and the generated
117110
// code doesn't need any additional files.
118-
if (!service.hasTrait(HttpApiKeyAuthTrait.class) || areAllOptionalAuthOperations(model, service)) {
111+
if (!hasEffectiveHttpApiKeyAuthTrait(model, service)) {
119112
return;
120113
}
121114

@@ -125,23 +118,33 @@ public void writeAdditionalFiles(
125118

126119
// Write the middleware source.
127120
writerFactory.accept(
128-
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.ts").toString(),
129-
writer -> {
130-
String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.ts");
131-
writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.ts");
132-
writer.write("$L", source);
133-
});
121+
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.ts").toString(),
122+
writer -> {
123+
String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.ts");
124+
writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.ts");
125+
writer.write("$L", source);
126+
});
134127

135128
// Write the middleware tests.
136129
writerFactory.accept(
137-
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.spec.ts").toString(),
138-
writer -> {
139-
String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.spec.ts");
140-
writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.spec.ts");
141-
writer.write("$L", source);
142-
});
130+
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.spec.ts").toString(),
131+
writer -> {
132+
String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.spec.ts");
133+
writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.spec.ts");
134+
writer.write("$L", source);
135+
});
143136
}
144137

138+
// The service has the effective trait if it's in the "effective auth schemes" response
139+
// AND if not all of the operations have the optional auth trait.
140+
private static boolean hasEffectiveHttpApiKeyAuthTrait(Model model, ServiceShape service) {
141+
return ServiceIndex.of(model).getEffectiveAuthSchemes(service)
142+
.keySet()
143+
.contains(HttpApiKeyAuthTrait.ID)
144+
&& !areAllOptionalAuthOperations(model, service);
145+
}
146+
147+
145148
// This is derived from https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddAwsAuthPlugin.java.
146149
private static boolean areAllOptionalAuthOperations(Model model, ServiceShape service) {
147150
TopDownIndex topDownIndex = TopDownIndex.of(model);

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPluginTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ private void testDoesNotInject(String filename) {
122122
not(containsString("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration")));
123123

124124
// Make sure that the middleware file was not written.
125-
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts")
126-
.isPresent(),
125+
assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts"),
127126
is(false));
128127
}
129128
}

smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/integration/http-api-key-auth-trait-all-optional.smithy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ structure GetBarInput {}
3636
structure GetBarOutput {}
3737

3838
@error("client")
39-
structure GetBarError {}
39+
structure GetBarError {}
40+

smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/integration/http-api-key-auth-trait-no-scheme.smithy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ structure GetBarInput {}
3535
structure GetBarOutput {}
3636

3737
@error("client")
38-
structure GetBarError {}
38+
structure GetBarError {}
39+

smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/integration/http-api-key-auth-trait-on-operation.smithy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ structure GetBarInput {}
3535
structure GetBarOutput {}
3636

3737
@error("client")
38-
structure GetBarError {}
38+
structure GetBarError {}
39+

smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/integration/http-api-key-auth-trait.smithy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ structure GetBarInput {}
3636
structure GetBarOutput {}
3737

3838
@error("client")
39-
structure GetBarError {}
39+
structure GetBarError {}
40+

0 commit comments

Comments
 (0)