Skip to content

Commit 59edca7

Browse files
Use server symbol provider in protocol test gen
1 parent 1a6a906 commit 59edca7

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenVisitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ void execute() {
213213
// Generate protocol tests IFF found in the model.
214214
if (protocolGenerator != null) {
215215
ShapeId protocol = protocolGenerator.getProtocol();
216-
new HttpProtocolTestGenerator(settings, model, protocol, symbolProvider, writers, protocolGenerator).run();
216+
new HttpProtocolTestGenerator(
217+
settings, model, protocol, symbolProvider, serverSymbolProvider, writers, protocolGenerator).run();
217218
}
218219

219220
// Write each pending writer.

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
import software.amazon.smithy.utils.IoUtils;
6767
import software.amazon.smithy.utils.MapUtils;
6868
import software.amazon.smithy.utils.Pair;
69-
import software.amazon.smithy.utils.StringUtils;
7069

7170
/**
7271
* Generates HTTP protocol test cases to be run using Jest.
@@ -91,6 +90,7 @@ final class HttpProtocolTestGenerator implements Runnable {
9190
private final ServiceShape service;
9291
private final SymbolProvider symbolProvider;
9392
private final Symbol serviceSymbol;
93+
private final SymbolProvider serverSymbolProvider;
9494
private final Set<String> additionalStubs = new TreeSet<>();
9595
private final ProtocolGenerator protocolGenerator;
9696

@@ -105,6 +105,7 @@ final class HttpProtocolTestGenerator implements Runnable {
105105
Model model,
106106
ShapeId protocol,
107107
SymbolProvider symbolProvider,
108+
SymbolProvider serverSymbolProvider,
108109
TypeScriptDelegator delegator,
109110
ProtocolGenerator protocolGenerator
110111
) {
@@ -113,6 +114,7 @@ final class HttpProtocolTestGenerator implements Runnable {
113114
this.protocol = protocol;
114115
this.service = settings.getService(model);
115116
this.symbolProvider = symbolProvider;
117+
this.serverSymbolProvider = serverSymbolProvider;
116118
this.delegator = delegator;
117119
this.protocolGenerator = protocolGenerator;
118120
serviceSymbol = symbolProvider.toSymbol(service);
@@ -247,7 +249,7 @@ private void generateClientRequestTest(OperationShape operation, HttpRequestTest
247249
}
248250

249251
private void generateServerRequestTest(OperationShape operation, HttpRequestTestCase testCase) {
250-
Symbol operationSymbol = symbolProvider.toSymbol(operation);
252+
Symbol operationSymbol = serverSymbolProvider.toSymbol(operation);
251253

252254
// Lowercase all the headers we're expecting as this is what we'll get.
253255
Map<String, String> headers = testCase.getHeaders().entrySet().stream()
@@ -260,9 +262,8 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest
260262
String testName = testCase.getId() + ":ServerRequest";
261263
testCase.getDocumentation().ifPresent(writer::writeDocs);
262264
writer.openBlock("it($S, async () => {", "});\n", testName, () -> {
263-
// TODO: use the symbol provider when it's ready
264-
String serviceName = StringUtils.capitalize(service.getId().getName());
265-
String operationName = StringUtils.capitalize(operation.getId().getName());
265+
Symbol serviceSymbol = serverSymbolProvider.toSymbol(service);
266+
Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class);
266267
Symbol inputType = operationSymbol.expectProperty("inputType", Symbol.class);
267268
Symbol outputType = operationSymbol.expectProperty("outputType", Symbol.class);
268269

@@ -274,18 +275,18 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest
274275
// We use a partial here so that we don't have to define the entire service, but still get the advantages
275276
// the type checker, including excess property checking. Later on we'll use `as` to cast this to the
276277
// full service so that we can actually use it.
277-
writer.addImport(serviceName + "Service", null, "./server");
278-
writer.openBlock("const testService: Partial<$LService> = {", "};", serviceName, () -> {
278+
writer.openBlock("const testService: Partial<$T> = {", "};", serviceSymbol, () -> {
279279
writer.addImport("Operation", "__Operation", "@aws-smithy/server-common");
280-
writer.write("$L: testFunction as __Operation<$T, $T>,", operationName, inputType, outputType);
280+
writer.write("$L: testFunction as __Operation<$T, $T>,",
281+
operationSymbol.getName(), inputType, outputType);
281282
});
282283

283-
String getHandlerName = String.format("get%sServiceHandler", serviceName);
284+
String getHandlerName = "get" + handlerSymbol.getName();
284285
writer.addImport(getHandlerName, getHandlerName,
285286
"./protocols/" + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
286287

287288
// Cast the service as any so TS will ignore the fact that the type being passed in is incomplete.
288-
writer.write("const handler = $L(testService as $LService);", getHandlerName, serviceName);
289+
writer.write("const handler = $L(testService as $T);", getHandlerName, serviceSymbol);
289290

290291
// Construct a new http request according to the test case definition.
291292
writer.openBlock("const request = new HttpRequest({", "});", () -> {

0 commit comments

Comments
 (0)