Skip to content

Commit 0ad6f05

Browse files
authored
chore(codegen): adopt checked dependency imports for TypeScriptWriter (#6000)
* chore: update smithy hash * chore: client-dynamodb compatibility with checked imports * chore: fix document client codegen * chore: codegen for lib-dynamodb * chore: codegen sync * chore: codegen for lib-dynamodb * chore: update smithy pkg versions and lockfile
1 parent 2a11640 commit 0ad6f05

File tree

36 files changed

+249
-164
lines changed

36 files changed

+249
-164
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddDocumentClientPlugin.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ private void writeAdditionalFiles(
123123
writer.write("export * from './pagination';");
124124
writer.write("export * from './$L';", DocumentClientUtils.CLIENT_NAME);
125125
writer.write("export * from './$L';", DocumentClientUtils.CLIENT_FULL_NAME);
126-
writer.write("export { NumberValueImpl as NumberValue } from \"@aws-sdk/util-dynamodb\";");
126+
writer.write("");
127+
writer.write("""
128+
export { NumberValueImpl as NumberValue } from "@aws-sdk/util-dynamodb";
129+
export { marshallOptions, unmarshallOptions } from "@aws-sdk/util-dynamodb";
130+
export { NativeAttributeValue, NativeAttributeBinary, NativeScalarAttributeValue } from "@aws-sdk/util-dynamodb";
131+
""");
127132
});
128133
}
129134
}

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ public enum AwsDependency implements Dependency {
9393
// feat(experimentalIdentityAndAuth): Conditionally added when @httpBearerAuth is used in an AWS service
9494
TOKEN_PROVIDERS(NORMAL_DEPENDENCY, "@aws-sdk/token-providers"),
9595
TYPES(NORMAL_DEPENDENCY, "@aws-sdk/types"),
96-
REGION_CONFIG_RESOLVER(NORMAL_DEPENDENCY, "@aws-sdk/region-config-resolver");
96+
REGION_CONFIG_RESOLVER(NORMAL_DEPENDENCY, "@aws-sdk/region-config-resolver"),
97+
98+
CLIENT_DYNAMODB_PEER(PEER_DEPENDENCY, "@aws-sdk/client-dynamodb", "^3.0.0"),
99+
UTIL_DYNAMODB(NORMAL_DEPENDENCY, "@aws-sdk/util-dynamodb", "*");
100+
97101

98102
public final String packageName;
99103
public final String version;

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsRestXml.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private void serializeDocumentBody(
187187
String xmlDeclVar = context.getStringStore().var("<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>");
188188
writer.write("body = $L;", xmlDeclVar);
189189

190-
writer.addImport("XmlNode", "__XmlNode", "@aws-sdk/xml-builder");
190+
writer.addImport("XmlNode", "__XmlNode", AwsDependency.XML_BUILDER);
191191

192192
// Handle the @xmlName trait for the input shape.
193193
StructureShape inputShape = context.getModel().expectShape(inputShapeId, StructureShape.class);

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentAggregatedClientGenerator.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ final class DocumentAggregatedClientGenerator implements Runnable {
6363

6464
@Override
6565
public void run() {
66-
writer.addImport(DocumentClientUtils.CLIENT_NAME,
67-
DocumentClientUtils.CLIENT_NAME, Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
66+
// Note: using addImport would register this dependency on the dynamodb client, which must be avoided.
67+
writer.write("""
68+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
69+
""");
70+
writer.addRelativeImport(DocumentClientUtils.CLIENT_NAME,
71+
DocumentClientUtils.CLIENT_NAME, Paths.get(".", DocumentClientUtils.CLIENT_NAME));
6872
writer.writeDocs(DocumentClientUtils.getClientDocs());
6973
writer.openBlock("export class $L extends $L {", "}",
7074
DocumentClientUtils.CLIENT_FULL_NAME, DocumentClientUtils.CLIENT_NAME, () -> {
@@ -77,8 +81,7 @@ public void run() {
7781

7882
private void generateStaticFactoryFrom() {
7983
String translateConfig = DocumentClientUtils.CLIENT_TRANSLATE_CONFIG_TYPE;
80-
writer.addImport(serviceName, serviceName, "@aws-sdk/client-dynamodb");
81-
writer.addImport(translateConfig, translateConfig, Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
84+
writer.addRelativeImport(translateConfig, translateConfig, Paths.get(".", DocumentClientUtils.CLIENT_NAME));
8285
writer.openBlock("static from(client: $L, translateConfig?: $L) {", "}",
8386
serviceName, translateConfig, () -> {
8487
writer.write("return new $L(client, translateConfig);", DocumentClientUtils.CLIENT_FULL_NAME);

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentBareBonesClientGenerator.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ public void run() {
6969
String serviceOutputTypes = "ServiceOutputTypes";
7070

7171
// Add required imports.
72-
writer.addImport(serviceName, serviceName, "@aws-sdk/client-dynamodb");
73-
writer.addImport(configType, configType, "@aws-sdk/client-dynamodb");
72+
// Note: using addImport would register these dependencies on the dynamodb client, which must be avoided.
73+
writer.write("""
74+
import {
75+
DynamoDBClient,
76+
DynamoDBClientResolvedConfig,
77+
ServiceInputTypes as __ServiceInputTypes,
78+
ServiceOutputTypes as __ServiceOutputTypes,
79+
} from "@aws-sdk/client-dynamodb";
80+
import { marshallOptions, unmarshallOptions } from "@aws-sdk/util-dynamodb";
81+
""");
82+
7483
writer.addImport("Client", "__Client", TypeScriptDependency.AWS_SMITHY_CLIENT);
7584
writer.writeDocs("@public");
7685
writer.write("export { __Client };");
@@ -101,8 +110,6 @@ public void run() {
101110
}
102111

103112
private void generateInputOutputImports(String serviceInputTypes, String serviceOutputTypes) {
104-
writer.addImport(serviceInputTypes, String.format("__%s", serviceInputTypes), "@aws-sdk/client-dynamodb");
105-
writer.addImport(serviceOutputTypes, String.format("__%s", serviceOutputTypes), "@aws-sdk/client-dynamodb");
106113
Set<OperationShape> containedOperations =
107114
new TreeSet<>(TopDownIndex.of(model).getContainedOperations(service));
108115

@@ -199,7 +206,6 @@ private void generateConfiguration() {
199206
}
200207

201208
private void generateTranslateConfigOption(String translateOption) {
202-
writer.addImport(translateOption, translateOption, "@aws-sdk/util-dynamodb");
203209
writer.write("${1L}?: ${1L};", translateOption);
204210
}
205211
}

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentClientCommandGenerator.java

+62-10
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515

1616
package software.amazon.smithy.aws.typescript.codegen;
1717

18+
import java.nio.file.Path;
1819
import java.nio.file.Paths;
1920
import java.util.ArrayList;
2021
import java.util.HashSet;
2122
import java.util.List;
23+
import java.util.Map;
2224
import java.util.Optional;
25+
import java.util.TreeMap;
2326
import java.util.stream.Collectors;
2427
import software.amazon.smithy.codegen.core.CodegenException;
2528
import software.amazon.smithy.codegen.core.Symbol;
@@ -62,6 +65,10 @@ final class DocumentClientCommandGenerator implements Runnable {
6265
private final List<MemberShape> outputMembersWithAttr;
6366
private final String clientCommandClassName;
6467
private final String clientCommandLocalName;
68+
/**
69+
* Map of package name to external:local name entries.
70+
*/
71+
private final Map<String, Map<String, String>> orderedUncheckedImports = new TreeMap<>();
6572

6673
DocumentClientCommandGenerator(
6774
TypeScriptSettings settings,
@@ -97,12 +104,21 @@ final class DocumentClientCommandGenerator implements Runnable {
97104

98105
@Override
99106
public void run() {
100-
String servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString();
107+
Path servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME);
101108
String configType = DocumentClientUtils.CLIENT_CONFIG_NAME;
102109

110+
// Note: using addImport would register these dependencies on the dynamodb client, which must be avoided.
111+
writer.write("""
112+
import { %s as %s } from "%s";
113+
""".formatted(
114+
clientCommandClassName,
115+
clientCommandLocalName,
116+
AwsDependency.CLIENT_DYNAMODB_PEER.getPackageName()
117+
)
118+
);
103119

104120
// Add required imports.
105-
writer.addImport(configType, configType, servicePath);
121+
writer.addRelativeImport(configType, configType, servicePath);
106122
writer.addImport(
107123
"DynamoDBDocumentClientCommand",
108124
"DynamoDBDocumentClientCommand",
@@ -159,6 +175,24 @@ public void run() {
159175
writer.pushState(COMMAND_BODY_EXTRA_SECTION).popState();
160176
}
161177
);
178+
179+
// Note: using addImport would register these dependencies on the dynamodb client, which must be avoided.
180+
writer.write("");
181+
orderedUncheckedImports.forEach((dep, symbols) -> {
182+
writer.openBlock("import type {", """
183+
} from "%s";
184+
""".formatted(dep), () -> {
185+
symbols.forEach((externalName, localName) -> {
186+
if (externalName.equals(localName)) {
187+
writer.writeInline(localName).write(",");
188+
} else {
189+
writer.write("""
190+
%s as %s,
191+
""".formatted(externalName, localName));
192+
}
193+
});
194+
});
195+
});
162196
}
163197

164198
private void generateCommandConstructor() {
@@ -180,9 +214,9 @@ private void generateCommandMiddlewareResolver(String configType) {
180214
String handler = "Handler";
181215
String middlewareStack = "MiddlewareStack";
182216

183-
String servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString();
184-
writer.addImport(serviceInputTypes, serviceInputTypes, servicePath);
185-
writer.addImport(serviceOutputTypes, serviceOutputTypes, servicePath);
217+
Path servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME);
218+
writer.addRelativeImport(serviceInputTypes, serviceInputTypes, servicePath);
219+
writer.addRelativeImport(serviceOutputTypes, serviceOutputTypes, servicePath);
186220
writer.addImport(handler, handler, TypeScriptDependency.SMITHY_TYPES);
187221
writer.addImport(middlewareStack, middlewareStack, TypeScriptDependency.SMITHY_TYPES);
188222

@@ -194,8 +228,6 @@ private void generateCommandMiddlewareResolver(String configType) {
194228
.dedent();
195229
writer.openBlock("): $L<$L, $L> {", "}", handler, inputTypeName, outputTypeName, () -> {
196230

197-
writer.addImport(clientCommandClassName, clientCommandLocalName, "@aws-sdk/client-dynamodb");
198-
199231
String commandVarName = "this.clientCommand";
200232

201233
// marshall middlewares
@@ -312,7 +344,11 @@ private void writeType(
312344
) {
313345
writer.writeDocs("@public");
314346
if (optionalShape.isPresent()) {
315-
writer.addImport(originalTypeName, "__" + originalTypeName, "@aws-sdk/client-dynamodb");
347+
registerTypeImport(
348+
originalTypeName,
349+
"__" + originalTypeName,
350+
AwsDependency.CLIENT_DYNAMODB_PEER.getPackageName()
351+
);
316352
if (membersWithAttr.isEmpty()) {
317353
writer.write("export type $L = __$L;", typeName, originalTypeName);
318354
} else {
@@ -339,7 +375,11 @@ private void writeStructureOmitType(StructureShape structureTarget) {
339375
.map(memberWithAttr -> "'" + symbolProvider.toMemberName(memberWithAttr) + "'")
340376
.collect(Collectors.joining(" | "));
341377
String typeNameToOmit = symbolProvider.toSymbol(structureTarget).getName();
342-
writer.addImport(typeNameToOmit, typeNameToOmit, "@aws-sdk/client-dynamodb");
378+
registerTypeImport(
379+
typeNameToOmit,
380+
typeNameToOmit,
381+
AwsDependency.CLIENT_DYNAMODB_PEER.getPackageName()
382+
);
343383
writer.openBlock("Omit<$L, $L> & {", "}", typeNameToOmit,
344384
memberUnionToOmit, () -> {
345385
for (MemberShape memberWithAttr: membersWithAttr) {
@@ -387,7 +427,11 @@ private void writeMemberOmitType(MemberShape member) {
387427

388428
private void writeNativeAttributeValue() {
389429
String nativeAttributeValue = "NativeAttributeValue";
390-
writer.addImport(nativeAttributeValue, nativeAttributeValue, "@aws-sdk/util-dynamodb");
430+
registerTypeImport(
431+
nativeAttributeValue,
432+
nativeAttributeValue,
433+
AwsDependency.UTIL_DYNAMODB.getPackageName()
434+
);
391435
writer.write(nativeAttributeValue);
392436
}
393437

@@ -402,4 +446,12 @@ private void writeNativeAttributeValue() {
402446
private boolean isRequiredMember(MemberShape member) {
403447
return member.isRequired() && !member.hasTrait(IdempotencyTokenTrait.class);
404448
}
449+
450+
private void registerTypeImport(String externalName, String localName, String packageName) {
451+
orderedUncheckedImports.putIfAbsent(packageName, new TreeMap<>());
452+
orderedUncheckedImports.get(packageName)
453+
.put(
454+
externalName, localName
455+
);
456+
}
405457
}

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentClientPaginationGenerator.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package software.amazon.smithy.aws.typescript.codegen;
1717

18+
import java.nio.file.Path;
1819
import java.nio.file.Paths;
1920
import java.util.Optional;
2021
import software.amazon.smithy.codegen.core.CodegenException;
@@ -77,20 +78,20 @@ final class DocumentClientPaginationGenerator implements Runnable {
7778
@Override
7879
public void run() {
7980
// Import Service Types
80-
String commandFileLocation = Paths.get(".", DocumentClientUtils.CLIENT_COMMANDS_FOLDER,
81-
DocumentClientUtils.getModifiedName(operationTypeName)).toString();
82-
writer.addImport(operationTypeName, operationTypeName, commandFileLocation);
83-
writer.addImport(inputTypeName, inputTypeName, commandFileLocation);
84-
writer.addImport(outputTypeName, outputTypeName, commandFileLocation);
85-
writer.addImport(
81+
Path commandFileLocation = Paths.get(".", DocumentClientUtils.CLIENT_COMMANDS_FOLDER,
82+
DocumentClientUtils.getModifiedName(operationTypeName));
83+
writer.addRelativeImport(operationTypeName, operationTypeName, commandFileLocation);
84+
writer.addRelativeImport(inputTypeName, inputTypeName, commandFileLocation);
85+
writer.addRelativeImport(outputTypeName, outputTypeName, commandFileLocation);
86+
writer.addRelativeImport(
8687
DocumentClientUtils.CLIENT_NAME,
8788
DocumentClientUtils.CLIENT_NAME,
88-
Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
89+
Paths.get(".", DocumentClientUtils.CLIENT_NAME));
8990

9091
// Import Pagination types
9192
writer.addImport("Paginator", "Paginator", TypeScriptDependency.SMITHY_TYPES);
92-
writer.addImport(paginationType, paginationType,
93-
Paths.get(".", getInterfaceFilelocation().replace(".ts", "")).toString());
93+
writer.addRelativeImport(paginationType, paginationType,
94+
Paths.get(".", getInterfaceFilelocation().replace(".ts", "")));
9495

9596
writer.writeDocs("@public");
9697
writer.write("export { Paginator }");
@@ -113,14 +114,14 @@ static String getInterfaceFilelocation() {
113114
static void generateServicePaginationInterfaces(TypeScriptWriter writer) {
114115
writer.addImport("PaginationConfiguration", "PaginationConfiguration", TypeScriptDependency.SMITHY_TYPES);
115116

116-
writer.addImport(
117+
writer.addRelativeImport(
117118
DocumentClientUtils.CLIENT_NAME,
118119
DocumentClientUtils.CLIENT_NAME,
119-
Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
120-
writer.addImport(
120+
Paths.get(".", DocumentClientUtils.CLIENT_NAME));
121+
writer.addRelativeImport(
121122
DocumentClientUtils.CLIENT_FULL_NAME,
122123
DocumentClientUtils.CLIENT_FULL_NAME,
123-
Paths.get(".", DocumentClientUtils.CLIENT_FULL_NAME).toString());
124+
Paths.get(".", DocumentClientUtils.CLIENT_FULL_NAME));
124125

125126
writer.writeDocs("@public");
126127
writer.write("export { PaginationConfiguration };");

lib/lib-dynamodb/src/commands/BatchExecuteStatementCommand.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
// smithy-typescript generated code
2-
import {
3-
BatchExecuteStatementCommand as __BatchExecuteStatementCommand,
4-
BatchExecuteStatementCommandInput as __BatchExecuteStatementCommandInput,
5-
BatchExecuteStatementCommandOutput as __BatchExecuteStatementCommandOutput,
6-
BatchStatementError,
7-
BatchStatementRequest,
8-
BatchStatementResponse,
9-
} from "@aws-sdk/client-dynamodb";
10-
import { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
2+
import { BatchExecuteStatementCommand as __BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb";
113
import { Command as $Command } from "@smithy/smithy-client";
124
import { Handler, HttpHandlerOptions as __HttpHandlerOptions, MiddlewareStack } from "@smithy/types";
135

@@ -104,3 +96,12 @@ export class BatchExecuteStatementCommand extends DynamoDBDocumentClientCommand<
10496
return async () => handler(this.clientCommand);
10597
}
10698
}
99+
100+
import type {
101+
BatchExecuteStatementCommandInput as __BatchExecuteStatementCommandInput,
102+
BatchExecuteStatementCommandOutput as __BatchExecuteStatementCommandOutput,
103+
BatchStatementError,
104+
BatchStatementRequest,
105+
BatchStatementResponse,
106+
} from "@aws-sdk/client-dynamodb";
107+
import type { NativeAttributeValue } from "@aws-sdk/util-dynamodb";

lib/lib-dynamodb/src/commands/BatchGetCommand.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
// smithy-typescript generated code
2-
import {
3-
BatchGetItemCommand as __BatchGetItemCommand,
4-
BatchGetItemCommandInput as __BatchGetItemCommandInput,
5-
BatchGetItemCommandOutput as __BatchGetItemCommandOutput,
6-
KeysAndAttributes,
7-
} from "@aws-sdk/client-dynamodb";
8-
import { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
2+
import { BatchGetItemCommand as __BatchGetItemCommand } from "@aws-sdk/client-dynamodb";
93
import { Command as $Command } from "@smithy/smithy-client";
104
import { Handler, HttpHandlerOptions as __HttpHandlerOptions, MiddlewareStack } from "@smithy/types";
115

@@ -112,3 +106,10 @@ export class BatchGetCommand extends DynamoDBDocumentClientCommand<
112106
return async () => handler(this.clientCommand);
113107
}
114108
}
109+
110+
import type {
111+
BatchGetItemCommandInput as __BatchGetItemCommandInput,
112+
BatchGetItemCommandOutput as __BatchGetItemCommandOutput,
113+
KeysAndAttributes,
114+
} from "@aws-sdk/client-dynamodb";
115+
import type { NativeAttributeValue } from "@aws-sdk/util-dynamodb";

lib/lib-dynamodb/src/commands/BatchWriteCommand.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
// smithy-typescript generated code
2-
import {
3-
BatchWriteItemCommand as __BatchWriteItemCommand,
4-
BatchWriteItemCommandInput as __BatchWriteItemCommandInput,
5-
BatchWriteItemCommandOutput as __BatchWriteItemCommandOutput,
6-
DeleteRequest,
7-
ItemCollectionMetrics,
8-
PutRequest,
9-
WriteRequest,
10-
} from "@aws-sdk/client-dynamodb";
11-
import { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
2+
import { BatchWriteItemCommand as __BatchWriteItemCommand } from "@aws-sdk/client-dynamodb";
123
import { Command as $Command } from "@smithy/smithy-client";
134
import { Handler, HttpHandlerOptions as __HttpHandlerOptions, MiddlewareStack } from "@smithy/types";
145

@@ -145,3 +136,13 @@ export class BatchWriteCommand extends DynamoDBDocumentClientCommand<
145136
return async () => handler(this.clientCommand);
146137
}
147138
}
139+
140+
import type {
141+
BatchWriteItemCommandInput as __BatchWriteItemCommandInput,
142+
BatchWriteItemCommandOutput as __BatchWriteItemCommandOutput,
143+
DeleteRequest,
144+
ItemCollectionMetrics,
145+
PutRequest,
146+
WriteRequest,
147+
} from "@aws-sdk/client-dynamodb";
148+
import type { NativeAttributeValue } from "@aws-sdk/util-dynamodb";

0 commit comments

Comments
 (0)