Skip to content
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

fix: missing export for api key auth middleware #528

Merged
merged 4 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
@SmithyInternalApi
public final class AddHttpApiKeyAuthPlugin implements TypeScriptIntegration {

public static final String INTEGRATION_NAME = "HttpApiKeyAuth";

/**
* Plug into code generation for the client.
*
Expand All @@ -62,15 +64,15 @@ public List<RuntimeClientPlugin> getClientPlugins() {
// Add the config if the service uses HTTP API key authorization.
RuntimeClientPlugin.builder()
.inputConfig(Symbol.builder()
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/")
.name("HttpApiKeyAuthInputConfig")
.build())
.resolvedConfig(Symbol.builder()
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/")
.name("HttpApiKeyAuthResolvedConfig")
.build())
.resolveFunction(Symbol.builder()
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/")
.name("resolveHttpApiKeyAuthConfig")
.build())
.servicePredicate((m, s) -> hasEffectiveHttpApiKeyAuthTrait(m, s))
Expand All @@ -79,7 +81,7 @@ public List<RuntimeClientPlugin> getClientPlugins() {
// Add the middleware to operations that use HTTP API key authorization.
RuntimeClientPlugin.builder()
.pluginFunction(Symbol.builder()
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth", "/")
.namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/")
.name("getHttpApiKeyAuthPlugin")
.build())
.additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap<String, Object>() {{
Expand Down Expand Up @@ -119,7 +121,7 @@ public void writeAdditionalFiles(

// Write the middleware source.
writerFactory.accept(
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.ts").toString(),
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", INTEGRATION_NAME, "index.ts").toString(),
writer -> {
String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.ts");
writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.ts");
Expand All @@ -128,7 +130,7 @@ public void writeAdditionalFiles(

// Write the middleware tests.
writerFactory.accept(
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", "HttpApiKeyAuth", "index.spec.ts").toString(),
Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", INTEGRATION_NAME, "index.spec.ts").toString(),
writer -> {
writer.addDependency(SymbolDependency.builder()
.dependencyType("devDependencies")
Expand All @@ -142,6 +144,20 @@ public void writeAdditionalFiles(
});
}

@Override
public void writeAdditionalExports(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
boolean isClientSdk = settings.generateClient();
ServiceShape service = settings.getService(model);
if (isClientSdk && hasEffectiveHttpApiKeyAuthTrait(model, service)) {
writer.write("export * from \"./middleware/$1L\";", INTEGRATION_NAME);
}
}

// The service has the effective trait if it's in the "effective auth schemes" response
// AND if not all of the operations have the optional auth trait.
private static boolean hasEffectiveHttpApiKeyAuthTrait(Model model, ServiceShape service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ private void testInjects(String filename, String extra) {
// Make sure that the middleware file was written and exports the plugin symbol.
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts").get(),
containsString("export const getHttpApiKeyAuthPlugin"));

// Ensure that the middleware was being exported in the index file.
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/index.ts").get(),
containsString("from \"./middleware/HttpApiKeyAuth\""));
}

private MockManifest generate(String filename)
Expand Down Expand Up @@ -124,5 +128,9 @@ private void testDoesNotInject(String filename) {
// Make sure that the middleware file was not written.
assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts"),
is(false));

// Ensure that the middleware was not being exported in the index file.
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/index.ts").get(),
not(containsString("from \"./middleware/HttpApiKeyAuth\"")));
}
}