Skip to content

Commit e357787

Browse files
committed
fixup! feat: support @httpApiKeyAuth trait
1 parent cdcd5c2 commit e357787

File tree

5 files changed

+193
-143
lines changed

5 files changed

+193
-143
lines changed

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

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,41 @@ public final class AddHttpApiKeyAuthPlugin implements TypeScriptIntegration {
5757
@Override
5858
public List<RuntimeClientPlugin> getClientPlugins() {
5959
return ListUtils.of(
60-
// Add the config if the service uses HTTP API key authorization
60+
// Add the config if the service uses HTTP API key authorization.
6161
RuntimeClientPlugin.builder()
62-
.inputConfig(Symbol.builder().namespace(
63-
"./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/"
64-
).name("HttpApiKeyAuthInputConfig").build())
65-
.resolvedConfig(Symbol.builder().namespace(
66-
"./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/"
67-
).name("HttpApiKeyAuthResolvedConfig").build())
68-
.resolveFunction(Symbol.builder().namespace(
69-
"./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/"
70-
).name("resolveHttpApiKeyAuthConfig").build())
71-
.servicePredicate((m, s) -> hasHttpApiKeyAuthTrait(s) && !areAllOptionalAuthOperations(m, s))
72-
.build(),
62+
.inputConfig(Symbol.builder()
63+
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
64+
.name("HttpApiKeyAuthInputConfig")
65+
.build())
66+
.resolvedConfig(Symbol.builder()
67+
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
68+
.name("HttpApiKeyAuthResolvedConfig")
69+
.build())
70+
.resolveFunction(Symbol.builder()
71+
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
72+
.name("resolveHttpApiKeyAuthConfig")
73+
.build())
74+
.servicePredicate((m, s) -> s.hasTrait(HttpApiKeyAuthTrait.class)
75+
&& !areAllOptionalAuthOperations(m, s))
76+
.build(),
7377

74-
// Add the middleware to operations that use HTTP API key authorization
78+
// Add the middleware to operations that use HTTP API key authorization.
7579
RuntimeClientPlugin.builder()
76-
.pluginFunction(Symbol.builder()
77-
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
78-
.name("getHttpApiKeyAuthPlugin")
79-
.build())
80-
.additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap<String, Object>() {{
81-
// It's safe to do getTrait().get() because the operation predicate ensures that the trait exists
82-
// `in` and `name` are required attributes of the trait, `scheme` is optional
83-
put("in", s.getTrait(HttpApiKeyAuthTrait.class).get().getIn().toString());
84-
put("name", s.getTrait(HttpApiKeyAuthTrait.class).get().getName());
85-
put("scheme", s.getTrait(HttpApiKeyAuthTrait.class).get().getScheme().orElse(null));
86-
}})
87-
.operationPredicate((m, s, o) -> hasHttpApiKeyAuthTrait(s)
88-
&& !operationUsesOptionalAuth(m, s, o))
89-
.build()
80+
.pluginFunction(Symbol.builder()
81+
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
82+
.name("getHttpApiKeyAuthPlugin")
83+
.build())
84+
.additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap<String, Object>() {{
85+
// It's safe to do getTrait().get() because the operation predicate ensures that the trait
86+
// exists `in` and `name` are required attributes of the trait, `scheme` is optional.
87+
put("in", s.getTrait(HttpApiKeyAuthTrait.class).get().getIn().toString());
88+
put("name", s.getTrait(HttpApiKeyAuthTrait.class).get().getName());
89+
s.getTrait(HttpApiKeyAuthTrait.class).get().getScheme().ifPresent(scheme ->
90+
put("scheme", scheme));
91+
}})
92+
.operationPredicate((m, s, o) -> s.hasTrait(HttpApiKeyAuthTrait.class)
93+
&& !o.hasTrait(OptionalAuthTrait.class))
94+
.build()
9095
);
9196
}
9297

