Skip to content

Commit

Permalink
Reorganize models in smithy-typescript-codegen-test (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan authored Oct 10, 2023
1 parent d5d3c62 commit 8d107a9
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.ToShapeId;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.typescript.codegen.CodegenUtils;
import software.amazon.smithy.typescript.codegen.Dependency;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
Expand All @@ -29,15 +31,16 @@
import software.amazon.smithy.utils.SmithyInternalApi;

@SmithyInternalApi
public final class GenericTestAddCustomEndpointsRuntimeConfig implements TypeScriptIntegration {
public final class ExampleWeatherCustomEndpointsRuntimeConfig implements TypeScriptIntegration {
public static final String GENERIC_TEST_DIR = Paths.get(".", CodegenUtils.SOURCE_FOLDER, "generic").toString();
public static final String INDEX_MODULE = GENERIC_TEST_DIR + "/index";
public static final String INDEX_FILE = INDEX_MODULE + ".ts";
public static final String ADD_CUSTOM_ENDPOINTS_FILE = GENERIC_TEST_DIR + "/customEndpoints" + ".ts";
public static final String getClientFile(ShapeId service) {
return Paths.get(".", CodegenUtils.SOURCE_FOLDER, service.getName() + "Client.ts").toString();
}

public static final ShapeId EXAMPLE_WEATHER_SERVICE_ID = ShapeId.from("example.weather#Weather");

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return List.of(
Expand All @@ -54,10 +57,12 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.namespace(INDEX_MODULE, "/")
.name("resolveGenericCustomEndpointsConfig")
.build())
.servicePredicate((m, s) -> isExampleWeatherService(s))
.build(),
RuntimeClientPlugin.builder()
.withConventions(
TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2.dependency, "Endpoint", Convention.HAS_CONFIG)
.servicePredicate((m, s) -> isExampleWeatherService(s))
.build());
}

Expand All @@ -67,12 +72,18 @@ public void customize(TypeScriptCodegenContext codegenContext) {
return;
}

if (!isExampleWeatherService(codegenContext.settings().getService())) {
return;
}

codegenContext.writerDelegator().useFileWriter(INDEX_FILE, w -> {
w.write("export * from \"./customEndpoints\";");
});

codegenContext.writerDelegator().useFileWriter(getClientFile(codegenContext.settings().getService()), w -> {
w.addImport("EndpointParameters", null, EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY);
if (codegenContext.settings().getService(codegenContext.model()).hasTrait(EndpointRuleSetTrait.ID)) {
w.addImport("EndpointParameters", null, EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY);
}
});

codegenContext.writerDelegator().useFileWriter(ADD_CUSTOM_ENDPOINTS_FILE, w -> {
Expand Down Expand Up @@ -102,4 +113,8 @@ export interface GenericCustomEndpointsResolvedConfig {
""");
});
}

private static boolean isExampleWeatherService(ToShapeId toShapeId) {
return toShapeId.toShapeId().equals(EXAMPLE_WEATHER_SERVICE_ID);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
example.weather.GenericTestAddCustomEndpointsRuntimeConfig
example.weather.ExampleWeatherCustomEndpointsRuntimeConfig
7 changes: 7 additions & 0 deletions smithy-typescript-codegen-test/model/common/fakeAuth.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$version: "2.0"

namespace common

@trait
@authDefinition
structure fakeAuth {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$version: "2.0"

namespace common

@trait
@protocolDefinition
structure fakeProtocol {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ use aws.auth#sigv4
use smithy.test#httpRequestTests
use smithy.test#httpResponseTests
use smithy.waiters#waitable

@authDefinition
@trait
structure customAuth {}
use common#fakeProtocol
use common#fakeAuth

/// Provides weather forecasts.
@fakeProtocol
@httpApiKeyAuth(name: "X-Api-Key", in: "header")
@httpBearerAuth
@sigv4(name: "weather")
@customAuth
@fakeAuth
@auth([sigv4])
@paginated(inputToken: "nextToken", outputToken: "nextToken", pageSize: "pageSize")
service Weather {
Expand All @@ -35,8 +33,8 @@ service Weather {
OnlyHttpApiKeyAndBearerAuthReversed
OnlySigv4Auth
OnlySigv4AuthOptional
OnlyCustomAuth
OnlyCustomAuthOptional
OnlyFakeAuth
OnlyFakeAuthOptional
SameAsService
]
}
Expand Down Expand Up @@ -76,14 +74,14 @@ operation OnlyHttpBearerAuthOptional {}
@optionalAuth
operation OnlySigv4AuthOptional {}

@http(method: "GET", uri: "/OnlyCustomAuth")
@auth([customAuth])
operation OnlyCustomAuth {}
@http(method: "GET", uri: "/OnlyFakeAuth")
@auth([fakeAuth])
operation OnlyFakeAuth {}

@http(method: "GET", uri: "/OnlyCustomAuthOptional")
@auth([customAuth])
@http(method: "GET", uri: "/OnlyFakeAuthOptional")
@auth([fakeAuth])
@optionalAuth
operation OnlyCustomAuthOptional {}
operation OnlyFakeAuthOptional {}

@http(method: "GET", uri: "/SameAsService")
operation SameAsService {}
Expand Down Expand Up @@ -126,7 +124,7 @@ apply GetCity @httpRequestTests(
{
id: "WriteGetCityAssertions"
documentation: "Does something"
protocol: "example.weather#fakeProtocol"
protocol: "common#fakeProtocol"
method: "GET"
uri: "/cities/123"
body: ""
Expand All @@ -140,7 +138,7 @@ apply GetCity @httpResponseTests(
{
id: "WriteGetCityResponseAssertions"
documentation: "Does something"
protocol: "example.weather#fakeProtocol"
protocol: "common#fakeProtocol"
code: 200
body: """
{
Expand Down Expand Up @@ -234,7 +232,7 @@ apply NoSuchResource @httpResponseTests(
{
id: "WriteNoSuchResourceAssertions"
documentation: "Does something"
protocol: "example.weather#fakeProtocol"
protocol: "common#fakeProtocol"
code: 404
body: """
{
Expand Down Expand Up @@ -279,7 +277,7 @@ apply ListCities @httpRequestTests(
{
id: "WriteListCitiesAssertions"
documentation: "Does something"
protocol: "example.weather#fakeProtocol"
protocol: "common#fakeProtocol"
method: "GET"
uri: "/cities"
body: ""
Expand Down Expand Up @@ -462,11 +460,6 @@ structure Message {
author: String
}

// Define a fake protocol trait for use.
@trait
@protocolDefinition
structure fakeProtocol {}

apply Weather @smithy.rules#endpointRuleSet({
"version": "1.3",
"parameters": {
Expand All @@ -492,4 +485,4 @@ apply Weather @smithy.rules#endpointRuleSet({

apply Weather @smithy.rules#clientContextParams(
Region: {type: "string", documentation: "docs"}
)
)
22 changes: 22 additions & 0 deletions smithy-typescript-codegen-test/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"args": {
"tags": ["client-only"]
}
},
{
"name": "includeServices",
"args": {
"services": ["example.weather#Weather"]
}
}
],
"plugins": {
Expand All @@ -25,6 +31,14 @@
}
},
"client-experimental-identity-and-auth": {
"transforms": [
{
"name": "includeServices",
"args": {
"services": ["example.weather#Weather"]
}
}
],
"plugins": {
"typescript-codegen": {
"service": "example.weather#Weather",
Expand All @@ -40,6 +54,14 @@
}
},
"control-experimental-identity-and-auth": {
"transforms": [
{
"name": "includeServices",
"args": {
"services": ["example.weather#Weather"]
}
}
],
"plugins": {
"typescript-codegen": {
"service": "example.weather#Weather",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TestProtocolGenerator extends HttpBindingProtocolGenerator {

@Override
public ShapeId getProtocol() {
return ShapeId.from("example.weather#fakeProtocol");
return ShapeId.from("common#fakeProtocol");
}

@Override
Expand Down

0 comments on commit 8d107a9

Please sign in to comment.