@@ -101,15 +106,15 @@ public void writeAdditionalFiles(
101106

102107
// If the service doesn't use HTTP API keys, we don't need to do anything and the generated
103108
// code doesn't need any additional files.
104-
if (!hasHttpApiKeyAuthTrait(service) || areAllOptionalAuthOperations(model, service)) {
109+
if (!service.hasTrait(HttpApiKeyAuthTrait.class) || areAllOptionalAuthOperations(model, service)) {
105110
return;
106111
}
107112

108113
String noTouchNoticePrefix = "// Please do not touch this file. It's generated from a template in:\n"
109114
+ "// https://github.com/awslabs/smithy-typescript/blob/main/smithy-typescript-codegen/"
110115
+ "src/main/resources/software/amazon/smithy/aws/typescript/codegen/integration/";
111116

112-
// write the middleware source
117+
// Write the middleware source.
113118
writerFactory.accept(
114119
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.ts").toString(),
115120
writer -> {
@@ -118,7 +123,7 @@ public void writeAdditionalFiles(
118123
writer.write("$L", source);
119124
});
120125

121-
// write the middleware tests
126+
// Write the middleware tests.
122127
writerFactory.accept(
123128
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.spec.ts").toString(),
124129
writer -> {
@@ -128,51 +133,15 @@ public void writeAdditionalFiles(
128133
});
129134
}
130135

131-
/**
132-
* Check if the service has the @httpApiKeyAuth trait.
133-
*
134-
* @param service the service shape
135-
*
136-
* @return true if the service has the @httpApiKeyAuth trait
137-
*/
138-
private static boolean hasHttpApiKeyAuthTrait(ServiceShape service) {
139-
return service.hasTrait(HttpApiKeyAuthTrait.class);
140-
}
141-
142-
// 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
136+
// 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.
143137
private static boolean areAllOptionalAuthOperations(Model model, ServiceShape service) {
144138
TopDownIndex topDownIndex = TopDownIndex.of(model);
145139
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
146140
for (OperationShape operation : operations) {
147-
if (!operationUsesOptionalAuth(model, service, operation)) {
141+
if (!operation.hasTrait(OptionalAuthTrait.class)) {
148142
return false;
149143
}
150144
}
151145
return true;
152146
}
153-
154-
// 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
155-
private static boolean hasOptionalAuthOperation(Model model, ServiceShape service) {
156-
TopDownIndex topDownIndex = TopDownIndex.of(model);
157-
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
158-
for (OperationShape operation : operations) {
159-
if (operationUsesOptionalAuth(model, service, operation)) {
160-
return true;
161-
}
162-
}
163-
return false;
164-
}
165-
166-
/**
167-
* Check if the operation has @optionalAuth, implying it doesn't need the middleware.
168-
*
169-
* @param model the model
170-
* @param service the service shape
171-
* @param operation the operation shape
172-
*
173-
* @return true if the operation has the @optionalAuth trait
174-
*/
175-
private static boolean operationUsesOptionalAuth(Model model, ServiceShape service, OperationShape operation) {
176-
return operation.hasTrait(OptionalAuthTrait.class);
177-
}
178147
}

smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/integration/http-api-key-auth.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("getHttpApiKeyAuthPlugin", () => {
3232
// TODO there's got to be a better way to do this mocking
3333
plugin.applyToStack({
3434
add: mockAdd,
35-
// we don't expect any of these others to be called
35+
// We don't expect any of these others to be called.
3636
addRelativeTo: mockOther,
3737
concat: mockOther,
3838
resolve: mockOther,
@@ -169,3 +169,4 @@ describe("httpApiKeyAuthMiddleware", () => {
169169
});
170170
});
171171
});
172+

smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/integration/http-api-key-auth.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,20 @@ export const httpApiKeyAuthMiddleware =
8080

8181
const { request } = args;
8282

83-
// this middleware will not be injected if the operation has the @optionalAuth trait
83+
// This middleware will not be injected if the operation has the @optionalAuth trait.
8484
if (!resolvedConfig.apiKey) {
8585
throw new Error(
8686
"API key authorization is required but no API key was provided in the client configuration"
8787
);
8888
}
8989

9090
if (options.in === "header") {
91-
// set the header, even if it's already been set
91+
// Set the header, even if it's already been set.
9292
request.headers[options.name.toLowerCase()] = options.scheme
9393
? `${options.scheme} ${resolvedConfig.apiKey}`
9494
: resolvedConfig.apiKey;
9595
} else if (options.in === "query") {
96-
// It's terrifying that this is an option; API keys should not be part of the URL
96+
// It's terrifying that this is an option; API keys should not be part of the URL.
9797
throw new Error(
9898
"Sorry, passing the API key in a query parameter is not supported yet."
9999
);
@@ -122,3 +122,4 @@ export const getHttpApiKeyAuthPlugin = (
122122
);
123123
},
124124
});
125+

0 commit comments

Comments
 (0)