diff --git a/build.gradle.kts b/build.gradle.kts index 8d26a4726f0..f2aee3c9fcf 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,20 +1,12 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - +import com.diffplug.spotless.FormatterFunc import com.github.spotbugs.snom.Effort import org.jreleaser.model.Active +import java.io.Serializable +import java.util.regex.Pattern plugins { `java-library` @@ -24,6 +16,7 @@ plugins { jacoco id("com.github.spotbugs") version "6.3.0" id("org.jreleaser") version "1.20.0" + id("com.diffplug.spotless") version "8.1.0" } allprojects { @@ -34,7 +27,6 @@ allprojects { // The root project doesn't produce a JAR. tasks["jar"].enabled = false - repositories { mavenLocal() mavenCentral() @@ -75,10 +67,11 @@ subprojects { } // Reusable license copySpec - val licenseSpec = copySpec { - from("${project.rootDir}/LICENSE") - from("${project.rootDir}/NOTICE") - } + val licenseSpec = + copySpec { + from("${project.rootDir}/LICENSE") + from("${project.rootDir}/NOTICE") + } // Set up tasks that build source and javadoc jars. tasks.register("sourcesJar") { @@ -127,7 +120,12 @@ subprojects { repositories { maven { name = "stagingRepository" - url = rootProject.layout.buildDirectory.dir("staging").get().asFile.toURI() + url = + rootProject.layout.buildDirectory + .dir("staging") + .get() + .asFile + .toURI() } } @@ -194,7 +192,9 @@ subprojects { * * Configure the running of tests. */ + // Log on passed, skipped, and failed test events if the `-Plog-tests` property is set. + if (project.hasProperty("log-tests")) { tasks.test { testLogging { @@ -226,7 +226,10 @@ subprojects { xml.required.set(false) csv.required.set(false) html.outputLocation.set( - layout.buildDirectory.dir("reports/jacoco").get().asFile + layout.buildDirectory + .dir("reports/jacoco") + .get() + .asFile, ) } } @@ -249,6 +252,62 @@ subprojects { } } } + + apply(plugin = "com.diffplug.spotless") + + spotless { + java { + // Enforce a common license header on all files + licenseHeaderFile("${project.rootDir}/config/spotless/license-header.txt") + .onlyIfContentMatches("^((?!SKIPLICENSECHECK)[\\s\\S])*\$") + leadingTabsToSpaces() + endWithNewline() + eclipse().configFile("${project.rootDir}/config/spotless/formatting.xml") + + // Fixes for some strange formatting applied by eclipse: + // see: https://github.com/kamkie/demo-spring-jsf/blob/bcacb9dc90273a5f8d2569470c5bf67b171c7d62/build.gradle.kts#L159 + // These have to be implemented with anonymous classes this way instead of lambdas because of: + // https://github.com/diffplug/spotless/issues/2387 + custom( + "Lambda fix", + object : Serializable, FormatterFunc { + override fun apply(input: String): String = input.replace("} )", "})").replace("} ,", "},") + }, + ) + custom( + "Long literal fix", + object : Serializable, FormatterFunc { + override fun apply(input: String): String = Pattern.compile("([0-9_]+) [Ll]").matcher(input).replaceAll("\$1L") + }, + ) + + // Static first, then everything else alphabetically + removeUnusedImports() + importOrder("\\#", "") + // Ignore generated code for formatter check + targetExclude("*/build/**/*.*") + } + + // Formatting for build.gradle.kts files + kotlinGradle { + ktlint() + leadingTabsToSpaces() + trimTrailingWhitespace() + endWithNewline() + licenseHeaderFile( + "${project.rootDir}/config/spotless/license-header.txt", + "import|tasks|apply|plugins|rootProject", + ) + } + tasks { + // If the property "noFormat" is set, don't auto-format source file (like in CI) + if (!project.hasProperty("noFormat")) { + build { + dependsOn(spotlessApply) + } + } + } + } } /* @@ -290,10 +349,13 @@ jreleaser { active = Active.ALWAYS url = "https://central.sonatype.com/api/v1/publisher" stagingRepositories.add( - rootProject.layout.buildDirectory.dir("staging").get().asFile.absolutePath + rootProject.layout.buildDirectory + .dir("staging") + .get() + .asFile.absolutePath, ) } } } } -} \ No newline at end of file +} diff --git a/config/spotless/formatting.xml b/config/spotless/formatting.xml new file mode 100644 index 00000000000..9c93ae8a0e6 --- /dev/null +++ b/config/spotless/formatting.xml @@ -0,0 +1,391 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config/spotless/license-header.txt b/config/spotless/license-header.txt new file mode 100644 index 00000000000..35d0f3613ab --- /dev/null +++ b/config/spotless/license-header.txt @@ -0,0 +1,4 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ diff --git a/settings.gradle.kts b/settings.gradle.kts index 73e3d1531c5..f709f78032f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - import java.io.FileInputStream import java.nio.charset.StandardCharsets.UTF_8 @@ -25,8 +14,10 @@ include(":smithy-typescript-codegen-test:released-version-test") include(":smithy-typescript-ssdk-codegen-test-utils") file( - java.nio.file.Paths.get(rootProject.projectDir.absolutePath, "local.properties")) - .takeIf { it.isFile }?.let { f -> + java.nio.file.Paths + .get(rootProject.projectDir.absolutePath, "local.properties"), +).takeIf { it.isFile } + ?.let { f -> java.util.Properties().apply { load(java.io.InputStreamReader(FileInputStream(f), UTF_8)) } }?.run { listOf("smithy") diff --git a/smithy-typescript-codegen/build.gradle.kts b/smithy-typescript-codegen/build.gradle.kts index 81f0c523c37..b2a2c9a3e26 100644 --- a/smithy-typescript-codegen/build.gradle.kts +++ b/smithy-typescript-codegen/build.gradle.kts @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - import software.amazon.smithy.model.node.Node description = "Generates TypeScript code from Smithy models" @@ -48,10 +37,15 @@ dependencies { sourceSets { main { resources { - setSrcDirs(listOf( - "src/main/resources", - layout.buildDirectory.dir("generated/resources").get().asFile - )) + setSrcDirs( + listOf( + "src/main/resources", + layout.buildDirectory + .dir("generated/resources") + .get() + .asFile, + ), + ) } } } @@ -59,21 +53,30 @@ sourceSets { abstract class SetDependencyVersionsTask : DefaultTask() { @get:InputDirectory abstract val packagesDir: DirectoryProperty - + @get:InputDirectory abstract val smithyTsSsdkLibs: DirectoryProperty - + @get:OutputFile abstract val versionsFile: RegularFileProperty - + @TaskAction fun execute() { val outputFile = versionsFile.get().asFile outputFile.parentFile.mkdirs() outputFile.printWriter().close() - - val roots = packagesDir.get().asFile.listFiles().toMutableList() + - smithyTsSsdkLibs.get().asFile.listFiles().toList() + + val roots = + packagesDir + .get() + .asFile + .listFiles() + .toMutableList() + + smithyTsSsdkLibs + .get() + .asFile + .listFiles() + .toList() roots.forEach { packageDir -> val packageJsonFile = File(packageDir, "package.json") if (packageJsonFile.isFile()) { @@ -92,7 +95,9 @@ abstract class SetDependencyVersionsTask : DefaultTask() { tasks.register("set-dependency-versions") { packagesDir.set(project.file("../packages")) smithyTsSsdkLibs.set(project.file("../smithy-typescript-ssdk-libs")) - versionsFile.set(layout.buildDirectory.file("generated/resources/software/amazon/smithy/typescript/codegen/dependencyVersions.properties")) + versionsFile.set( + layout.buildDirectory.file("generated/resources/software/amazon/smithy/typescript/codegen/dependencyVersions.properties"), + ) } tasks["processResources"].dependsOn(tasks["set-dependency-versions"]) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ApplicationProtocol.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ApplicationProtocol.java index 9255a7261c9..c4e3b8e194f 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ApplicationProtocol.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ApplicationProtocol.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.Objects; @@ -41,10 +30,10 @@ public final class ApplicationProtocol { * @param responseType The type used to represent response messages for the protocol. */ public ApplicationProtocol( - String name, - SymbolReference optionsType, - SymbolReference requestType, - SymbolReference responseType + String name, + SymbolReference optionsType, + SymbolReference requestType, + SymbolReference responseType ) { this.name = name; this.optionsType = optionsType; @@ -59,34 +48,33 @@ public ApplicationProtocol( */ public static ApplicationProtocol createDefaultHttpApplicationProtocol() { return new ApplicationProtocol( - "http", - SymbolReference.builder() - .symbol(createHttpSymbol(TypeScriptDependency.SMITHY_TYPES, "HttpHandlerOptions", true)) - .alias("__HttpHandlerOptions") - .build(), - SymbolReference.builder() - .symbol(createHttpSymbol(TypeScriptDependency.PROTOCOL_HTTP, "HttpRequest", true)) - .alias("__HttpRequest") - .build(), - SymbolReference.builder() - .symbol(createHttpSymbol(TypeScriptDependency.PROTOCOL_HTTP, "HttpResponse", true)) - .alias("__HttpResponse") - .build() - ); + "http", + SymbolReference.builder() + .symbol(createHttpSymbol(TypeScriptDependency.SMITHY_TYPES, "HttpHandlerOptions", true)) + .alias("__HttpHandlerOptions") + .build(), + SymbolReference.builder() + .symbol(createHttpSymbol(TypeScriptDependency.PROTOCOL_HTTP, "HttpRequest", true)) + .alias("__HttpRequest") + .build(), + SymbolReference.builder() + .symbol(createHttpSymbol(TypeScriptDependency.PROTOCOL_HTTP, "HttpResponse", true)) + .alias("__HttpResponse") + .build()); } private static Symbol createHttpSymbol(TypeScriptDependency dependency, String symbolName, boolean typeOnly) { Symbol.Builder builder = Symbol.builder() - .namespace(dependency.packageName, "/") - .name(symbolName) - .addDependency(dependency) - .addDependency(TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER) - .addDependency(TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); + .namespace(dependency.packageName, "/") + .name(symbolName) + .addDependency(dependency) + .addDependency(TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER) + .addDependency(TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); if (typeOnly) { builder.putProperty("typeOnly", true); } return builder - .build(); + .build(); } /** @@ -156,8 +144,8 @@ public boolean equals(Object o) { ApplicationProtocol that = (ApplicationProtocol) o; return optionsType.equals(that.optionsType) - && requestType.equals(that.requestType) - && responseType.equals(that.responseType); + && requestType.equals(that.requestType) + && responseType.equals(that.responseType); } @Override diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java index 73a642fabac..59edf70c87d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.ArrayList; @@ -72,8 +61,9 @@ public static String getOperationSerializerContextType( // If event stream trait exists, add corresponding serde context type to the intersection type. EventStreamIndex eventStreamIndex = EventStreamIndex.of(model); if (eventStreamIndex.getInputInfo(operation).isPresent()) { - writer.addTypeImport("EventStreamSerdeContext", "__EventStreamSerdeContext", - TypeScriptDependency.SMITHY_TYPES); + writer.addTypeImport("EventStreamSerdeContext", + "__EventStreamSerdeContext", + TypeScriptDependency.SMITHY_TYPES); contextInterfaceList.add("__EventStreamSerdeContext"); } return String.join(" & ", contextInterfaceList); @@ -98,12 +88,14 @@ public static String getOperationDeserializerContextType( // If event stream trait exists, add corresponding serde context type to the intersection type. EventStreamIndex eventStreamIndex = EventStreamIndex.of(model); if (eventStreamIndex.getOutputInfo(operation).isPresent()) { - writer.addTypeImport("EventStreamSerdeContext", "__EventStreamSerdeContext", + writer.addTypeImport("EventStreamSerdeContext", + "__EventStreamSerdeContext", TypeScriptDependency.SMITHY_TYPES); contextInterfaceList.add("__EventStreamSerdeContext"); } if (AddSdkStreamMixinDependency.hasStreamingBlobDeser(settings, model, operation)) { - writer.addTypeImport("SdkStreamSerdeContext", "__SdkStreamSerdeContext", + writer.addTypeImport("SdkStreamSerdeContext", + "__SdkStreamSerdeContext", TypeScriptDependency.SMITHY_TYPES); contextInterfaceList.add("__SdkStreamSerdeContext"); } @@ -119,7 +111,9 @@ private static List getDefaultOperationSerdeContextTypes(TypeScriptWrite } static List getBlobStreamingMembers(Model model, StructureShape shape) { - return shape.getAllMembers().values().stream() + return shape.getAllMembers() + .values() + .stream() .filter(memberShape -> { // Streaming blobs need to have their types modified // See `writeClientCommandStreamingInputType` @@ -138,11 +132,11 @@ static List getBlobStreamingMembers(Model model, StructureShape sha * Refer here for more rationales: https://github.com/aws/aws-sdk-js-v3/issues/843 */ static void writeClientCommandStreamingInputType( - TypeScriptWriter writer, - Symbol containerSymbol, - String typeName, - MemberShape streamingMember, - String commandName + TypeScriptWriter writer, + Symbol containerSymbol, + String typeName, + MemberShape streamingMember, + String commandName ) { writer.addTypeImport("StreamingBlobPayloadInputTypes", null, TypeScriptDependency.SMITHY_TYPES); String memberName = streamingMember.getMemberName(); @@ -150,17 +144,16 @@ static void writeClientCommandStreamingInputType( writer.writeDocs("@public\n\nThe input for {@link " + commandName + "}."); writer.write( - """ - export interface $L extends Omit<$T, $S> { - $L$L: StreamingBlobPayloadInputTypes; - } - """, - typeName, - containerSymbol, - memberName, - memberName, - optionalSuffix - ); + """ + export interface $L extends Omit<$T, $S> { + $L$L: StreamingBlobPayloadInputTypes; + } + """, + typeName, + containerSymbol, + memberName, + memberName, + optionalSuffix); } /** @@ -169,11 +162,11 @@ export interface $L extends Omit<$T, $S> { * stream to string, buffer or WHATWG stream API. */ static void writeClientCommandStreamingOutputType( - TypeScriptWriter writer, - Symbol containerSymbol, - String typeName, - MemberShape streamingMember, - String commandName + TypeScriptWriter writer, + Symbol containerSymbol, + String typeName, + MemberShape streamingMember, + String commandName ) { String memberName = streamingMember.getMemberName(); String optionalSuffix = streamingMember.isRequired() ? "" : "?"; @@ -182,36 +175,37 @@ static void writeClientCommandStreamingOutputType( writer.writeDocs("@public\n\nThe output of {@link " + commandName + "}."); writer.write( - """ - export interface $L extends Omit<$T, $S>, __MetadataBearer { - $L$L: StreamingBlobPayloadOutputTypes; - } - """, - typeName, - containerSymbol, - memberName, - memberName, - optionalSuffix - ); + """ + export interface $L extends Omit<$T, $S>, __MetadataBearer { + $L$L: StreamingBlobPayloadOutputTypes; + } + """, + typeName, + containerSymbol, + memberName, + memberName, + optionalSuffix); } static List getBlobPayloadMembers(Model model, StructureShape shape) { - return shape.getAllMembers().values().stream() - .filter(memberShape -> { - Shape target = model.expectShape(memberShape.getTarget()); - return target.isBlobShape() - && memberShape.hasTrait(HttpPayloadTrait.class) - && !target.hasTrait(StreamingTrait.class); - }) - .collect(Collectors.toList()); + return shape.getAllMembers() + .values() + .stream() + .filter(memberShape -> { + Shape target = model.expectShape(memberShape.getTarget()); + return target.isBlobShape() + && memberShape.hasTrait(HttpPayloadTrait.class) + && !target.hasTrait(StreamingTrait.class); + }) + .collect(Collectors.toList()); } static void writeClientCommandBlobPayloadInputType( - TypeScriptWriter writer, - Symbol containerSymbol, - String typeName, - MemberShape payloadMember, - String commandName + TypeScriptWriter writer, + Symbol containerSymbol, + String typeName, + MemberShape payloadMember, + String commandName ) { String memberName = payloadMember.getMemberName(); String optionalSuffix = payloadMember.isRequired() ? "" : "?"; @@ -220,27 +214,26 @@ static void writeClientCommandBlobPayloadInputType( writer.writeDocs("@public"); writer.write( - """ - export type $LType = Omit<$T, $S> & { - $L: BlobPayloadInputTypes; - }; - """, - typeName, - containerSymbol, - memberName, - memberName + optionalSuffix - ); + """ + export type $LType = Omit<$T, $S> & { + $L: BlobPayloadInputTypes; + }; + """, + typeName, + containerSymbol, + memberName, + memberName + optionalSuffix); writer.writeDocs("@public\n\nThe input for {@link " + commandName + "}."); writer.write("export interface $1L extends $1LType {}", typeName); } static void writeClientCommandBlobPayloadOutputType( - TypeScriptWriter writer, - Symbol containerSymbol, - String typeName, - MemberShape payloadMember, - String commandName + TypeScriptWriter writer, + Symbol containerSymbol, + String typeName, + MemberShape payloadMember, + String commandName ) { String memberName = payloadMember.getMemberName(); String optionalSuffix = payloadMember.isRequired() ? "" : "?"; @@ -250,22 +243,20 @@ static void writeClientCommandBlobPayloadOutputType( writer.writeDocs("@public"); writer.write( - """ - export type $LType = Omit<$T, $S> & { - $L: Uint8ArrayBlobAdapter; - }; - """, - typeName, - containerSymbol, - memberName, - memberName + optionalSuffix - ); + """ + export type $LType = Omit<$T, $S> & { + $L: Uint8ArrayBlobAdapter; + }; + """, + typeName, + containerSymbol, + memberName, + memberName + optionalSuffix); writer.writeDocs("@public\n\nThe output of {@link " + commandName + "}."); writer.write( - "export interface $1L extends $1LType, __MetadataBearer {}", - typeName - ); + "export interface $1L extends $1LType, __MetadataBearer {}", + typeName); } /** @@ -277,9 +268,10 @@ static void writeClientCommandBlobPayloadOutputType( */ static List getFunctionParametersList(Map paramsMap) { List functionParametersList = new ArrayList(); - List> sortedParamsMap = paramsMap.entrySet().stream() - .sorted(Map.Entry.comparingByKey()) - .toList(); + List> sortedParamsMap = paramsMap.entrySet() + .stream() + .sorted(Map.Entry.comparingByKey()) + .toList(); if (!sortedParamsMap.isEmpty()) { for (Map.Entry param : sortedParamsMap) { @@ -302,21 +294,24 @@ static List getFunctionParametersList(Map paramsMap) { throw new CodegenException("Plugin function parameters list must be List"); } List valueStringList = valueList.stream() - .map(item -> String.format("'%s'", item)) - .collect(Collectors.toList()); + .map(item -> String.format("'%s'", item)) + .collect(Collectors.toList()); functionParametersList.add(String.format("'%s': [%s]", - key, valueStringList.stream().collect(Collectors.joining(", ")))); + key, + valueStringList.stream().collect(Collectors.joining(", ")))); } else if (value instanceof Map) { Map valueMap = (Map) value; if (!valueMap.isEmpty() && valueMap.keySet().stream().anyMatch(k -> !(k instanceof String)) - && valueMap.values().stream().anyMatch(v -> !(v instanceof String))) { + && valueMap.values().stream().anyMatch(v -> !(v instanceof String))) { throw new CodegenException("Plugin function parameters map must be Map"); } - List valueStringList = valueMap.entrySet().stream() - .map(entry -> String.format("'%s': '%s'", entry.getKey(), entry.getValue())) - .collect(Collectors.toList()); + List valueStringList = valueMap.entrySet() + .stream() + .map(entry -> String.format("'%s': '%s'", entry.getKey(), entry.getValue())) + .collect(Collectors.toList()); functionParametersList.add(String.format("%s: {%s}", - key, valueStringList.stream().collect(Collectors.joining(", ")))); + key, + valueStringList.stream().collect(Collectors.joining(", ")))); } else { // Future support for param type should be added in else if. throw new CodegenException("Plugin function parameters not supported for type " @@ -341,7 +336,9 @@ static void writeInlineStreamingMemberType( String memberName = streamingMember.getMemberName(); String optionalSuffix = streamingMember.isRequired() ? "" : "?"; writer.writeInline("Omit<$1T, $2S> & { $2L$3L: $1T[$2S]|string|Uint8Array|Buffer }", - containerSymbol, memberName, optionalSuffix); + containerSymbol, + memberName, + optionalSuffix); } public static String getServiceName( @@ -373,12 +370,12 @@ public static String getSyntheticBaseExceptionName(String serviceName, Model mod while (true) { String finalServiceExceptionName = serviceExceptionName; boolean namingCollision = model.getStructureShapes() - .stream() - .anyMatch(structureShape -> structureShape.getId().getName().equals(finalServiceExceptionName)); + .stream() + .anyMatch(structureShape -> structureShape.getId().getName().equals(finalServiceExceptionName)); if (namingCollision) { serviceExceptionName = serviceExceptionName - .replaceAll("ServiceException$", "SyntheticServiceException"); + .replaceAll("ServiceException$", "SyntheticServiceException"); } else { break; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index 0309e04206a..7c5f9961b3b 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobPayloadMembers; @@ -96,8 +85,8 @@ final class CommandGenerator implements Runnable { private final ApplicationProtocol applicationProtocol; private final SensitiveDataFinder sensitiveDataFinder; private final ReservedWords reservedWords = new ReservedWordsBuilder() - .loadWords(Objects.requireNonNull(TypeScriptClientCodegenPlugin.class.getResource("reserved-words.txt"))) - .build(); + .loadWords(Objects.requireNonNull(TypeScriptClientCodegenPlugin.class.getResource("reserved-words.txt"))) + .build(); CommandGenerator( TypeScriptSettings settings, @@ -107,7 +96,8 @@ final class CommandGenerator implements Runnable { TypeScriptWriter writer, List runtimePlugins, ProtocolGenerator protocolGenerator, - ApplicationProtocol applicationProtocol) { + ApplicationProtocol applicationProtocol + ) { this.settings = settings; this.model = model; this.service = settings.getService(model); @@ -146,93 +136,98 @@ private void generateClientCommand() { String name = symbol.getName(); StringBuilder additionalDocs = new StringBuilder() - .append("\n") - .append(getCommandExample( - serviceSymbol.getName(), configType, name, inputType.getName(), outputType.getName())) - .append("\n") - .append(getThrownExceptions()) - .append("\n") - .append(getCuratedExamples(name)); + .append("\n") + .append(getCommandExample( + serviceSymbol.getName(), + configType, + name, + inputType.getName(), + outputType.getName())) + .append("\n") + .append(getThrownExceptions()) + .append("\n") + .append(getCuratedExamples(name)); boolean operationHasDocumentation = operation.hasTrait(DocumentationTrait.class); if (operationHasDocumentation) { writer.writeShapeDocs( - operation, - shapeDoc -> shapeDoc + additionalDocs - ); + operation, + shapeDoc -> shapeDoc + additionalDocs); } else { boolean isPublic = !operation.hasTrait(InternalTrait.class); boolean isDeprecated = operation.hasTrait(DeprecatedTrait.class); writer.writeDocs( - (isPublic ? "@public\n" : "@internal\n") - + (isDeprecated ? "@deprecated\n" : "") - + additionalDocs - ); + (isPublic ? "@public\n" : "@internal\n") + + (isDeprecated ? "@deprecated\n" : "") + + additionalDocs); } // Section of items like TypeScript @ts-ignore writer.injectSection(PreCommandClassCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .operation(operation) - .symbolProvider(symbolProvider) - .runtimeClientPlugins(runtimePlugins) - .protocolGenerator(protocolGenerator) - .applicationProtocol(applicationProtocol) - .build()); + .settings(settings) + .model(model) + .service(service) + .operation(operation) + .symbolProvider(symbolProvider) + .runtimeClientPlugins(runtimePlugins) + .protocolGenerator(protocolGenerator) + .applicationProtocol(applicationProtocol) + .build()); writer.openBlock( - """ - export class $L extends $$Command - .classBuilder< - $T, - $T, - $L, - ServiceInputTypes, - ServiceOutputTypes - >()""", - " .build() {", // class open bracket. - name, inputType, outputType, configType, - () -> { - generateEndpointParameterInstructionProvider(); - generateCommandMiddlewareResolver(configType); - writeSerde(); - }); + """ + export class $L extends $$Command + .classBuilder< + $T, + $T, + $L, + ServiceInputTypes, + ServiceOutputTypes + >()""", + " .build() {", // class open bracket. + name, + inputType, + outputType, + configType, + () -> { + generateEndpointParameterInstructionProvider(); + generateCommandMiddlewareResolver(configType); + writeSerde(); + }); // Ctor section. writer.injectSection(CommandConstructorCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .operation(operation) - .symbolProvider(symbolProvider) - .runtimeClientPlugins(runtimePlugins) - .protocolGenerator(protocolGenerator) - .applicationProtocol(applicationProtocol) - .build()); + .settings(settings) + .model(model) + .service(service) + .operation(operation) + .symbolProvider(symbolProvider) + .runtimeClientPlugins(runtimePlugins) + .protocolGenerator(protocolGenerator) + .applicationProtocol(applicationProtocol) + .build()); // Section for adding custom command properties. writer.injectSection(CommandPropertiesCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .operation(operation) - .symbolProvider(symbolProvider) - .runtimeClientPlugins(runtimePlugins) - .protocolGenerator(protocolGenerator) - .applicationProtocol(applicationProtocol) - .build()); + .settings(settings) + .model(model) + .service(service) + .operation(operation) + .symbolProvider(symbolProvider) + .runtimeClientPlugins(runtimePlugins) + .protocolGenerator(protocolGenerator) + .applicationProtocol(applicationProtocol) + .build()); // Hook for adding more methods to the command. writer.injectSection(CommandBodyExtraCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .operation(operation) - .symbolProvider(symbolProvider) - .runtimeClientPlugins(runtimePlugins) - .protocolGenerator(protocolGenerator) - .applicationProtocol(applicationProtocol) - .build()); + .settings(settings) + .model(model) + .service(service) + .operation(operation) + .symbolProvider(symbolProvider) + .runtimeClientPlugins(runtimePlugins) + .protocolGenerator(protocolGenerator) + .applicationProtocol(applicationProtocol) + .build()); { // This block places the most commonly sought type definitions @@ -255,21 +250,21 @@ export class $L extends $$Command writer.write("/** @internal type navigation helper, not in runtime. */"); writer.openBlock("protected declare static __types: {", "};", () -> { String baseInputStr = operationInputShape.getAllMembers().isEmpty() - ? "{}" - : baseInput.getName(); + ? "{}" + : baseInput.getName(); String baseOutputStr = operationOutputShape.getAllMembers().isEmpty() - ? "{}" - : baseOutput.getName(); + ? "{}" + : baseOutput.getName(); writer.write(""" - api: { - input: $L; - output: $L; - };""", baseInputStr, baseOutputStr); + api: { + input: $L; + output: $L; + };""", baseInputStr, baseOutputStr); writer.write(""" - sdk: { - input: $T; - output: $T; - };""", inputType, outputType); + sdk: { + input: $T; + output: $T; + };""", inputType, outputType); }); writer.dedent(); } @@ -278,37 +273,50 @@ export class $L extends $$Command } private String getCommandExample( - String serviceName, String configName, String commandName, - String commandInput, String commandOutput + String serviceName, + String configName, + String commandName, + String commandInput, + String commandOutput ) { String packageName = settings.getPackageName(); String exampleDoc = "@example\n" - + "Use a bare-bones client and the command you need to make an API call.\n" - + "```javascript\n" - + String.format("import { %s, %s } from \"%s\"; // ES Modules import%n", serviceName, commandName, - packageName) - + String.format("// const { %s, %s } = require(\"%s\"); // CommonJS import%n", serviceName, commandName, - packageName) - + String.format("// import type { %sConfig } from \"%s\";%n", serviceName, packageName) - + String.format("const config = {}; // type is %sConfig%n", serviceName) - + String.format("const client = new %s(config);%n", serviceName) - + String.format("const input = %s%n", - StructureExampleGenerator.generateStructuralHintDocumentation( - model.getShape(operation.getInputShape()).get(), model, false, true)) - + String.format("const command = new %s(input);%n", commandName) - + "const response = await client.send(command);" - + getStreamingBlobOutputAddendum() - + "\n" - + String.format("%s%n", - StructureExampleGenerator.generateStructuralHintDocumentation( - model.getShape(operation.getOutputShape()).get(), model, true, false)) - + "\n```\n" - + "\n" - + String.format("@param %s - {@link %s}%n", commandInput, commandInput) - + String.format("@returns {@link %s}%n", commandOutput) - + String.format("@see {@link %s} for command's `input` shape.%n", commandInput) - + String.format("@see {@link %s} for command's `response` shape.%n", commandOutput) - + String.format("@see {@link %s | config} for %s's `config` shape.%n", configName, serviceName); + + "Use a bare-bones client and the command you need to make an API call.\n" + + "```javascript\n" + + String.format("import { %s, %s } from \"%s\"; // ES Modules import%n", + serviceName, + commandName, + packageName) + + String.format("// const { %s, %s } = require(\"%s\"); // CommonJS import%n", + serviceName, + commandName, + packageName) + + String.format("// import type { %sConfig } from \"%s\";%n", serviceName, packageName) + + String.format("const config = {}; // type is %sConfig%n", serviceName) + + String.format("const client = new %s(config);%n", serviceName) + + String.format("const input = %s%n", + StructureExampleGenerator.generateStructuralHintDocumentation( + model.getShape(operation.getInputShape()).get(), + model, + false, + true)) + + String.format("const command = new %s(input);%n", commandName) + + "const response = await client.send(command);" + + getStreamingBlobOutputAddendum() + + "\n" + + String.format("%s%n", + StructureExampleGenerator.generateStructuralHintDocumentation( + model.getShape(operation.getOutputShape()).get(), + model, + true, + false)) + + "\n```\n" + + "\n" + + String.format("@param %s - {@link %s}%n", commandInput, commandInput) + + String.format("@returns {@link %s}%n", commandOutput) + + String.format("@see {@link %s} for command's `input` shape.%n", commandInput) + + String.format("@see {@link %s} for command's `response` shape.%n", commandOutput) + + String.format("@see {@link %s | config} for %s's `config` shape.%n", configName, serviceName); return exampleDoc; } @@ -326,28 +334,27 @@ private String getCuratedExamples(String commandName) { ObjectNode input = example.getInput(); Optional output = example.getOutput(); buffer - .append("\n") - .append(String.format("@example %s%n", example.getTitle())) - .append("```javascript\n") - .append(String.format("// %s%n", example.getDocumentation().orElse(""))) - .append(""" - const input = %s; - const command = new %s(input); - const response = await client.send(command);%s - /* response is - %s - */ - """.formatted( - DocumentationExampleGenerator.inputToJavaScriptObject(input), - commandName, - getStreamingBlobOutputAddendum(), - DocumentationExampleGenerator.outputToJavaScriptObject(output.orElse(null)) - )) - .append("```") - .append("\n"); + .append("\n") + .append(String.format("@example %s%n", example.getTitle())) + .append("```javascript\n") + .append(String.format("// %s%n", example.getDocumentation().orElse(""))) + .append(""" + const input = %s; + const command = new %s(input); + const response = await client.send(command);%s + /* response is + %s + */ + """.formatted( + DocumentationExampleGenerator.inputToJavaScriptObject(input), + commandName, + getStreamingBlobOutputAddendum(), + DocumentationExampleGenerator.outputToJavaScriptObject(output.orElse(null)))) + .append("```") + .append("\n"); } - exampleDoc += buffer.toString(); + exampleDoc += buffer.toString(); } return exampleDoc; } @@ -358,19 +365,17 @@ private String getCuratedExamples(String commandName) { */ private String getStreamingBlobOutputMember(OperationShape operation) { return (model.expectShape(operation.getOutputShape())) - .getAllMembers() - .values() - .stream() - .filter(memberShape -> { - Shape target = model.expectShape(memberShape.getTarget()); - return target.isBlobShape() && ( - target.hasTrait(StreamingTrait.class) - || memberShape.hasTrait(StreamingTrait.class) - ); - }) - .map(MemberShape::getMemberName) - .findFirst() - .orElse(""); + .getAllMembers() + .values() + .stream() + .filter(memberShape -> { + Shape target = model.expectShape(memberShape.getTarget()); + return target.isBlobShape() && (target.hasTrait(StreamingTrait.class) + || memberShape.hasTrait(StreamingTrait.class)); + }) + .map(MemberShape::getMemberName) + .findFirst() + .orElse(""); } /** @@ -387,10 +392,9 @@ private String getStreamingBlobOutputAddendum() { // const str = await %s.transformToString(); // %s.destroy(); // only applicable to Node.js Readable streams. """.formatted( - propAccess, - propAccess, - propAccess - ); + propAccess, + propAccess, + propAccess); } return streamingBlobAddendum; } @@ -405,10 +409,13 @@ private String getThrownExceptions() { if (doc.isPresent()) { buffer.append(String.format("@throws {@link %s} (%s fault)%n %s", - error.getName(), errorTrait.getValue(), doc.get().getValue())); + error.getName(), + errorTrait.getValue(), + doc.get().getValue())); } else { buffer.append(String.format("@throws {@link %s} (%s fault)", - error.getName(), errorTrait.getValue())); + error.getName(), + errorTrait.getValue())); } buffer.append("\n\n"); } @@ -422,15 +429,14 @@ private String getThrownExceptions() { private void generateEndpointParameterInstructionProvider() { writer.addImport( - "commonParams", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "endpoint/EndpointParameters").toString() - ); + "commonParams", + null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "endpoint/EndpointParameters").toString()); RuleSetParameterFinder parameterFinder = new RuleSetParameterFinder(service); Map staticContextParamValues = parameterFinder.getStaticContextParamValues(operation); Map contextParams = parameterFinder.getContextParams( - model.getShape(operation.getInputShape()).get() - ); + model.getShape(operation.getInputShape()).get()); Map operationContextParamValues = parameterFinder.getOperationContextParamValues(operation); if (staticContextParamValues.isEmpty() && contextParams.isEmpty() && operationContextParamValues.isEmpty()) { @@ -439,7 +445,7 @@ private void generateEndpointParameterInstructionProvider() { } writer.write(".ep({") - .indent(); + .indent(); { writer.write("...commonParams,"); Set paramNames = new HashSet<>(); @@ -447,29 +453,32 @@ private void generateEndpointParameterInstructionProvider() { staticContextParamValues.forEach((name, value) -> { paramNames.add(name); writer.write( - "$L: { type: \"staticContextParams\", value: $L },", - name, value); + "$L: { type: \"staticContextParams\", value: $L },", + name, + value); }); contextParams.forEach((name, memberName) -> { if (!paramNames.contains(name)) { writer.write( - "$L: { type: \"contextParams\", name: \"$L\" },", - name, memberName); + "$L: { type: \"contextParams\", name: \"$L\" },", + name, + memberName); } paramNames.add(name); }); operationContextParamValues.forEach((name, jmesPathForInputInJs) -> { writer.write( - """ - $L: { type: "operationContextParams", get: (input?: any) => $L }, - """, - name, jmesPathForInputInJs); + """ + $L: { type: "operationContextParams", get: (input?: any) => $L }, + """, + name, + jmesPathForInputInJs); }); } writer.write("})") - .dedent(); + .dedent(); } private void generateCommandMiddlewareResolver(String configType) { @@ -481,49 +490,48 @@ private void generateCommandMiddlewareResolver(String configType) { Symbol inputSymbol = symbolProvider.toSymbol(input); String filterFunctionName = inputSymbol.getName() + "FilterSensitiveLog"; writer.addRelativeImport( - filterFunctionName, - null, - Paths.get(".", inputSymbol.getNamespace())); + filterFunctionName, + null, + Paths.get(".", inputSymbol.getNamespace())); return filterFunctionName; } return "void 0"; }; String inputFilterFn = operationIndex - .getInput(operation) - .map(getFilterFunctionName) - .orElse("void 0"); + .getInput(operation) + .map(getFilterFunctionName) + .orElse("void 0"); String outputFilterFn = operationIndex - .getOutput(operation) - .map(getFilterFunctionName) - .orElse("void 0"); + .getOutput(operation) + .map(getFilterFunctionName) + .orElse("void 0"); writer.pushState() - .putContext("client", symbolProvider.toSymbol(service).getName()) - .putContext("command", symbolProvider.toSymbol(operation).getName()) - .putContext("service", service.toShapeId().getName()) - .putContext("operation", operation.toShapeId().getName()) - .putContext("inputFilter", inputFilterFn) - .putContext("outputFilter", outputFilterFn) - .putContext("configType", configType) - .putContext("optionsType", applicationProtocol.getOptionsType()) - .putContext("inputType", inputType) - .putContext("outputType", outputType); + .putContext("client", symbolProvider.toSymbol(service).getName()) + .putContext("command", symbolProvider.toSymbol(operation).getName()) + .putContext("service", service.toShapeId().getName()) + .putContext("operation", operation.toShapeId().getName()) + .putContext("inputFilter", inputFilterFn) + .putContext("outputFilter", outputFilterFn) + .putContext("configType", configType) + .putContext("optionsType", applicationProtocol.getOptionsType()) + .putContext("inputType", inputType) + .putContext("outputType", outputType); writer.writeInline( - """ - .m(function (this: any, Command: any, cs: any, config: $configType:L, o: any) { - return [""" - ); + """ + .m(function (this: any, Command: any, cs: any, config: $configType:L, o: any) { + return ["""); { boolean multiplePlugins = !schemaMode || runtimePlugins.stream() - .map(RuntimeClientPlugin::getPluginFunction) - .anyMatch(Optional::isPresent); + .map(RuntimeClientPlugin::getPluginFunction) + .anyMatch(Optional::isPresent); writer.addImport( - "getEndpointPlugin", - null, - TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2); + "getEndpointPlugin", + null, + TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2); if (multiplePlugins) { writer.write(""); @@ -535,10 +543,10 @@ private void generateCommandMiddlewareResolver(String configType) { writer.dedent(); } - writer.indent().write( - """ - getEndpointPlugin(config, Command.getEndpointParameterInstructions()),""" - ); + writer.indent() + .write( + """ + getEndpointPlugin(config, Command.getEndpointParameterInstructions()),"""); // Add customizations. addCommandSpecificPlugins(); writer.dedent(); @@ -546,9 +554,8 @@ private void generateCommandMiddlewareResolver(String configType) { writer.dedent(); } else { writer.writeInline( - """ - getEndpointPlugin(config, Command.getEndpointParameterInstructions())""" - ); + """ + getEndpointPlugin(config, Command.getEndpointParameterInstructions())"""); writer.write("];"); // end middleware list. } } @@ -559,25 +566,22 @@ private void generateCommandMiddlewareResolver(String configType) { // context, filters writer.writeInline( """ - .s($service:S, $operation:S, {""" - ) - .pushState( - SmithyContextCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .operation(operation) - .symbolProvider(symbolProvider) - .runtimeClientPlugins(runtimePlugins) - .protocolGenerator(protocolGenerator) - .applicationProtocol(applicationProtocol) - .build() - ) - .popState() - .write("})") - .write(""" - .n($client:S, $command:S)""" - ); + .s($service:S, $operation:S, {""") + .pushState( + SmithyContextCodeSection.builder() + .settings(settings) + .model(model) + .service(service) + .operation(operation) + .symbolProvider(symbolProvider) + .runtimeClientPlugins(runtimePlugins) + .protocolGenerator(protocolGenerator) + .applicationProtocol(applicationProtocol) + .build()) + .popState() + .write("})") + .write(""" + .n($client:S, $command:S)"""); if (!filters.isEmpty()) { writer.write(filters); } @@ -601,22 +605,25 @@ private void writeInputType(String typeName, Optional inputShape if (!blobStreamingMembers.isEmpty()) { writeClientCommandStreamingInputType( - writer, symbolProvider.toSymbol(input), typeName, - blobStreamingMembers.get(0), commandName - ); + writer, + symbolProvider.toSymbol(input), + typeName, + blobStreamingMembers.get(0), + commandName); } else if (!blobPayloadMembers.isEmpty()) { writeClientCommandBlobPayloadInputType( - writer, symbolProvider.toSymbol(input), typeName, - blobPayloadMembers.get(0), commandName - ); + writer, + symbolProvider.toSymbol(input), + typeName, + blobPayloadMembers.get(0), + commandName); } else { writer.writeDocs("@public\n\nThe input for {@link " + commandName + "}."); Symbol inputSymbol = symbolProvider.toSymbol(input); writer.addRelativeTypeImport( - inputSymbol.getName(), - null, - Path.of(inputSymbol.getNamespace()) - ); + inputSymbol.getName(), + null, + Path.of(inputSymbol.getNamespace())); writer.write("export interface $L extends $L {}", typeName, inputSymbol.getName()); } } else { @@ -637,24 +644,28 @@ private void writeOutputType(String typeName, Optional outputSha if (!blobStreamingMembers.isEmpty()) { writeClientCommandStreamingOutputType( - writer, symbolProvider.toSymbol(output), typeName, - blobStreamingMembers.get(0), commandName - ); + writer, + symbolProvider.toSymbol(output), + typeName, + blobStreamingMembers.get(0), + commandName); } else if (!blobPayloadMembers.isEmpty()) { writeClientCommandBlobPayloadOutputType( - writer, symbolProvider.toSymbol(output), typeName, - blobPayloadMembers.get(0), commandName - ); + writer, + symbolProvider.toSymbol(output), + typeName, + blobPayloadMembers.get(0), + commandName); } else { writer.writeDocs("@public\n\nThe output of {@link " + commandName + "}."); Symbol outputSymbol = symbolProvider.toSymbol(output); writer.addRelativeTypeImport( - outputSymbol.getName(), - null, - Path.of(outputSymbol.getNamespace()) - ); + outputSymbol.getName(), + null, + Path.of(outputSymbol.getNamespace())); writer.write("export interface $L extends $L, __MetadataBearer {}", - typeName, outputSymbol.getName()); + typeName, + outputSymbol.getName()); } } else { writer.writeDocs("@public\n\nThe output of {@link " + commandName + "}."); @@ -671,7 +682,9 @@ private void addCommandSpecificPlugins() { plugin.getPluginFunction().ifPresent(pluginSymbol -> { // Construct additional parameters string Map paramsMap = plugin.getAdditionalPluginFunctionParameters( - model, service, operation); + model, + service, + operation); // Construct writer context Map symbolMap = new HashMap<>(); @@ -686,7 +699,7 @@ private void addCommandSpecificPlugins() { writer.openBlock("$pluginFn:T(config", "),", () -> { List additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap); Map clientAddParamsWriterConsumers = - plugin.getOperationAddParamsWriterConsumers(); + plugin.getOperationAddParamsWriterConsumers(); if (additionalParameters.isEmpty() && clientAddParamsWriterConsumers.isEmpty()) { return; } @@ -698,14 +711,15 @@ private void addCommandSpecificPlugins() { } clientAddParamsWriterConsumers.forEach((key, consumer) -> { writer.writeInline("$L: $C,", key, (Consumer) (w -> { - consumer.accept(w, CommandConstructorCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .symbolProvider(symbolProvider) - .runtimeClientPlugins(runtimePlugins) - .applicationProtocol(applicationProtocol) - .build()); + consumer.accept(w, + CommandConstructorCodeSection.builder() + .settings(settings) + .model(model) + .service(service) + .symbolProvider(symbolProvider) + .runtimeClientPlugins(runtimePlugins) + .applicationProtocol(applicationProtocol) + .build()); })); }); }); @@ -717,13 +731,16 @@ private void addCommandSpecificPlugins() { private void writeSchemaSerde() { String operationSchema = reservedWords.escape(operation.getId().getName()); - writer.addRelativeImport(operationSchema, null, Paths.get( - ".", CodegenUtils.SOURCE_FOLDER, SCHEMAS_FOLDER, "schemas_0" - )); + writer.addRelativeImport(operationSchema, + null, + Paths.get( + ".", + CodegenUtils.SOURCE_FOLDER, + SCHEMAS_FOLDER, + "schemas_0")); writer.write(""" - .sc($L)""", - operationSchema - ); + .sc($L)""", + operationSchema); } private void writeSerde() { @@ -731,8 +748,8 @@ private void writeSerde() { writeSchemaSerde(); } else { writer - .write(".ser($L)", getSerdeDispatcher(true)) - .write(".de($L)", getSerdeDispatcher(false)); + .write(".ser($L)", getSerdeDispatcher(true)) + .write(".de($L)", getSerdeDispatcher(false)); } } @@ -741,11 +758,14 @@ private String getSerdeDispatcher(boolean isInput) { return "() => { throw new Error(\"No supported protocol was found\"); }"; } else { String serdeFunctionName = isInput - ? ProtocolGenerator.getSerFunctionShortName(symbol) - : ProtocolGenerator.getDeserFunctionShortName(symbol); - writer.addRelativeImport(serdeFunctionName, null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER, - ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); + ? ProtocolGenerator.getSerFunctionShortName(symbol) + : ProtocolGenerator.getDeserFunctionShortName(symbol); + writer.addRelativeImport(serdeFunctionName, + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + ProtocolGenerator.PROTOCOLS_FOLDER, + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); return serdeFunctionName; } } @@ -754,7 +774,8 @@ static void writeIndex( Model model, ServiceShape service, SymbolProvider symbolProvider, - FileManifest fileManifest) { + FileManifest fileManifest + ) { TypeScriptWriter writer = new TypeScriptWriter(""); TopDownIndex topDownIndex = TopDownIndex.of(model); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/Dependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/Dependency.java index 1b618a820db..61799c0f4de 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/Dependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/Dependency.java @@ -1,21 +1,9 @@ /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import software.amazon.smithy.codegen.core.SymbolDependencyContainer; -public interface Dependency extends PackageContainer, SymbolDependencyContainer { -} +public interface Dependency extends PackageContainer, SymbolDependencyContainer {} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java index a86a37db45f..53992b7900b 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/DirectedTypeScriptCodegen.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -73,11 +62,14 @@ final class DirectedTypeScriptCodegen * A mapping of static resource files to copy over to a new filename. */ private static final Map STATIC_FILE_COPIES = MapUtils.of( - "tsconfig.json", "tsconfig.json", - "tsconfig.cjs.json", "tsconfig.cjs.json", - "tsconfig.es.json", "tsconfig.es.json", - "tsconfig.types.json", "tsconfig.types.json" - ); + "tsconfig.json", + "tsconfig.json", + "tsconfig.cjs.json", + "tsconfig.cjs.json", + "tsconfig.es.json", + "tsconfig.es.json", + "tsconfig.types.json", + "tsconfig.types.json"); private static final ShapeId VALIDATION_EXCEPTION_SHAPE = ShapeId.fromParts("smithy.framework", "ValidationException"); @@ -87,8 +79,10 @@ public SymbolProvider createSymbolProvider(CreateSymbolProviderDirective directive) { + public TypeScriptCodegenContext createContext( + CreateContextDirective directive + ) { List runtimePlugins = new ArrayList<>(); directive.integrations().forEach(integration -> { @@ -171,7 +165,7 @@ public void generateService(GenerateServiceDirective { ProtocolGenerator.GenerationContext context = new ProtocolGenerator.GenerationContext(); @@ -251,41 +246,61 @@ private void generateClient(GenerateServiceDirective new ServiceBareBonesClientGenerator( - settings, model, symbolProvider, writer, integrations, runtimePlugins, applicationProtocol).run()); + delegator.useShapeWriter(service, + writer -> new ServiceBareBonesClientGenerator( + settings, + model, + symbolProvider, + writer, + integrations, + runtimePlugins, + applicationProtocol).run()); if (!directive.settings().useLegacyAuth()) { new HttpAuthSchemeProviderGenerator( - delegator, - settings, - model, - symbolProvider, - directive.context().integrations() - ).run(); + delegator, + settings, + model, + symbolProvider, + directive.context().integrations()).run(); } // Generate the aggregated service client. Symbol serviceSymbol = symbolProvider.toSymbol(service); String aggregatedClientName = ReplaceLast.in(serviceSymbol.getName(), "Client", ""); String filename = ReplaceLast.in(serviceSymbol.getDefinitionFile(), "Client", ""); - delegator.useFileWriter(filename, writer -> new ServiceAggregatedClientGenerator( - settings, model, symbolProvider, aggregatedClientName, writer, applicationProtocol).run()); + delegator.useFileWriter(filename, + writer -> new ServiceAggregatedClientGenerator( + settings, + model, + symbolProvider, + aggregatedClientName, + writer, + applicationProtocol).run()); // Generate each operation for the service. Set containedOperations = directive.operations(); for (OperationShape operation : containedOperations) { if (operation.hasTrait(PaginatedTrait.ID)) { String outputFilename = PaginationGenerator.getOutputFilelocation(operation); - delegator.useFileWriter(outputFilename, paginationWriter -> - new PaginationGenerator(model, service, operation, symbolProvider, paginationWriter, + delegator.useFileWriter(outputFilename, + paginationWriter -> new PaginationGenerator(model, + service, + operation, + symbolProvider, + paginationWriter, aggregatedClientName).run()); } if (operation.hasTrait(WaitableTrait.ID)) { WaitableTrait waitableTrait = operation.expectTrait(WaitableTrait.class); waitableTrait.getWaiters().forEach((String waiterName, Waiter waiter) -> { String outputFilename = WaiterGenerator.getOutputFileLocation(waiterName); - delegator.useFileWriter(outputFilename, waiterWriter -> - new WaiterGenerator(waiterName, waiter, service, operation, waiterWriter, + delegator.useFileWriter(outputFilename, + waiterWriter -> new WaiterGenerator(waiterName, + waiter, + service, + operation, + waiterWriter, symbolProvider).run()); }); } @@ -295,8 +310,8 @@ private void generateClient(GenerateServiceDirective operation.hasTrait(PaginatedTrait.ID))) { PaginationGenerator.writeIndex(model, service, fileManifest); - delegator.useFileWriter(PaginationGenerator.PAGINATION_INTERFACE_FILE, paginationWriter -> - PaginationGenerator.generateServicePaginationInterfaces( + delegator.useFileWriter(PaginationGenerator.PAGINATION_INTERFACE_FILE, + paginationWriter -> PaginationGenerator.generateServicePaginationInterfaces( aggregatedClientName, serviceSymbol, paginationWriter)); @@ -330,15 +345,28 @@ private void generateCommands(GenerateServiceDirective new CommandGenerator( - settings, model, operation, symbolProvider, commandWriter, - runtimePlugins, protocolGenerator, applicationProtocol).run()); + delegator.useShapeWriter(operation, + commandWriter -> new CommandGenerator( + settings, + model, + operation, + symbolProvider, + commandWriter, + runtimePlugins, + protocolGenerator, + applicationProtocol).run()); } if (settings.generateServerSdk()) { - delegator.useShapeWriter(operation, commandWriter -> new ServerCommandGenerator( - settings, model, operation, symbolProvider, commandWriter, - protocolGenerator, applicationProtocol).run()); + delegator.useShapeWriter(operation, + commandWriter -> new ServerCommandGenerator( + settings, + model, + operation, + symbolProvider, + commandWriter, + protocolGenerator, + applicationProtocol).run()); } } } @@ -347,8 +375,10 @@ private void generateEndpointV2(GenerateServiceDirective directive) { + private void generateServiceInterface( + GenerateServiceDirective directive + ) { ServiceShape service = directive.shape(); SymbolProvider symbolProvider = directive.symbolProvider(); Set operations = directive.operations(); @@ -364,17 +394,15 @@ private void generateServiceInterface(GenerateServiceDirective directive) { directive.context().writerDelegator().useShapeWriter(directive.shape(), writer -> { StructureGenerator generator = new StructureGenerator( - directive.model(), - directive.symbolProvider(), - writer, - directive.shape(), - directive.settings().generateServerSdk(), - directive.settings().getRequiredMemberMode(), - SchemaGenerationAllowlist.allows( - directive.settings().getService(), - directive.settings() - ) - ); + directive.model(), + directive.symbolProvider(), + writer, + directive.shape(), + directive.settings().generateServerSdk(), + directive.settings().getRequiredMemberMode(), + SchemaGenerationAllowlist.allows( + directive.settings().getService(), + directive.settings())); generator.run(); }); } @@ -383,17 +411,15 @@ public void generateStructure(GenerateStructureDirective directive) { directive.context().writerDelegator().useShapeWriter(directive.shape(), writer -> { StructureGenerator generator = new StructureGenerator( - directive.model(), - directive.symbolProvider(), - writer, - directive.shape(), - directive.settings().generateServerSdk(), - directive.settings().getRequiredMemberMode(), - SchemaGenerationAllowlist.allows( - directive.settings().getService(), - directive.settings() - ) - ); + directive.model(), + directive.symbolProvider(), + writer, + directive.shape(), + directive.settings().generateServerSdk(), + directive.settings().getRequiredMemberMode(), + SchemaGenerationAllowlist.allows( + directive.settings().getService(), + directive.settings())); generator.run(); }); } @@ -402,16 +428,14 @@ public void generateError(GenerateErrorDirective directive) { directive.context().writerDelegator().useShapeWriter(directive.shape(), writer -> { UnionGenerator generator = new UnionGenerator( - directive.model(), - directive.symbolProvider(), - writer, - directive.shape(), - directive.settings().generateServerSdk(), - SchemaGenerationAllowlist.allows( - directive.settings().getService(), - directive.settings() - ) - ); + directive.model(), + directive.symbolProvider(), + writer, + directive.shape(), + directive.settings().generateServerSdk(), + SchemaGenerationAllowlist.allows( + directive.settings().getService(), + directive.settings())); generator.run(); }); } @@ -422,8 +446,7 @@ public void generateEnumShape(GenerateEnumDirective directive) { + CustomizeDirective directive + ) { // Write shared / static content. STATIC_FILE_COPIES.forEach((from, to) -> { LOGGER.fine(() -> "Writing contents of `" + from + "` to `" + to + "`"); @@ -450,9 +473,8 @@ public void customizeBeforeIntegrations( }); TypeScriptWriter modelIndexer = SymbolVisitor.modelIndexer( - directive.connectedShapes().values(), - directive.symbolProvider() - ); + directive.connectedShapes().values(), + directive.symbolProvider()); // Generate the client Node and Browser configuration files. These // files are switched between in package.json based on the targeted @@ -472,65 +494,59 @@ public void customizeBeforeIntegrations( configGenerator.generate(target); } new ExtensionConfigurationGenerator( - directive.model(), - directive.settings(), - directive.service(), - directive.symbolProvider(), - directive.context().writerDelegator(), - directive.context().integrations() - ).generate(); + directive.model(), + directive.settings(), + directive.service(), + directive.symbolProvider(), + directive.context().writerDelegator(), + directive.context().integrations()).generate(); new RuntimeExtensionsGenerator( - directive.model(), - directive.settings(), - directive.service(), - directive.symbolProvider(), - directive.context().writerDelegator(), - directive.context().integrations() - ).generate(); + directive.model(), + directive.settings(), + directive.service(), + directive.symbolProvider(), + directive.context().writerDelegator(), + directive.context().integrations()).generate(); } // Generate index for client. BiConsumer> writerFactory = - directive.context().writerDelegator()::useFileWriter; + directive.context().writerDelegator()::useFileWriter; writerFactory.accept(Paths.get(CodegenUtils.SOURCE_FOLDER, "index.ts").toString(), writer -> { IndexGenerator.writeIndex( - directive.settings(), - directive.model(), - directive.symbolProvider(), - directive.context().protocolGenerator(), - writer, - modelIndexer - ); + directive.settings(), + directive.model(), + directive.symbolProvider(), + directive.context().protocolGenerator(), + writer, + modelIndexer); }); if (directive.settings().generateClient() && directive.settings().generateIndexTests()) { writerFactory.accept(Paths.get(CodegenUtils.TEST_FOLDER, "index-types.ts").toString(), writer -> { new PackageApiValidationGenerator( - writer, - directive.settings(), - directive.model(), - directive.symbolProvider() - ).writeTypeIndexTest(); + writer, + directive.settings(), + directive.model(), + directive.symbolProvider()).writeTypeIndexTest(); }); writerFactory.accept(Paths.get(CodegenUtils.TEST_FOLDER, "index-objects.spec.mjs").toString(), writer -> { new PackageApiValidationGenerator( - writer, - directive.settings(), - directive.model(), - directive.symbolProvider() - ).writeRuntimeIndexTest(); + writer, + directive.settings(), + directive.model(), + directive.symbolProvider()).writeRuntimeIndexTest(); }); } if (directive.settings().generateServerSdk()) { // Generate index for server IndexGenerator.writeServerIndex( - directive.settings(), - directive.model(), - directive.symbolProvider(), - directive.fileManifest() - ); + directive.settings(), + directive.model(), + directive.symbolProvider(), + directive.fileManifest()); } // Generate protocol tests IFF found in the model. @@ -558,7 +574,8 @@ private void checkValidationSettings(TypeScriptSettings settings, Model model, S List unvalidatedOperations = TopDownIndex.of(model) .getContainedOperations(service) .stream() - .filter(o -> operationIndex.getErrors(o, service).stream() + .filter(o -> operationIndex.getErrors(o, service) + .stream() .noneMatch(e -> e.getId().equals(VALIDATION_EXCEPTION_SHAPE))) .map(s -> s.getId().toString()) .sorted() @@ -566,8 +583,8 @@ private void checkValidationSettings(TypeScriptSettings settings, Model model, S if (!unvalidatedOperations.isEmpty()) { throw new CodegenException(String.format("Every operation must have the %s error attached unless %s is set " - + "to 'true' in the plugin settings. Operations without %s " - + "errors attached: %s", + + "to 'true' in the plugin settings. Operations without %s " + + "errors attached: %s", VALIDATION_EXCEPTION_SHAPE, TypeScriptSettings.DISABLE_DEFAULT_VALIDATION, VALIDATION_EXCEPTION_SHAPE, diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/EnumGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/EnumGenerator.java index 9e5bff0e5e7..97140fa170d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/EnumGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/EnumGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.Comparator; @@ -83,24 +72,25 @@ public void run() { private void generateUnnamedEnum() { String variants = TypeScriptUtils.getEnumVariants(enumTrait.getEnumDefinitionValues()); writer.writeDocs("@public") - .write("export type $L = $L", symbol.getName(), variants); + .write("export type $L = $L", symbol.getName(), variants); } // Named enums generate an actual enum type. private void generateNamedEnum() { writer.writeDocs("@public\n@enum") - .openBlock("export const $L = {", "} as const;", symbol.getName(), () -> { - // Sort the named values to ensure a stable order and sane diffs. - // TODO: Should we just sort these in the trait itself? - enumTrait.getValues() - .stream() - .sorted(Comparator.comparing(e -> e.getName().get())) - .forEach(this::writeNamedEnumConstant); - }); + .openBlock("export const $L = {", "} as const;", symbol.getName(), () -> { + // Sort the named values to ensure a stable order and sane diffs. + // TODO: Should we just sort these in the trait itself? + enumTrait.getValues() + .stream() + .sorted(Comparator.comparing(e -> e.getName().get())) + .forEach(this::writeNamedEnumConstant); + }); writer.writeDocs("@public") - .write("export type $L = (typeof $L)[keyof typeof $L];", - symbol.getName(), symbol.getName(), symbol.getName() - ); + .write("export type $L = (typeof $L)[keyof typeof $L];", + symbol.getName(), + symbol.getName(), + symbol.getName()); } private void writeNamedEnumConstant(EnumDefinition body) { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java index 280da317112..d9e8e94dfc9 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ExtensionConfigurationGenerator.java @@ -1,18 +1,7 @@ /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -38,12 +27,12 @@ public class ExtensionConfigurationGenerator { private final List integrations; public ExtensionConfigurationGenerator( - Model model, - TypeScriptSettings settings, - ServiceShape service, - SymbolProvider symbolProvider, - TypeScriptDelegator delegator, - List integrations + Model model, + TypeScriptSettings settings, + ServiceShape service, + SymbolProvider symbolProvider, + TypeScriptDelegator delegator, + List integrations ) { this.model = model; this.settings = settings; @@ -64,17 +53,17 @@ void generate() { } String clientName = ReplaceLast.in( - ReplaceLast.in( - symbolProvider.toSymbol(service).getName(), - "Client", "" - ), - "client", "" - ); + ReplaceLast.in( + symbolProvider.toSymbol(service).getName(), + "Client", + ""), + "client", + ""); String clientConfigurationContent = TypeScriptUtils - .loadResourceAsString(CLIENT_CONFIGURATION_TEMPLATE) - .replace("${extensionConfigName}", clientName + "ExtensionConfiguration") - .replace("${extensionConfigInterfaces}", String.join(",\n ", interfaces.keySet())); + .loadResourceAsString(CLIENT_CONFIGURATION_TEMPLATE) + .replace("${extensionConfigName}", clientName + "ExtensionConfiguration") + .replace("${extensionConfigInterfaces}", String.join(",\n ", interfaces.keySet())); delegator.useFileWriter(Paths.get(CodegenUtils.SOURCE_FOLDER, FILENAME).toString(), writer -> { interfaces.entrySet().forEach(entry -> { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/FrameworkErrorModel.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/FrameworkErrorModel.java index cbf14b0c755..375210abaf2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/FrameworkErrorModel.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/FrameworkErrorModel.java @@ -1,18 +1,7 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import software.amazon.smithy.model.Model; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java index 289232d77d8..449d7c67ceb 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import static java.lang.String.format; @@ -108,10 +97,10 @@ public final class HttpProtocolTestGenerator implements Runnable { private TypeScriptWriter writer; public HttpProtocolTestGenerator( - GenerationContext context, - ProtocolGenerator protocolGenerator, - TestFilter testFilter, - MalformedRequestTestFilter malformedRequestTestFilter + GenerationContext context, + ProtocolGenerator protocolGenerator, + TestFilter testFilter, + MalformedRequestTestFilter malformedRequestTestFilter ) { this.settings = context.getSettings(); this.model = context.getModel(); @@ -120,9 +109,9 @@ public HttpProtocolTestGenerator( this.symbolProvider = context.getSymbolProvider(); this.protocolGenerator = protocolGenerator; serviceSymbol = symbolProvider.toSymbol(service) - .toBuilder() - .putProperty("typeOnly", false) - .build(); + .toBuilder() + .putProperty("typeOnly", false) + .build(); this.testFilter = testFilter; this.malformedRequestTestFilter = malformedRequestTestFilter; this.context = context; @@ -169,7 +158,7 @@ private void generateClientOperationTests(OperationShape operation, OperationInd error.getTrait(HttpResponseTestsTrait.class).ifPresent(trait -> { for (HttpResponseTestCase testCase : trait.getTestCasesFor(AppliesTo.CLIENT)) { onlyIfProtocolMatches(testCase, - () -> generateErrorResponseTest(operation, error, testCase)); + () -> generateErrorResponseTest(operation, error, testCase)); } }); } @@ -203,7 +192,7 @@ private void generateServerOperationTests(OperationShape operation, OperationInd error.getTrait(HttpResponseTestsTrait.class).ifPresent(trait -> { for (HttpResponseTestCase testCase : trait.getTestCasesFor(AppliesTo.SERVER)) { onlyIfProtocolMatches(testCase, - () -> generateServerErrorResponseTest(operation, error, testCase)); + () -> generateServerErrorResponseTest(operation, error, testCase)); } }); } @@ -224,7 +213,8 @@ private void onlyIfProtocolMatches(T testCase, R private void onlyIfProtocolMatches(HttpMalformedRequestTestCase testCase, Runnable runnable) { if (testCase.getProtocol().equals(protocol)) { LOGGER.fine(() -> format("Generating malformed request test case for %s.%s", - service.getId(), testCase.getId())); + service.getId(), + testCase.getId())); initializeWriterIfNeeded(); runnable.run(); } @@ -243,9 +233,10 @@ private void initializeWriterIfNeeded() { } private String createTestCaseFilename() { - String baseName = protocol.getName().toLowerCase(Locale.US) - .replace("-", "_") - .replace(".", "_"); + String baseName = protocol.getName() + .toLowerCase(Locale.US) + .replace("-", "_") + .replace(".", "_"); return TEST_CASE_FILE_TEMPLATE.replace("%s", baseName); } @@ -270,10 +261,10 @@ private void generateClientRequestTest(OperationShape operation, HttpRequestTest if (inputOptional.isPresent()) { StructureShape inputShape = model.expectShape(inputOptional.get(), StructureShape.class); writer.write("const command = new $T(", operationSymbol) - .indent() - .call(() -> params.accept(new CommandInputNodeVisitor(inputShape))) - .dedent() - .write(");"); + .indent() + .call(() -> params.accept(new CommandInputNodeVisitor(inputShape))) + .dedent() + .write(");"); } else { writer.write("const command = new $T({});", operationSymbol); } @@ -290,10 +281,10 @@ private void generateClientRequestTest(OperationShape operation, HttpRequestTest return; } const r = err.request;""") - .indent() - .call(() -> writeHttpRequestAssertions(testCase)) - .dedent() - .write("}"); + .indent() + .call(() -> writeHttpRequestAssertions(testCase)) + .dedent() + .write("}"); }); } @@ -301,9 +292,11 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest Symbol operationSymbol = symbolProvider.toSymbol(operation); // Lowercase all the headers we're expecting as this is what we'll get. - Map headers = testCase.getHeaders().entrySet().stream() - .map(entry -> new Pair<>(entry.getKey().toLowerCase(Locale.US), entry.getValue())) - .collect(MapUtils.toUnmodifiableMap(Pair::getLeft, Pair::getRight)); + Map headers = testCase.getHeaders() + .entrySet() + .stream() + .map(entry -> new Pair<>(entry.getKey().toLowerCase(Locale.US), entry.getValue())) + .collect(MapUtils.toUnmodifiableMap(Pair::getLeft, Pair::getRight)); String queryParameters = Node.prettyPrintJson(buildQueryBag(testCase.getQueryParams())); String headerParameters = Node.prettyPrintJson(ObjectNode.fromStringMap(headers)); String body = testCase.getBody().orElse(null); @@ -347,9 +340,12 @@ private void generateServerRequestTest(OperationShape operation, HttpRequestTest private void generateMalformedRequestTest(OperationShape operation, HttpMalformedRequestTestCase testCase) { Symbol operationSymbol = symbolProvider.toSymbol(operation); - Map requestHeaders = testCase.getRequest().getHeaders().entrySet().stream() - .map(entry -> new Pair<>(entry.getKey().toLowerCase(Locale.US), entry.getValue())) - .collect(MapUtils.toUnmodifiableMap(Pair::getLeft, Pair::getRight)); + Map requestHeaders = testCase.getRequest() + .getHeaders() + .entrySet() + .stream() + .map(entry -> new Pair<>(entry.getKey().toLowerCase(Locale.US), entry.getValue())) + .collect(MapUtils.toUnmodifiableMap(Pair::getLeft, Pair::getRight)); String queryParameters = Node.prettyPrintJson(buildQueryBag(testCase.getRequest().getQueryParams())); String requestHeaderParameters = Node.prettyPrintJson(ObjectNode.fromStringMap(requestHeaders)); String requestBody = testCase.getRequest().getBody().orElse(null); @@ -389,10 +385,12 @@ private void generateMalformedRequestTest(OperationShape operation, HttpMalforme }); } - private void setupStubService(Symbol operationSymbol, - Symbol serviceSymbol, - Symbol handlerSymbol, - boolean usesDefaultValidation) { + private void setupStubService( + Symbol operationSymbol, + Symbol serviceSymbol, + Symbol handlerSymbol, + boolean usesDefaultValidation + ) { // We use a partial here so that we don't have to define the entire service, but still get the advantages // the type checker, including excess property checking. Later on we'll use `as` to cast this to the // full service so that we can actually use it. @@ -401,18 +399,20 @@ private void setupStubService(Symbol operationSymbol, }); String getHandlerName = "get" + handlerSymbol.getName(); - writer.addRelativeImport(getHandlerName, null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER)); + writer.addRelativeImport(getHandlerName, + null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER)); if (!usesDefaultValidation) { writer.addImport("ValidationFailure", "__ValidationFailure", TypeScriptDependency.SERVER_COMMON); // Cast the service as any so TS will ignore the fact that the type being passed in is incomplete. writer.openBlock( - "const handler = $L(testService as $T<{}>, (ctx: {}, failures: __ValidationFailure[]) => {", - "});", getHandlerName, serviceSymbol, - () -> writer.write("if (failures) { throw failures; } return undefined;") - ); + "const handler = $L(testService as $T<{}>, (ctx: {}, failures: __ValidationFailure[]) => {", + "});", + getHandlerName, + serviceSymbol, + () -> writer.write("if (failures) { throw failures; } return undefined;")); } else { writer.write("const handler = $L(testService as $T<{}>);", getHandlerName, serviceSymbol); } @@ -422,25 +422,25 @@ private ObjectNode buildQueryBag(List queryParams) { // The query params in the test definition is a list of strings that looks like // "Foo=Bar", so we need to split the keys from the values. Map> query = queryParams.stream() - .map(pair -> { - String[] split = pair.split("="); - String key; - String value = ""; - try { - // The strings we're given are url encoded, so we need to decode them. In an actual implementation - // the request we're given will have already decoded these. - key = URLDecoder.decode(split[0], StandardCharsets.UTF_8.toString()); - if (split.length > 1) { - value = URLDecoder.decode(split[1], StandardCharsets.UTF_8.toString()); + .map(pair -> { + String[] split = pair.split("="); + String key; + String value = ""; + try { + // The strings we're given are url encoded, so we need to decode them. In an actual implementation + // the request we're given will have already decoded these. + key = URLDecoder.decode(split[0], StandardCharsets.UTF_8.toString()); + if (split.length > 1) { + value = URLDecoder.decode(split[1], StandardCharsets.UTF_8.toString()); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - return Pair.of(key, value); - }) - // Query lists/sets will just use the same key repeatedly, so here we collect all the values that - // share a key. - .collect(Collectors.groupingBy(Pair::getKey, Collectors.mapping(Pair::getValue, Collectors.toList()))); + return Pair.of(key, value); + }) + // Query lists/sets will just use the same key repeatedly, so here we collect all the values that + // share a key. + .collect(Collectors.groupingBy(Pair::getKey, Collectors.mapping(Pair::getValue, Collectors.toList()))); ObjectNode.Builder nodeBuilder = ObjectNode.objectNodeBuilder(); for (Map.Entry> entry : query.entrySet()) { @@ -490,17 +490,14 @@ private void writeHttpResponseAssertions(HttpMalformedResponseDefinition respons } private void writeHttpQueryAssertions(HttpRequestTestCase testCase) { - testCase.getRequireQueryParams().forEach(requiredQueryParam -> - writer.write(""" + testCase.getRequireQueryParams().forEach(requiredQueryParam -> writer.write(""" expect( r.query[$1S], `Query key $1S should have been defined in $${JSON.stringify(r.query)}` - ).toBeDefined();""", requiredQueryParam) - ); + ).toBeDefined();""", requiredQueryParam)); writer.write(""); - testCase.getForbidQueryParams().forEach(forbidQueryParam -> - writer.write(""" + testCase.getForbidQueryParams().forEach(forbidQueryParam -> writer.write(""" expect( r.query[$1S], `Query key $1S should have been undefined in $${JSON.stringify(r.query)}` @@ -514,8 +511,8 @@ private void writeHttpQueryAssertions(HttpRequestTestCase testCase) { writer.addImport("buildQueryString", null, TypeScriptDependency.AWS_SDK_QUERYSTRING_BUILDER); writer.write("const queryString = buildQueryString(r.query);"); - explicitQueryValues.forEach(explicitQueryValue -> - writer.write("expect(queryString).toContain($S);", explicitQueryValue)); + explicitQueryValues.forEach( + explicitQueryValue -> writer.write("expect(queryString).toContain($S);", explicitQueryValue)); } writer.write(""); } @@ -523,19 +520,18 @@ private void writeHttpQueryAssertions(HttpRequestTestCase testCase) { private void writeHttpHeaderAssertions(HttpMessageTestCase testCase) { testCase.getRequireHeaders().forEach(requiredHeader -> { writer.write(""" - expect( - r.headers[$1S], - `Header key $1S should have been defined in $${JSON.stringify(r.headers)}` - ).toBeDefined();""", requiredHeader.toLowerCase()); + expect( + r.headers[$1S], + `Header key $1S should have been defined in $${JSON.stringify(r.headers)}` + ).toBeDefined();""", requiredHeader.toLowerCase()); }); writer.write(""); - testCase.getForbidHeaders().forEach(forbidHeader -> - writer.write(""" - expect( - r.headers[$1S], - `Header key $1S should have been undefined in $${JSON.stringify(r.headers)}` - ).toBeUndefined();""", forbidHeader.toLowerCase())); + testCase.getForbidHeaders().forEach(forbidHeader -> writer.write(""" + expect( + r.headers[$1S], + `Header key $1S should have been undefined in $${JSON.stringify(r.headers)}` + ).toBeUndefined();""", forbidHeader.toLowerCase())); writer.write(""); testCase.getHeaders().forEach((header, value) -> { @@ -594,8 +590,8 @@ private void writeHttpBodyMessageAssertion(String messageRegex, String mediaType String comparatorInvoke = registerMessageRegexStub(mediaType); writer.writeInline("expect(") - .writeInline(comparatorInvoke, messageRegex) - .write(").toEqual(true);"); + .writeInline(comparatorInvoke, messageRegex) + .write(").toEqual(true);"); } private String registerBodyComparatorStub(String mediaType) { @@ -623,13 +619,15 @@ private String registerBodyComparatorStub(String mediaType) { additionalStubs.add("protocol-test-text-stub.ts"); return "compareEquivalentTextBodies(bodyString, r.body)"; case "application/cbor": - writer.addImportSubmodule("cbor", null, - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR); + writer.addImportSubmodule("cbor", + null, + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); additionalStubs.add("protocol-test-cbor-stub.ts"); return "compareEquivalentCborBodies(bodyString, r.body)"; default: LOGGER.warning("Unable to compare bodies with unknown media type `" + mediaType - + "`, defaulting to direct comparison."); + + "`, defaulting to direct comparison."); writer.addTypeImport("Encoder", "__Encoder", TypeScriptDependency.SMITHY_TYPES); additionalStubs.add("protocol-test-unknown-type-stub.ts"); return "compareEquivalentUnknownTypeBodies(utf8Encoder, bodyString, r.body)"; @@ -656,18 +654,22 @@ public void generateServerResponseTest(OperationShape operation, HttpResponseTes openTestBlock(operation, testCase, testName, () -> { Symbol outputType = operationSymbol.expectProperty("outputType", Symbol.class); writer.openBlock("class TestService implements Partial<$T<{}>> {", "}", serviceSymbol, () -> { - writer.openBlock("$L(input: any, ctx: {}): Promise<$T> {", "}", - operationSymbol.getName(), outputType, () -> { - Optional outputOptional = operation.getOutput(); - if (outputOptional.isPresent()) { - StructureShape outputShape = model.expectShape(outputOptional.get(), StructureShape.class); - writer.writeInline("let response = "); - testCase.getParams().accept(new CommandInputNodeVisitor(outputShape, true)); - writer.write("return Promise.resolve({ ...response, '$$metadata': {} });"); - } else { - writer.write("return Promise.resolve({ '$$metadata': {} });"); - } - }); + writer.openBlock("$L(input: any, ctx: {}): Promise<$T> {", + "}", + operationSymbol.getName(), + outputType, + () -> { + Optional outputOptional = operation.getOutput(); + if (outputOptional.isPresent()) { + StructureShape outputShape = + model.expectShape(outputOptional.get(), StructureShape.class); + writer.writeInline("let response = "); + testCase.getParams().accept(new CommandInputNodeVisitor(outputShape, true)); + writer.write("return Promise.resolve({ ...response, '$$metadata': {} });"); + } else { + writer.write("return Promise.resolve({ '$$metadata': {} });"); + } + }); }); writeServerResponseTest(operation, testCase); }); @@ -682,20 +684,20 @@ private void generateResponseTest(OperationShape operation, HttpResponseTestCase // Invoke the handler and look for the expected response to then perform assertions. writer.write("let r: any;"); writer.write(""" - try { - r = await client.send(command); - } catch (err) { - fail("Expected a valid response to be returned, got " + err); - return; - }"""); + try { + r = await client.send(command); + } catch (err) { + fail("Expected a valid response to be returned, got " + err); + return; + }"""); writeResponseAssertions(operation, testCase); }); } private void generateServerErrorResponseTest( - OperationShape operation, - StructureShape error, - HttpResponseTestCase testCase + OperationShape operation, + StructureShape error, + HttpResponseTestCase testCase ) { Symbol serviceSymbol = symbolProvider.toSymbol(service); Symbol operationSymbol = symbolProvider.toSymbol(operation); @@ -711,23 +713,26 @@ private void generateServerErrorResponseTest( // but using the partial in the meantime will give us proper type checking on the // operation we want. writer.openBlock("class TestService implements Partial<$T<{}>> {", "}", serviceSymbol, () -> { - writer.openBlock("$L(input: any, ctx: {}): Promise<$T> {", "}", - operationSymbol.getName(), outputType, () -> { - // Write out an object according to what's defined in the test case. - writer.writeInline("const response = "); - testCase.getParams().accept(new CommandInputNodeVisitor(error, true)); - - // Add in the necessary wrapping information to make the error satisfy its interface. - // TODO: having proper constructors for these errors would be really nice so we don't - // have to do this. - writer.openBlock("const error: $T = {", "};", errorSymbol, () -> { - writer.write("...response,"); - writer.write("name: $S,", error.getId().getName()); - writer.write("$$fault: $S,", errorTrait.isClientError() ? "client" : "server"); - writer.write("$$metadata: {},"); + writer.openBlock("$L(input: any, ctx: {}): Promise<$T> {", + "}", + operationSymbol.getName(), + outputType, + () -> { + // Write out an object according to what's defined in the test case. + writer.writeInline("const response = "); + testCase.getParams().accept(new CommandInputNodeVisitor(error, true)); + + // Add in the necessary wrapping information to make the error satisfy its interface. + // TODO: having proper constructors for these errors would be really nice so we don't + // have to do this. + writer.openBlock("const error: $T = {", "};", errorSymbol, () -> { + writer.write("...response,"); + writer.write("name: $S,", error.getId().getName()); + writer.write("$$fault: $S,", errorTrait.isClientError() ? "client" : "server"); + writer.write("$$metadata: {},"); + }); + writer.write("throw error;"); }); - writer.write("throw error;"); - }); }); writeServerResponseTest(operation, testCase); }); @@ -745,14 +750,20 @@ private void writeServerResponseTest(OperationShape operation, HttpResponseTestC // our own service handler. This is largely in service of avoiding having to go through the // request deserializer writer.addImport("httpbinding", null, TypeScriptDependency.SERVER_COMMON); - writer.openBlock("const testMux = new httpbinding.HttpBindingMux<$S, keyof $T<{}>>([", "]);", - service.getId().getName(), serviceSymbol, () -> { - writer.openBlock("new httpbinding.UriSpec<$S, $S>('POST', [], [], {", "}),", - service.getId().getName(), operation.getId().getName(), () -> { - writer.write("service: $S,", service.getId().getName()); - writer.write("operation: $S,", operation.getId().getName()); - }); - }); + writer.openBlock("const testMux = new httpbinding.HttpBindingMux<$S, keyof $T<{}>>([", + "]);", + service.getId().getName(), + serviceSymbol, + () -> { + writer.openBlock("new httpbinding.UriSpec<$S, $S>('POST', [], [], {", + "}),", + service.getId().getName(), + operation.getId().getName(), + () -> { + writer.write("service: $S,", service.getId().getName()); + writer.write("operation: $S,", operation.getId().getName()); + }); + }); // Extend the existing serializer and replace the deserialize with a noop so we don't have to // worry about trying to construct something that matches. @@ -770,22 +781,25 @@ private void writeServerResponseTest(OperationShape operation, HttpResponseTestC writer.addImport("ServiceException", "__ServiceException", TypeScriptDependency.SERVER_COMMON); writer.addImport("OperationSerializer", "__OperationSerializer", TypeScriptDependency.SERVER_COMMON); writer.openBlock("const serFn: (op: $1T) => __OperationSerializer<$2T<{}>, $1T, __ServiceException> = (op) =>" - + " { return new TestSerializer(); };", serviceOperationsSymbol, serviceSymbol); - - writer.addRelativeImport("serializeFrameworkException", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER, - ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); + + " { return new TestSerializer(); };", serviceOperationsSymbol, serviceSymbol); + + writer.addRelativeImport("serializeFrameworkException", + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + ProtocolGenerator.PROTOCOLS_FOLDER, + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); writer.addImport("ValidationFailure", "__ValidationFailure", TypeScriptDependency.SERVER_COMMON); writer.write("const handler = new $T(service, testMux, serFn, serializeFrameworkException, " - + "(ctx: {}, f: __ValidationFailure[]) => { if (f) { throw f; } return undefined;});", handlerSymbol); + + "(ctx: {}, f: __ValidationFailure[]) => { if (f) { throw f; } return undefined;});", handlerSymbol); writer.write("let r = await handler.handle(request, {})").write(""); writeHttpResponseAssertions(testCase); } private void generateErrorResponseTest( - OperationShape operation, - StructureShape error, - HttpResponseTestCase testCase + OperationShape operation, + StructureShape error, + HttpResponseTestCase testCase ) { // Use a compound test_case name so we generate unique tests // for each error on each operation safely. This is useful in validating @@ -807,11 +821,11 @@ private void generateErrorResponseTest( return; } const r: any = err;""", error.getId().getName()) - .indent() - .call(() -> writeResponseAssertions(error, testCase)) - .write("return;") - .dedent() - .write("}"); + .indent() + .call(() -> writeResponseAssertions(error, testCase)) + .write("return;") + .dedent() + .write("}"); writer.write("fail(\"Expected an exception to be thrown from response\");"); }); } @@ -820,9 +834,11 @@ private void writeResponseTestSetup(OperationShape operation, HttpResponseTestCa Symbol operationSymbol = symbolProvider.toSymbol(operation); // Lowercase all the headers we're expecting as this is what we'll get. - Map headers = testCase.getHeaders().entrySet().stream() - .map(entry -> new Pair<>(entry.getKey().toLowerCase(Locale.US), entry.getValue())) - .collect(MapUtils.toUnmodifiableMap(Pair::getLeft, Pair::getRight)); + Map headers = testCase.getHeaders() + .entrySet() + .stream() + .map(entry -> new Pair<>(entry.getKey().toLowerCase(Locale.US), entry.getValue())) + .collect(MapUtils.toUnmodifiableMap(Pair::getLeft, Pair::getRight)); String body = testCase.getBody().orElse(null); // Create a client with a custom request handler that intercepts requests. @@ -839,10 +855,9 @@ private void writeResponseTestSetup(OperationShape operation, HttpResponseTestCa String key = entry.getKey().toLowerCase(Locale.US); String value = entry.getValue(); writer.write( - "$L: $S,", - PropertyAccessor.inlineKey(key), - value - ); + "$L: $S,", + PropertyAccessor.inlineKey(key), + value); } }); } @@ -870,15 +885,15 @@ private void writeRequestParamAssertions(OperationShape operation, HttpRequestTe ObjectNode params = testCase.getParams(); if (!params.isEmpty()) { StructureShape testInputShape = model.expectShape( - operation.getInput().orElseThrow(() -> new CodegenException("Foo")), - StructureShape.class); + operation.getInput().orElseThrow(() -> new CodegenException("Foo")), + StructureShape.class); // Use this trick wrapper to not need more complex trailing comma handling. writer.write("const paramsToValidate: any = [") - .indent() - .call(() -> params.accept(new CommandOutputNodeVisitor(testInputShape))) - .dedent() - .write("][0];"); + .indent() + .call(() -> params.accept(new CommandOutputNodeVisitor(testInputShape))) + .dedent() + .write("][0];"); // Extract a payload binding if present. Optional pb = Optional.empty(); @@ -891,10 +906,11 @@ private void writeRequestParamAssertions(OperationShape operation, HttpRequestTe writeParamAssertions(writer, payloadBinding, () -> { // TODO: replace this with a collector from the server config once it's available - writer.addImport("streamCollector", "__streamCollector", - TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); + writer.addImport("streamCollector", + "__streamCollector", + TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); writer.write("const comparableBlob = await __streamCollector(r[$S]);", - payloadBinding.get().getMemberName()); + payloadBinding.get().getMemberName()); }); } } @@ -907,51 +923,52 @@ private void writeResponseParamAssertions(Shape operationOrError, HttpResponseTe testOutputShape = operationOrError.asStructureShape().get(); } else { testOutputShape = model.expectShape( - operationOrError.asOperationShape().get().getOutput() - .orElseThrow(() -> new CodegenException("Foo")), - StructureShape.class); + operationOrError.asOperationShape() + .get() + .getOutput() + .orElseThrow(() -> new CodegenException("Foo")), + StructureShape.class); } // Use this trick wrapper to not need more complex trailing comma handling. writer.write("const paramsToValidate: any = [") - .indent() - .call(() -> params.accept(new CommandOutputNodeVisitor(testOutputShape))) - .dedent() - .write("][0];"); + .indent() + .call(() -> params.accept(new CommandOutputNodeVisitor(testOutputShape))) + .dedent() + .write("][0];"); // Extract a payload binding if present. Optional payloadBinding = operationOrError.asOperationShape() - .map(operationShape -> { - HttpBindingIndex index = HttpBindingIndex.of(model); - List payloadBindings = index.getResponseBindings(operationOrError, - Location.PAYLOAD); - if (!payloadBindings.isEmpty()) { - return payloadBindings.get(0); - } - return null; - }); + .map(operationShape -> { + HttpBindingIndex index = HttpBindingIndex.of(model); + List payloadBindings = index.getResponseBindings(operationOrError, + Location.PAYLOAD); + if (!payloadBindings.isEmpty()) { + return payloadBindings.get(0); + } + return null; + }); writeParamAssertions(writer, payloadBinding, () -> { writer.write("const comparableBlob = await client.config.streamCollector(r[$S]);", - payloadBinding.get().getMemberName()); + payloadBinding.get().getMemberName()); }); } } private void writeParamAssertions( - TypeScriptWriter writer, - Optional payloadBinding, - Runnable writeComparableBlob + TypeScriptWriter writer, + Optional payloadBinding, + Runnable writeComparableBlob ) { // If we have a streaming payload blob, we need to collect it to something that // can be compared with the test contents. This emulates the customer experience. boolean hasStreamingPayloadBlob = payloadBinding - .map(binding -> - model.getShape(binding.getMember().getTarget()) - .filter(Shape::isBlobShape) - .filter(s -> s.hasTrait(StreamingTrait.ID)) - .isPresent()) - .orElse(false); + .map(binding -> model.getShape(binding.getMember().getTarget()) + .filter(Shape::isBlobShape) + .filter(s -> s.hasTrait(StreamingTrait.ID)) + .isPresent()) + .orElse(false); if (hasStreamingPayloadBlob) { writeComparableBlob.run(); @@ -960,16 +977,18 @@ private void writeParamAssertions( // Perform parameter comparisons. writer.openBlock("Object.keys(paramsToValidate).forEach((param) => {", "});", () -> { writer.write(""" - expect( - r[param], - `The output field $${param} should have been defined in $${JSON.stringify(r, null, 2)}` - ).toBeDefined();"""); + expect( + r[param], + `The output field $${param} should have been defined in $${JSON.stringify(r, null, 2)}` + ).toBeDefined();"""); if (hasStreamingPayloadBlob) { - writer.openBlock("if (param === $S) {", "} else {", payloadBinding.get().getMemberName(), () -> - writer.write(""" - expect(equivalentContents(paramsToValidate[param], \ - comparableBlob)).toBe(true); - """)); + writer.openBlock("if (param === $S) {", + "} else {", + payloadBinding.get().getMemberName(), + () -> writer.write(""" + expect(equivalentContents(paramsToValidate[param], \ + comparableBlob)).toBe(true); + """)); writer.indent(); } @@ -983,10 +1002,10 @@ private void writeParamAssertions( } private void openTestBlock( - OperationShape operation, - HttpMessageTestCase testCase, - String testName, - Runnable f + OperationShape operation, + HttpMessageTestCase testCase, + String testName, + Runnable f ) { // Skipped tests are still generated, just not run. if (testFilter.skip(service, operation, testCase, settings)) { @@ -997,10 +1016,10 @@ private void openTestBlock( } private void openTestBlock( - OperationShape operation, - HttpMalformedRequestTestCase testCase, - String testName, - Runnable f + OperationShape operation, + HttpMalformedRequestTestCase testCase, + String testName, + Runnable f ) { // Skipped tests are still generated, just not run. if (malformedRequestTestFilter.skip(service, operation, testCase, settings)) { @@ -1117,7 +1136,7 @@ public Void objectNode(ObjectNode node) { memberShape = wrapperShape.asMapShape().get().getValue(); } else { throw new CodegenException("Unknown shape type for object node when " - + "generating protocol test input: " + wrapperShape.getType()); + + "generating protocol test input: " + wrapperShape.getType()); } // Handle auto-filling idempotency token values to the explicit value. @@ -1171,7 +1190,9 @@ public Void stringNode(StringNode node) { break; default: throw new CodegenException(String.format( - "Unexpected string value for `%s`: \"%s\"", workingShape.getId(), node.getValue())); + "Unexpected string value for `%s`: \"%s\"", + workingShape.getId(), + node.getValue())); } } else { writer.write("$S,", node.getValue()); @@ -1199,10 +1220,10 @@ public interface TestFilter { * @return True if the test should be skipped, false otherwise. */ boolean skip( - ServiceShape service, - OperationShape operation, - HttpMessageTestCase testCase, - TypeScriptSettings settings + ServiceShape service, + OperationShape operation, + HttpMessageTestCase testCase, + TypeScriptSettings settings ); } @@ -1225,10 +1246,10 @@ public interface MalformedRequestTestFilter { * @return True if the test should be skipped, false otherwise. */ boolean skip( - ServiceShape service, - OperationShape operation, - HttpMalformedRequestTestCase testCase, - TypeScriptSettings settings + ServiceShape service, + OperationShape operation, + HttpMalformedRequestTestCase testCase, + TypeScriptSettings settings ); } @@ -1321,7 +1342,7 @@ public Void objectNode(ObjectNode node) { memberShape = wrapperShape.asMapShape().get().getValue(); } else { throw new CodegenException("Unknown shape type for object node when " - + "generating protocol test output: " + wrapperShape.getType()); + + "generating protocol test output: " + wrapperShape.getType()); } // Handle error standardization to the down-cased "message". @@ -1335,8 +1356,8 @@ public Void objectNode(ObjectNode node) { // Alter valueNode to downcase keys if it's a map for prefixHeaders. // This is an enforced behavior of the fetch handler. Node renderNode = memberShape.hasTrait(HttpPrefixHeadersTrait.class) - ? downcaseNodeKeys(valueNode.expectObjectNode()) - : valueNode; + ? downcaseNodeKeys(valueNode.expectObjectNode()) + : valueNode; writer.call(() -> renderNode.accept(this)); }); this.workingShape = wrapperShape; @@ -1348,7 +1369,7 @@ private ObjectNode downcaseNodeKeys(ObjectNode startingNode) { ObjectNode downcasedNode = Node.objectNode(); for (Map.Entry entry : startingNode.getMembers().entrySet()) { downcasedNode = downcasedNode.withMember(entry.getKey().getValue().toLowerCase(Locale.US), - entry.getValue()); + entry.getValue()); } return downcasedNode; } @@ -1371,7 +1392,9 @@ public Void stringNode(StringNode node) { break; default: throw new CodegenException(String.format( - "Unexpected string value for `%s`: \"%s\"", workingShape.getId(), node.getValue())); + "Unexpected string value for `%s`: \"%s\"", + workingShape.getId(), + node.getValue())); } } else { writer.write("$S,", node.getValue()); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ImportDeclarations.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ImportDeclarations.java index 721976604e6..998dea0c11c 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ImportDeclarations.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ImportDeclarations.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.io.File; @@ -119,10 +108,10 @@ public String toString() { result.append("// @ts-ignore: ").append(importEntry.getValue().getRight().reason).append("\n"); } result.append("import ") - .append(importEntry.getValue().getLeft()) - .append(" from \"") - .append(importEntry.getKey()) - .append("\";"); + .append(importEntry.getValue().getLeft()) + .append(" from \"") + .append(importEntry.getKey()) + .append("\";"); if (ignore) { result.append(" // eslint-disable-line"); } @@ -136,9 +125,11 @@ public String toString() { return result.toString(); } - private static void createImports(Map> namedImports, - Map> namedTypeImports, - StringBuilder buffer) { + private static void createImports( + Map> namedImports, + Map> namedTypeImports, + StringBuilder buffer + ) { TreeSet mergedModuleKeys = new TreeSet<>((a, b) -> { if (a.startsWith(".") && !b.startsWith(".")) { return 1; @@ -156,8 +147,8 @@ private static void createImports(Map> namedImports, // separate non-relative and relative imports. long separatorIndex = mergedModuleKeys.stream() - .filter(k -> !k.startsWith(".")) - .count(); + .filter(k -> !k.startsWith(".")) + .count(); int i = 0; boolean needsSeparator = separatorIndex > 0 && separatorIndex < mergedModuleKeys.size(); @@ -196,7 +187,7 @@ private static void createImports(Map> namedImports, // "*" imports are not supported https://github.com/smithy-lang/smithy-typescript/issues/211 if ("*".equals(runtimeSymbol) || "*".equals(typeSymbol)) { throw new CodegenException("Star imports are not supported, attempted for " + module - + ". Use default import instead."); + + ". Use default import instead."); } if (runtimeSymbol != null) { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java index f07bc9d0d56..72891ef1b28 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -41,18 +30,18 @@ final class IndexGenerator { private IndexGenerator() {} static void writeIndex( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - ProtocolGenerator protocolGenerator, - TypeScriptWriter writer, - TypeScriptWriter modelIndexer + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + ProtocolGenerator protocolGenerator, + TypeScriptWriter writer, + TypeScriptWriter modelIndexer ) { writer.write("/* eslint-disable */"); - settings.getService(model).getTrait(DocumentationTrait.class).ifPresent(trait -> - writer.writeDocs(trait.getValue() + "\n\n" + "@packageDocumentation")); - + settings.getService(model) + .getTrait(DocumentationTrait.class) + .ifPresent(trait -> writer.writeDocs(trait.getValue() + "\n\n" + "@packageDocumentation")); if (settings.generateClient()) { writeClientExports(settings, model, symbolProvider, writer); @@ -65,9 +54,8 @@ static void writeIndex( // write export statement for models writer.write( - // the header comment is already present in the upper writer. - modelIndexer.toString().replace("// smithy-typescript generated code", "") - ); + // the header comment is already present in the upper writer. + modelIndexer.toString().replace("// smithy-typescript generated code", "")); } private static void writeProtocolExports(ProtocolGenerator protocolGenerator, TypeScriptWriter writer) { @@ -76,10 +64,10 @@ private static void writeProtocolExports(ProtocolGenerator protocolGenerator, Ty } static void writeServerIndex( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - FileManifest fileManifest + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + FileManifest fileManifest ) { TypeScriptWriter writer = new TypeScriptWriter(""); ServiceShape service = settings.getService(model); @@ -90,15 +78,15 @@ static void writeServerIndex( writer.write("export * from \"./$L\"", symbol.getName()); fileManifest.writeFile( - Paths.get(CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER, "index.ts").toString(), - writer.toString()); + Paths.get(CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER, "index.ts").toString(), + writer.toString()); } private static void writeClientExports( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - TypeScriptWriter writer + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + TypeScriptWriter writer ) { ServiceShape service = settings.getService(model); Symbol symbol = symbolProvider.toSymbol(service); @@ -117,9 +105,8 @@ private static void writeClientExports( // Export Runtime Extension and Client ExtensionConfiguration interfaces writer.write("export type { RuntimeExtension } from \"./runtimeExtensions\";"); writer.write( - "export type { $LExtensionConfiguration } from \"./extensionConfiguration\";", - normalizedClientName - ); + "export type { $LExtensionConfiguration } from \"./extensionConfiguration\";", + normalizedClientName); // Write export statement for commands. writer.write("export * from \"./commands\";"); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IntEnumGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IntEnumGenerator.java index 1a1a5262c89..fb09d207251 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IntEnumGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IntEnumGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.Comparator; @@ -76,7 +65,8 @@ public void run() { private void generateIntEnum() { writer.openBlock("export enum $L {", "}", symbol.getName(), () -> { // Sort by the values to ensure a stable order and sane diffs. - shape.getEnumValues().entrySet() + shape.getEnumValues() + .entrySet() .stream() .sorted(Comparator.comparing(e -> e.getValue())) .forEach(this::writeIntEnumEntry); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/LanguageTarget.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/LanguageTarget.java index 0039e74155f..d3b60713473 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/LanguageTarget.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/LanguageTarget.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageApiValidationGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageApiValidationGenerator.java index 5fd1f5db525..8f45d4621c5 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageApiValidationGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageApiValidationGenerator.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Path; @@ -33,10 +32,10 @@ public final class PackageApiValidationGenerator { private final ServiceClosure closure; public PackageApiValidationGenerator( - TypeScriptWriter writer, - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider + TypeScriptWriter writer, + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider ) { this.writer = writer; this.settings = settings; @@ -50,61 +49,60 @@ public PackageApiValidationGenerator( */ public void writeTypeIndexTest() { writer.openBlock(""" - export type {""", - """ - } from "../dist-types/index.d";""", - () -> { - // exportable types include: - - // the barebones client - String aggregateClientName = CodegenUtils.getServiceName(settings, model, symbolProvider); - writer.write("$L", aggregateClientName + "Client,"); - - // the aggregate client - writer.write(aggregateClientName + ","); - - // all commands - Set containedOperations = TopDownIndex.of(model).getContainedOperations( - settings.getService() - ); - for (OperationShape operation : containedOperations) { - String commandName = symbolProvider.toSymbol(operation).getName(); - writer.write("$L,", commandName); - writer.write("$LInput,", commandName); - writer.write("$LOutput,", commandName); - } - - // enums - TreeSet enumShapes = closure.getEnums(); - for (Shape enumShape : enumShapes) { - writer.write("$L,", symbolProvider.toSymbol(enumShape).getName()); - } - - // structure & union types & modeled errors - TreeSet structuralShapes = closure.getStructuralNonErrorShapes(); - for (Shape structuralShape : structuralShapes) { - writer.write("$L,", symbolProvider.toSymbol(structuralShape).getName()); - } - - TreeSet errorShapes = closure.getErrorShapes(); - for (Shape errorShape : errorShapes) { - writer.write("$L,", symbolProvider.toSymbol(errorShape).getName()); - } - - // synthetic base exception - String baseExceptionName = CodegenUtils.getSyntheticBaseExceptionName(aggregateClientName, model); - writer.write("$L,", baseExceptionName); - - // waiters - closure.getWaiterNames().forEach(waiter -> { - writer.write("$L,", waiter); - }); - // paginators - closure.getPaginatorNames().forEach(paginator -> { - writer.write("$L,", paginator); + export type {""", + """ + } from "../dist-types/index.d";""", + () -> { + // exportable types include: + + // the barebones client + String aggregateClientName = CodegenUtils.getServiceName(settings, model, symbolProvider); + writer.write("$L", aggregateClientName + "Client,"); + + // the aggregate client + writer.write(aggregateClientName + ","); + + // all commands + Set containedOperations = TopDownIndex.of(model) + .getContainedOperations( + settings.getService()); + for (OperationShape operation : containedOperations) { + String commandName = symbolProvider.toSymbol(operation).getName(); + writer.write("$L,", commandName); + writer.write("$LInput,", commandName); + writer.write("$LOutput,", commandName); + } + + // enums + TreeSet enumShapes = closure.getEnums(); + for (Shape enumShape : enumShapes) { + writer.write("$L,", symbolProvider.toSymbol(enumShape).getName()); + } + + // structure & union types & modeled errors + TreeSet structuralShapes = closure.getStructuralNonErrorShapes(); + for (Shape structuralShape : structuralShapes) { + writer.write("$L,", symbolProvider.toSymbol(structuralShape).getName()); + } + + TreeSet errorShapes = closure.getErrorShapes(); + for (Shape errorShape : errorShapes) { + writer.write("$L,", symbolProvider.toSymbol(errorShape).getName()); + } + + // synthetic base exception + String baseExceptionName = CodegenUtils.getSyntheticBaseExceptionName(aggregateClientName, model); + writer.write("$L,", baseExceptionName); + + // waiters + closure.getWaiterNames().forEach(waiter -> { + writer.write("$L,", waiter); + }); + // paginators + closure.getPaginatorNames().forEach(paginator -> { + writer.write("$L,", paginator); + }); }); - } - ); } /** @@ -112,7 +110,7 @@ public void writeTypeIndexTest() { */ public void writeRuntimeIndexTest() { writer.write(""" - import assert from "node:assert";"""); + import assert from "node:assert";"""); // runtime components include: Path cjsIndex = Paths.get("./dist-cjs/index.js"); @@ -125,38 +123,36 @@ public void writeRuntimeIndexTest() { // the aggregate client writer.write("// clients"); writer.write(""" - assert(typeof $L === "function");""", - aggregateClientName + "Client" - ); + assert(typeof $L === "function");""", + aggregateClientName + "Client"); writer.write(""" - assert(typeof $L === "function");""", - aggregateClientName - ); + assert(typeof $L === "function");""", + aggregateClientName); // all commands writer.write("// commands"); - Set containedOperations = TopDownIndex.of(model).getContainedOperations( - settings.getService() - ); + Set containedOperations = TopDownIndex.of(model) + .getContainedOperations( + settings.getService()); for (OperationShape operation : containedOperations) { Symbol operationSymbol = symbolProvider.toSymbol(operation); writer.addRelativeImport(operationSymbol.getName(), null, cjsIndex); writer.write(""" - assert(typeof $L === "function");""", - operationSymbol.getName() - ); + assert(typeof $L === "function");""", + operationSymbol.getName()); } // enums // string shapes with enum trait do not generate anything if // any enum value does not have a name. - TreeSet enumShapes = closure.getEnums().stream() - .filter(shape -> shape - .getTrait(EnumTrait.class) - .map(EnumTrait::hasNames) - .orElse(true)) - .collect(TreeSet::new, Set::add, Set::addAll); + TreeSet enumShapes = closure.getEnums() + .stream() + .filter(shape -> shape + .getTrait(EnumTrait.class) + .map(EnumTrait::hasNames) + .orElse(true)) + .collect(TreeSet::new, Set::add, Set::addAll); if (!enumShapes.isEmpty()) { writer.write("// enums"); @@ -165,9 +161,8 @@ public void writeRuntimeIndexTest() { Symbol enumSymbol = symbolProvider.toSymbol(enumShape); writer.addRelativeImport(enumSymbol.getName(), null, cjsIndex); writer.write(""" - assert(typeof $L === "object");""", - enumSymbol.getName() - ); + assert(typeof $L === "object");""", + enumSymbol.getName()); } String baseExceptionName = CodegenUtils.getSyntheticBaseExceptionName(aggregateClientName, model); @@ -179,10 +174,9 @@ public void writeRuntimeIndexTest() { Symbol errorSymbol = symbolProvider.toSymbol(error); writer.addRelativeImport(errorSymbol.getName(), null, cjsIndex); writer.write( - "assert($L.prototype instanceof $L);", - errorSymbol.getName(), - baseExceptionName - ); + "assert($L.prototype instanceof $L);", + errorSymbol.getName(), + baseExceptionName); } writer.addRelativeImport(baseExceptionName, null, cjsIndex); writer.write("assert($L.prototype instanceof Error);", baseExceptionName); @@ -195,9 +189,8 @@ public void writeRuntimeIndexTest() { waiterNames.forEach(waiter -> { writer.addRelativeImport(waiter, null, cjsIndex); writer.write(""" - assert(typeof $L === "function");""", - waiter - ); + assert(typeof $L === "function");""", + waiter); }); TreeSet paginatorNames = closure.getPaginatorNames(); if (!paginatorNames.isEmpty()) { @@ -206,9 +199,8 @@ public void writeRuntimeIndexTest() { paginatorNames.forEach(paginator -> { writer.addRelativeImport(paginator, null, cjsIndex); writer.write(""" - assert(typeof $L === "function");""", - paginator - ); + assert(typeof $L === "function");""", + paginator); }); writer.write("console.log(`$L index test passed.`);", aggregateClientName); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageContainer.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageContainer.java index ba155684182..bb7fc503547 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageContainer.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageContainer.java @@ -1,18 +1,7 @@ /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; /** diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageJsonGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageJsonGenerator.java index b73e5c7ce3b..9d3f251f5ea 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageJsonGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PackageJsonGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.io.InputStream; @@ -46,16 +35,15 @@ static void writePackageJson( ObjectNode userSuppliedPackageJson = settings.getPackageJson(); ObjectNode defaultPackageJson = Node.parse(IoUtils.toUtf8String(resource)) - .expectObjectNode(); + .expectObjectNode(); ObjectNode mergedScripts = defaultPackageJson.expectObjectMember("scripts") - .merge( - userSuppliedPackageJson.getObjectMember("scripts") - .orElse(ObjectNode.builder().build()) - ); + .merge( + userSuppliedPackageJson.getObjectMember("scripts") + .orElse(ObjectNode.builder().build())); ObjectNode node = defaultPackageJson.merge(userSuppliedPackageJson) - .withMember("scripts", mergedScripts); + .withMember("scripts", mergedScripts); // Merge TypeScript dependencies into the package.json file. for (Map.Entry> depEntry : dependencies.entrySet()) { @@ -74,24 +62,25 @@ static void writePackageJson( scripts = scripts.withMember("test", "yarn g:vitest run --passWithNoTests"); node = node.withMember("scripts", scripts); - manifest.writeFile(VITEST_CONFIG_FILENAME, IoUtils.toUtf8String( - PackageJsonGenerator.class.getResourceAsStream(VITEST_CONFIG_FILENAME))); + manifest.writeFile(VITEST_CONFIG_FILENAME, + IoUtils.toUtf8String( + PackageJsonGenerator.class.getResourceAsStream(VITEST_CONFIG_FILENAME))); } if (settings.generateTypeDoc()) { // Add typedoc to the "devDependencies" if not present if (devDeps.getMember(TypeScriptDependency.TYPEDOC.packageName).isEmpty()) { devDeps = devDeps.withMember( - TypeScriptDependency.TYPEDOC.packageName, - TypeScriptDependency.TYPEDOC.version); + TypeScriptDependency.TYPEDOC.packageName, + TypeScriptDependency.TYPEDOC.version); node = node.withMember("devDependencies", devDeps); } // Add @smithy/service-client-documentation-generator to the "devDependencies" if not present if (devDeps.getMember(TypeScriptDependency.AWS_SDK_CLIENT_DOCGEN.packageName).isEmpty()) { devDeps = devDeps.withMember( - TypeScriptDependency.AWS_SDK_CLIENT_DOCGEN.packageName, - TypeScriptDependency.AWS_SDK_CLIENT_DOCGEN.version); + TypeScriptDependency.AWS_SDK_CLIENT_DOCGEN.packageName, + TypeScriptDependency.AWS_SDK_CLIENT_DOCGEN.version); node = node.withMember("devDependencies", devDeps); } @@ -107,11 +96,15 @@ static void writePackageJson( // These are currently only generated for clients, but they may be needed for ssdk as well. if (settings.generateClient()) { // Add the Node vs Browser hook. - node = node.withMember("browser", node.getObjectMember("browser").orElse(Node.objectNode()) - .withMember("./dist-es/runtimeConfig", "./dist-es/runtimeConfig.browser")); + node = node.withMember("browser", + node.getObjectMember("browser") + .orElse(Node.objectNode()) + .withMember("./dist-es/runtimeConfig", "./dist-es/runtimeConfig.browser")); // Add the ReactNative hook. - node = node.withMember("react-native", node.getObjectMember("react-native").orElse(Node.objectNode()) - .withMember("./dist-es/runtimeConfig", "./dist-es/runtimeConfig.native")); + node = node.withMember("react-native", + node.getObjectMember("react-native") + .orElse(Node.objectNode()) + .withMember("./dist-es/runtimeConfig", "./dist-es/runtimeConfig.native")); } // Set the package to private if required. @@ -121,9 +114,8 @@ static void writePackageJson( if (!settings.generateIndexTests()) { node = node.withMember( - "scripts", - node.getObjectMember("scripts").get().withoutMember("test:index") - ); + "scripts", + node.getObjectMember("scripts").get().withoutMember("test:index")); } // Expand template parameters. diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PaginationGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PaginationGenerator.java index 9c68e2ccaa9..fd1a9bd3adc 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PaginationGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PaginationGenerator.java @@ -1,19 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -39,7 +27,7 @@ final class PaginationGenerator implements Runnable { static final String PAGINATION_FOLDER = "pagination"; static final String PAGINATION_INTERFACE_FILE = - Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, "Interfaces.ts").toString(); + Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, "Interfaces.ts").toString(); private final TypeScriptWriter writer; private final String aggregatedClientName; @@ -93,19 +81,22 @@ public void run() { writer.addRelativeImport(outputSymbol.getName(), outputSymbol.getName(), Paths.get(".", outputSymbol.getNamespace())); - writer.addRelativeImport(serviceSymbol.getName(), serviceSymbol.getName(), + writer.addRelativeImport(serviceSymbol.getName(), + serviceSymbol.getName(), Paths.get(".", serviceSymbol.getNamespace())); // Import Pagination types writer.addTypeImport("Paginator", null, TypeScriptDependency.SMITHY_TYPES); - writer.addRelativeImport(paginationType, paginationType, - Paths.get(".", PAGINATION_INTERFACE_FILE.replace(".ts", ""))); + writer.addRelativeImport(paginationType, + paginationType, + Paths.get(".", PAGINATION_INTERFACE_FILE.replace(".ts", ""))); writePager(); } static String getOutputFilelocation(OperationShape operation) { - return Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, + return Paths.get(CodegenUtils.SOURCE_FOLDER, + PAGINATION_FOLDER, operation.getId().getName() + "Paginator.ts").toString(); } @@ -117,17 +108,18 @@ static void generateServicePaginationInterfaces( writer.addTypeImport("PaginationConfiguration", null, TypeScriptDependency.SMITHY_TYPES); writer.addRelativeImport(service.getName(), service.getName(), Paths.get(".", service.getNamespace())); writer.writeDocs("@public") - .openBlock("export interface $LPaginationConfiguration extends PaginationConfiguration {", - "}", aggregatedClientName, () -> { - writer.write("client: $L;", service.getName()); - }); + .openBlock("export interface $LPaginationConfiguration extends PaginationConfiguration {", + "}", + aggregatedClientName, + () -> { + writer.write("client: $L;", service.getName()); + }); } private static String getModulePath(String fileLocation) { return fileLocation.substring( - fileLocation.lastIndexOf("/") + 1, - fileLocation.length() - ).replace(".ts", ""); + fileLocation.lastIndexOf("/") + 1, + fileLocation.length()).replace(".ts", ""); } static void writeIndex( @@ -148,8 +140,8 @@ static void writeIndex( } fileManifest.writeFile( - Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, "index.ts").toString(), - writer.toString()); + Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, "index.ts").toString(), + writer.toString()); } private void writePager() { @@ -166,7 +158,7 @@ private void writePager() { writer.writeDocs("@public"); writer - .pushState() + .pushState() .putContext("operation", operationName) .putContext("aggClient", aggregatedClientName) .putContext("inputType", inputTypeName) @@ -177,25 +169,23 @@ private void writePager() { .putContext("inputToken", inputTokenName) .putContext("outputToken", outputTokenName) .putContext( - "pageSizeMember", - paginatedInfo.getPageSizeMember().map(MemberShape::getMemberName).orElse("") - ) - .write( - """ - export const paginate${operation:L}: ( - config: ${aggClient:L}PaginationConfiguration, - input: ${inputType:L}, - ...rest: any[] - ) => Paginator<${outputType:L}> = - createPaginator<${paginationType:L}, ${inputType:L}, ${outputType:L}>( - ${serviceTypeName:L}, - ${operationName:L}, - ${inputToken:S}, - ${outputToken:S}, - ${pageSizeMember:S} - ); - """ - ) - .popState(); + "pageSizeMember", + paginatedInfo.getPageSizeMember().map(MemberShape::getMemberName).orElse("")) + .write( + """ + export const paginate${operation:L}: ( + config: ${aggClient:L}PaginationConfiguration, + input: ${inputType:L}, + ...rest: any[] + ) => Paginator<${outputType:L}> = + createPaginator<${paginationType:L}, ${inputType:L}, ${outputType:L}>( + ${serviceTypeName:L}, + ${operationName:L}, + ${inputToken:S}, + ${outputToken:S}, + ${pageSizeMember:S} + ); + """) + .popState(); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java index e6de333d06e..f8e3a2b11af 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -55,94 +44,114 @@ final class RuntimeConfigGenerator { private final List integrations; private final ApplicationProtocol applicationProtocol; private final Map> nodeRuntimeConfigDefaults = MapUtils.of( - "requestHandler", writer -> { - writer.addImport("NodeHttpHandler", "RequestHandler", - TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); - writer.write("RequestHandler.create(config?.requestHandler ?? defaultConfigProvider)"); - }, - "sha256", writer -> { - writer.addImport("Hash", null, TypeScriptDependency.AWS_SDK_HASH_NODE); - writer.write("Hash.bind(null, \"sha256\")"); - }, - "bodyLengthChecker", writer -> { - writer.addImport("calculateBodyLength", null, TypeScriptDependency.AWS_SDK_UTIL_BODY_LENGTH_NODE); - writer.write("calculateBodyLength"); - }, - "streamCollector", writer -> { - writer.addImport("streamCollector", null, TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); - writer.write("streamCollector"); - } - ); + "requestHandler", + writer -> { + writer.addImport("NodeHttpHandler", + "RequestHandler", + TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); + writer.write("RequestHandler.create(config?.requestHandler ?? defaultConfigProvider)"); + }, + "sha256", + writer -> { + writer.addImport("Hash", null, TypeScriptDependency.AWS_SDK_HASH_NODE); + writer.write("Hash.bind(null, \"sha256\")"); + }, + "bodyLengthChecker", + writer -> { + writer.addImport("calculateBodyLength", null, TypeScriptDependency.AWS_SDK_UTIL_BODY_LENGTH_NODE); + writer.write("calculateBodyLength"); + }, + "streamCollector", + writer -> { + writer.addImport("streamCollector", null, TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER); + writer.write("streamCollector"); + }); private final Map> browserRuntimeConfigDefaults = MapUtils.of( - "requestHandler", writer -> { - writer.addImport("FetchHttpHandler", "RequestHandler", - TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER); - writer.write("RequestHandler.create(config?.requestHandler ?? defaultConfigProvider)"); - }, - "sha256", writer -> { - writer.addImport("Sha256", null, TypeScriptDependency.AWS_CRYPTO_SHA256_BROWSER); - writer.write("Sha256"); - }, - "bodyLengthChecker", writer -> { - writer.addImport("calculateBodyLength", null, TypeScriptDependency.AWS_SDK_UTIL_BODY_LENGTH_BROWSER); - writer.write("calculateBodyLength"); - }, - "streamCollector", writer -> { - writer.addImport("streamCollector", null, - TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER); - writer.write("streamCollector"); - } - ); + "requestHandler", + writer -> { + writer.addImport("FetchHttpHandler", + "RequestHandler", + TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER); + writer.write("RequestHandler.create(config?.requestHandler ?? defaultConfigProvider)"); + }, + "sha256", + writer -> { + writer.addImport("Sha256", null, TypeScriptDependency.AWS_CRYPTO_SHA256_BROWSER); + writer.write("Sha256"); + }, + "bodyLengthChecker", + writer -> { + writer.addImport("calculateBodyLength", null, TypeScriptDependency.AWS_SDK_UTIL_BODY_LENGTH_BROWSER); + writer.write("calculateBodyLength"); + }, + "streamCollector", + writer -> { + writer.addImport("streamCollector", + null, + TypeScriptDependency.AWS_SDK_FETCH_HTTP_HANDLER); + writer.write("streamCollector"); + }); private final Map> reactNativeRuntimeConfigDefaults = MapUtils.of( - "sha256", writer -> { - writer.addImport("Sha256", null, TypeScriptDependency.AWS_CRYPTO_SHA256_JS); - writer.write("Sha256"); - } - ); + "sha256", + writer -> { + writer.addImport("Sha256", null, TypeScriptDependency.AWS_CRYPTO_SHA256_JS); + writer.write("Sha256"); + }); private final Map> sharedRuntimeConfigDefaults = MapUtils.of( - "base64Decoder", writer -> { - writer.addImport("fromBase64", null, - TypeScriptDependency.AWS_SDK_UTIL_BASE64); - writer.write("fromBase64"); - }, - "base64Encoder", writer -> { - writer.addImport("toBase64", null, - TypeScriptDependency.AWS_SDK_UTIL_BASE64); - writer.write("toBase64"); - }, - "disableHostPrefix", writer -> { - writer.write("false"); - }, - "urlParser", writer -> { - writer.addImport("parseUrl", null, - TypeScriptDependency.AWS_SDK_URL_PARSER); - writer.write("parseUrl"); - }, - "utf8Decoder", writer -> { - writer.addImport("fromUtf8", null, - TypeScriptDependency.AWS_SDK_UTIL_UTF8); - writer.write("fromUtf8"); - }, - "utf8Encoder", writer -> { - writer.addImport("toUtf8", null, - TypeScriptDependency.AWS_SDK_UTIL_UTF8); - writer.write("toUtf8"); - }, - "extensions", writer -> { - writer.write("[]"); - } - ); + "base64Decoder", + writer -> { + writer.addImport("fromBase64", + null, + TypeScriptDependency.AWS_SDK_UTIL_BASE64); + writer.write("fromBase64"); + }, + "base64Encoder", + writer -> { + writer.addImport("toBase64", + null, + TypeScriptDependency.AWS_SDK_UTIL_BASE64); + writer.write("toBase64"); + }, + "disableHostPrefix", + writer -> { + writer.write("false"); + }, + "urlParser", + writer -> { + writer.addImport("parseUrl", + null, + TypeScriptDependency.AWS_SDK_URL_PARSER); + writer.write("parseUrl"); + }, + "utf8Decoder", + writer -> { + writer.addImport("fromUtf8", + null, + TypeScriptDependency.AWS_SDK_UTIL_UTF8); + writer.write("fromUtf8"); + }, + "utf8Encoder", + writer -> { + writer.addImport("toUtf8", + null, + TypeScriptDependency.AWS_SDK_UTIL_UTF8); + writer.write("toUtf8"); + }, + "extensions", + writer -> { + writer.write("[]"); + }); private final Map runtimeConfigDefaultValuePrefixes = MapUtils.of( - "requestHandler", "" - ); + "requestHandler", + ""); RuntimeConfigGenerator( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - TypeScriptDelegator delegator, - List integrations, - ApplicationProtocol applicationProtocol + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + TypeScriptDelegator delegator, + List integrations, + ApplicationProtocol applicationProtocol ) { this.settings = settings; this.model = model; @@ -155,16 +164,17 @@ final class RuntimeConfigGenerator { void generate(LanguageTarget target) { String clientConfigName = symbolProvider.toSymbol(service).getName() + "Config"; - String clientModuleName = symbolProvider.toSymbol(service).getNamespace() - .replaceFirst(CodegenUtils.SOURCE_FOLDER + "/", ""); + String clientModuleName = symbolProvider.toSymbol(service) + .getNamespace() + .replaceFirst(CodegenUtils.SOURCE_FOLDER + "/", ""); String template = TypeScriptUtils.loadResourceAsString(target.getTemplateFileName()); String contents = template - .replace("${clientConfigName}", clientConfigName) - .replace("${apiVersion}", service.getVersion()) - .replace("${", "$${") // sanitize template place holders. - .replace("$${customizations}", "${L@customizations}") - .replace("$${prepareCustomizations}", "${L@prepareCustomizations}"); + .replace("${clientConfigName}", clientConfigName) + .replace("${apiVersion}", service.getVersion()) + .replace("${", "$${") // sanitize template place holders. + .replace("$${customizations}", "${L@customizations}") + .replace("$${prepareCustomizations}", "${L@prepareCustomizations}"); delegator.useFileWriter(target.getTargetFilename(), writer -> { writer.trimBlankLines(0); @@ -178,13 +188,12 @@ void generate(LanguageTarget target) { writer.indent().onSection("customizations", original -> { // Start with defaults, use a TreeMap for keeping entries sorted. Map> configs = - new TreeMap<>(getDefaultRuntimeConfigs(target)); + new TreeMap<>(getDefaultRuntimeConfigs(target)); // Add any integration supplied runtime config writers. for (TypeScriptIntegration integration : integrations) { configs.putAll( - integration.getRuntimeConfigWriters(settings, model, symbolProvider, target) - ); + integration.getRuntimeConfigWriters(settings, model, symbolProvider, target)); } // Needs a separate integration point since not all the information is accessible in // {@link TypeScriptIntegration#getRuntimeConfigWriters()} @@ -193,15 +202,15 @@ void generate(LanguageTarget target) { } configs.forEach((key, value) -> { String valuePrefix = - runtimeConfigDefaultValuePrefixes.getOrDefault(key, "config?.$1L ?? "); + runtimeConfigDefaultValuePrefixes.getOrDefault(key, "config?.$1L ?? "); if (key.equals("retryMode") && target.equals(LanguageTarget.NODE)) { valuePrefix = """ - \n config?.retryMode ?? - """; + \n config?.retryMode ?? + """; } writer - .indent(2) - .writeInline("$1L: " + valuePrefix, key); + .indent(2) + .writeInline("$1L: " + valuePrefix, key); value.accept(writer); writer.unwrite("\n"); writer.dedent(2); @@ -211,19 +220,21 @@ void generate(LanguageTarget target) { writer.dedent(); writer.addRelativeTypeImport( - clientConfigName, null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, clientModuleName)); + clientConfigName, + null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, clientModuleName)); switch (target) { case NODE: case BROWSER: writer.addRelativeImport( - "getRuntimeConfig", - "getSharedRuntimeConfig", - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig.shared") - ); + "getRuntimeConfig", + "getSharedRuntimeConfig", + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig.shared")); writer.addImport( - "loadConfigsForDefaultMode", null, TypeScriptDependency.AWS_SMITHY_CLIENT); + "loadConfigsForDefaultMode", + null, + TypeScriptDependency.AWS_SMITHY_CLIENT); break; default: break; @@ -232,24 +243,25 @@ void generate(LanguageTarget target) { switch (target) { case NODE -> { writer.addImport( - "emitWarningIfUnsupportedVersion", null, TypeScriptDependency.AWS_SMITHY_CLIENT); + "emitWarningIfUnsupportedVersion", + null, + TypeScriptDependency.AWS_SMITHY_CLIENT); writer.addImport( - "resolveDefaultsModeConfig", null, - TypeScriptDependency.AWS_SDK_UTIL_DEFAULTS_MODE_NODE - ); + "resolveDefaultsModeConfig", + null, + TypeScriptDependency.AWS_SDK_UTIL_DEFAULTS_MODE_NODE); } case BROWSER -> { writer.addImport( - "resolveDefaultsModeConfig", null, - TypeScriptDependency.AWS_SDK_UTIL_DEFAULTS_MODE_BROWSER - ); + "resolveDefaultsModeConfig", + null, + TypeScriptDependency.AWS_SDK_UTIL_DEFAULTS_MODE_BROWSER); } case REACT_NATIVE -> { writer.addRelativeImport( - "getRuntimeConfig", - "getBrowserRuntimeConfig", - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig.browser") - ); + "getRuntimeConfig", + "getBrowserRuntimeConfig", + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig.browser")); } default -> { // checkstyle @@ -261,27 +273,28 @@ void generate(LanguageTarget target) { } private void generateHttpAuthSchemeConfig( - Map> configs, - TypeScriptWriter writer, - LanguageTarget target + Map> configs, + TypeScriptWriter writer, + LanguageTarget target ) { SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(integrations, model, settings); if (target.equals(LanguageTarget.SHARED)) { configs.put("httpAuthSchemeProvider", w -> { - w.write("$T", Symbol.builder() - .name("default" - + CodegenUtils.getServiceName(settings, model, symbolProvider) - + "HttpAuthSchemeProvider") - .namespace(AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY.getPackageName(), "/") - .build()); + w.write("$T", + Symbol.builder() + .name("default" + + CodegenUtils.getServiceName(settings, model, symbolProvider) + + "HttpAuthSchemeProvider") + .namespace(AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY.getPackageName(), "/") + .build()); }); } ServiceIndex serviceIndex = ServiceIndex.of(model); TopDownIndex topDownIndex = TopDownIndex.of(model); Map allEffectiveHttpAuthSchemes = - AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(service, serviceIndex, authIndex, topDownIndex); + AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(service, serviceIndex, authIndex, topDownIndex); List targetAuthSchemes = getHttpAuthSchemeTargets(target, allEffectiveHttpAuthSchemes); // Generate only if the "inherited" target is different from the current target @@ -317,29 +330,27 @@ private void generateHttpAuthSchemeConfig( w.indent(); if (entry.identityProvider == null) { w.writeInline(""" - { - schemeId: $S, - identityProvider: (ipc: IdentityProviderConfig) => - ipc.getIdentityProvider($S), - signer: $C, - }""", - entry.httpAuthScheme.getSchemeId(), - entry.httpAuthScheme.getSchemeId(), - entry.signer - ); + { + schemeId: $S, + identityProvider: (ipc: IdentityProviderConfig) => + ipc.getIdentityProvider($S), + signer: $C, + }""", + entry.httpAuthScheme.getSchemeId(), + entry.httpAuthScheme.getSchemeId(), + entry.signer); } else { w.writeInline(""" - { - schemeId: $S, - identityProvider: (ipc: IdentityProviderConfig) => - ipc.getIdentityProvider($S) || ($C), - signer: $C, - }""", - entry.httpAuthScheme.getSchemeId(), - entry.httpAuthScheme.getSchemeId(), - entry.identityProvider, - entry.signer - ); + { + schemeId: $S, + identityProvider: (ipc: IdentityProviderConfig) => + ipc.getIdentityProvider($S) || ($C), + signer: $C, + }""", + entry.httpAuthScheme.getSchemeId(), + entry.httpAuthScheme.getSchemeId(), + entry.identityProvider, + entry.signer); } w.write(","); w.dedent(); @@ -354,9 +365,9 @@ private static class HttpAuthSchemeTarget { public Consumer signer; HttpAuthSchemeTarget( - HttpAuthScheme httpAuthScheme, - Consumer identityProvider, - Consumer signer + HttpAuthScheme httpAuthScheme, + Consumer identityProvider, + Consumer signer ) { this.httpAuthScheme = httpAuthScheme; this.identityProvider = identityProvider; @@ -370,8 +381,8 @@ public boolean equals(Object other) { } HttpAuthSchemeTarget o = (HttpAuthSchemeTarget) other; return Objects.equals(httpAuthScheme, o.httpAuthScheme) - && Objects.equals(identityProvider, o.identityProvider) - && Objects.equals(signer, o.signer); + && Objects.equals(identityProvider, o.identityProvider) + && Objects.equals(signer, o.signer); } @Override @@ -381,19 +392,19 @@ public int hashCode() { } private List getHttpAuthSchemeTargets( - LanguageTarget target, - Map httpAuthSchemes + LanguageTarget target, + Map httpAuthSchemes ) { return getPartialHttpAuthSchemeTargets(target, httpAuthSchemes) - .values() - .stream() - .filter(httpAuthSchemeTarget -> httpAuthSchemeTarget.signer != null) - .toList(); + .values() + .stream() + .filter(httpAuthSchemeTarget -> httpAuthSchemeTarget.signer != null) + .toList(); } private Map getPartialHttpAuthSchemeTargets( - LanguageTarget target, - Map httpAuthSchemes + LanguageTarget target, + Map httpAuthSchemes ) { LanguageTarget inherited; if (target.equals(LanguageTarget.SHARED)) { @@ -408,10 +419,10 @@ private Map getPartialHttpAuthSchemeTargets( } Map httpAuthSchemeTargets = inherited == null - // SHARED inherits no HttpAuthSchemeTargets - ? new TreeMap<>() - // Otherwise, get inherited HttpAuthSchemeTargets - : getPartialHttpAuthSchemeTargets(inherited, httpAuthSchemes); + // SHARED inherits no HttpAuthSchemeTargets + ? new TreeMap<>() + // Otherwise, get inherited HttpAuthSchemeTargets + : getPartialHttpAuthSchemeTargets(inherited, httpAuthSchemes); for (HttpAuthScheme httpAuthScheme : httpAuthSchemes.values()) { // If HttpAuthScheme is not registered, skip code generation @@ -421,17 +432,17 @@ private Map getPartialHttpAuthSchemeTargets( // Get identity provider and signer for the current target Consumer identityProvider = - httpAuthScheme.getDefaultIdentityProviders().get(target); + httpAuthScheme.getDefaultIdentityProviders().get(target); Consumer signer = - httpAuthScheme.getDefaultSigners().get(target); + httpAuthScheme.getDefaultSigners().get(target); HttpAuthSchemeTarget existingEntry = - httpAuthSchemeTargets.get(httpAuthScheme.getSchemeId()); + httpAuthSchemeTargets.get(httpAuthScheme.getSchemeId()); // If HttpAuthScheme is not added yet, add the entry if (existingEntry == null) { httpAuthSchemeTargets.put(httpAuthScheme.getSchemeId(), - new HttpAuthSchemeTarget(httpAuthScheme, identityProvider, signer)); + new HttpAuthSchemeTarget(httpAuthScheme, identityProvider, signer)); continue; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java index 2b15a412546..6e16870a7e5 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/RuntimeExtensionsGenerator.java @@ -1,18 +1,7 @@ /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -37,12 +26,12 @@ public class RuntimeExtensionsGenerator { private final List integrations; public RuntimeExtensionsGenerator( - Model model, - TypeScriptSettings settings, - ServiceShape service, - SymbolProvider symbolProvider, - TypeScriptDelegator delegator, - List integrations + Model model, + TypeScriptSettings settings, + ServiceShape service, + SymbolProvider symbolProvider, + TypeScriptDelegator delegator, + List integrations ) { this.model = model; this.settings = settings; @@ -54,21 +43,21 @@ public RuntimeExtensionsGenerator( void generate() { String clientName = ReplaceLast.in( - ReplaceLast.in( - symbolProvider.toSymbol(service).getName(), - "Client", "" - ), - "client", "" - ); + ReplaceLast.in( + symbolProvider.toSymbol(service).getName(), + "Client", + ""), + "client", + ""); String template1Contents = TypeScriptUtils.loadResourceAsString(TEMPLATE_1) - .replace("${extensionConfigName}", clientName + "ExtensionConfiguration") - .replace("$", "$$") // sanitize template place holders. - .replace("$${getPartialExtensionConfigurations}", "${L@getPartialExtensionConfigurations}"); + .replace("${extensionConfigName}", clientName + "ExtensionConfiguration") + .replace("$", "$$") // sanitize template place holders. + .replace("$${getPartialExtensionConfigurations}", "${L@getPartialExtensionConfigurations}"); String template2Contents = TypeScriptUtils.loadResourceAsString(TEMPLATE_2) - .replace("$", "$$") // sanitize template place holders. - .replace("$${resolvePartialRuntimeConfigs}", "${L@resolvePartialRuntimeConfigs}"); + .replace("$", "$$") // sanitize template place holders. + .replace("$${resolvePartialRuntimeConfigs}", "${L@resolvePartialRuntimeConfigs}"); delegator.useFileWriter(Paths.get(CodegenUtils.SOURCE_FOLDER, FILENAME).toString(), writer -> { for (TypeScriptIntegration integration : integrations) { @@ -76,9 +65,11 @@ void generate() { writer.addDependency(configurationInterface.getExtensionConfigurationFn().right); writer.addDependency(configurationInterface.resolveRuntimeConfigFn().right); - writer.addImport(configurationInterface.getExtensionConfigurationFn().left, null, + writer.addImport(configurationInterface.getExtensionConfigurationFn().left, + null, configurationInterface.getExtensionConfigurationFn().right); - writer.addImport(configurationInterface.resolveRuntimeConfigFn().left, null, + writer.addImport(configurationInterface.resolveRuntimeConfigFn().left, + null, configurationInterface.resolveRuntimeConfigFn().right); }); } @@ -86,8 +77,9 @@ void generate() { writer.indent().onSection("getPartialExtensionConfigurations", original -> { for (TypeScriptIntegration integration : integrations) { integration.getExtensionConfigurationInterfaces(model, settings).forEach(configurationInterface -> { - writer.indent(2).write("$L(runtimeConfig),", - configurationInterface.getExtensionConfigurationFn().left); + writer.indent(2) + .write("$L(runtimeConfig),", + configurationInterface.getExtensionConfigurationFn().left); writer.dedent(2); }); } @@ -98,8 +90,9 @@ void generate() { writer.indent().onSection("resolvePartialRuntimeConfigs", original -> { for (TypeScriptIntegration integration : integrations) { integration.getExtensionConfigurationInterfaces(model, settings).forEach(configurationInterface -> { - writer.indent(2).write("$L(extensionConfiguration),", - configurationInterface.resolveRuntimeConfigFn().left); + writer.indent(2) + .write("$L(extensionConfiguration),", + configurationInterface.resolveRuntimeConfigFn().left); writer.dedent(2); }); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerCommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerCommandGenerator.java index 371c9a5f1bc..6e19ccc15e8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerCommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerCommandGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -42,7 +31,7 @@ final class ServerCommandGenerator implements Runnable { static final String COMMANDS_FOLDER = "operations"; private static final String NO_PROTOCOL_FOUND_SERDE_FUNCTION = - "(async (...args: any[]) => { throw new Error(\"No supported protocol was found\"); })"; + "(async (...args: any[]) => { throw new Error(\"No supported protocol was found\"); })"; private final TypeScriptSettings settings; private final Model model; @@ -120,14 +109,15 @@ private void renderNamespace(String typeName, StructureShape input) { // Streaming makes the type of the object being validated weird on occasion. // Using `Parameters` here means we don't have to try to derive the weird type twice writer.write("export const validate: (obj: Parameters[0]) => " - + "__ValidationFailure[] = $1T.validate;", symbol); + + "__ValidationFailure[] = $1T.validate;", symbol); }); } private void writeOutputType(String typeName, Optional outputShape) { if (outputShape.isPresent()) { writer.write("export interface $L extends $T {}", - typeName, symbolProvider.toSymbol(outputShape.get())); + typeName, + symbolProvider.toSymbol(outputShape.get())); } else { writer.write("export interface $L {}", typeName); } @@ -153,7 +143,9 @@ private void writeOperationType() { Symbol operationSymbol = symbolProvider.toSymbol(operation); writer.addImport("Operation", "__Operation", TypeScriptDependency.SERVER_COMMON); writer.write("export type $L = __Operation<$T, $T, Context>", - operationSymbol.getName(), inputType, outputType); + operationSymbol.getName(), + inputType, + outputType); writer.write(""); } @@ -163,29 +155,40 @@ private void writeOperationSerializer() { Symbol serverSymbol = symbolProvider.toSymbol(model.expectShape(settings.getService())); writer.addImport("OperationSerializer", "__OperationSerializer", TypeScriptDependency.SERVER_COMMON); - writer.openBlock("export class $L implements __OperationSerializer<$T, $S, $T> {", "}", - serializerName, serverSymbol, operationSymbol.getName(), errorsType, () -> { - if (protocolGenerator == null) { - writer.write("serialize = $L as any;", NO_PROTOCOL_FOUND_SERDE_FUNCTION); - writer.write("deserialize = $L as any;", NO_PROTOCOL_FOUND_SERDE_FUNCTION); - } else { - String serializerFunction = - ProtocolGenerator.getGenericSerFunctionName(operationSymbol) + "Response"; - writer.addRelativeImport(serializerFunction, null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER, - ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); - writer.write("serialize = $L;", serializerFunction); - String deserializerFunction = - ProtocolGenerator.getGenericDeserFunctionName(operationSymbol) + "Request"; - writer.addRelativeImport(deserializerFunction, null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER, - ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); - writer.write("deserialize = $L;", deserializerFunction); - } - writer.write(""); - writeErrorChecker(); - writeErrorHandler(); - }); + writer.openBlock("export class $L implements __OperationSerializer<$T, $S, $T> {", + "}", + serializerName, + serverSymbol, + operationSymbol.getName(), + errorsType, + () -> { + if (protocolGenerator == null) { + writer.write("serialize = $L as any;", NO_PROTOCOL_FOUND_SERDE_FUNCTION); + writer.write("deserialize = $L as any;", NO_PROTOCOL_FOUND_SERDE_FUNCTION); + } else { + String serializerFunction = + ProtocolGenerator.getGenericSerFunctionName(operationSymbol) + "Response"; + writer.addRelativeImport(serializerFunction, + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + ProtocolGenerator.PROTOCOLS_FOLDER, + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); + writer.write("serialize = $L;", serializerFunction); + String deserializerFunction = + ProtocolGenerator.getGenericDeserFunctionName(operationSymbol) + "Request"; + writer.addRelativeImport(deserializerFunction, + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + ProtocolGenerator.PROTOCOLS_FOLDER, + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); + writer.write("deserialize = $L;", deserializerFunction); + } + writer.write(""); + writeErrorChecker(); + writeErrorHandler(); + }); writer.write(""); } @@ -210,19 +213,22 @@ private void writeErrorChecker() { private void writeErrorHandler() { writer.addImport("ServerSerdeContext", null, TypeScriptDependency.SERVER_COMMON); - writer.openBlock("serializeError(error: $T, ctx: ServerSerdeContext): Promise<$T> {", "}", - errorsType, applicationProtocol.getResponseType(), () -> { - if (errors.isEmpty()) { - writer.write("throw error;"); - } else { - writer.openBlock("switch (error.name) {", "}", () -> { - for (StructureShape error : errors) { - writeErrorHandlerCase(error); + writer.openBlock("serializeError(error: $T, ctx: ServerSerdeContext): Promise<$T> {", + "}", + errorsType, + applicationProtocol.getResponseType(), + () -> { + if (errors.isEmpty()) { + writer.write("throw error;"); + } else { + writer.openBlock("switch (error.name) {", "}", () -> { + for (StructureShape error : errors) { + writeErrorHandlerCase(error); + } + writer.openBlock("default: {", "}", () -> writer.write("throw error;")); + }); } - writer.openBlock("default: {", "}", () -> writer.write("throw error;")); }); - } - }); writer.write(""); } @@ -233,9 +239,12 @@ private void writeErrorHandlerCase(StructureShape error) { writer.write("return $L(error, ctx);", NO_PROTOCOL_FOUND_SERDE_FUNCTION); } else { String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(errorSymbol) + "Error"; - writer.addRelativeImport(serializerFunction, null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER, - ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); + writer.addRelativeImport(serializerFunction, + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + ProtocolGenerator.PROTOCOLS_FOLDER, + ProtocolGenerator.getSanitizedName(protocolGenerator.getName()))); writer.write("return $L(error, ctx);", serializerFunction); } }); @@ -260,8 +269,8 @@ static void writeIndex( } fileManifest.writeFile( - Paths.get(CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER, COMMANDS_FOLDER, "index.ts") - .toString(), - writer.toString()); + Paths.get(CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER, COMMANDS_FOLDER, "index.ts") + .toString(), + writer.toString()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerGenerator.java index a3fe09e82f2..767ad2aea40 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.Iterator; @@ -29,10 +18,12 @@ final class ServerGenerator { private ServerGenerator() {} - static void generateOperationsType(SymbolProvider symbolProvider, - Shape serviceShape, - Set operations, - TypeScriptWriter writer) { + static void generateOperationsType( + SymbolProvider symbolProvider, + Shape serviceShape, + Set operations, + TypeScriptWriter writer + ) { Symbol serviceSymbol = symbolProvider.toSymbol(serviceShape); writer.writeInline("export type $L = ", serviceSymbol.expectProperty("operations", Symbol.class).getName()); for (Iterator iter = operations.iterator(); iter.hasNext();) { @@ -44,13 +35,16 @@ static void generateOperationsType(SymbolProvider symbolProvider, writer.write(";"); } - static void generateServiceHandler(SymbolProvider symbolProvider, - Shape serviceShape, - Set operations, - TypeScriptWriter writer) { + static void generateServiceHandler( + SymbolProvider symbolProvider, + Shape serviceShape, + Set operations, + TypeScriptWriter writer + ) { addCommonHandlerImports(writer); - writer.addImport("UnknownOperationException", "__UnknownOperationException", - TypeScriptDependency.SERVER_COMMON); + writer.addImport("UnknownOperationException", + "__UnknownOperationException", + TypeScriptDependency.SERVER_COMMON); Symbol serviceSymbol = symbolProvider.toSymbol(serviceShape); Symbol handlerSymbol = serviceSymbol.expectProperty("handler", Symbol.class); @@ -64,10 +58,11 @@ static void generateServiceHandler(SymbolProvider symbolProvider, writer.write("private readonly service: $T;", serviceSymbol); writer.write("private readonly mux: __Mux<$S, $T>;", serviceShape.getId().getName(), operationsType); writer.write("private readonly serializerFactory: (operation: T) => " - + "__OperationSerializer<$T, T, __ServiceException>;", - operationsType, serviceSymbol); + + "__OperationSerializer<$T, T, __ServiceException>;", + operationsType, + serviceSymbol); writer.write("private readonly serializeFrameworkException: (e: __SmithyFrameworkException, " - + "ctx: __ServerSerdeContext) => Promise<__HttpResponse>;"); + + "ctx: __ServerSerdeContext) => Promise<__HttpResponse>;"); writer.write("private readonly validationCustomizer: __ValidationCustomizer<$T>;", operationsType); writer.writeDocs(() -> { writer.write("Construct a $T handler.", serviceSymbol); @@ -78,7 +73,7 @@ static void generateServiceHandler(SymbolProvider symbolProvider, writer.writeInline("@param serializerFactory A factory for an {@link __OperationSerializer} for each "); writer.write("operation in $T that ", serviceSymbol); writer.writeInline(" ") - .write("handles deserialization of requests and serialization of responses"); + .write("handles deserialization of requests and serialization of responses"); writer.write("@param serializeFrameworkException A function that can serialize " + "{@link __SmithyFrameworkException}s"); writer.write("@param validationCustomizer A {@link __ValidationCustomizer} for turning validation " @@ -88,8 +83,9 @@ static void generateServiceHandler(SymbolProvider symbolProvider, writer.write("service: $T,", serviceSymbol); writer.write("mux: __Mux<$S, $T>,", serviceShape.getId().getName(), operationsType); writer.write("serializerFactory:(op: T) => " - + "__OperationSerializer<$T, T, __ServiceException>,", - operationsType, serviceSymbol); + + "__OperationSerializer<$T, T, __ServiceException>,", + operationsType, + serviceSymbol); writer.write("serializeFrameworkException: (e: __SmithyFrameworkException, ctx: __ServerSerdeContext) " + "=> Promise<__HttpResponse>,"); writer.write("validationCustomizer: __ValidationCustomizer<$T>", operationsType); @@ -116,7 +112,8 @@ static void generateServiceHandler(SymbolProvider symbolProvider, writer.write("return handle(request, context, $1S, this.serializerFactory($1S), " + "this.service.$1L, this.serializeFrameworkException, $2T.validate, " + "this.validationCustomizer);", - operationSymbol.getName(), inputSymbol); + operationSymbol.getName(), + inputSymbol); }); } }); @@ -124,10 +121,12 @@ static void generateServiceHandler(SymbolProvider symbolProvider, }); } - static void generateOperationHandler(SymbolProvider symbolProvider, - Shape serviceShape, - OperationShape operation, - TypeScriptWriter writer) { + static void generateOperationHandler( + SymbolProvider symbolProvider, + Shape serviceShape, + OperationShape operation, + TypeScriptWriter writer + ) { addCommonHandlerImports(writer); writeSerdeContextBase(writer); @@ -147,14 +146,16 @@ static void generateOperationHandler(SymbolProvider symbolProvider, writer.write("private readonly operation: __Operation<$T, $T, Context>;", inputSymbol, outputSymbol); writer.write("private readonly mux: __Mux<$S, $S>;", serviceShape.getId().getName(), operationName); writer.write("private readonly serializer: __OperationSerializer<$T, $S, $T>;", - serviceSymbol, operationName, errorsSymbol); + serviceSymbol, + operationName, + errorsSymbol); writer.write("private readonly serializeFrameworkException: (e: __SmithyFrameworkException, " + "ctx: __ServerSerdeContext) => Promise<__HttpResponse>;"); writer.write("private readonly validationCustomizer: __ValidationCustomizer<$S>;", operationName); writer.writeDocs(() -> { writer.write("Construct a $T handler.", operationSymbol); writer.write("@param operation The {@link __Operation} implementation that supplies the business " - + "logic for $1T", operationSymbol); + + "logic for $1T", operationSymbol); writer.writeInline("@param mux The {@link __Mux} that verifies which service and operation are being "); writer.write("invoked by a given {@link __HttpRequest}"); writer.write("@param serializer An {@link __OperationSerializer} for $T that ", operationSymbol); @@ -169,7 +170,9 @@ static void generateOperationHandler(SymbolProvider symbolProvider, writer.write("operation: __Operation<$T, $T, Context>,", inputSymbol, outputSymbol); writer.write("mux: __Mux<$S, $S>,", serviceShape.getId().getName(), operationName); writer.write("serializer: __OperationSerializer<$T, $S, $T>,", - serviceSymbol, operationName, errorsSymbol); + serviceSymbol, + operationName, + errorsSymbol); writer.write("serializeFrameworkException: (e: __SmithyFrameworkException, ctx: __ServerSerdeContext) " + "=> Promise<__HttpResponse>,"); writer.write("validationCustomizer: __ValidationCustomizer<$S>", operationName); @@ -182,20 +185,20 @@ static void generateOperationHandler(SymbolProvider symbolProvider, writer.write("this.validationCustomizer = validationCustomizer;"); writer.closeBlock("}"); writer.openBlock("async handle(request: __HttpRequest, context: Context): Promise<__HttpResponse> {", - "}", - () -> { - writer.write("const target = this.mux.match(request);"); - writer.openBlock("if (target === undefined) {", "}", () -> { - writer.write("console.log('Received a request that did not match $L.$L. This indicates a " - + "misconfiguration.');", serviceShape.getId(), operation.getId().getName()); - writer.write("return this.serializeFrameworkException(new __InternalFailureException(), " - + "serdeContextBase);"); + "}", + () -> { + writer.write("const target = this.mux.match(request);"); + writer.openBlock("if (target === undefined) {", "}", () -> { + writer.write("console.log('Received a request that did not match $L.$L. This indicates a " + + "misconfiguration.');", serviceShape.getId(), operation.getId().getName()); + writer.write("return this.serializeFrameworkException(new __InternalFailureException(), " + + "serdeContextBase);"); + }); + writer.write("return handle(request, context, $S, this.serializer, this.operation, " + + "this.serializeFrameworkException, $T.validate, this.validationCustomizer);", + operationName, + inputSymbol); }); - writer.write("return handle(request, context, $S, this.serializer, this.operation, " - + "this.serializeFrameworkException, $T.validate, this.validationCustomizer);", - operationName, inputSymbol); - } - ); }); } @@ -252,7 +255,7 @@ private static void writeHandleFunction(TypeScriptWriter writer) { writer.write("let validationFailures = validationFn(input);"); writer.openBlock("if (validationFailures && validationFailures.length > 0) {", "}", () -> { writer.write("let validationException = validationCustomizer({ operation: operationName }, " - + "validationFailures);"); + + "validationFailures);"); writer.openBlock("if (validationException) {", "}", () -> { writer.write("return serializer.serializeError(validationException, serdeContextBase);"); }); @@ -291,25 +294,26 @@ private static void writeSerdeContextBase(TypeScriptWriter writer) { }); } - static void generateServerInterfaces(SymbolProvider symbolProvider, - ServiceShape service, - Set operations, - TypeScriptWriter writer) { + static void generateServerInterfaces( + SymbolProvider symbolProvider, + ServiceShape service, + Set operations, + TypeScriptWriter writer + ) { writer.addImport("Operation", "__Operation", TypeScriptDependency.SERVER_COMMON); String serviceInterfaceName = symbolProvider.toSymbol(service).getName(); writer.openCollapsibleBlock( - "export interface $L {", - "}", - !operations.isEmpty(), - serviceInterfaceName, - () -> { - for (OperationShape operation : operations) { - Symbol symbol = symbolProvider.toSymbol(operation); - writer.write("$L: $T", symbol.getName(), symbol); - } - } - ); + "export interface $L {", + "}", + !operations.isEmpty(), + serviceInterfaceName, + () -> { + for (OperationShape operation : operations) { + Symbol symbol = symbolProvider.toSymbol(operation); + writer.write("$L: $T", symbol.getName(), symbol); + } + }); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerSymbolVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerSymbolVisitor.java index 746c4338273..e30749279c2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerSymbolVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerSymbolVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import static software.amazon.smithy.typescript.codegen.TypeScriptDependency.SERVER_COMMON; @@ -69,7 +58,7 @@ final class ServerSymbolVisitor extends ShapeVisitor.Default implements @Override public Symbol toSymbol(Shape shape) { if (shape.getType() == ShapeType.SERVICE - || shape.getType() == ShapeType.OPERATION) { + || shape.getType() == ShapeType.OPERATION) { Symbol symbol = shape.accept(this); LOGGER.fine(() -> "Creating symbol from " + shape + ": " + symbol); return escaper.escapeSymbol(shape, symbol); @@ -120,8 +109,9 @@ private String flattenShapeName(ToShapeId id) { } private Symbol.Builder createGeneratedSymbolBuilder(Shape shape, String typeName, String namespace) { - String prefixedNamespace = Paths.get(".", CodegenUtils.SOURCE_FOLDER, - (namespace.startsWith(".") ? namespace.substring(1) : namespace)).toString(); + String prefixedNamespace = Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + (namespace.startsWith(".") ? namespace.substring(1) : namespace)).toString(); return createSymbolBuilder(shape, typeName, prefixedNamespace) .definitionFile(toFilename(prefixedNamespace)); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceAggregatedClientGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceAggregatedClientGenerator.java index 9a6d5fb2ffa..086c225b5e2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceAggregatedClientGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceAggregatedClientGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.Set; @@ -48,12 +37,12 @@ final class ServiceAggregatedClientGenerator implements Runnable { private final ApplicationProtocol applicationProtocol; ServiceAggregatedClientGenerator( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - String aggregateClientName, - TypeScriptWriter writer, - ApplicationProtocol applicationProtocol + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + String aggregateClientName, + TypeScriptWriter writer, + ApplicationProtocol applicationProtocol ) { this.settings = settings; this.model = model; @@ -63,9 +52,9 @@ final class ServiceAggregatedClientGenerator implements Runnable { this.aggregateClientName = aggregateClientName; this.applicationProtocol = applicationProtocol; serviceSymbol = symbolProvider.toSymbol(service) - .toBuilder() - .putProperty("typeOnly", false) - .build(); + .toBuilder() + .putProperty("typeOnly", false) + .build(); } @Override @@ -75,9 +64,9 @@ public void run() { writer.openBlock("const commands = {", "};", () -> { for (OperationShape operation : containedOperations) { Symbol operationSymbol = symbolProvider.toSymbol(operation) - .toBuilder() - .putProperty("typeOnly", false) - .build(); + .toBuilder() + .putProperty("typeOnly", false) + .build(); writer.write("$T,", operationSymbol); } }); @@ -92,35 +81,36 @@ public void run() { Symbol output = operationSymbol.expectProperty("outputType", Symbol.class); writer.addUseImports(operationSymbol); String methodName = StringUtils.uncapitalize( - operationSymbol.getName().replaceAll("Command$", "") - ); + operationSymbol.getName().replaceAll("Command$", "")); // Generate a multiple overloaded methods for each command. writer.writeDocs( - "@see {@link " + operationSymbol.getName() + "}" - ); - boolean inputOptional = model.getShape(operation.getInputShape()).map( - shape -> shape.getAllMembers().values().stream().noneMatch(MemberShape::isRequired) - ).orElse(true); + "@see {@link " + operationSymbol.getName() + "}"); + boolean inputOptional = model.getShape(operation.getInputShape()) + .map( + shape -> shape.getAllMembers().values().stream().noneMatch(MemberShape::isRequired)) + .orElse(true); if (inputOptional) { writer.write("$L(): Promise<$T>;", methodName, output); } writer.write(""" - $1L( - args: $2T, - options?: $3T - ): Promise<$4T>; - $1L( - args: $2T, - cb: (err: any, data?: $4T) => void - ): void; - $1L( - args: $2T, - options: $3T, - cb: (err: any, data?: $4T) => void - ): void;""", - methodName, input, applicationProtocol.getOptionsType(), output - ); + $1L( + args: $2T, + options?: $3T + ): Promise<$4T>; + $1L( + args: $2T, + cb: (err: any, data?: $4T) => void + ): void; + $1L( + args: $2T, + options: $3T, + cb: (err: any, data?: $4T) => void + ): void;""", + methodName, + input, + applicationProtocol.getOptionsType(), + output); writer.write(""); } writer.unwrite("\n"); @@ -131,8 +121,9 @@ public void run() { // Generate the client and extend from the bare-bones client. writer.writeShapeDocs(service); writer.write("export class $L extends $T implements $L {}", - aggregateClientName, serviceSymbol, aggregateClientName - ); + aggregateClientName, + serviceSymbol, + aggregateClientName); writer.addImport("createAggregatedClient", null, TypeScriptDependency.AWS_SMITHY_CLIENT); writer.write("createAggregatedClient(commands, $L);", aggregateClientName); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java index 958cfd68af7..9f7e9ecb61e 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -64,13 +53,13 @@ public final class ServiceBareBonesClientGenerator implements Runnable { private final ApplicationProtocol applicationProtocol; ServiceBareBonesClientGenerator( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - TypeScriptWriter writer, - List integrations, - List runtimePlugins, - ApplicationProtocol applicationProtocol + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + TypeScriptWriter writer, + List integrations, + List runtimePlugins, + ApplicationProtocol applicationProtocol ) { this.settings = settings; this.model = model; @@ -101,23 +90,28 @@ public static String getResolvedConfigTypeName(Symbol symbol) { public void run() { writer.addImport("Client", "__Client", TypeScriptDependency.AWS_SMITHY_CLIENT); writer.write("export { __Client };\n"); - writer.addRelativeImport("getRuntimeConfig", "__getRuntimeConfig", - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig")); + writer.addRelativeImport("getRuntimeConfig", + "__getRuntimeConfig", + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig")); // Normalize the input and output types of the command to account for // things like an operation adding input where there once wasn't any // input, adding output, naming differences between services, etc. - writeInputOutputTypeUnion("ServiceInputTypes", writer, - operationSymbol -> operationSymbol.getProperty("inputType", Symbol.class), writer -> { - // Use an empty object if an operation doesn't define input. - writer.write("| {}"); - }); - writeInputOutputTypeUnion("ServiceOutputTypes", writer, - operationSymbol -> operationSymbol.getProperty("outputType", Symbol.class), writer -> { - // Use a MetadataBearer if an operation doesn't define output. - writer.addTypeImport("MetadataBearer", "__MetadataBearer", TypeScriptDependency.SMITHY_TYPES); - writer.write("| __MetadataBearer"); - }); + writeInputOutputTypeUnion("ServiceInputTypes", + writer, + operationSymbol -> operationSymbol.getProperty("inputType", Symbol.class), + writer -> { + // Use an empty object if an operation doesn't define input. + writer.write("| {}"); + }); + writeInputOutputTypeUnion("ServiceOutputTypes", + writer, + operationSymbol -> operationSymbol.getProperty("outputType", Symbol.class), + writer -> { + // Use a MetadataBearer if an operation doesn't define output. + writer.addTypeImport("MetadataBearer", "__MetadataBearer", TypeScriptDependency.SMITHY_TYPES); + writer.write("| __MetadataBearer"); + }); generateConfig(); writer.write(""); @@ -125,10 +119,10 @@ public void run() { } private void writeInputOutputTypeUnion( - String typeName, - TypeScriptWriter writer, - Function> mapper, - Consumer defaultTypeGenerator + String typeName, + TypeScriptWriter writer, + Function> mapper, + Consumer defaultTypeGenerator ) { TopDownIndex topDownIndex = TopDownIndex.of(model); Set containedOperations = topDownIndex.getContainedOperations(service); @@ -155,22 +149,24 @@ private void writeInputOutputTypeUnion( } private void generateConfig() { - writer.addRelativeTypeImport("RuntimeExtensionsConfig", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeExtensions")); + writer.addRelativeTypeImport("RuntimeExtensionsConfig", + null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeExtensions")); writer.addTypeImport("SmithyConfiguration", "__SmithyConfiguration", TypeScriptDependency.AWS_SMITHY_CLIENT); - writer.addTypeImport("SmithyResolvedConfiguration", "__SmithyResolvedConfiguration", - TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.addTypeImport("SmithyResolvedConfiguration", + "__SmithyResolvedConfiguration", + TypeScriptDependency.AWS_SMITHY_CLIENT); // Hook for intercepting the client configuration. writer.pushState(ClientConfigCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .symbolProvider(symbolProvider) - .integrations(integrations) - .runtimeClientPlugins(runtimePlugins) - .applicationProtocol(applicationProtocol) - .build()); + .settings(settings) + .model(model) + .service(service) + .symbolProvider(symbolProvider) + .integrations(integrations) + .runtimeClientPlugins(runtimePlugins) + .applicationProtocol(applicationProtocol) + .build()); generateClientDefaults(); @@ -178,10 +174,9 @@ private void generateConfig() { // The default configuration type is always just the base-level // Smithy configuration requirements. writer.write( - "export type $LType = Partial<__SmithyConfiguration<$T>> &", - configType, - applicationProtocol.getOptionsType() - ); + "export type $LType = Partial<__SmithyConfiguration<$T>> &", + configType, + applicationProtocol.getOptionsType()); writer.indent(); writer.write("ClientDefaults &"); @@ -196,17 +191,17 @@ private void generateConfig() { for (SymbolReference symbolReference : inputTypes) { if (symbolReference.getAlias().equals("EndpointInputConfig")) { writer.addTypeImport( - "EndpointParameters", - null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY - ); + "EndpointParameters", + null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); writer.write("$T<$L> &", symbolReference, "EndpointParameters"); } else { writer.write("$T &", symbolReference); } } - writer.addTypeImport("ClientInputEndpointParameters", null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); + writer.addTypeImport("ClientInputEndpointParameters", + null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); writer.write("ClientInputEndpointParameters;"); writer.dedent(); } @@ -220,35 +215,38 @@ private void generateConfig() { writer.write(""); writer.writeDocs("@public"); writer.write("export type $LType = __SmithyResolvedConfiguration<$T> &", - resolvedConfigType, applicationProtocol.getOptionsType()); + resolvedConfigType, + applicationProtocol.getOptionsType()); writer.indent(); writer.write("Required &"); writer.write("RuntimeExtensionsConfig &"); if (!inputTypes.isEmpty()) { runtimePlugins.stream() - .flatMap(p -> OptionalUtils.stream(p.getResolvedConfig())) - .forEach(symbol -> { - if (symbol.getAlias().equals("EndpointResolvedConfig")) { - writer.addTypeImport( - "EndpointParameters", - null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY - ); - writer.write("$T<$L> &", symbol, "EndpointParameters"); - } else { - writer.write("$T &", symbol); - } - }); - writer.addTypeImport("ClientResolvedEndpointParameters", null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); + .flatMap(p -> OptionalUtils.stream(p.getResolvedConfig())) + .forEach(symbol -> { + if (symbol.getAlias().equals("EndpointResolvedConfig")) { + writer.addTypeImport( + "EndpointParameters", + null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); + writer.write("$T<$L> &", symbol, "EndpointParameters"); + } else { + writer.write("$T &", symbol); + } + }); + writer.addTypeImport("ClientResolvedEndpointParameters", + null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); writer.write("ClientResolvedEndpointParameters;"); writer.dedent(); } writer.writeDocs(String.format("%s The resolved configuration interface of %s class. This is resolved and" - + " normalized from the {@link %s | constructor configuration interface}.", "@public\n\n", - symbol.getName(), configType)); + + " normalized from the {@link %s | constructor configuration interface}.", + "@public\n\n", + symbol.getName(), + configType)); writer.write("export interface $1L extends $1LType {}", resolvedConfigType); writer.popState(); @@ -263,31 +261,30 @@ private void generateClientDefaults() { Runnable innerContent = () -> { writer.addTypeImport("HttpHandlerUserInput", "__HttpHandlerUserInput", TypeScriptDependency.PROTOCOL_HTTP); writer.writeDocs( - "The HTTP handler to use or its constructor options. Fetch in browser and Https in Nodejs." - ); + "The HTTP handler to use or its constructor options. Fetch in browser and Https in Nodejs."); writer.write("requestHandler?: __HttpHandlerUserInput;\n"); writer.addTypeImport("HashConstructor", "__HashConstructor", TypeScriptDependency.SMITHY_TYPES); writer.addTypeImport("ChecksumConstructor", "__ChecksumConstructor", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs(""" - A constructor for a class implementing the {@link @smithy/types#ChecksumConstructor} interface - that computes the SHA-256 HMAC or checksum of a string or binary buffer. - @internal"""); + A constructor for a class implementing the {@link @smithy/types#ChecksumConstructor} interface + that computes the SHA-256 HMAC or checksum of a string or binary buffer. + @internal"""); writer.write("sha256?: __ChecksumConstructor | __HashConstructor;\n"); writer.addTypeImport("UrlParser", "__UrlParser", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs("The function that will be used to convert strings into HTTP endpoints.\n" - + "@internal"); + + "@internal"); writer.write("urlParser?: __UrlParser;\n"); writer.addTypeImport("BodyLengthCalculator", "__BodyLengthCalculator", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs("A function that can calculate the length of a request body.\n" - + "@internal"); + + "@internal"); writer.write("bodyLengthChecker?: __BodyLengthCalculator;\n"); writer.addTypeImport("StreamCollector", "__StreamCollector", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs("A function that converts a stream into an array of bytes.\n" - + "@internal"); + + "@internal"); writer.write("streamCollector?: __StreamCollector;\n"); // Note: Encoder and Decoder are both used for base64 and UTF. @@ -295,27 +292,27 @@ private void generateClientDefaults() { writer.addTypeImport("Decoder", "__Decoder", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs("The function that will be used to convert a base64-encoded string to a byte array.\n" - + "@internal"); + + "@internal"); writer.write("base64Decoder?: __Decoder;\n"); writer.writeDocs("The function that will be used to convert binary data to a base64-encoded string.\n" - + "@internal"); + + "@internal"); writer.write("base64Encoder?: __Encoder;\n"); writer.writeDocs("The function that will be used to convert a UTF8-encoded string to a byte array.\n" - + "@internal"); + + "@internal"); writer.write("utf8Decoder?: __Decoder;\n"); writer.writeDocs("The function that will be used to convert binary data to a UTF-8 encoded string.\n" - + "@internal"); + + "@internal"); writer.write("utf8Encoder?: __Encoder;\n"); writer.writeDocs("The runtime environment.\n" - + "@internal"); + + "@internal"); writer.write("runtime?: string;\n"); writer.writeDocs("Disable dynamically changing the endpoint of the client based on the hostPrefix \n" - + "trait of an operation."); + + "trait of an operation."); writer.write("disableHostPrefix?: boolean;\n"); // Write custom configuration dependencies. @@ -324,12 +321,11 @@ private void generateClientDefaults() { } }; writer.writeDocs("@public") - .openBlock( - "export interface ClientDefaults extends Partial<__SmithyConfiguration<$T>> {", - "}\n", - applicationProtocol.getOptionsType(), - innerContent - ); + .openBlock( + "export interface ClientDefaults extends Partial<__SmithyConfiguration<$T>> {", + "}\n", + applicationProtocol.getOptionsType(), + innerContent); } private void generateService() { @@ -341,35 +337,39 @@ export class $L extends __Client< ServiceInputTypes, ServiceOutputTypes, $L - > {""", "}", - symbol.getName(), applicationProtocol.getOptionsType(), resolvedConfigType, () -> { - generateClientProperties(); - generateConstructor(); - writer.write(""); - generateDestroyMethod(); - // Hook for adding more methods to the client. - writer.injectSection(ClientBodyExtraCodeSection.builder() + > {""", + "}", + symbol.getName(), + applicationProtocol.getOptionsType(), + resolvedConfigType, + () -> { + generateClientProperties(); + generateConstructor(); + writer.write(""); + generateDestroyMethod(); + // Hook for adding more methods to the client. + writer.injectSection(ClientBodyExtraCodeSection.builder() + .settings(settings) + .model(model) + .service(service) + .symbolProvider(symbolProvider) + .integrations(integrations) + .runtimeClientPlugins(runtimePlugins) + .applicationProtocol(applicationProtocol) + .build()); + }); + } + + private void generateClientProperties() { + // Hook for adding/changing client properties. + writer.pushState(ClientPropertiesCodeSection.builder() .settings(settings) .model(model) .service(service) .symbolProvider(symbolProvider) .integrations(integrations) - .runtimeClientPlugins(runtimePlugins) .applicationProtocol(applicationProtocol) .build()); - }); - } - - private void generateClientProperties() { - // Hook for adding/changing client properties. - writer.pushState(ClientPropertiesCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .symbolProvider(symbolProvider) - .integrations(integrations) - .applicationProtocol(applicationProtocol) - .build()); writer.writeDocs(String.format("The resolved configuration of %s class. This is resolved and normalized from " + "the {@link %s | constructor configuration interface}.", symbol.getName(), configType)); writer.write("readonly config: $L;\n", resolvedConfigType); @@ -377,30 +377,32 @@ private void generateClientProperties() { } private void generateConstructor() { - writer.addTypeImport("CheckOptionalClientConfig", "__CheckOptionalClientConfig", - TypeScriptDependency.SMITHY_TYPES); + writer.addTypeImport("CheckOptionalClientConfig", + "__CheckOptionalClientConfig", + TypeScriptDependency.SMITHY_TYPES); writer.openBlock("constructor(...[configuration]: __CheckOptionalClientConfig<$L>) {", "}", configType, () -> { // Hook for adding/changing the client constructor. writer.pushState(ClientConstructorCodeSection.builder() - .service(service) - .runtimeClientPlugins(runtimePlugins) - .model(model) - .build()); + .service(service) + .runtimeClientPlugins(runtimePlugins) + .model(model) + .build()); int configVariable = 0; String initialConfigVar = generateConfigVariable(configVariable); writer.write("const $L = __getRuntimeConfig(configuration || {});", - initialConfigVar); + initialConfigVar); writer.write("super($L as any);", initialConfigVar); writer.write("this.initConfig = $L;", initialConfigVar); configVariable++; - writer.addImport("resolveClientEndpointParameters", null, - EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); + writer.addImport("resolveClientEndpointParameters", + null, + EndpointsV2Generator.ENDPOINT_PARAMETERS_DEPENDENCY); writer.write("const $L = $L($L);", - generateConfigVariable(configVariable), - "resolveClientEndpointParameters", - generateConfigVariable(configVariable - 1)); + generateConfigVariable(configVariable), + "resolveClientEndpointParameters", + generateConfigVariable(configVariable - 1)); // Add runtime plugin "resolve" method calls. These are invoked one // after the other until all the runtime plugins have been called. @@ -412,7 +414,9 @@ private void generateConstructor() { configVariable++; // Construct additional parameters string Map paramsMap = plugin.getAdditionalResolveFunctionParameters( - model, service, null); + model, + service, + null); List additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap); String additionalParamsString = additionalParameters.isEmpty() ? "" @@ -435,23 +439,25 @@ private void generateConstructor() { } } - writer.addRelativeImport("resolveRuntimeExtensions", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeExtensions")); + writer.addRelativeImport("resolveRuntimeExtensions", + null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeExtensions")); configVariable++; writer.write("const $L = resolveRuntimeExtensions($L, configuration?.extensions || []);", - generateConfigVariable(configVariable), - generateConfigVariable(configVariable - 1)); + generateConfigVariable(configVariable), + generateConfigVariable(configVariable - 1)); writer.write("this.config = $L;", generateConfigVariable(configVariable)); if (SchemaGenerationAllowlist.allows(service.getId(), settings)) { writer.addImportSubmodule( - "getSchemaSerdePlugin", null, - TypeScriptDependency.SMITHY_CORE, "/schema" - ); + "getSchemaSerdePlugin", + null, + TypeScriptDependency.SMITHY_CORE, + "/schema"); writer.write(""" - this.middlewareStack.use(getSchemaSerdePlugin(this.config));"""); + this.middlewareStack.use(getSchemaSerdePlugin(this.config));"""); } // Add runtime plugins that contain middleware to the middleware stack @@ -460,7 +466,9 @@ private void generateConstructor() { plugin.getPluginFunction().ifPresent(pluginSymbol -> { // Construct additional parameters string Map paramsMap = plugin.getAdditionalPluginFunctionParameters( - model, service, null); + model, + service, + null); // Construct writer context Map symbolMap = new HashMap<>(); @@ -474,7 +482,7 @@ private void generateConstructor() { writer.putContext(symbolMap); boolean isAuthSchemeEndpointRuleSetPlugin = - pluginSymbol.getAlias().equals("getHttpAuthSchemeEndpointRuleSetPlugin"); + pluginSymbol.getAlias().equals("getHttpAuthSchemeEndpointRuleSetPlugin"); if (isAuthSchemeEndpointRuleSetPlugin) { writer.openBlock("this.middlewareStack.use("); @@ -486,15 +494,15 @@ private void generateConstructor() { List additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap); Map clientAddParamsWriterConsumers = - plugin.getClientAddParamsWriterConsumers(); + plugin.getClientAddParamsWriterConsumers(); boolean noAdditionalParams = additionalParameters.isEmpty() - && clientAddParamsWriterConsumers.isEmpty(); + && clientAddParamsWriterConsumers.isEmpty(); if (!noAdditionalParams) { writer.writeInline(", "); boolean hasInnerContent = - !clientAddParamsWriterConsumers.isEmpty() || !additionalParameters.isEmpty(); + !clientAddParamsWriterConsumers.isEmpty() || !additionalParameters.isEmpty(); if (!hasInnerContent) { writer.writeInline("{}"); @@ -508,17 +516,16 @@ private void generateConstructor() { clientAddParamsWriterConsumers.forEach((key, consumer) -> { writer.write("$L: $C,", key, (Consumer) (w2 -> { consumer.accept( - w2, - ClientBodyExtraCodeSection.builder() - .settings(settings) - .model(model) - .service(service) - .symbolProvider(symbolProvider) - .integrations(integrations) - .runtimeClientPlugins(runtimePlugins) - .applicationProtocol(applicationProtocol) - .build() - ); + w2, + ClientBodyExtraCodeSection.builder() + .settings(settings) + .model(model) + .service(service) + .symbolProvider(symbolProvider) + .integrations(integrations) + .runtimeClientPlugins(runtimePlugins) + .applicationProtocol(applicationProtocol) + .build()); })); }); writer.dedent(); @@ -553,8 +560,8 @@ private void generateDestroyMethod() { } writer.openBlock("destroy(): void {", "}", () -> { writer.pushState(ClientDestroyCodeSection.builder() - .runtimeClientPlugins(runtimePlugins) - .build()); + .runtimeClientPlugins(runtimePlugins) + .build()); for (RuntimeClientPlugin plugin : runtimePlugins) { plugin.getDestroyFunction().ifPresent(destroy -> { writer.write("$T(this.config);", destroy); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SmithyCoreSubmodules.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SmithyCoreSubmodules.java index 796c7b9efd8..8052cc78ec9 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SmithyCoreSubmodules.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SmithyCoreSubmodules.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; /** diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructureGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructureGenerator.java index 86874e91507..f8fc2dcfa3d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructureGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructureGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers; @@ -81,17 +70,24 @@ final class StructureGenerator implements Runnable { * to {@link RequiredMemberMode#NULLABLE}. */ StructureGenerator(Model model, SymbolProvider symbolProvider, TypeScriptWriter writer, StructureShape shape) { - this(model, symbolProvider, writer, shape, false, - RequiredMemberMode.NULLABLE, false); + this(model, + symbolProvider, + writer, + shape, + false, + RequiredMemberMode.NULLABLE, + false); } - StructureGenerator(Model model, - SymbolProvider symbolProvider, - TypeScriptWriter writer, - StructureShape shape, - boolean includeValidation, - RequiredMemberMode requiredMemberMode, - boolean schemaMode) { + StructureGenerator( + Model model, + SymbolProvider symbolProvider, + TypeScriptWriter writer, + StructureShape shape, + boolean includeValidation, + RequiredMemberMode requiredMemberMode, + boolean schemaMode + ) { this.model = model; this.symbolProvider = symbolProvider; this.writer = writer; @@ -166,10 +162,11 @@ private void renderNonErrorStructure() { writer.writeShapeDocs(shape); // Find symbol references with the "extends" property. - String extendsFrom = symbol.getReferences().stream() - .filter(ref -> ref.getProperty(SymbolVisitor.IMPLEMENTS_INTERFACE_PROPERTY).isPresent()) - .map(SymbolReference::getAlias) - .collect(Collectors.joining(", ")); + String extendsFrom = symbol.getReferences() + .stream() + .filter(ref -> ref.getProperty(SymbolVisitor.IMPLEMENTS_INTERFACE_PROPERTY).isPresent()) + .map(SymbolReference::getAlias) + .collect(Collectors.joining(", ")); if (extendsFrom.isEmpty()) { writer.openBlock("export interface $L {", symbol.getName()); @@ -178,11 +175,11 @@ private void renderNonErrorStructure() { } StructuredMemberWriter config = new StructuredMemberWriter( - model, - symbolProvider, - shape.getAllMembers().values(), - this.requiredMemberMode, - sensitiveDataFinder); + model, + symbolProvider, + shape.getAllMembers().values(), + this.requiredMemberMode, + sensitiveDataFinder); config.writeMembers(writer, shape); writer.closeBlock("}"); writer.write(""); @@ -195,13 +192,14 @@ private void renderStructureNamespace(StructuredMemberWriter structuredMemberWri if (sensitiveDataFinder.findsSensitiveDataIn(shape) && !schemaMode) { writer.writeDocs("@internal"); - writer.openBlock("export const $LFilterSensitiveLog = ($L: $L): any => ({", "})", - symbol.getName(), - objectParam, - symbol.getName(), - () -> { - structuredMemberWriter.writeFilterSensitiveLog(writer, objectParam); - }); + writer.openBlock("export const $LFilterSensitiveLog = ($L: $L): any => ({", + "})", + symbol.getName(), + objectParam, + symbol.getName(), + () -> { + structuredMemberWriter.writeFilterSensitiveLog(writer, objectParam); + }); } if (!includeValidation) { @@ -285,9 +283,11 @@ private void renderErrorStructure() { HttpProtocolGeneratorUtils.writeRetryableTrait(writer, shape, ";"); } StructuredMemberWriter structuredMemberWriter = new StructuredMemberWriter( - model, symbolProvider, - shape.getAllMembers().values(), this.requiredMemberMode, sensitiveDataFinder - ); + model, + symbolProvider, + shape.getAllMembers().values(), + this.requiredMemberMode, + sensitiveDataFinder); // since any error interface must extend from JavaScript Error interface, // message member is already // required in the JavaScript Error interface diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java index 2a3bc32e00e..c21c9a2d22d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -75,17 +64,22 @@ final class StructuredMemberWriter { this(model, symbolProvider, members, RequiredMemberMode.NULLABLE); } - StructuredMemberWriter(Model model, SymbolProvider symbolProvider, Collection members, - RequiredMemberMode requiredMemberMode) { + StructuredMemberWriter( + Model model, + SymbolProvider symbolProvider, + Collection members, + RequiredMemberMode requiredMemberMode + ) { this(model, symbolProvider, members, requiredMemberMode, new SensitiveDataFinder(model)); } StructuredMemberWriter( - Model model, - SymbolProvider symbolProvider, - Collection members, - RequiredMemberMode requiredMemberMode, - SensitiveDataFinder sensitiveDataFinder) { + Model model, + SymbolProvider symbolProvider, + Collection members, + RequiredMemberMode requiredMemberMode, + SensitiveDataFinder sensitiveDataFinder + ) { this.model = model; this.symbolProvider = symbolProvider; this.members = new LinkedHashSet<>(members); @@ -105,19 +99,20 @@ void writeMembers(TypeScriptWriter writer, Shape shape) { String memberName = getSanitizedMemberName(member); String optionalSuffix = shape.isUnionShape() || !isRequiredMember(member) ? "?" : ""; String typeSuffix = requiredMemberMode == RequiredMemberMode.NULLABLE - && isRequiredMember(member) ? " | undefined" : ""; + && isRequiredMember(member) ? " | undefined" : ""; if (optionalSuffix.equals("?")) { typeSuffix = " | undefined"; // support exactOptionalPropertyTypes. } writer.write( - "${L}${L}${L}: ${T}${L};", - memberPrefix, memberName, optionalSuffix, - symbolProvider.toSymbol(member) - .toBuilder() - .putProperty("typeOnly", true) - .build(), - typeSuffix - ); + "${L}${L}${L}: ${T}${L};", + memberPrefix, + memberName, + optionalSuffix, + symbolProvider.toSymbol(member) + .toBuilder() + .putProperty("typeOnly", true) + .build(), + typeSuffix); if (wroteDocs && position < members.size() - 1) { writer.write(""); @@ -154,7 +149,8 @@ void writeMemberFilterSensitiveLog(TypeScriptWriter writer, MemberShape member, writeMapFilterSensitiveLog(writer, mapMember, memberParam); } else { throw new CodegenException(String.format( - "MemberFilterSensitiveLog attempted for %s", memberTarget.getType())); + "MemberFilterSensitiveLog attempted for %s", + memberTarget.getType())); } } @@ -167,8 +163,9 @@ void writeErrorConstructor(TypeScriptWriter writer, Shape shape, boolean isServe if (!isServerSdk) { writer.writeDocs("@internal"); } - writer.addTypeImport("ExceptionOptionType", "__ExceptionOptionType", - TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.addTypeImport("ExceptionOptionType", + "__ExceptionOptionType", + TypeScriptDependency.AWS_SMITHY_CLIENT); writer.openBlock("constructor(opts: __ExceptionOptionType<$L, __BaseException>) {", symbol.getName()); writer.openBlock("super({", "});", () -> { writer.write("name: $S,", shape.getId().getName()); @@ -197,9 +194,10 @@ private void writeSensitiveString(TypeScriptWriter writer) { * Recursively writes filterSensitiveLog for StructureShape. */ private void writeStructureFilterSensitiveLog( - TypeScriptWriter writer, - Shape structureTarget, - String structureParam) { + TypeScriptWriter writer, + Shape structureTarget, + String structureParam + ) { if (structureTarget.hasTrait(SensitiveTrait.class)) { writeSensitiveString(writer); } else if (structureTarget.hasTrait(StreamingTrait.class) && structureTarget.isUnionShape()) { @@ -227,9 +225,10 @@ private void writeStructureFilterSensitiveLog( * Recursively writes filterSensitiveLog for CollectionShape. */ private void writeCollectionFilterSensitiveLog( - TypeScriptWriter writer, - MemberShape collectionMember, - String collectionParam) { + TypeScriptWriter writer, + MemberShape collectionMember, + String collectionParam + ) { if (collectionMember.getMemberTrait(model, SensitiveTrait.class).isPresent()) { writeSensitiveString(writer); } else if (model.expectShape(collectionMember.getTarget()) instanceof SimpleShape) { @@ -257,14 +256,20 @@ private void writeMapFilterSensitiveLog(TypeScriptWriter writer, MemberShape map String valueParam = "value"; // value of the Object.entries() key-value pair // Reducer is common to all shapes. - writer.openBlock("Object.entries($L).reduce(($L: any, [$L, $L]: [string, $T]) => (", "), {})", - mapParam, accParam, keyParam, valueParam, symbolProvider.toSymbol(mapMember), () -> { - writer.openBlock("$L[$L] =", "", accParam, keyParam, () -> { - writeMemberFilterSensitiveLog(writer, mapMember, valueParam); - writer.writeInline(","); + writer.openBlock("Object.entries($L).reduce(($L: any, [$L, $L]: [string, $T]) => (", + "), {})", + mapParam, + accParam, + keyParam, + valueParam, + symbolProvider.toSymbol(mapMember), + () -> { + writer.openBlock("$L[$L] =", "", accParam, keyParam, () -> { + writeMemberFilterSensitiveLog(writer, mapMember, valueParam); + writer.writeInline(","); + }); + writer.write(accParam); }); - writer.write(accParam); - }); } } @@ -291,7 +296,7 @@ private boolean isMemberOverwriteRequired(MemberShape member, Set parent Collection structureMemberList = ((StructureShape) memberTarget).getAllMembers().values(); for (MemberShape structureMember : structureMemberList) { if (!parents.contains(symbolProvider.toMemberName(structureMember)) - && isMemberOverwriteRequired(structureMember, parents)) { + && isMemberOverwriteRequired(structureMember, parents)) { return true; } } @@ -353,8 +358,8 @@ void writeMemberValidatorCache(TypeScriptWriter writer, String cacheName) { writer.openBlock("const $L : {", "} = {};", cacheName, () -> { for (MemberShape member : members) { writer.addImport("MultiConstraintValidator", - "__MultiConstraintValidator", - TypeScriptDependency.SERVER_COMMON); + "__MultiConstraintValidator", + TypeScriptDependency.SERVER_COMMON); final Shape targetShape = model.expectShape(member.getTarget()); writer.writeInline("$L?: ", getSanitizedMemberName(member)); writer.writeInline("__MultiConstraintValidator<"); @@ -375,29 +380,29 @@ void writeMemberValidatorCache(TypeScriptWriter writer, String cacheName) { */ void writeMemberValidatorFactory(TypeScriptWriter writer, String cacheName) { writer.openBlock("function getMemberValidator(member: T): " - + "NonNullable {", - "}", - cacheName, - () -> { - writer.openBlock("if ($L[member] === undefined) {", "}", cacheName, () -> { - writer.openBlock("switch (member) {", "}", () -> { - for (MemberShape member : members) { - final Shape targetShape = model.expectShape(member.getTarget()); - Collection constraintTraits = getConstraintTraits(member); - writer.openBlock("case $S: {", "}", getSanitizedMemberName(member), () -> { - writer.writeInline("$L[$S] = ", cacheName, getSanitizedMemberName(member)); - if (member.getMemberTrait(model, SensitiveTrait.class).isPresent()) { - writeSensitiveWrappedMemberValidator(writer, targetShape, constraintTraits); - } else { - writeMemberValidator(writer, targetShape, constraintTraits, ";"); - } - writer.write("break;"); - }); - } + + "NonNullable {", + "}", + cacheName, + () -> { + writer.openBlock("if ($L[member] === undefined) {", "}", cacheName, () -> { + writer.openBlock("switch (member) {", "}", () -> { + for (MemberShape member : members) { + final Shape targetShape = model.expectShape(member.getTarget()); + Collection constraintTraits = getConstraintTraits(member); + writer.openBlock("case $S: {", "}", getSanitizedMemberName(member), () -> { + writer.writeInline("$L[$S] = ", cacheName, getSanitizedMemberName(member)); + if (member.getMemberTrait(model, SensitiveTrait.class).isPresent()) { + writeSensitiveWrappedMemberValidator(writer, targetShape, constraintTraits); + } else { + writeMemberValidator(writer, targetShape, constraintTraits, ";"); + } + writer.write("break;"); + }); + } + }); }); + writer.write("return $L[member]!!;", cacheName); }); - writer.write("return $L[member]!!;", cacheName); - }); } /** @@ -411,7 +416,7 @@ void writeValidateMethodContents(TypeScriptWriter writer, String param) { for (MemberShape member : members) { String optionalSuffix = ""; if (member.getMemberTrait(model, MediaTypeTrait.class).isPresent() - && model.expectShape(member.getTarget()) instanceof StringShape) { + && model.expectShape(member.getTarget()) instanceof StringShape) { // lazy JSON wrapper validation should be done based on the serialized form of // the object optionalSuffix = "?.toString()"; @@ -421,10 +426,16 @@ void writeValidateMethodContents(TypeScriptWriter writer, String param) { // todo: validating event streams in unsupported. writer.write("// unsupported event stream validation"); writer.write("// ...getMemberValidator($1S).validate($2L.$1L$4L, `$${path}/$3L`),", - getSanitizedMemberName(member), param, member.getMemberName(), optionalSuffix); + getSanitizedMemberName(member), + param, + member.getMemberName(), + optionalSuffix); } else { writer.write("...getMemberValidator($1S).validate($2L.$1L$4L, `$${path}/$3L`),", - getSanitizedMemberName(member), param, member.getMemberName(), optionalSuffix); + getSanitizedMemberName(member), + param, + member.getMemberName(), + optionalSuffix); } } }); @@ -434,16 +445,19 @@ void writeValidateMethodContents(TypeScriptWriter writer, String param) { * Writes a SensitiveConstraintValidator enclosing the shape validator for a * sensitive member. */ - private void writeSensitiveWrappedMemberValidator(TypeScriptWriter writer, - Shape targetShape, - Collection constraintTraits) { + private void writeSensitiveWrappedMemberValidator( + TypeScriptWriter writer, + Shape targetShape, + Collection constraintTraits + ) { writer.addImport("SensitiveConstraintValidator", - "__SensitiveConstraintValidator", - TypeScriptDependency.SERVER_COMMON); + "__SensitiveConstraintValidator", + TypeScriptDependency.SERVER_COMMON); writer.writeInline("new __SensitiveConstraintValidator<"); writeConstraintValidatorType(writer, targetShape); - writer.openBlock(">(", ");", - () -> writeMemberValidator(writer, targetShape, constraintTraits, "")); + writer.openBlock(">(", + ");", + () -> writeMemberValidator(writer, targetShape, constraintTraits, "")); } /** @@ -456,10 +470,12 @@ private void writeSensitiveWrappedMemberValidator(TypeScriptWriter writer, * @param trailer what to append to the output (such as a comma or * semicolon) */ - private void writeMemberValidator(TypeScriptWriter writer, - Shape shape, - Collection constraintTraits, - String trailer) { + private void writeMemberValidator( + TypeScriptWriter writer, + Shape shape, + Collection constraintTraits, + String trailer + ) { if (shape instanceof SimpleShape) { writeShapeValidator(writer, shape, constraintTraits, trailer); return; @@ -467,58 +483,61 @@ private void writeMemberValidator(TypeScriptWriter writer, if (shape.isStructureShape() || shape.isUnionShape()) { writer.addImport("CompositeStructureValidator", - "__CompositeStructureValidator", - TypeScriptDependency.SERVER_COMMON); - writer.openBlock("new __CompositeStructureValidator<$T>(", ")" + trailer, - getValidatorValueType(shape), - () -> { - writeShapeValidator(writer, shape, constraintTraits, ","); - if (!shape.hasTrait(ErrorTrait.class)) { - writer.write("$T.validate", symbolProvider.toSymbol(shape)); - } else { - // todo: unsupported - // Error classes have no static validator. - writer.write(""" - () => [/*Error validator unsupported*/]"""); - } - }); + "__CompositeStructureValidator", + TypeScriptDependency.SERVER_COMMON); + writer.openBlock("new __CompositeStructureValidator<$T>(", + ")" + trailer, + getValidatorValueType(shape), + () -> { + writeShapeValidator(writer, shape, constraintTraits, ","); + if (!shape.hasTrait(ErrorTrait.class)) { + writer.write("$T.validate", symbolProvider.toSymbol(shape)); + } else { + // todo: unsupported + // Error classes have no static validator. + writer.write(""" + () => [/*Error validator unsupported*/]"""); + } + }); } else if (shape.isListShape() || shape.isSetShape()) { writer.addImport("CompositeCollectionValidator", - "__CompositeCollectionValidator", - TypeScriptDependency.SERVER_COMMON); + "__CompositeCollectionValidator", + TypeScriptDependency.SERVER_COMMON); MemberShape collectionMemberShape = ((CollectionShape) shape).getMember(); Shape collectionMemberTargetShape = model.expectShape(collectionMemberShape.getTarget()); - writer.openBlock("new __CompositeCollectionValidator<$T>(", ")" + trailer, - getValidatorValueType(shape), - () -> { - writeShapeValidator(writer, shape, constraintTraits, ","); - writeMemberValidator(writer, - collectionMemberTargetShape, - getConstraintTraits(collectionMemberShape), - ""); - }); + writer.openBlock("new __CompositeCollectionValidator<$T>(", + ")" + trailer, + getValidatorValueType(shape), + () -> { + writeShapeValidator(writer, shape, constraintTraits, ","); + writeMemberValidator(writer, + collectionMemberTargetShape, + getConstraintTraits(collectionMemberShape), + ""); + }); } else if (shape.isMapShape()) { writer.addImport("CompositeMapValidator", "__CompositeMapValidator", TypeScriptDependency.SERVER_COMMON); MapShape mapShape = (MapShape) shape; final MemberShape keyShape = mapShape.getKey(); final MemberShape valueShape = mapShape.getValue(); - writer.openBlock("new __CompositeMapValidator<$T>(", ")" + trailer, - getValidatorValueType(shape), - () -> { - writeShapeValidator(writer, mapShape, constraintTraits, ","); - writeMemberValidator(writer, - model.expectShape(keyShape.getTarget()), - getConstraintTraits(keyShape), - ","); - writeMemberValidator(writer, - model.expectShape(valueShape.getTarget()), - getConstraintTraits(valueShape), - ""); - }); + writer.openBlock("new __CompositeMapValidator<$T>(", + ")" + trailer, + getValidatorValueType(shape), + () -> { + writeShapeValidator(writer, mapShape, constraintTraits, ","); + writeMemberValidator(writer, + model.expectShape(keyShape.getTarget()), + getConstraintTraits(keyShape), + ","); + writeMemberValidator(writer, + model.expectShape(valueShape.getTarget()), + getConstraintTraits(valueShape), + ""); + }); } else { throw new IllegalArgumentException( - String.format("Unsupported shape found when generating validator: %s", shape)); + String.format("Unsupported shape found when generating validator: %s", shape)); } } @@ -535,10 +554,12 @@ private void writeMemberValidator(TypeScriptWriter writer, * @param trailer what to append to the output (for instance, a comma or * semicolon) */ - private void writeShapeValidator(TypeScriptWriter writer, - Shape shape, - Collection constraints, - String trailer) { + private void writeShapeValidator( + TypeScriptWriter writer, + Shape shape, + Collection constraints, + String trailer + ) { boolean shouldWriteIntEnumValidator = shape.isIntEnumShape(); if (constraints.isEmpty() && !shouldWriteIntEnumValidator) { @@ -548,38 +569,41 @@ private void writeShapeValidator(TypeScriptWriter writer, } writer.addImport("CompositeValidator", "__CompositeValidator", TypeScriptDependency.SERVER_COMMON); - writer.openBlock("new __CompositeValidator<$T>([", "])" + trailer, getSymbolForValidatedType(shape), - () -> { - if (shouldWriteIntEnumValidator) { - writer.addImport("IntegerEnumValidator", "__IntegerEnumValidator", - TypeScriptDependency.SERVER_COMMON); - writer.openBlock("new __IntegerEnumValidator([", "]),", () -> { - for (int i : ((IntEnumShape) shape).getEnumValues().values()) { - writer.write("$L,", i); - } - }); - } + writer.openBlock("new __CompositeValidator<$T>([", + "])" + trailer, + getSymbolForValidatedType(shape), + () -> { + if (shouldWriteIntEnumValidator) { + writer.addImport("IntegerEnumValidator", + "__IntegerEnumValidator", + TypeScriptDependency.SERVER_COMMON); + writer.openBlock("new __IntegerEnumValidator([", "]),", () -> { + for (int i : ((IntEnumShape) shape).getEnumValues().values()) { + writer.write("$L,", i); + } + }); + } - if (shape.isEnumShape()) { - writer.addImport("EnumValidator", "__EnumValidator", TypeScriptDependency.SERVER_COMMON); - Collection enumValues = shape.asEnumShape().get().getAllMembers().values(); - writer.openBlock("new __EnumValidator([", "]),", () -> { - for (MemberShape member : enumValues) { - writer.write("$S,", member.expectTrait(EnumValueTrait.class).expectStringValue()); - } - writer.write("], ["); - for (MemberShape member : shape.asEnumShape().get().getAllMembers().values()) { - if (!member.hasTrait((InternalTrait.class))) { + if (shape.isEnumShape()) { + writer.addImport("EnumValidator", "__EnumValidator", TypeScriptDependency.SERVER_COMMON); + Collection enumValues = shape.asEnumShape().get().getAllMembers().values(); + writer.openBlock("new __EnumValidator([", "]),", () -> { + for (MemberShape member : enumValues) { writer.write("$S,", member.expectTrait(EnumValueTrait.class).expectStringValue()); } - } - }); - } + writer.write("], ["); + for (MemberShape member : shape.asEnumShape().get().getAllMembers().values()) { + if (!member.hasTrait((InternalTrait.class))) { + writer.write("$S,", member.expectTrait(EnumValueTrait.class).expectStringValue()); + } + } + }); + } - for (Trait t : constraints) { - writeSingleConstraintValidator(writer, t); - } - }); + for (Trait t : constraints) { + writeSingleConstraintValidator(writer, t); + } + }); } /** @@ -606,8 +630,8 @@ private void writeSingleConstraintValidator(TypeScriptWriter writer, Trait trait LengthTrait lengthTrait = (LengthTrait) trait; writer.addImport("LengthValidator", "__LengthValidator", TypeScriptDependency.SERVER_COMMON); writer.write("new __LengthValidator($L, $L),", - lengthTrait.getMin().map(Object::toString).orElse("undefined"), - lengthTrait.getMax().map(Object::toString).orElse("undefined")); + lengthTrait.getMin().map(Object::toString).orElse("undefined"), + lengthTrait.getMax().map(Object::toString).orElse("undefined")); } else if (trait instanceof PatternTrait) { writer.addImport("PatternValidator", "__PatternValidator", TypeScriptDependency.SERVER_COMMON); writer.write("new __PatternValidator($S),", ((PatternTrait) trait).getValue()); @@ -615,8 +639,8 @@ private void writeSingleConstraintValidator(TypeScriptWriter writer, Trait trait RangeTrait rangeTrait = (RangeTrait) trait; writer.addImport("RangeValidator", "__RangeValidator", TypeScriptDependency.SERVER_COMMON); writer.write("new __RangeValidator($L, $L),", - rangeTrait.getMin().map(Object::toString).orElse("undefined"), - rangeTrait.getMax().map(Object::toString).orElse("undefined")); + rangeTrait.getMin().map(Object::toString).orElse("undefined"), + rangeTrait.getMax().map(Object::toString).orElse("undefined")); } else if (trait instanceof UniqueItemsTrait) { writer.addImport("UniqueItemsValidator", "__UniqueItemsValidator", TypeScriptDependency.SERVER_COMMON); writer.write("new __UniqueItemsValidator(),"); @@ -641,20 +665,18 @@ private void writeConstraintValidatorType(TypeScriptWriter writer, Shape shape) if (keyType.equals("string")) { writer.writeInline("Record<$T, $T>", - getSymbolForValidatedType(mapShape.getKey()), - getSymbolForValidatedType(mapShape.getValue()) - ); + getSymbolForValidatedType(mapShape.getKey()), + getSymbolForValidatedType(mapShape.getValue())); } else { writer.writeInline("Partial>", - getSymbolForValidatedType(mapShape.getKey()), - getSymbolForValidatedType(mapShape.getValue()) - ); + getSymbolForValidatedType(mapShape.getKey()), + getSymbolForValidatedType(mapShape.getValue())); } } else if (shape instanceof SimpleShape) { writer.writeInline("$T", getSymbolForValidatedType(shape)); } else { throw new IllegalArgumentException( - String.format("Unsupported shape found when generating validator: %s", shape)); + String.format("Unsupported shape found when generating validator: %s", shape)); } } @@ -677,7 +699,7 @@ private Symbol getValidatorValueType(Shape shape) { return getSymbolForValidatedType(shape); } else { throw new IllegalArgumentException( - String.format("Unsupported shape found when generating validator: %s", shape)); + String.format("Unsupported shape found when generating validator: %s", shape)); } } @@ -701,12 +723,13 @@ private Symbol getSymbolForValidatedType(Shape shape) { // widen the symbol if (shape.isBlobShape() && shape.hasTrait(StreamingTrait.class)) { return symbolProvider.toSymbol(shape) - .toBuilder() - .addReference(Symbol.builder() - .name("Readable").namespace("stream", "/") - .build()) - .name("Readable | ReadableStream | Blob | string | Uint8Array | Buffer") - .build(); + .toBuilder() + .addReference(Symbol.builder() + .name("Readable") + .namespace("stream", "/") + .build()) + .name("Readable | ReadableStream | Blob | string | Uint8Array | Buffer") + .build(); } return symbolProvider.toSymbol(shape); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SymbolVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SymbolVisitor.java index d1049f6ae70..7c7326bb0a2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SymbolVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/SymbolVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import static java.lang.String.format; @@ -105,19 +94,19 @@ final class SymbolVisitor implements SymbolProvider, ShapeVisitor { // Load reserved words from a new-line delimited file. ReservedWords reservedWords = new ReservedWordsBuilder() - .loadWords(TypeScriptCodegenPlugin.class.getResource("reserved-words.txt")) - .build(); + .loadWords(TypeScriptCodegenPlugin.class.getResource("reserved-words.txt")) + .build(); ReservedWords memberReservedWords = new ReservedWordsBuilder() - .loadWords(TypeScriptCodegenPlugin.class.getResource("reserved-words-members.txt")) - .build(); + .loadWords(TypeScriptCodegenPlugin.class.getResource("reserved-words-members.txt")) + .build(); escaper = ReservedWordSymbolProvider.builder() - .nameReservedWords(reservedWords) - .memberReservedWords(memberReservedWords) - // Only escape words when the symbol has a definition file to - // prevent escaping intentional references to built-in types. - .escapePredicate((shape, symbol) -> !StringUtils.isEmpty(symbol.getDefinitionFile())) - .buildEscaper(); + .nameReservedWords(reservedWords) + .memberReservedWords(memberReservedWords) + // Only escape words when the symbol has a definition file to + // prevent escaping intentional references to built-in types. + .escapePredicate((shape, symbol) -> !StringUtils.isEmpty(symbol.getDefinitionFile())) + .buildEscaper(); // Get each structure that's used an error. OperationIndex operationIndex = OperationIndex.of(model); @@ -137,16 +126,15 @@ public Symbol toSymbol(Shape shape) { boolean typeOnly = schemaMode; boolean isError = shape.asStructureShape().isPresent() && shape.hasTrait(ErrorTrait.class); if (shape.isOperationShape() - || shape.isResourceShape() - || isError - || shape.isServiceShape() - ) { + || shape.isResourceShape() + || isError + || shape.isServiceShape()) { typeOnly = false; } Symbol symbol = shape.accept(this) - .toBuilder() - .putProperty("typeOnly", typeOnly) - .build(); + .toBuilder() + .putProperty("typeOnly", typeOnly) + .build(); LOGGER.fine(() -> "Creating symbol from " + shape + ": " + symbol); return escaper.escapeSymbol(shape, symbol); } @@ -161,15 +149,14 @@ public Symbol blobShape(BlobShape shape) { if (shape.hasTrait(StreamingTrait.class)) { // Note: `Readable` needs an import and a dependency. return createSymbolBuilder(shape, "StreamingBlobTypes", null) - .addReference( - Symbol.builder() - .addDependency(TypeScriptDependency.SMITHY_TYPES) - .name("StreamingBlobTypes") - .namespace("@smithy/types", "/") - .putProperty("typeOnly", schemaMode) - .build() - ) - .build(); + .addReference( + Symbol.builder() + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .name("StreamingBlobTypes") + .namespace("@smithy/types", "/") + .putProperty("typeOnly", schemaMode) + .build()) + .build(); } return createSymbolBuilder(shape, "Uint8Array").build(); @@ -184,18 +171,18 @@ public Symbol booleanShape(BooleanShape shape) { public Symbol listShape(ListShape shape) { Symbol reference = toSymbol(shape.getMember()); return createSymbolBuilder(shape, format("%s[]", reference.getName()), null) - .addReference(reference) - .putProperty("typeOnly", schemaMode) - .build(); + .addReference(reference) + .putProperty("typeOnly", schemaMode) + .build(); } @Override public Symbol setShape(SetShape shape) { Symbol reference = toSymbol(shape.getMember()); return createSymbolBuilder(shape, format("%s[]", reference.getName()), null) - .addReference(reference) - .putProperty("typeOnly", schemaMode) - .build(); + .addReference(reference) + .putProperty("typeOnly", schemaMode) + .build(); } /** @@ -219,14 +206,13 @@ public Symbol mapShape(MapShape shape) { boolean stringKey = key.toString().equals("string"); return createSymbolBuilder( - shape, - format(stringKey ? "Record<%s, %s>" : "Partial>", key.getName(), value.getName()), - null - ) - .addReference(key) - .addReference(value) - .putProperty("typeOnly", schemaMode) - .build(); + shape, + format(stringKey ? "Record<%s, %s>" : "Partial>", key.getName(), value.getName()), + null) + .addReference(key) + .addReference(value) + .putProperty("typeOnly", schemaMode) + .build(); } @Override @@ -275,40 +261,39 @@ public Symbol bigIntegerShape(BigIntegerShape shape) { public Symbol bigDecimalShape(BigDecimalShape shape) { if ("native".equals(settings.getBigNumberMode())) { return createSymbolBuilder(shape, "NumericValue", null) - .addReference( - Symbol.builder() - .addDependency(TypeScriptDependency.SMITHY_CORE) - .name("NumericValue") - .putProperty("typeOnly", schemaMode) - .namespace("@smithy/core/serde", "/") - .build() - ) - .build(); + .addReference( + Symbol.builder() + .addDependency(TypeScriptDependency.SMITHY_CORE) + .name("NumericValue") + .putProperty("typeOnly", schemaMode) + .namespace("@smithy/core/serde", "/") + .build()) + .build(); } return createBigJsSymbol(shape); } private Symbol createBigJsSymbol(Shape shape) { return createSymbolBuilder(shape, "Big", TypeScriptDependency.BIG_JS.packageName) - .addDependency(TypeScriptDependency.TYPES_BIG_JS) - .addDependency(TypeScriptDependency.BIG_JS) - .build(); + .addDependency(TypeScriptDependency.TYPES_BIG_JS) + .addDependency(TypeScriptDependency.BIG_JS) + .build(); } @Override public Symbol documentShape(DocumentShape shape) { Symbol.Builder builder = createSymbolBuilder(shape, "__DocumentType"); Symbol importSymbol = Symbol.builder() - .name("DocumentType") - .namespace(TypeScriptDependency.SMITHY_TYPES.packageName, "/") - .putProperty("typeOnly", schemaMode) - .build(); + .name("DocumentType") + .namespace(TypeScriptDependency.SMITHY_TYPES.packageName, "/") + .putProperty("typeOnly", schemaMode) + .build(); SymbolReference reference = SymbolReference.builder() - .symbol(importSymbol) - .alias("__DocumentType") - .options(SymbolReference.ContextOption.USE) - .putProperty("typeOnly", schemaMode) - .build(); + .symbol(importSymbol) + .alias("__DocumentType") + .options(SymbolReference.ContextOption.USE) + .putProperty("typeOnly", schemaMode) + .build(); return builder.addReference(reference).build(); } @@ -318,22 +303,20 @@ public Symbol operationShape(OperationShape shape) { String moduleName = moduleNameDelegator.formatModuleName(shape, commandName); Symbol intermediate = createGeneratedSymbolBuilder(shape, commandName, moduleName).build(); Symbol.Builder builder = intermediate.toBuilder() - .putProperty("typeOnly", false); + .putProperty("typeOnly", false); // Add input and output type symbols (XCommandInput / XCommandOutput). builder.putProperty( - "inputType", - intermediate.toBuilder() - .putProperty("typeOnly", schemaMode) - .name(commandName + "Input") - .build() - ); + "inputType", + intermediate.toBuilder() + .putProperty("typeOnly", schemaMode) + .name(commandName + "Input") + .build()); builder.putProperty( - "outputType", - intermediate.toBuilder() - .putProperty("typeOnly", schemaMode) - .name(commandName + "Output") - .build() - ); + "outputType", + intermediate.toBuilder() + .putProperty("typeOnly", schemaMode) + .name(commandName + "Output") + .build()); return builder.build(); } @@ -352,10 +335,9 @@ public Symbol stringShape(StringShape shape) { if (CodegenUtils.isJsonMediaType(mediaType)) { Symbol.Builder builder = createSymbolBuilder(shape, "__AutomaticJsonStringConversion | string"); return addSmithyUseImport( - builder, - "AutomaticJsonStringConversion", - "__AutomaticJsonStringConversion" - ).build(); + builder, + "AutomaticJsonStringConversion", + "__AutomaticJsonStringConversion").build(); } else { LOGGER.warning(() -> "Found unsupported mediatype " + mediaType + " on String shape: " + shape); } @@ -367,23 +349,23 @@ public Symbol stringShape(StringShape shape) { @Override public Symbol enumShape(EnumShape shape) { return stringShape(shape) - .toBuilder() - .putProperty("typeOnly", true) - .build(); + .toBuilder() + .putProperty("typeOnly", true) + .build(); } @Override public Symbol intEnumShape(IntEnumShape shape) { return createObjectSymbolBuilder(shape) - .putProperty("typeOnly", true) - .build(); + .putProperty("typeOnly", true) + .build(); } private Symbol createEnumSymbol(StringShape shape, EnumTrait enumTrait) { return createObjectSymbolBuilder(shape) - .putProperty(EnumTrait.class.getName(), enumTrait) - .putProperty("typeOnly", true) - .build(); + .putProperty(EnumTrait.class.getName(), enumTrait) + .putProperty("typeOnly", true) + .build(); } @Override @@ -396,8 +378,8 @@ public Symbol serviceShape(ServiceShape shape) { String name = StringUtils.capitalize(shape.getId().getName(shape)) + "Client"; String moduleName = moduleNameDelegator.formatModuleName(shape, name); return createGeneratedSymbolBuilder(shape, name, moduleName) - .putProperty("typeOnly", false) - .build(); + .putProperty("typeOnly", false) + .build(); } @Override @@ -407,14 +389,14 @@ public Symbol structureShape(StructureShape shape) { private Symbol.Builder addSmithyUseImport(Symbol.Builder builder, String name, String as) { Symbol importSymbol = Symbol.builder() - .name(name) - .namespace("@smithy/smithy-client", "/") - .build(); + .name(name) + .namespace("@smithy/smithy-client", "/") + .build(); SymbolReference reference = SymbolReference.builder() - .symbol(importSymbol) - .alias(as) - .options(SymbolReference.ContextOption.USE) - .build(); + .symbol(importSymbol) + .alias(as) + .options(SymbolReference.ContextOption.USE) + .build(); return builder.addReference(reference); } @@ -426,7 +408,7 @@ public Symbol unionShape(UnionShape shape) { @Override public Symbol memberShape(MemberShape shape) { Shape targetShape = model.getShape(shape.getTarget()) - .orElseThrow(() -> new CodegenException("Shape not found: " + shape.getTarget())); + .orElseThrow(() -> new CodegenException("Shape not found: " + shape.getTarget())); Symbol targetSymbol = toSymbol(targetShape); if (targetSymbol.getProperties().containsKey(EnumTrait.class.getName()) || targetShape.isIntEnumShape()) { @@ -450,19 +432,19 @@ public Symbol memberShape(MemberShape shape) { // pragma comments. private Symbol createMemberSymbolWithEnumTarget(Symbol targetSymbol) { return targetSymbol.toBuilder() - .namespace(null, "/") - .name(targetSymbol.getName()) - .addReference(targetSymbol) - .build(); + .namespace(null, "/") + .name(targetSymbol.getName()) + .addReference(targetSymbol) + .build(); } private Symbol createMemberSymbolWithEventStream(Symbol targetSymbol) { return targetSymbol.toBuilder() - .putProperty("typeOnly", schemaMode) - .namespace(null, "/") - .name(String.format("AsyncIterable<%s>", targetSymbol.getName())) - .addReference(targetSymbol) - .build(); + .putProperty("typeOnly", schemaMode) + .namespace(null, "/") + .name(String.format("AsyncIterable<%s>", targetSymbol.getName())) + .addReference(targetSymbol) + .build(); } @Override @@ -487,16 +469,16 @@ private Symbol.Builder createSymbolBuilder(Shape shape, String typeName) { private Symbol.Builder createSymbolBuilder(Shape shape, String typeName, String namespace) { return Symbol.builder() - .putProperty("shape", shape) - .name(typeName) - .namespace(namespace, "/"); + .putProperty("shape", shape) + .name(typeName) + .namespace(namespace, "/"); } private Symbol.Builder createGeneratedSymbolBuilder(Shape shape, String typeName, String namespace) { String trimmedNamespace = namespace.startsWith("./") ? namespace.substring(2) : namespace; String prefixedNamespace = String.join("/", ".", CodegenUtils.SOURCE_FOLDER, trimmedNamespace); return createSymbolBuilder(shape, typeName, prefixedNamespace) - .definitionFile(toFilename(prefixedNamespace)); + .definitionFile(toFilename(prefixedNamespace)); } private String toFilename(String namespace) { @@ -552,21 +534,20 @@ public String formatModuleName(Shape shape, String name) { } static TypeScriptWriter modelIndexer( - Collection shapes, - SymbolProvider symbolProvider + Collection shapes, + SymbolProvider symbolProvider ) { TypeScriptWriter writer = new TypeScriptWriter(""); String modelPrefix = String.join("/", ".", CodegenUtils.SOURCE_FOLDER, SHAPE_NAMESPACE_PREFIX); List collectedModelNamespaces = shapes.stream() - .map(shape -> symbolProvider.toSymbol(shape).getNamespace()) - .filter(namespace -> namespace.startsWith(modelPrefix)) - .distinct() - .sorted(Comparator.naturalOrder()) - .map(namespace -> namespace.replaceFirst( - Matcher.quoteReplacement(modelPrefix), - String.join("/", ".", SHAPE_NAMESPACE_PREFIX) - )) - .toList(); + .map(shape -> symbolProvider.toSymbol(shape).getNamespace()) + .filter(namespace -> namespace.startsWith(modelPrefix)) + .distinct() + .sorted(Comparator.naturalOrder()) + .map(namespace -> namespace.replaceFirst( + Matcher.quoteReplacement(modelPrefix), + String.join("/", ".", SHAPE_NAMESPACE_PREFIX))) + .toList(); // Export empty model index if no other files are present. if (collectedModelNamespaces.isEmpty()) { @@ -587,11 +568,9 @@ static TypeScriptWriter modelIndexer( if (shape.isStructureShape() && !shape.hasTrait(ErrorTrait.class)) { Symbol symbol = symbolProvider.toSymbol(shape); String namespace = symbol.getNamespace() - .replaceFirst(Matcher.quoteReplacement( - String.join("/", ".", CodegenUtils.SOURCE_FOLDER) - ), - "." - ); + .replaceFirst(Matcher.quoteReplacement( + String.join("/", ".", CodegenUtils.SOURCE_FOLDER)), + "."); namespaceToShapes.computeIfAbsent(namespace, k -> new TreeSet<>()); namespaceToShapes.get(namespace).add(symbol.getName()); } @@ -605,7 +584,7 @@ static TypeScriptWriter modelIndexer( // String symbols = String.join(", ", types); writer.write(""" - export type * from $S;""", namespace); + export type * from $S;""", namespace); } return writer; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptClientCodegenPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptClientCodegenPlugin.java index 1eeb09eb4ad..3b504519b22 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptClientCodegenPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptClientCodegenPlugin.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.ServiceLoader; @@ -28,8 +27,8 @@ public String getName() { @Override public void execute(PluginContext context) { - CodegenDirector runner - = new CodegenDirector<>(); + CodegenDirector runner = + new CodegenDirector<>(); runner.directedCodegen(new DirectedTypeScriptCodegen()); @@ -41,25 +40,27 @@ public void execute(PluginContext context) { runner.model(context.getModel()); // Create the TypeScriptSettings object from the plugin settings. - TypeScriptSettings settings = TypeScriptSettings.from(context.getModel(), context.getSettings(), + TypeScriptSettings settings = TypeScriptSettings.from(context.getModel(), + context.getSettings(), TypeScriptSettings.ArtifactType.CLIENT); runner.settings(settings); // Only add integrations if the integrations match the settings // This uses {@link TypeScriptIntegration#matchesSettings}, which is a // Smithy internal API. This may be removed at any point. - runner.integrationFinder(() -> - () -> ServiceLoader.load(TypeScriptIntegration.class, CodegenDirector.class.getClassLoader()) - .stream() - .map(Provider::get) - .filter(integration -> { - boolean matchesSettings = integration.matchesSettings(settings); - if (!matchesSettings) { - LOGGER.fine(() -> "Skipping TypeScript integration based on settings: " + integration.name()); - } - return matchesSettings; - }) - .iterator()); + runner.integrationFinder( + () -> () -> ServiceLoader.load(TypeScriptIntegration.class, CodegenDirector.class.getClassLoader()) + .stream() + .map(Provider::get) + .filter(integration -> { + boolean matchesSettings = integration.matchesSettings(settings); + if (!matchesSettings) { + LOGGER.fine(() -> "Skipping TypeScript integration based on settings: " + + integration.name()); + } + return matchesSettings; + }) + .iterator()); runner.service(settings.getService()); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenContext.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenContext.java index 9cd4545d62f..7c8155962ce 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenContext.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenContext.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.ArrayList; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPlugin.java index 3119d7430b0..e55e10e4318 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPlugin.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import software.amazon.smithy.utils.SmithyInternalApi; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegator.java index a3f38711fb9..8105d8dae9c 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.ArrayList; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java index c0ca6535d02..c762a3e424c 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.io.BufferedReader; @@ -53,7 +42,8 @@ public enum TypeScriptDependency implements Dependency { UTIL_RETRY("dependencies", "@smithy/util-retry", false), MIDDLEWARE_STACK("dependencies", "@smithy/middleware-stack", true), MIDDLEWARE_ENDPOINTS_V2("dependencies", "@smithy/middleware-endpoint", false), - @Deprecated AWS_SDK_UTIL_ENDPOINTS("dependencies", "@aws-sdk/util-endpoints", false), + @Deprecated + AWS_SDK_UTIL_ENDPOINTS("dependencies", "@aws-sdk/util-endpoints", false), UTIL_ENDPOINTS("dependencies", "@smithy/util-endpoints", false), AWS_CRYPTO_SHA256_BROWSER("dependencies", "@aws-crypto/sha256-browser", "5.2.0", true), @@ -63,8 +53,10 @@ public enum TypeScriptDependency implements Dependency { AWS_SDK_URL_PARSER("dependencies", "@smithy/url-parser", true), - @Deprecated AWS_SDK_UTIL_BASE64_BROWSER("dependencies", "@aws-sdk/util-base64-browser", false), - @Deprecated AWS_SDK_UTIL_BASE64_NODE("dependencies", "@aws-sdk/util-base64-node", false), + @Deprecated + AWS_SDK_UTIL_BASE64_BROWSER("dependencies", "@aws-sdk/util-base64-browser", false), + @Deprecated + AWS_SDK_UTIL_BASE64_NODE("dependencies", "@aws-sdk/util-base64-node", false), AWS_SDK_UTIL_BASE64("dependencies", "@smithy/util-base64", true), AWS_SDK_UTIL_BODY_LENGTH_BROWSER("dependencies", "@smithy/util-body-length-browser", true), @@ -72,15 +64,17 @@ public enum TypeScriptDependency implements Dependency { AWS_SDK_UTIL_UTF8("dependencies", "@smithy/util-utf8", true), - AWS_SDK_UTIL_WAITERS("dependencies", "@smithy/util-waiter", false), + AWS_SDK_UTIL_WAITERS("dependencies", "@smithy/util-waiter", false), AWS_SDK_UTIL_DEFAULTS_MODE_NODE("dependencies", "@smithy/util-defaults-mode-node", true), AWS_SDK_UTIL_DEFAULTS_MODE_BROWSER("dependencies", "@smithy/util-defaults-mode-browser", true), NODE_CONFIG_PROVIDER("dependencies", "@smithy/node-config-provider", false), - @Deprecated UUID_TYPES("dependencies", "@types/uuid", "^9.0.1", false), - @Deprecated UUID("dependencies", "uuid", "^9.0.1", false), + @Deprecated + UUID_TYPES("dependencies", "@types/uuid", "^9.0.1", false), + @Deprecated + UUID("dependencies", "uuid", "^9.0.1", false), SMITHY_UUID("dependencies", "@smithy/uuid", false), // Conditionally added when httpChecksumRequired trait exists @@ -96,11 +90,13 @@ public enum TypeScriptDependency implements Dependency { // Conditionally added when setting the auth middleware. UTIL_MIDDLEWARE("dependencies", "@smithy/util-middleware", false), - @Deprecated AWS_SDK_UTIL_MIDDLEWARE("dependencies", "@smithy/util-middleware", false), + @Deprecated + AWS_SDK_UTIL_MIDDLEWARE("dependencies", "@smithy/util-middleware", false), // Conditionally added if a event stream shape is found anywhere in the model - AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER("dependencies", "@smithy/eventstream-serde-config-resolver", - false), + AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER("dependencies", + "@smithy/eventstream-serde-config-resolver", + false), AWS_SDK_EVENTSTREAM_SERDE_NODE("dependencies", "@smithy/eventstream-serde-node", false), AWS_SDK_EVENTSTREAM_SERDE_BROWSER("dependencies", "@smithy/eventstream-serde-browser", false), AWS_SDK_EVENTSTREAM_CODEC("dependencies", "@smithy/eventstream-codec", false), @@ -120,8 +116,10 @@ public enum TypeScriptDependency implements Dependency { HTML_ENTITIES("dependencies", "entities", "2.2.0", false), // Conditionally added when streaming blob response payload exists. - @Deprecated UTIL_STREAM_NODE("dependencies", "@smithy/util-stream-node", false), - @Deprecated UTIL_STREAM_BROWSER("dependencies", "@smithy/util-stream-browser", false), + @Deprecated + UTIL_STREAM_NODE("dependencies", "@smithy/util-stream-node", false), + @Deprecated + UTIL_STREAM_BROWSER("dependencies", "@smithy/util-stream-browser", false), UTIL_STREAM("dependencies", "@smithy/util-stream", false), // Conditionally added when @aws.auth#sigv4 is used @@ -129,7 +127,8 @@ public enum TypeScriptDependency implements Dependency { // This package should never have a major version, and should only use minor and patch versions in development. // Exports are located between @smithy/types and @smithy/core - @Deprecated EXPERIMENTAL_IDENTITY_AND_AUTH("dependencies", "@smithy/experimental-identity-and-auth", false), + @Deprecated + EXPERIMENTAL_IDENTITY_AND_AUTH("dependencies", "@smithy/experimental-identity-and-auth", false), // Conditionally added when specs have been generated. VITEST("devDependencies", "vitest", "^3.2.4", false), diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java index ad73bd1dd46..ccedcc9b50b 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.ArrayList; @@ -228,7 +217,10 @@ public Void visitOr(OrExpression expression) { expression.getRight().accept(this); String rightContext = executionContext; - executionContext = String.format("((%s || %s) && (%s || %s)) ", leftContext, rightContext, rightContext, + executionContext = String.format("((%s || %s) && (%s || %s)) ", + leftContext, + rightContext, + rightContext, leftContext); return null; @@ -247,12 +239,16 @@ public Void visitObjectProjection(ObjectProjectionExpression expression) { String element = makeNewScope("element_"); String result = makeNewScope("objectProjection_"); - writer.openBlock("let $L = Object.values($L).map(($L: any) => {", "});", result, - executionContext, element, () -> { - executionContext = element; - expression.getRight().accept(this); - writer.write("return $L;", executionContext); - }); + writer.openBlock("let $L = Object.values($L).map(($L: any) => {", + "});", + result, + executionContext, + element, + () -> { + executionContext = element; + expression.getRight().accept(this); + writer.write("return $L;", executionContext); + }); executionContext = result; return null; } @@ -264,8 +260,12 @@ public Void visitProjection(ProjectionExpression expression) { if (!(expression.getRight() instanceof CurrentExpression)) { String element = makeNewScope("element_"); String result = makeNewScope("projection_"); - writer.openBlock("let $L = $L.map(($L: any) => {", "});", result, - executionContext, element, () -> { + writer.openBlock("let $L = $L.map(($L: any) => {", + "});", + result, + executionContext, + element, + () -> { executionContext = element; expression.getRight().accept(this); writer.write("return $L;", executionContext); @@ -284,12 +284,16 @@ public Void visitFilterProjection(FilterProjectionExpression expression) { String elementScope = makeNewScope("element_"); String resultScope = makeNewScope("filterRes_"); - writer.openBlock("let $L = $L.filter(($L: any) => {", "});", resultScope, - executionContext, elementScope, () -> { - executionContext = elementScope; - expression.getComparison().accept(this); - writer.write("return $L;", executionContext); - }); + writer.openBlock("let $L = $L.filter(($L: any) => {", + "});", + resultScope, + executionContext, + elementScope, + () -> { + executionContext = elementScope; + expression.getComparison().accept(this); + writer.write("return $L;", executionContext); + }); executionContext = resultScope; return null; @@ -346,17 +350,18 @@ private String makeNewScope(String prefix) { } private String serializeObject(Map obj) { - return "{" + obj.entrySet().stream() - .map(entry -> "\"" + entry.getKey() + "\":" + serializeValue(entry.getValue())) - .collect(Collectors.joining(",")) - + "}"; + return "{" + obj.entrySet() + .stream() + .map(entry -> "\"" + entry.getKey() + "\":" + serializeValue(entry.getValue())) + .collect(Collectors.joining(",")) + + "}"; } private String serializeArray(List array) { return "[" + array.stream() - .map(this::serializeValue) - .collect(Collectors.joining(",")) - + "]"; + .map(this::serializeValue) + .collect(Collectors.joining(",")) + + "]"; } @SuppressWarnings("unchecked") diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSSDKCodegenPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSSDKCodegenPlugin.java index 5bbb54436ed..c2438765513 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSSDKCodegenPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSSDKCodegenPlugin.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import software.amazon.smithy.utils.SmithyInternalApi; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptServerCodegenPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptServerCodegenPlugin.java index 50d57d783a3..6ba03a321c4 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptServerCodegenPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptServerCodegenPlugin.java @@ -1,18 +1,7 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.ServiceLoader; @@ -38,8 +27,8 @@ public String getName() { @Override public void execute(PluginContext context) { - CodegenDirector runner - = new CodegenDirector<>(); + CodegenDirector runner = + new CodegenDirector<>(); runner.directedCodegen(new DirectedTypeScriptCodegen()); @@ -51,25 +40,27 @@ public void execute(PluginContext context) { runner.model(context.getModel()); // Create the TypeScriptSettings object from the plugin settings. - TypeScriptSettings settings = TypeScriptSettings.from(context.getModel(), context.getSettings(), + TypeScriptSettings settings = TypeScriptSettings.from(context.getModel(), + context.getSettings(), TypeScriptSettings.ArtifactType.SSDK); runner.settings(settings); // Only add integrations if the integrations match the settings // This uses {@link TypeScriptIntegration#matchesSettings}, which is a // Smithy internal API. This may be removed at any point. - runner.integrationFinder(() -> - () -> ServiceLoader.load(TypeScriptIntegration.class, CodegenDirector.class.getClassLoader()) - .stream() - .map(Provider::get) - .filter(integration -> { - boolean matchesSettings = integration.matchesSettings(settings); - if (!matchesSettings) { - LOGGER.fine(() -> "Skipping TypeScript integration based on settings: " + integration.name()); - } - return matchesSettings; - }) - .iterator()); + runner.integrationFinder( + () -> () -> ServiceLoader.load(TypeScriptIntegration.class, CodegenDirector.class.getClassLoader()) + .stream() + .map(Provider::get) + .filter(integration -> { + boolean matchesSettings = integration.matchesSettings(settings); + if (!matchesSettings) { + LOGGER.fine(() -> "Skipping TypeScript integration based on settings: " + + integration.name()); + } + return matchesSettings; + }) + .iterator()); runner.service(settings.getService()); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSettings.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSettings.java index b9bde744695..e5c495875be 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSettings.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSettings.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.ArrayList; @@ -85,7 +74,7 @@ public final class TypeScriptSettings { private ArtifactType artifactType = ArtifactType.CLIENT; private boolean disableDefaultValidation = false; private RequiredMemberMode requiredMemberMode = - RequiredMemberMode.NULLABLE; + RequiredMemberMode.NULLABLE; private PackageManager packageManager = PackageManager.YARN; private boolean createDefaultReadme = false; private boolean useLegacyAuth = false; @@ -121,7 +110,8 @@ public static TypeScriptSettings from(Model model, ObjectNode config, ArtifactTy settings.setPackageName(config.expectStringMember(PACKAGE).getValue()); settings.setPackageVersion(config.expectStringMember(PACKAGE_VERSION).getValue()); settings.setPackageDescription(config.getStringMemberOrDefault( - PACKAGE_DESCRIPTION, settings.getDefaultDescription())); + PACKAGE_DESCRIPTION, + settings.getDefaultDescription())); settings.packageJson = config.getObjectMember(PACKAGE_JSON).orElse(Node.objectNode()); config.getStringMember(PROTOCOL).map(StringNode::getValue).map(ShapeId::from).ifPresent(settings::setProtocol); settings.setPrivate(config.getBooleanMember(PRIVATE).map(BooleanNode::getValue).orElse(false)); @@ -133,32 +123,29 @@ public static TypeScriptSettings from(Model model, ObjectNode config, ArtifactTy config.getBooleanMember(GENERATE_TYPEDOC).map(BooleanNode::getValue).orElse(false)); settings.setPackageManager( config.getStringMember(PACKAGE_MANAGER) - .map(s -> PackageManager.fromString(s.getValue())) - .orElse(PackageManager.YARN)); + .map(s -> PackageManager.fromString(s.getValue())) + .orElse(PackageManager.YARN)); if (artifactType == ArtifactType.SSDK) { settings.setDisableDefaultValidation(config.getBooleanMemberOrDefault(DISABLE_DEFAULT_VALIDATION)); } settings.setRequiredMemberMode( - config.getStringMember(REQUIRED_MEMBER_MODE) - .map(s -> RequiredMemberMode.fromString(s.getValue())) - .orElse(RequiredMemberMode.NULLABLE)); + config.getStringMember(REQUIRED_MEMBER_MODE) + .map(s -> RequiredMemberMode.fromString(s.getValue())) + .orElse(RequiredMemberMode.NULLABLE)); settings.setPluginSettings(config); settings.readProtocolPriorityConfiguration(config); settings.setBigNumberMode( - config.getStringMemberOrDefault("bigNumberMode", "native") - ); + config.getStringMemberOrDefault("bigNumberMode", "native")); // Internal undocumented configuration used to control rollout of schemas. // `true` will eventually be the only available option, and this should not be set by users. settings.setGenerateSchemas( - config.getBooleanMemberOrDefault("generateSchemas", false) - ); + config.getBooleanMemberOrDefault("generateSchemas", false)); settings.setGenerateIndexTests( - config.getBooleanMemberOrDefault("generateIndexTests", false) - ); + config.getBooleanMemberOrDefault("generateIndexTests", false)); return settings; } @@ -187,10 +174,10 @@ private static ShapeId inferService(Model model) { if (services.isEmpty()) { throw new CodegenException("Cannot infer a service to generate because the model does not " - + "contain any service shapes"); + + "contain any service shapes"); } else if (services.size() > 1) { throw new CodegenException("Cannot infer a service to generate because the model contains " - + "multiple service shapes: " + services); + + "multiple service shapes: " + services); } else { LOGGER.fine("Inferring service to generate as " + services.get(0)); return services.get(0); @@ -249,7 +236,7 @@ public String getBigNumberMode() { public void setBigNumberMode(String mode) { if (!mode.equals("big.js") && !mode.equals("native")) { throw new IllegalArgumentException(""" - bigNumberMode must be one of ["native", "big.js"]"""); + bigNumberMode must be one of ["native", "big.js"]"""); } this.bigNumberMode = mode; } @@ -406,15 +393,17 @@ public RequiredMemberMode getRequiredMemberMode() { } public void setRequiredMemberMode( - RequiredMemberMode requiredMemberMode) { - if (requiredMemberMode != RequiredMemberMode.NULLABLE) { - LOGGER.warning(String.format("By setting the required member mode to '%s', a" + RequiredMemberMode requiredMemberMode + ) { + if (requiredMemberMode != RequiredMemberMode.NULLABLE) { + LOGGER.warning(String.format("By setting the required member mode to '%s', a" + " member that has the '@required' trait applied CANNOT be 'undefined'." + " It will be considered a BACKWARDS INCOMPATIBLE change for" + " Smithy services even when the required constraint is dropped from a member.", - requiredMemberMode.mode, RequiredMemberMode.NULLABLE.mode)); - } - this.requiredMemberMode = requiredMemberMode; + requiredMemberMode.mode, + RequiredMemberMode.NULLABLE.mode)); + } + this.requiredMemberMode = requiredMemberMode; } /** @@ -447,9 +436,9 @@ public boolean useLegacyAuth() { public void useLegacyAuth(boolean useLegacyAuth) { if (useLegacyAuth) { LOGGER.warning(""" - Legacy auth is considered deprecated and is no longer in development, - and should only be used for backward compatibility concerns. Consider - migrating to the default identity and auth behavior."""); + Legacy auth is considered deprecated and is no longer in development, + and should only be used for backward compatibility concerns. Consider + migrating to the default identity and auth behavior."""); } this.useLegacyAuth = useLegacyAuth; } @@ -517,22 +506,24 @@ public ShapeId resolveServiceProtocol(Model model, ServiceShape service, Set protocolPriority = this.protocolPriorityConfig.getProtocolPriority(service.toShapeId()); List protocolPriorityList = protocolPriority != null && !protocolPriority.isEmpty() - ? protocolPriority - : new ArrayList<>(supportedProtocols); + ? protocolPriority + : new ArrayList<>(supportedProtocols); return protocolPriorityList.stream() .filter(resolvedProtocols::contains) .findFirst() .orElseThrow(() -> new UnresolvableProtocolException(String.format( "The %s service supports the following unsupported protocols %s. The following protocol " - + "generators were found on the class path: %s", - service.getId(), resolvedProtocols, supportedProtocols))); + + "generators were found on the class path: %s", + service.getId(), + resolvedProtocols, + supportedProtocols))); } /** @@ -575,24 +566,46 @@ public void setProtocolPriority(ProtocolPriorityConfig protocolPriorityConfig) { public enum ArtifactType { CLIENT(SymbolVisitor::new, Arrays.asList( - PACKAGE, PACKAGE_DESCRIPTION, PACKAGE_JSON, PACKAGE_VERSION, PACKAGE_MANAGER, - SERVICE, PROTOCOL, PRIVATE, REQUIRED_MEMBER_MODE, - CREATE_DEFAULT_README, USE_LEGACY_AUTH, GENERATE_TYPEDOC, - GENERATE_INDEX_TESTS, BIG_NUMBER_MODE, GENERATE_SCHEMAS - )), + PACKAGE, + PACKAGE_DESCRIPTION, + PACKAGE_JSON, + PACKAGE_VERSION, + PACKAGE_MANAGER, + SERVICE, + PROTOCOL, + PRIVATE, + REQUIRED_MEMBER_MODE, + CREATE_DEFAULT_README, + USE_LEGACY_AUTH, + GENERATE_TYPEDOC, + GENERATE_INDEX_TESTS, + BIG_NUMBER_MODE, + GENERATE_SCHEMAS)), SSDK((m, s) -> new ServerSymbolVisitor(m, new SymbolVisitor(m, s)), Arrays.asList( - PACKAGE, PACKAGE_DESCRIPTION, PACKAGE_JSON, PACKAGE_VERSION, PACKAGE_MANAGER, - SERVICE, PROTOCOL, PRIVATE, REQUIRED_MEMBER_MODE, - DISABLE_DEFAULT_VALIDATION, CREATE_DEFAULT_README, GENERATE_TYPEDOC, - GENERATE_INDEX_TESTS, BIG_NUMBER_MODE, GENERATE_SCHEMAS - )); + PACKAGE, + PACKAGE_DESCRIPTION, + PACKAGE_JSON, + PACKAGE_VERSION, + PACKAGE_MANAGER, + SERVICE, + PROTOCOL, + PRIVATE, + REQUIRED_MEMBER_MODE, + DISABLE_DEFAULT_VALIDATION, + CREATE_DEFAULT_README, + GENERATE_TYPEDOC, + GENERATE_INDEX_TESTS, + BIG_NUMBER_MODE, + GENERATE_SCHEMAS)); private final BiFunction symbolProviderFactory; private final List configProperties; - ArtifactType(BiFunction symbolProviderFactory, - List configProperties) { + ArtifactType( + BiFunction symbolProviderFactory, + List configProperties + ) { this.symbolProviderFactory = symbolProviderFactory; this.configProperties = Collections.unmodifiableList(configProperties); } @@ -702,33 +715,29 @@ private void readProtocolPriorityConfiguration(ObjectNode config) { ObjectNode objectNode = protocolPriorityNode.get(); objectNode.getMembers().forEach((StringNode k, Node v) -> { ShapeId serviceShapeId = ShapeId.from(k.getValue()); - List protocolList = v.asArrayNode().get().getElementsAs( - e -> ShapeId.from(e.asStringNode().get().getValue()) - ); + List protocolList = v.asArrayNode() + .get() + .getElementsAs( + e -> ShapeId.from(e.asStringNode().get().getValue())); serviceProtocolPriorityCustomizations.put( - serviceShapeId, - protocolList - ); + serviceShapeId, + protocolList); }); } Optional defaultProtocolPriorityOpt = config.getArrayMember(DEFAULT_PROTOCOL_PRIORITY); if (defaultProtocolPriorityOpt.isPresent()) { ArrayNode defaultProtocolPriorityStringArr = defaultProtocolPriorityOpt.get(); customDefaultPriority.addAll( - defaultProtocolPriorityStringArr.getElementsAs( - e -> ShapeId.from(e.asStringNode().get().getValue()) - ) - ); + defaultProtocolPriorityStringArr.getElementsAs( + e -> ShapeId.from(e.asStringNode().get().getValue()))); } } catch (Exception e) { throw new IllegalArgumentException( - "Error while parsing serviceProtocolPriority or defaultProtocolPriority configuration fields", - e - ); + "Error while parsing serviceProtocolPriority or defaultProtocolPriority configuration fields", + e); } protocolPriorityConfig = new ProtocolPriorityConfig( - serviceProtocolPriorityCustomizations, - customDefaultPriority.isEmpty() ? null : customDefaultPriority - ); + serviceProtocolPriorityCustomizations, + customDefaultPriority.isEmpty() ? null : customDefaultPriority); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptUtils.java index c3cf7b089dc..4604ce397c9 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptUtils.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.io.IOException; @@ -44,8 +33,8 @@ private TypeScriptUtils() {} */ static String sanitizePropertyName(String memberName) { return isValidPropertyName(memberName) - ? memberName - : StringUtils.escapeJavaString(memberName, ""); + ? memberName + : StringUtils.escapeJavaString(memberName, ""); } /** diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptWriter.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptWriter.java index 9d2c20feed6..9413296f754 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptWriter.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptWriter.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Path; @@ -224,11 +213,13 @@ public TypeScriptWriter writeDocs(String docs) { * As openBlock, but collapses all space between open and close strings * if the condition is not met. */ - public TypeScriptWriter openCollapsibleBlock(String open, - String close, - boolean condition, - Object[] args, - Runnable runnable) { + public TypeScriptWriter openCollapsibleBlock( + String open, + String close, + boolean condition, + Object[] args, + Runnable runnable + ) { if (condition) { openBlock(open, close, args, runnable); } else { @@ -237,14 +228,22 @@ public TypeScriptWriter openCollapsibleBlock(String open, return this; } - public TypeScriptWriter openCollapsibleBlock(String open, String close, boolean condition, - Runnable runnable) { + public TypeScriptWriter openCollapsibleBlock( + String open, + String close, + boolean condition, + Runnable runnable + ) { return openCollapsibleBlock(open, close, condition, new Object[] {}, runnable); } - public TypeScriptWriter openCollapsibleBlock(String open, String close, boolean condition, - Object arg1, - Runnable runnable) { + public TypeScriptWriter openCollapsibleBlock( + String open, + String close, + boolean condition, + Object arg1, + Runnable runnable + ) { return openCollapsibleBlock(open, close, condition, new Object[] {arg1}, runnable); } @@ -261,11 +260,11 @@ boolean writeShapeDocs(Shape shape, UnaryOperator preprocessor) { .map(docs -> { // Escape valid '{' and '}' docs = docs.replace("{", "\\{") - .replace("}", "\\}"); + .replace("}", "\\}"); if (shape.getTrait(DeprecatedTrait.class).isPresent()) { DeprecatedTrait deprecatedTrait = shape.expectTrait(DeprecatedTrait.class); String deprecationMessage = deprecatedTrait.getMessage() - .orElse("deprecated"); + .orElse("deprecated"); String deprecationAnnotation = "@deprecated " + deprecationMessage; docs = docs + "\n\n" + deprecationAnnotation; } @@ -273,7 +272,8 @@ boolean writeShapeDocs(Shape shape, UnaryOperator preprocessor) { docs = addReleaseTag(shape, docs); writeDocs(docs); return true; - }).orElse(false); + }) + .orElse(false); } /** @@ -303,26 +303,27 @@ boolean writeMemberDocs(Model model, MemberShape member) { .map(docs -> { // Escape valid '{' and '}' docs = docs.replace("{", "\\{") - .replace("}", "\\}"); + .replace("}", "\\}"); if (member.getTrait(DeprecatedTrait.class).isPresent() || isTargetDeprecated(model, member)) { DeprecatedTrait deprecatedTrait = member.getTrait(DeprecatedTrait.class) - .or(() -> model.expectShape(member.getTarget()).getTrait(DeprecatedTrait.class)) - .orElseThrow(); + .or(() -> model.expectShape(member.getTarget()).getTrait(DeprecatedTrait.class)) + .orElseThrow(); String deprecationMessage = deprecatedTrait.getMessage() - .orElse("deprecated"); + .orElse("deprecated"); String deprecationAnnotation = "@deprecated " + deprecationMessage; docs = docs + "\n\n" + deprecationAnnotation; } docs = addReleaseTag(member, docs); writeDocs(docs); return true; - }).orElse(false); + }) + .orElse(false); } private boolean isTargetDeprecated(Model model, MemberShape member) { return model.expectShape(member.getTarget()).getTrait(DeprecatedTrait.class).isPresent() - // don't consider deprecated prelude shapes (like PrimitiveBoolean) - && !Prelude.isPreludeShape(member.getTarget()); + // don't consider deprecated prelude shapes (like PrimitiveBoolean) + && !Prelude.isPreludeShape(member.getTarget()); } private String addReleaseTag(Shape shape, String docs) { @@ -355,15 +356,14 @@ private void checkImport(ImportFrom importFrom, String from) { if (importFrom.isDeclarablePackageImport()) { String packageName = importFrom.getPackageName(); if (getDependencies() - .stream() - .map(SymbolDependency::getPackageName) - .noneMatch(packageName::equals)) { + .stream() + .map(SymbolDependency::getPackageName) + .noneMatch(packageName::equals)) { throw new CodegenException( - """ - The import %s does not correspond to a registered dependency. - TypeScriptWriter::addDependency() is required before ::addImport(). - """.formatted(from) - ); + """ + The import %s does not correspond to a registered dependency. + TypeScriptWriter::addDependency() is required before ::addImport(). + """.formatted(from)); } } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnionGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnionGenerator.java index 886ede3d973..5016b8ef809 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnionGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnionGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.util.Map; @@ -151,12 +140,14 @@ final class UnionGenerator implements Runnable { this(model, symbolProvider, writer, shape, false, false); } - UnionGenerator(Model model, + UnionGenerator( + Model model, SymbolProvider symbolProvider, TypeScriptWriter writer, UnionShape shape, boolean includeValidation, - boolean schemaMode) { + boolean schemaMode + ) { this.shape = shape; this.symbol = symbolProvider.toSymbol(shape); this.model = model; @@ -187,15 +178,15 @@ public void run() { // Write out the namespace that contains each variant and visitor. writer.writeDocs("@public") - .openBlock("export namespace $L {", "}", symbol.getName(), () -> { - writeUnionMemberInterfaces(); - writeVisitorType(); - writeVisitorFunction(); - if (includeValidation) { - writeValidate(); - } - writer.unwrite("\n"); - }); + .openBlock("export namespace $L {", "}", symbol.getName(), () -> { + writeUnionMemberInterfaces(); + writeVisitorType(); + writeVisitorFunction(); + if (includeValidation) { + writeValidate(); + } + writer.unwrite("\n"); + }); writeFilterSensitiveLog(symbol.getName()); } @@ -206,7 +197,8 @@ private void writeUnionMemberInterfaces() { writer.openBlock("export interface $L {", "}", name, () -> { for (MemberShape variantMember : shape.getAllMembers().values()) { if (variantMember.getMemberName().equals(member.getMemberName())) { - writer.write("$L: $T;", symbolProvider.toMemberName(variantMember), + writer.write("$L: $T;", + symbolProvider.toMemberName(variantMember), symbolProvider.toSymbol(variantMember)); } else { writer.write("$L?: never;", symbolProvider.toMemberName(variantMember)); @@ -231,13 +223,14 @@ private void writeUnionMemberInterfaces() { private void writeVisitorType() { if (schemaMode) { writer.writeDocs(""" - @deprecated unused in schema-serde mode. - """); + @deprecated unused in schema-serde mode. + """); } writer.openBlock("export interface Visitor {", "}", () -> { for (MemberShape member : shape.getAllMembers().values()) { writer.write("$L: (value: $T) => T;", - symbolProvider.toMemberName(member), symbolProvider.toSymbol(member)); + symbolProvider.toMemberName(member), + symbolProvider.toSymbol(member)); } writer.write("_: (name: string, value: any) => T;"); }); @@ -265,9 +258,11 @@ private void writeFilterSensitiveLog(String namespace) { if (sensitiveDataFinder.findsSensitiveDataIn(shape) && !schemaMode) { String objectParam = "obj"; writer.writeDocs("@internal"); - writer.openBlock("export const $LFilterSensitiveLog = ($L: $L): any => {", "}", + writer.openBlock("export const $LFilterSensitiveLog = ($L: $L): any => {", + "}", namespace, - objectParam, symbol.getName(), + objectParam, + symbol.getName(), () -> { for (MemberShape member : shape.getAllMembers().values()) { String memberName = symbolProvider.toMemberName(member); @@ -279,27 +274,25 @@ private void writeFilterSensitiveLog(String namespace) { sensitiveDataFinder); writer.writeInline(""" - if (${1L}.${2L} !== undefined) { - return { - ${2L}:\s""", - objectParam, - memberName - ); + if (${1L}.${2L} !== undefined) { + return { + ${2L}:\s""", + objectParam, + memberName); String memberParam = String.format("%s.%s", objectParam, memberName); writer.indent(2); structuredMemberWriter.writeMemberFilterSensitiveLog( - writer, member, - memberParam - ); + writer, + member, + memberParam); writer.dedent(1); writer.write("};"); writer.dedent(1); writer.write("}"); } writer.write( - "if (${1L}.$$unknown !== undefined) return { [${1L}.$$unknown[0]]: \"UNKNOWN\" };", - objectParam - ); + "if (${1L}.$$unknown !== undefined) return { [${1L}.$$unknown[0]]: \"UNKNOWN\" };", + objectParam); }); } } @@ -316,8 +309,10 @@ private void writeValidate() { writer.addImport("ValidationFailure", "__ValidationFailure", TypeScriptDependency.SERVER_COMMON); writer.writeDocs("@internal"); - writer.openBlock("export const validate = ($L: $L, path: string = \"\"): __ValidationFailure[] => {", "}", - "obj", symbol.getName(), + writer.openBlock("export const validate = ($L: $L, path: string = \"\"): __ValidationFailure[] => {", + "}", + "obj", + symbol.getName(), () -> { structuredMemberWriter.writeMemberValidatorFactory(writer, "memberValidators"); structuredMemberWriter.writeValidateMethodContents(writer, "obj"); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnresolvableProtocolException.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnresolvableProtocolException.java index 73a4ea75339..047459d8049 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnresolvableProtocolException.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/UnresolvableProtocolException.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import software.amazon.smithy.codegen.core.CodegenException; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/WaiterGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/WaiterGenerator.java index 73dbbf921de..5b93a0b5965 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/WaiterGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/WaiterGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen; import java.nio.file.Paths; @@ -54,7 +43,8 @@ class WaiterGenerator implements Runnable { ServiceShape service, OperationShape operation, TypeScriptWriter writer, - SymbolProvider symbolProvider) { + SymbolProvider symbolProvider + ) { this.waiterName = waiterName; this.waiter = waiter; this.writer = writer; @@ -88,10 +78,11 @@ private void generateWaiter() { + "waitFor" + waiterName + " does not throw error in non-success cases."); writer.openBlock("export const waitFor$L = async (params: WaiterConfiguration<$T>, input: $T): " + "Promise => {", "}", waiterName, serviceSymbol, inputSymbol, () -> { - writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", waiter.getMinDelay(), + writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", + waiter.getMinDelay(), waiter.getMaxDelay()); - writer.write("return createWaiter({...serviceDefaults, ...params}, input, checkState);"); - }); + writer.write("return createWaiter({...serviceDefaults, ...params}, input, checkState);"); + }); // generates WaitUtil.... writer.writeDocs(waiter.getDocumentation().orElse("") + " \n" @@ -99,16 +90,21 @@ private void generateWaiter() { + " @param input - The input to " + operationSymbol.getName() + " for polling."); writer.openBlock("export const waitUntil$L = async (params: WaiterConfiguration<$T>, input: $T): " + "Promise => {", "}", waiterName, serviceSymbol, inputSymbol, () -> { - writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", waiter.getMinDelay(), - waiter.getMaxDelay()); - writer.write("const result = await createWaiter({...serviceDefaults, ...params}, input, checkState);"); - writer.write("return checkExceptions(result);"); - }); + writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", + waiter.getMinDelay(), + waiter.getMaxDelay()); + writer.write( + "const result = await createWaiter({...serviceDefaults, ...params}, input, checkState);"); + writer.write("return checkExceptions(result);"); + }); } private void generateAcceptors() { - writer.openBlock("const checkState = async (client: $T, input: $T): Promise => {", "}", - serviceSymbol, inputSymbol, () -> { + writer.openBlock("const checkState = async (client: $T, input: $T): Promise => {", + "}", + serviceSymbol, + inputSymbol, + () -> { writer.write("let reason;"); writer.openBlock("try {", "}", () -> { writer.write("let result: any = await client.send(new $T(input))", operationSymbol); @@ -132,7 +128,8 @@ private void writeAcceptors(String accessor, boolean isException) { } } else if (acceptor.getMatcher() instanceof Matcher.ErrorTypeMember) { if (isException) { - generateErrorMatcher(accessor, (Matcher.ErrorTypeMember) acceptor.getMatcher(), + generateErrorMatcher(accessor, + (Matcher.ErrorTypeMember) acceptor.getMatcher(), acceptor.getState()); } } else if (acceptor.getMatcher() instanceof Matcher.InputOutputMember) { @@ -157,8 +154,12 @@ private void generateSuccessMatcher(Matcher.SuccessMember member, AcceptorState } private void generateErrorMatcher(String accessor, Matcher.ErrorTypeMember member, AcceptorState state) { - writer.openBlock("if ($L.name && $L.name == $S) {", "}", accessor, accessor, - member.getValue(), () -> { + writer.openBlock("if ($L.name && $L.name == $S) {", + "}", + accessor, + accessor, + member.getValue(), + () -> { writer.write("return $L", makeWaiterResult(state)); }); } @@ -191,20 +192,19 @@ private void generatePathMatcher(String accessor, PathMatcher pathMatcher, Accep private String makeWaiterResult(AcceptorState resultantState) { if (resultantState == AcceptorState.SUCCESS) { - return "{ state: WaiterState.SUCCESS, reason }"; + return "{ state: WaiterState.SUCCESS, reason }"; } else if (resultantState == AcceptorState.FAILURE) { - return "{ state: WaiterState.FAILURE, reason }"; + return "{ state: WaiterState.FAILURE, reason }"; } else if (resultantState == AcceptorState.RETRY) { - return "{ state: WaiterState.RETRY, reason }"; + return "{ state: WaiterState.RETRY, reason }"; } throw new CodegenException("Hit an invalid acceptor state to codegen " + resultantState.toString()); } private static String getModulePath(String fileLocation) { return fileLocation.substring( - fileLocation.lastIndexOf("/") + 1, - fileLocation.length() - ).replace(".ts", ""); + fileLocation.lastIndexOf("/") + 1, + fileLocation.length()).replace(".ts", ""); } static void writeIndex( @@ -227,7 +227,7 @@ static void writeIndex( } fileManifest.writeFile( - Paths.get(CodegenUtils.SOURCE_FOLDER, WAITERS_FOLDER, "index.ts").toString(), - writer.toString()); + Paths.get(CodegenUtils.SOURCE_FOLDER, WAITERS_FOLDER, "index.ts").toString(), + writer.toString()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java index a0737f2da70..099845bde5e 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/AuthUtils.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth; import java.nio.file.Paths; @@ -38,7 +37,7 @@ public final class AuthUtils { public static final String HTTP_AUTH_FOLDER = "auth"; public static final String HTTP_AUTH_SCHEME_PROVIDER_MODULE = - Paths.get(".", CodegenUtils.SOURCE_FOLDER, HTTP_AUTH_FOLDER, "httpAuthSchemeProvider").toString(); + Paths.get(".", CodegenUtils.SOURCE_FOLDER, HTTP_AUTH_FOLDER, "httpAuthSchemeProvider").toString(); public static final String HTTP_AUTH_SCHEME_PROVIDER_PATH = HTTP_AUTH_SCHEME_PROVIDER_MODULE + ".ts"; @@ -55,7 +54,7 @@ public List getDependencies() { }; public static final String HTTP_AUTH_SCHEME_EXTENSION_MODULE = - Paths.get(".", CodegenUtils.SOURCE_FOLDER, HTTP_AUTH_FOLDER, "httpAuthExtensionConfiguration").toString(); + Paths.get(".", CodegenUtils.SOURCE_FOLDER, HTTP_AUTH_FOLDER, "httpAuthExtensionConfiguration").toString(); public static final String HTTP_AUTH_SCHEME_EXTENSION_PATH = HTTP_AUTH_SCHEME_EXTENSION_MODULE + ".ts"; @@ -74,20 +73,20 @@ public List getDependencies() { private AuthUtils() {} public static Map getAllEffectiveNoAuthAwareAuthSchemes( - ServiceShape serviceShape, - ServiceIndex serviceIndex, - SupportedHttpAuthSchemesIndex authIndex, - TopDownIndex topDownIndex + ServiceShape serviceShape, + ServiceIndex serviceIndex, + SupportedHttpAuthSchemesIndex authIndex, + TopDownIndex topDownIndex ) { Map effectiveAuthSchemes = new TreeMap<>(); var serviceEffectiveAuthSchemes = - serviceIndex.getEffectiveAuthSchemes(serviceShape, AuthSchemeMode.NO_AUTH_AWARE); + serviceIndex.getEffectiveAuthSchemes(serviceShape, AuthSchemeMode.NO_AUTH_AWARE); for (ShapeId shapeId : serviceEffectiveAuthSchemes.keySet()) { effectiveAuthSchemes.put(shapeId, authIndex.getHttpAuthScheme(shapeId)); } for (var operation : topDownIndex.getContainedOperations(serviceShape)) { var operationEffectiveAuthSchemes = - serviceIndex.getEffectiveAuthSchemes(serviceShape, operation, AuthSchemeMode.NO_AUTH_AWARE); + serviceIndex.getEffectiveAuthSchemes(serviceShape, operation, AuthSchemeMode.NO_AUTH_AWARE); for (ShapeId shapeId : operationEffectiveAuthSchemes.keySet()) { effectiveAuthSchemes.put(shapeId, authIndex.getHttpAuthScheme(shapeId)); } @@ -115,11 +114,11 @@ public static Map collectConfigFields(Collection collectConfigFields(Collection collectResolveConfigFunctions( - Collection httpAuthSchemes + Collection httpAuthSchemes ) { Map resolveConfigFunctions = new HashMap<>(); for (HttpAuthScheme authScheme : httpAuthSchemes) { @@ -140,14 +139,14 @@ public static Map collectResolveConfigFunctions( for (ResolveConfigFunction fn : authScheme.getResolveConfigFunctions()) { if (resolveConfigFunctions.containsKey(fn.resolveConfigFunction())) { ResolveConfigFunction existingFn = - resolveConfigFunctions.get(fn.resolveConfigFunction()); + resolveConfigFunctions.get(fn.resolveConfigFunction()); if (!fn.equals(existingFn)) { throw new CodegenException("Contradicting `ResolveConfigFunction` definitions for `" - + fn.resolveConfigFunction() - + "`; existing: " - + existingFn - + ", conflict: " - + fn); + + fn.resolveConfigFunction() + + "`; existing: " + + existingFn + + ", conflict: " + + fn); } } else { resolveConfigFunctions.put(fn.resolveConfigFunction(), fn); @@ -158,7 +157,8 @@ public static Map collectResolveConfigFunctions( } public static Map collectHttpAuthSchemeParameters( - Collection httpAuthSchemes) { + Collection httpAuthSchemes + ) { Map httpAuthSchemeParameters = new HashMap<>(); for (HttpAuthScheme authScheme : httpAuthSchemes) { if (authScheme == null) { @@ -169,11 +169,11 @@ public static Map collectHttpAuthSchemeParamete HttpAuthSchemeParameter existingParam = httpAuthSchemeParameters.get(param.name()); if (!param.equals(existingParam)) { throw new CodegenException("Contradicting `HttpAuthSchemeParameter` definitions for `" - + param.name() - + "`; existing: " - + existingParam - + ", conflict: " - + param); + + param.name() + + "`; existing: " + + existingParam + + ", conflict: " + + param); } } else { httpAuthSchemeParameters.put(param.name(), param); @@ -184,8 +184,8 @@ public static Map collectHttpAuthSchemeParamete } public static boolean areHttpAuthSchemesEqual( - Map httpAuthSchemes1, - Map httpAuthSchemes2 + Map httpAuthSchemes1, + Map httpAuthSchemes2 ) { if (httpAuthSchemes1.size() != httpAuthSchemes2.size()) { return false; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java index fc1a60ed6cd..570d63f93e5 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ConfigField.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http; import java.util.Optional; @@ -29,13 +28,12 @@ */ @SmithyUnstableApi public record ConfigField( - String name, - Type type, - Symbol inputType, - Symbol resolvedType, - Optional> configFieldWriter, - Optional> docs -) implements ToSmithyBuilder { + String name, + Type type, + Symbol inputType, + Symbol resolvedType, + Optional> configFieldWriter, + Optional> docs) implements ToSmithyBuilder { /** * Defines the type of the config field. @@ -59,12 +57,12 @@ public static Builder builder() { @Override public Builder toBuilder() { return builder() - .name(name) - .type(type) - .inputType(inputType) - .resolvedType(resolvedType) - .configFieldWriter(configFieldWriter.orElse(null)) - .docs(docs.orElse(null)); + .name(name) + .type(type) + .inputType(inputType) + .resolvedType(resolvedType) + .configFieldWriter(configFieldWriter.orElse(null)) + .docs(docs.orElse(null)); } public static final class Builder implements SmithyBuilder { @@ -78,12 +76,12 @@ public static final class Builder implements SmithyBuilder { @Override public ConfigField build() { return new ConfigField( - SmithyBuilder.requiredState("name", name), - SmithyBuilder.requiredState("type", type), - SmithyBuilder.requiredState("inputType", inputType), - SmithyBuilder.requiredState("resolvedType", resolvedType), - Optional.ofNullable(configFieldWriter), - Optional.ofNullable(docs)); + SmithyBuilder.requiredState("name", name), + SmithyBuilder.requiredState("type", type), + SmithyBuilder.requiredState("inputType", inputType), + SmithyBuilder.requiredState("resolvedType", resolvedType), + Optional.ofNullable(configFieldWriter), + Optional.ofNullable(docs)); } public Builder name(String name) { @@ -119,33 +117,36 @@ public Builder configFieldWriter(BiConsumer confi @SmithyInternalApi public static void defaultMainConfigFieldWriter( - TypeScriptWriter w, - ConfigField configField + TypeScriptWriter w, + ConfigField configField ) { w.addDependency(TypeScriptDependency.SMITHY_CORE); - w.addImport("memoizeIdentityProvider", null, - TypeScriptDependency.SMITHY_CORE); - w.addImport("isIdentityExpired", null, - TypeScriptDependency.SMITHY_CORE); - w.addImport("doesIdentityRequireRefresh", null, - TypeScriptDependency.SMITHY_CORE); + w.addImport("memoizeIdentityProvider", + null, + TypeScriptDependency.SMITHY_CORE); + w.addImport("isIdentityExpired", + null, + TypeScriptDependency.SMITHY_CORE); + w.addImport("doesIdentityRequireRefresh", + null, + TypeScriptDependency.SMITHY_CORE); w.write(""" - const $L = memoizeIdentityProvider(config.$L, isIdentityExpired, \ - doesIdentityRequireRefresh);""", - configField.name(), - configField.name()); + const $L = memoizeIdentityProvider(config.$L, isIdentityExpired, \ + doesIdentityRequireRefresh);""", + configField.name(), + configField.name()); } @SmithyInternalApi public static void defaultAuxiliaryConfigFieldWriter( - TypeScriptWriter w, - ConfigField configField + TypeScriptWriter w, + ConfigField configField ) { w.addDependency(TypeScriptDependency.UTIL_MIDDLEWARE); w.addImport("normalizeProvider", null, TypeScriptDependency.UTIL_MIDDLEWARE); w.write("const $L = config.$L ? normalizeProvider(config.$L) : undefined;", - configField.name(), - configField.name(), - configField.name()); + configField.name(), + configField.name(), + configField.name()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthOptionProperty.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthOptionProperty.java index 6260da4d086..75d90264f0f 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthOptionProperty.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthOptionProperty.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http; import java.util.function.Consumer; @@ -23,10 +22,9 @@ */ @SmithyUnstableApi public record HttpAuthOptionProperty( - String name, - Type type, - Function> source -) implements ToSmithyBuilder { + String name, + Type type, + Function> source) implements ToSmithyBuilder { /** * Defines the type of the auth option property. */ @@ -42,9 +40,8 @@ public enum Type { } public record Source( - HttpAuthScheme httpAuthScheme, - Trait trait - ) implements ToSmithyBuilder { + HttpAuthScheme httpAuthScheme, + Trait trait) implements ToSmithyBuilder { public static Builder builder() { return new Builder(); } @@ -52,8 +49,8 @@ public static Builder builder() { @Override public Builder toBuilder() { return builder() - .httpAuthScheme(httpAuthScheme) - .trait(trait); + .httpAuthScheme(httpAuthScheme) + .trait(trait); } public static final class Builder implements SmithyBuilder { @@ -63,8 +60,8 @@ public static final class Builder implements SmithyBuilder { @Override public Source build() { return new Source( - SmithyBuilder.requiredState("httpAuthScheme", httpAuthScheme), - SmithyBuilder.requiredState("trait", trait)); + SmithyBuilder.requiredState("httpAuthScheme", httpAuthScheme), + SmithyBuilder.requiredState("trait", trait)); } public Builder httpAuthScheme(HttpAuthScheme httpAuthScheme) { @@ -86,9 +83,9 @@ public static Builder builder() { @Override public Builder toBuilder() { return builder() - .name(name) - .type(type) - .source(source); + .name(name) + .type(type) + .source(source); } public static final class Builder implements SmithyBuilder { @@ -99,9 +96,9 @@ public static final class Builder implements SmithyBuilder { private HttpAuthScheme(Builder builder) { this.schemeId = SmithyBuilder.requiredState( - "schemeId", builder.schemeId); + "schemeId", + builder.schemeId); this.traitId = builder.traitId != null ? builder.traitId : schemeId; this.applicationProtocol = SmithyBuilder.requiredState( - "applicationProtocol", builder.applicationProtocol); + "applicationProtocol", + builder.applicationProtocol); this.defaultIdentityProviders = SmithyBuilder.requiredState( - "defaultIdentityProviders", builder.defaultIdentityProviders.copy()); + "defaultIdentityProviders", + builder.defaultIdentityProviders.copy()); this.defaultSigners = SmithyBuilder.requiredState( - "defaultSigners", builder.defaultSigners.copy()); + "defaultSigners", + builder.defaultSigners.copy()); this.configFields = SmithyBuilder.requiredState( - "configFields", builder.configFields.copy()); + "configFields", + builder.configFields.copy()); this.resolveConfigFunctions = SmithyBuilder.requiredState( - "resolveConfigFunctions", builder.resolveConfigFunctions.copy()); + "resolveConfigFunctions", + builder.resolveConfigFunctions.copy()); this.httpAuthSchemeParameters = SmithyBuilder.requiredState( - "httpAuthSchemeParameters", builder.httpAuthSchemeParameters.copy()); + "httpAuthSchemeParameters", + builder.httpAuthSchemeParameters.copy()); this.httpAuthOptionProperties = SmithyBuilder.requiredState( - "httpAuthOptionProperties", builder.httpAuthOptionProperties.copy()); + "httpAuthOptionProperties", + builder.httpAuthOptionProperties.copy()); this.propertiesExtractor = - builder.propertiesExtractor; + builder.propertiesExtractor; } /** @@ -147,7 +154,6 @@ public Optional>> getPropertiesExtra return Optional.ofNullable(propertiesExtractor); } - /** * Creates a {@link Builder}. * @return a builder @@ -163,16 +169,16 @@ public static Builder builder() { @Override public Builder toBuilder() { return builder() - .schemeId(schemeId) - .traitId(traitId) - .applicationProtocol(applicationProtocol) - .defaultIdentityProviders(defaultIdentityProviders) - .defaultSigners(defaultSigners) - .configFields(configFields) - .resolveConfigFunctions(resolveConfigFunctions) - .httpAuthSchemeParameters(httpAuthSchemeParameters) - .httpAuthOptionProperties(httpAuthOptionProperties) - .propertiesExtractor(propertiesExtractor); + .schemeId(schemeId) + .traitId(traitId) + .applicationProtocol(applicationProtocol) + .defaultIdentityProviders(defaultIdentityProviders) + .defaultSigners(defaultSigners) + .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) + .httpAuthSchemeParameters(httpAuthSchemeParameters) + .httpAuthOptionProperties(httpAuthOptionProperties) + .propertiesExtractor(propertiesExtractor); } /** @@ -183,17 +189,17 @@ public static final class Builder implements SmithyBuilder { private ShapeId traitId; private ApplicationProtocol applicationProtocol; private BuilderRef>> defaultIdentityProviders = - BuilderRef.forOrderedMap(); + BuilderRef.forOrderedMap(); private BuilderRef>> defaultSigners = - BuilderRef.forOrderedMap(); + BuilderRef.forOrderedMap(); private BuilderRef> configFields = - BuilderRef.forList(); + BuilderRef.forList(); private BuilderRef> resolveConfigFunctions = - BuilderRef.forList(); + BuilderRef.forList(); private BuilderRef> httpAuthSchemeParameters = - BuilderRef.forList(); + BuilderRef.forList(); private BuilderRef> httpAuthOptionProperties = - BuilderRef.forList(); + BuilderRef.forList(); private Function> propertiesExtractor; private Builder() {} @@ -239,7 +245,8 @@ public Builder applicationProtocol(ApplicationProtocol applicationProtocol) { * @return the builder */ public Builder defaultIdentityProviders( - Map> defaultIdentityProviders) { + Map> defaultIdentityProviders + ) { this.defaultIdentityProviders.clear(); this.defaultIdentityProviders.get().putAll(defaultIdentityProviders); return this; @@ -252,8 +259,8 @@ public Builder defaultIdentityProviders( * @return the builder */ public Builder putDefaultIdentityProvider( - LanguageTarget languageTarget, - Consumer identityProvider + LanguageTarget languageTarget, + Consumer identityProvider ) { this.defaultIdentityProviders.get().put(languageTarget, identityProvider); return this; @@ -275,7 +282,8 @@ public Builder removeDefaultIdentityProvider(LanguageTarget languageTarget) { * @return the builder */ public Builder defaultSigners( - Map> defaultSigners) { + Map> defaultSigners + ) { this.defaultSigners.clear(); this.defaultSigners.get().putAll(defaultSigners); return this; @@ -288,8 +296,8 @@ public Builder defaultSigners( * @return the builder */ public Builder putDefaultSigner( - LanguageTarget languageTarget, - Consumer signer + LanguageTarget languageTarget, + Consumer signer ) { this.defaultSigners.get().put(languageTarget, signer); return this; @@ -358,7 +366,8 @@ public Builder removeResolveConfigFunction(Symbol resolveConfigFunction) { * @return the builder */ public Builder httpAuthSchemeParameters( - List httpAuthSchemeParameters) { + List httpAuthSchemeParameters + ) { this.httpAuthSchemeParameters.clear(); this.httpAuthSchemeParameters.get().addAll(httpAuthSchemeParameters); return this; @@ -390,7 +399,8 @@ public Builder removeHttpAuthSchemeParameter(String httpAuthSchemeParameter) { * @return the builder */ public Builder httpAuthOptionProperties( - List httpAuthOptionProperties) { + List httpAuthOptionProperties + ) { this.httpAuthOptionProperties.clear(); this.httpAuthOptionProperties.get().addAll(httpAuthOptionProperties); return this; @@ -422,7 +432,8 @@ public Builder removeHttpAuthOptionProperty(String httpAuthOptionProperty) { * @return the builder */ public Builder propertiesExtractor( - Function> propertiesExtractor) { + Function> propertiesExtractor + ) { this.propertiesExtractor = propertiesExtractor; return this; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeParameter.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeParameter.java index c66180b476d..ed49a4f08a6 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeParameter.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/HttpAuthSchemeParameter.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http; import java.util.function.Consumer; @@ -22,10 +21,9 @@ */ @SmithyUnstableApi public record HttpAuthSchemeParameter( - String name, - Consumer type, - Consumer source -) implements ToSmithyBuilder { + String name, + Consumer type, + Consumer source) implements ToSmithyBuilder { public static Builder builder() { return new Builder(); @@ -34,9 +32,9 @@ public static Builder builder() { @Override public SmithyBuilder toBuilder() { return builder() - .name(name) - .type(type) - .source(source); + .name(name) + .type(type) + .source(source); } public static final class Builder implements SmithyBuilder { @@ -47,9 +45,9 @@ public static final class Builder implements SmithyBuilder integrations + TypeScriptDelegator delegator, + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + List integrations ) { this.delegator = delegator; this.settings = settings; @@ -96,9 +95,9 @@ public HttpAuthSchemeProviderGenerator( this.serviceSymbol = symbolProvider.toSymbol(serviceShape); this.serviceName = CodegenUtils.getServiceName(settings, model, symbolProvider); this.effectiveHttpAuthSchemes = - AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex); + AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex); this.httpAuthSchemeParameters = - AuthUtils.collectHttpAuthSchemeParameters(effectiveHttpAuthSchemes.values()); + AuthUtils.collectHttpAuthSchemeParameters(effectiveHttpAuthSchemes.values()); } @Override @@ -113,37 +112,36 @@ public void run() { /* import { HttpAuthSchemeParameters } from "@smithy/types"; - + // ... - + export interface WeatherHttpAuthSchemeParameters extends HttpAuthSchemeParameters { } */ private void generateHttpAuthSchemeParametersInterface() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { w.pushState(HttpAuthSchemeParametersInterfaceCodeSection.builder() - .service(serviceShape) - .settings(settings) - .model(model) - .symbolProvider(symbolProvider) - .httpAuthSchemeParameters(httpAuthSchemeParameters) - .build()); + .service(serviceShape) + .settings(settings) + .model(model) + .symbolProvider(symbolProvider) + .httpAuthSchemeParameters(httpAuthSchemeParameters) + .build()); w.addTypeImport("HttpAuthSchemeParameters", null, TypeScriptDependency.SMITHY_TYPES); w.openCollapsibleBlock(""" - /** - * @internal - */ - export interface $LHttpAuthSchemeParameters extends HttpAuthSchemeParameters {""", - "}", - !httpAuthSchemeParameters.isEmpty(), - serviceName, - () -> { - for (HttpAuthSchemeParameter parameter : httpAuthSchemeParameters.values()) { - w.write("$L?: $C;", parameter.name(), parameter.type()); - } - } - ); + /** + * @internal + */ + export interface $LHttpAuthSchemeParameters extends HttpAuthSchemeParameters {""", + "}", + !httpAuthSchemeParameters.isEmpty(), + serviceName, + () -> { + for (HttpAuthSchemeParameter parameter : httpAuthSchemeParameters.values()) { + w.write("$L?: $C;", parameter.name(), parameter.type()); + } + }); w.popState(); }); } @@ -151,9 +149,9 @@ export interface $LHttpAuthSchemeParameters extends HttpAuthSchemeParameters {"" /* import { HttpAuthSchemeParametersProvider } from "@smithy/types"; import { WeatherClientResolvedConfig } from "../WeatherClient"; - + // ... - + export interface WeatherHttpAuthSchemeParametersProvider extends HttpAuthSchemeParametersProvider< WeatherClientResolvedConfig, HandlerExecutionContext, @@ -164,27 +162,30 @@ export interface WeatherHttpAuthSchemeParametersProvider extends HttpAuthSchemeP private void generateHttpAuthSchemeParametersProviderInterface() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { w.pushState(HttpAuthSchemeParametersProviderInterfaceCodeSection.builder() - .service(serviceShape) - .settings(settings) - .model(model) - .symbolProvider(symbolProvider) - .build()); - w.addRelativeTypeImport(serviceSymbol.getName() + "ResolvedConfig", null, - Paths.get(".", serviceSymbol.getNamespace())); + .service(serviceShape) + .settings(settings) + .model(model) + .symbolProvider(symbolProvider) + .build()); + w.addRelativeTypeImport(serviceSymbol.getName() + "ResolvedConfig", + null, + Paths.get(".", serviceSymbol.getNamespace())); w.addTypeImport("HttpAuthSchemeParametersProvider", null, TypeScriptDependency.SMITHY_TYPES); w.addTypeImport("HandlerExecutionContext", null, TypeScriptDependency.SMITHY_TYPES); w.write(""" - /** - * @internal - */ - export interface $LHttpAuthSchemeParametersProvider - extends HttpAuthSchemeParametersProvider< - $LResolvedConfig, - HandlerExecutionContext, - $LHttpAuthSchemeParameters, - object - > {}""", - serviceName, serviceSymbol.getName(), serviceName); + /** + * @internal + */ + export interface $LHttpAuthSchemeParametersProvider + extends HttpAuthSchemeParametersProvider< + $LResolvedConfig, + HandlerExecutionContext, + $LHttpAuthSchemeParameters, + object + > {}""", + serviceName, + serviceSymbol.getName(), + serviceName); w.popState(); }); } @@ -201,35 +202,39 @@ export interface $LHttpAuthSchemeParametersProvider private void generateDefaultHttpAuthSchemeParametersProviderFunction() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { w.pushState(DefaultHttpAuthSchemeParametersProviderFunctionCodeSection.builder() - .service(serviceShape) - .settings(settings) - .model(model) - .symbolProvider(symbolProvider) - .httpAuthSchemeParameters(httpAuthSchemeParameters) - .build()); - w.addRelativeTypeImport(serviceSymbol.getName() + "ResolvedConfig", null, - Paths.get(".", serviceSymbol.getNamespace())); + .service(serviceShape) + .settings(settings) + .model(model) + .symbolProvider(symbolProvider) + .httpAuthSchemeParameters(httpAuthSchemeParameters) + .build()); + w.addRelativeTypeImport(serviceSymbol.getName() + "ResolvedConfig", + null, + Paths.get(".", serviceSymbol.getNamespace())); w.addTypeImport("HandlerExecutionContext", null, TypeScriptDependency.SMITHY_TYPES); w.addDependency(TypeScriptDependency.UTIL_MIDDLEWARE); w.addImport("getSmithyContext", null, TypeScriptDependency.UTIL_MIDDLEWARE); w.openBlock(""" - /** - * @internal - */ - export const default$LHttpAuthSchemeParametersProvider = async ( - config: $LResolvedConfig, - context: HandlerExecutionContext, - input: object - ): Promise<$LHttpAuthSchemeParameters> => {""", "};", - serviceName, serviceSymbol.getName(), serviceName, - () -> { - w.openBlock("return {", "};", () -> { - w.write("operation: getSmithyContext(context).operation as string,"); - for (HttpAuthSchemeParameter parameter : httpAuthSchemeParameters.values()) { - w.write("$L: $C,", parameter.name(), parameter.source()); - } - }); - }); + /** + * @internal + */ + export const default$LHttpAuthSchemeParametersProvider = async ( + config: $LResolvedConfig, + context: HandlerExecutionContext, + input: object + ): Promise<$LHttpAuthSchemeParameters> => {""", + "};", + serviceName, + serviceSymbol.getName(), + serviceName, + () -> { + w.openBlock("return {", "};", () -> { + w.write("operation: getSmithyContext(context).operation as string,"); + for (HttpAuthSchemeParameter parameter : httpAuthSchemeParameters.values()) { + w.write("$L: $C,", parameter.name(), parameter.source()); + } + }); + }); w.popState(); }); } @@ -237,22 +242,23 @@ private void generateDefaultHttpAuthSchemeParametersProviderFunction() { private void generateHttpAuthOptionFunctions() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { w.pushState(HttpAuthOptionFunctionsCodeSection.builder() - .service(serviceShape) - .settings(settings) - .model(model) - .symbolProvider(symbolProvider) - .effectiveHttpAuthSchemes(effectiveHttpAuthSchemes) - .build()); - for (Entry entry : effectiveHttpAuthSchemes.entrySet()) { - generateHttpAuthOptionFunction(w, HttpAuthOptionFunctionCodeSection.builder() .service(serviceShape) .settings(settings) .model(model) .symbolProvider(symbolProvider) .effectiveHttpAuthSchemes(effectiveHttpAuthSchemes) - .schemeId(entry.getKey()) - .httpAuthScheme(entry.getValue()) .build()); + for (Entry entry : effectiveHttpAuthSchemes.entrySet()) { + generateHttpAuthOptionFunction(w, + HttpAuthOptionFunctionCodeSection.builder() + .service(serviceShape) + .settings(settings) + .model(model) + .symbolProvider(symbolProvider) + .effectiveHttpAuthSchemes(effectiveHttpAuthSchemes) + .schemeId(entry.getKey()) + .httpAuthScheme(entry.getValue()) + .build()); } w.popState(); }); @@ -260,9 +266,9 @@ private void generateHttpAuthOptionFunctions() { /* import { HttpAuthOption } from "@smithy/types"; - + // ... - + function createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters: WeatherHttpAuthSchemeParameters): HttpAuthOption[] { return { @@ -276,8 +282,8 @@ function createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters: WeatherHttp }; */ private void generateHttpAuthOptionFunction( - TypeScriptWriter w, - HttpAuthOptionFunctionCodeSection s + TypeScriptWriter w, + HttpAuthOptionFunctionCodeSection s ) { w.pushState(s); ShapeId schemeId = s.getSchemeId(); @@ -285,93 +291,99 @@ private void generateHttpAuthOptionFunction( Optional authSchemeOptional = s.getHttpAuthScheme(); w.addTypeImport("HttpAuthOption", null, TypeScriptDependency.SMITHY_TYPES); w.openBlock(""" - function create$LHttpAuthOption(authParameters: $LHttpAuthSchemeParameters): \ - HttpAuthOption {""", "}\n", - normalizedAuthSchemeName, serviceName, - () -> { - w.openBlock("return {", "};", () -> { - w.write("schemeId: $S,", schemeId.toString()); - // If no HttpAuthScheme is registered, there are no HttpAuthOptionProperties available. - if (authSchemeOptional.isEmpty()) { - return; - } - HttpAuthScheme authScheme = authSchemeOptional.get(); - Trait trait = serviceShape.findTrait(authScheme.getTraitId()).orElse(null); - List identityProperties = - authScheme.getHttpAuthSchemeOptionParametersByType(Type.IDENTITY); - if (!identityProperties.isEmpty()) { - w.openBlock("identityProperties: {", "},", () -> { - for (HttpAuthOptionProperty parameter : identityProperties) { - w.write("$L: $C,", - parameter.name(), - parameter.source().apply(HttpAuthOptionProperty.Source.builder() - .httpAuthScheme(authScheme) - .trait(trait) - .build())); + function create$LHttpAuthOption(authParameters: $LHttpAuthSchemeParameters): \ + HttpAuthOption {""", + "}\n", + normalizedAuthSchemeName, + serviceName, + () -> { + w.openBlock("return {", "};", () -> { + w.write("schemeId: $S,", schemeId.toString()); + // If no HttpAuthScheme is registered, there are no HttpAuthOptionProperties available. + if (authSchemeOptional.isEmpty()) { + return; } - }); - } - List signingProperties = - authScheme.getHttpAuthSchemeOptionParametersByType(Type.SIGNING); - if (!signingProperties.isEmpty()) { - w.openBlock("signingProperties: {", "},", () -> { - for (HttpAuthOptionProperty parameter : signingProperties) { - w.write("$L: $C,", - parameter.name(), - parameter.source().apply(HttpAuthOptionProperty.Source.builder() - .httpAuthScheme(authScheme) - .trait(trait) - .build())); + HttpAuthScheme authScheme = authSchemeOptional.get(); + Trait trait = serviceShape.findTrait(authScheme.getTraitId()).orElse(null); + List identityProperties = + authScheme.getHttpAuthSchemeOptionParametersByType(Type.IDENTITY); + if (!identityProperties.isEmpty()) { + w.openBlock("identityProperties: {", "},", () -> { + for (HttpAuthOptionProperty parameter : identityProperties) { + w.write("$L: $C,", + parameter.name(), + parameter.source() + .apply(HttpAuthOptionProperty.Source.builder() + .httpAuthScheme(authScheme) + .trait(trait) + .build())); + } + }); + } + List signingProperties = + authScheme.getHttpAuthSchemeOptionParametersByType(Type.SIGNING); + if (!signingProperties.isEmpty()) { + w.openBlock("signingProperties: {", "},", () -> { + for (HttpAuthOptionProperty parameter : signingProperties) { + w.write("$L: $C,", + parameter.name(), + parameter.source() + .apply(HttpAuthOptionProperty.Source.builder() + .httpAuthScheme(authScheme) + .trait(trait) + .build())); + } + }); } + authScheme.getPropertiesExtractor() + .ifPresent(extractor -> w.write("propertiesExtractor: $C", + extractor.apply(serviceSymbol.toBuilder() + .name(ServiceBareBonesClientGenerator.getConfigTypeName(serviceSymbol)) + .build()))); }); - } - authScheme.getPropertiesExtractor() - .ifPresent(extractor -> w.write("propertiesExtractor: $C", - extractor.apply(serviceSymbol.toBuilder() - .name(ServiceBareBonesClientGenerator.getConfigTypeName(serviceSymbol)) - .build()))); - }); - }); + }); w.popState(); } public static String normalizeAuthSchemeName(ShapeId shapeId) { - return String.join("", Arrays - .asList(shapeId.toString().split("[.#]")) - .stream().map(StringUtils::capitalize) - .toList()); + return String.join("", + Arrays + .asList(shapeId.toString().split("[.#]")) + .stream() + .map(StringUtils::capitalize) + .toList()); } /* import { HttpAuthSchemeProvider } from "@smithy/types"; - + // ... - + export interface WeatherHttpAuthSchemeProvider extends HttpAuthSchemeProvider {} */ private void generateHttpAuthSchemeProviderInterface() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { w.pushState(HttpAuthSchemeProviderInterfaceCodeSection.builder() - .service(serviceShape) - .settings(settings) - .model(model) - .symbolProvider(symbolProvider) - .build()); + .service(serviceShape) + .settings(settings) + .model(model) + .symbolProvider(symbolProvider) + .build()); w.addTypeImport("HttpAuthSchemeProvider", null, TypeScriptDependency.SMITHY_TYPES); w.writeDocs("@internal"); String candidate = "export interface %sHttpAuthSchemeProvider extends".formatted(serviceName) - + " HttpAuthSchemeProvider<%sHttpAuthSchemeParameters> {}".formatted(serviceName); + + " HttpAuthSchemeProvider<%sHttpAuthSchemeParameters> {}".formatted(serviceName); if (candidate.length() <= TypeScriptWriter.LINE_WIDTH) { w.write(candidate); } else { w.write(""" - export interface $LHttpAuthSchemeProvider - extends HttpAuthSchemeProvider<$LHttpAuthSchemeParameters> {} - """, - serviceName, serviceName - ); + export interface $LHttpAuthSchemeProvider + extends HttpAuthSchemeProvider<$LHttpAuthSchemeParameters> {} + """, + serviceName, + serviceName); } w.popState(); }); @@ -392,48 +404,54 @@ export interface $LHttpAuthSchemeProvider private void generateDefaultHttpAuthSchemeProviderFunction() { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { w.pushState(DefaultHttpAuthSchemeProviderFunctionCodeSection.builder() - .service(serviceShape) - .settings(settings) - .model(model) - .symbolProvider(symbolProvider) - .build()); + .service(serviceShape) + .settings(settings) + .model(model) + .symbolProvider(symbolProvider) + .build()); w.openBlock(""" - /** - * @internal - */ - export const default$LHttpAuthSchemeProvider: $LHttpAuthSchemeProvider = \ - (authParameters) => {""", "};", - serviceName, serviceName, () -> { - w.write("const options: HttpAuthOption[] = [];"); - w.openBlock("switch (authParameters.operation) {", "}", () -> { - var serviceAuthSchemes = serviceIndex.getEffectiveAuthSchemes( - serviceShape, AuthSchemeMode.NO_AUTH_AWARE); - for (OperationShape operationShape : topDownIndex.getContainedOperations(serviceShape)) { - ShapeId operationShapeId = operationShape.getId(); - var operationAuthSchemes = serviceIndex.getEffectiveAuthSchemes( - serviceShape, operationShapeId, AuthSchemeMode.NO_AUTH_AWARE); - // Skip operation generation if operation auth schemes are equivalent to the default service - // auth schemes. - if (AuthUtils.areHttpAuthSchemesEqual(serviceAuthSchemes, operationAuthSchemes)) { - continue; - } - w.openBlock("case $S: {", "};", operationShapeId.getName(), () -> { - operationAuthSchemes.keySet().forEach(shapeId -> { - w.write("options.push(create$LHttpAuthOption(authParameters));", - normalizeAuthSchemeName(shapeId)); + /** + * @internal + */ + export const default$LHttpAuthSchemeProvider: $LHttpAuthSchemeProvider = \ + (authParameters) => {""", + "};", + serviceName, + serviceName, + () -> { + w.write("const options: HttpAuthOption[] = [];"); + w.openBlock("switch (authParameters.operation) {", "}", () -> { + var serviceAuthSchemes = serviceIndex.getEffectiveAuthSchemes( + serviceShape, + AuthSchemeMode.NO_AUTH_AWARE); + for (OperationShape operationShape : topDownIndex.getContainedOperations(serviceShape)) { + ShapeId operationShapeId = operationShape.getId(); + var operationAuthSchemes = serviceIndex.getEffectiveAuthSchemes( + serviceShape, + operationShapeId, + AuthSchemeMode.NO_AUTH_AWARE); + // Skip operation generation if operation auth schemes are equivalent to the default service + // auth schemes. + if (AuthUtils.areHttpAuthSchemesEqual(serviceAuthSchemes, operationAuthSchemes)) { + continue; + } + w.openBlock("case $S: {", "};", operationShapeId.getName(), () -> { + operationAuthSchemes.keySet().forEach(shapeId -> { + w.write("options.push(create$LHttpAuthOption(authParameters));", + normalizeAuthSchemeName(shapeId)); + }); + w.write("break;"); + }); + } + w.openBlock("default: {", "}", () -> { + serviceAuthSchemes.keySet().forEach(shapeId -> { + w.write("options.push(create$LHttpAuthOption(authParameters));", + normalizeAuthSchemeName(shapeId)); + }); }); - w.write("break;"); - }); - } - w.openBlock("default: {", "}", () -> { - serviceAuthSchemes.keySet().forEach(shapeId -> { - w.write("options.push(create$LHttpAuthOption(authParameters));", - normalizeAuthSchemeName(shapeId)); }); + w.write("return options;"); }); - }); - w.write("return options;"); - }); w.popState(); }); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java index 403ae5c6372..578d1f5c776 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/ResolveConfigFunction.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http; import java.util.ArrayList; @@ -18,12 +17,11 @@ */ @SmithyUnstableApi public record ResolveConfigFunction( - Symbol resolveConfigFunction, - Symbol inputConfig, - Symbol resolvedConfig, - List addArgs, - Optional previouslyResolved -) implements ToSmithyBuilder { + Symbol resolveConfigFunction, + Symbol inputConfig, + Symbol resolvedConfig, + List addArgs, + Optional previouslyResolved) implements ToSmithyBuilder { public static Builder builder() { return new Builder(); @@ -32,11 +30,11 @@ public static Builder builder() { @Override public Builder toBuilder() { return builder() - .resolveConfigFunction(resolveConfigFunction) - .inputConfig(inputConfig) - .resolvedConfig(resolvedConfig) - .addArgs(addArgs) - .previouslyResolved(previouslyResolved.orElse(null)); + .resolveConfigFunction(resolveConfigFunction) + .inputConfig(inputConfig) + .resolvedConfig(resolvedConfig) + .addArgs(addArgs) + .previouslyResolved(previouslyResolved.orElse(null)); } public static final class Builder implements SmithyBuilder { @@ -49,11 +47,11 @@ public static final class Builder implements SmithyBuilder integrations, - Model model, - TypeScriptSettings settings + List integrations, + Model model, + TypeScriptSettings settings ) { for (TypeScriptIntegration integration : integrations) { if (!(integration instanceof HttpAuthTypeScriptIntegration)) { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java index be8e48de5c0..90a791438ec 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.Iterator; @@ -52,86 +51,90 @@ public boolean matchesSettings(TypeScriptSettings settings) { @Override public List getClientPlugins() { Map httpAuthSchemeParametersProvider = Map.of( - "httpAuthSchemeParametersProvider", AddHttpAuthSchemePlugin::httpAuthSchemeParametersProvider, - "identityProviderConfigProvider", AddHttpAuthSchemePlugin::identityProviderConfigProvider - ); + "httpAuthSchemeParametersProvider", + AddHttpAuthSchemePlugin::httpAuthSchemeParametersProvider, + "identityProviderConfigProvider", + AddHttpAuthSchemePlugin::identityProviderConfigProvider); return List.of( - RuntimeClientPlugin.builder() - .withConventions( - TypeScriptDependency.SMITHY_CORE.dependency, - "HttpAuthSchemeEndpointRuleSet", - Convention.HAS_MIDDLEWARE) - .withAdditionalClientParams(httpAuthSchemeParametersProvider) - .build(), - RuntimeClientPlugin.builder() - .inputConfig(Symbol.builder() - .namespace(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_MODULE, "/") - .name("HttpAuthSchemeInputConfig") - .putProperty("typeOnly", true) - .build()) - .resolvedConfig(Symbol.builder() - .namespace(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_MODULE, "/") - .name("HttpAuthSchemeResolvedConfig") - .putProperty("typeOnly", true) - .build()) - .resolveFunction(Symbol.builder() - .namespace(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_MODULE, "/") - .name("resolveHttpAuthSchemeConfig") - .build()) - .build() - ); + RuntimeClientPlugin.builder() + .withConventions( + TypeScriptDependency.SMITHY_CORE.dependency, + "HttpAuthSchemeEndpointRuleSet", + Convention.HAS_MIDDLEWARE) + .withAdditionalClientParams(httpAuthSchemeParametersProvider) + .build(), + RuntimeClientPlugin.builder() + .inputConfig(Symbol.builder() + .namespace(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_MODULE, "/") + .name("HttpAuthSchemeInputConfig") + .putProperty("typeOnly", true) + .build()) + .resolvedConfig(Symbol.builder() + .namespace(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_MODULE, "/") + .name("HttpAuthSchemeResolvedConfig") + .putProperty("typeOnly", true) + .build()) + .resolveFunction(Symbol.builder() + .namespace(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_MODULE, "/") + .name("resolveHttpAuthSchemeConfig") + .build()) + .build()); } @Override public void customize(TypeScriptCodegenContext codegenContext) { if (!codegenContext.settings().generateClient() - || codegenContext.settings().useLegacyAuth() - || !codegenContext.applicationProtocol().isHttpProtocol()) { + || codegenContext.settings().useLegacyAuth() + || !codegenContext.applicationProtocol().isHttpProtocol()) { return; } codegenContext.writerDelegator().useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> { SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex( - codegenContext.integrations(), - codegenContext.model(), - codegenContext.settings()); + codegenContext.integrations(), + codegenContext.model(), + codegenContext.settings()); ServiceShape serviceShape = codegenContext.settings().getService(codegenContext.model()); ServiceIndex serviceIndex = ServiceIndex.of(codegenContext.model()); TopDownIndex topDownIndex = TopDownIndex.of(codegenContext.model()); Map httpAuthSchemes = - AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex); + AuthUtils + .getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex); Map configFields = - AuthUtils.collectConfigFields(httpAuthSchemes.values()); + AuthUtils.collectConfigFields(httpAuthSchemes.values()); Map resolveConfigFunctions = - AuthUtils.collectResolveConfigFunctions(httpAuthSchemes.values()); + AuthUtils.collectResolveConfigFunctions(httpAuthSchemes.values()); - generateHttpAuthSchemeInputConfigInterface(w, HttpAuthSchemeInputConfigInterfaceCodeSection.builder() - .service(serviceShape) - .settings(codegenContext.settings()) - .model(codegenContext.model()) - .symbolProvider(codegenContext.symbolProvider()) - .integrations(codegenContext.integrations()) - .configFields(configFields) - .resolveConfigFunctions(resolveConfigFunctions) - .build()); - generateHttpAuthSchemeResolvedConfigInterface(w, HttpAuthSchemeResolvedConfigInterfaceCodeSection.builder() - .service(serviceShape) - .settings(codegenContext.settings()) - .model(codegenContext.model()) - .symbolProvider(codegenContext.symbolProvider()) - .integrations(codegenContext.integrations()) - .configFields(configFields) - .resolveConfigFunctions(resolveConfigFunctions) - .build()); - generateResolveHttpAuthSchemeConfigFunction(w, ResolveHttpAuthSchemeConfigFunctionCodeSection.builder() - .service(serviceShape) - .settings(codegenContext.settings()) - .model(codegenContext.model()) - .symbolProvider(codegenContext.symbolProvider()) - .integrations(codegenContext.integrations()) - .configFields(configFields) - .resolveConfigFunctions(resolveConfigFunctions) - .build()); + generateHttpAuthSchemeInputConfigInterface(w, + HttpAuthSchemeInputConfigInterfaceCodeSection.builder() + .service(serviceShape) + .settings(codegenContext.settings()) + .model(codegenContext.model()) + .symbolProvider(codegenContext.symbolProvider()) + .integrations(codegenContext.integrations()) + .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) + .build()); + generateHttpAuthSchemeResolvedConfigInterface(w, + HttpAuthSchemeResolvedConfigInterfaceCodeSection.builder() + .service(serviceShape) + .settings(codegenContext.settings()) + .model(codegenContext.model()) + .symbolProvider(codegenContext.symbolProvider()) + .integrations(codegenContext.integrations()) + .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) + .build()); + generateResolveHttpAuthSchemeConfigFunction(w, + ResolveHttpAuthSchemeConfigFunctionCodeSection.builder() + .service(serviceShape) + .settings(codegenContext.settings()) + .model(codegenContext.model()) + .symbolProvider(codegenContext.symbolProvider()) + .integrations(codegenContext.integrations()) + .configFields(configFields) + .resolveConfigFunctions(resolveConfigFunctions) + .build()); }); } @@ -142,15 +145,16 @@ public void customize(TypeScriptCodegenContext codegenContext) { * defaultWeatherHttpAuthSchemeParametersProvider; * ``` */ - private static void httpAuthSchemeParametersProvider(TypeScriptWriter w, - ClientBodyExtraCodeSection clientBodySection) { + private static void httpAuthSchemeParametersProvider( + TypeScriptWriter w, + ClientBodyExtraCodeSection clientBodySection + ) { String httpAuthSchemeParametersProviderName = "default" - + CodegenUtils.getServiceName( - clientBodySection.getSettings(), - clientBodySection.getModel(), - clientBodySection.getSymbolProvider() - ) - + "HttpAuthSchemeParametersProvider"; + + CodegenUtils.getServiceName( + clientBodySection.getSettings(), + clientBodySection.getModel(), + clientBodySection.getSymbolProvider()) + + "HttpAuthSchemeParametersProvider"; w.addImport(httpAuthSchemeParametersProviderName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); w.writeInline(httpAuthSchemeParametersProviderName); } @@ -166,52 +170,53 @@ private static void httpAuthSchemeParametersProvider(TypeScriptWriter w, * }) * ``` */ - private static void identityProviderConfigProvider(TypeScriptWriter w, - ClientBodyExtraCodeSection s) { + private static void identityProviderConfigProvider( + TypeScriptWriter w, + ClientBodyExtraCodeSection s + ) { w.addDependency(TypeScriptDependency.SMITHY_CORE); w.addImport("DefaultIdentityProviderConfig", null, TypeScriptDependency.SMITHY_CORE); SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex( - s.getIntegrations(), - s.getModel(), - s.getSettings() - ); + s.getIntegrations(), + s.getModel(), + s.getSettings()); ServiceIndex serviceIndex = ServiceIndex.of(s.getModel()); TopDownIndex topDownIndex = TopDownIndex.of(s.getModel()); Map httpAuthSchemes = AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes( - s.getService(), serviceIndex, authIndex, topDownIndex - ); + s.getService(), + serviceIndex, + authIndex, + topDownIndex); w.openBlock(""" - async (config: $LResolvedConfig) =>""", - s.getSymbolProvider().toSymbol(s.getService()).getName() - ); + async (config: $LResolvedConfig) =>""", + s.getSymbolProvider().toSymbol(s.getService()).getName()); w.openCollapsibleBlock(""" - new DefaultIdentityProviderConfig({""", "})", - httpAuthSchemes.values().stream() - .filter(Objects::nonNull) - .anyMatch(scheme -> - scheme.getConfigFields().stream().anyMatch( - field -> field.type().equals(ConfigField.Type.MAIN) - ) - ), - () -> { - for (HttpAuthScheme scheme : httpAuthSchemes.values()) { - if (scheme == null) { - continue; - } - for (ConfigField configField : scheme.getConfigFields()) { - if (configField.type().equals(ConfigField.Type.MAIN)) { - w.writeInline( - "$S: config.$L,", - scheme.getSchemeId().toString(), - configField.name() - ); + new DefaultIdentityProviderConfig({""", + "})", + httpAuthSchemes.values() + .stream() + .filter(Objects::nonNull) + .anyMatch(scheme -> scheme.getConfigFields() + .stream() + .anyMatch( + field -> field.type().equals(ConfigField.Type.MAIN))), + () -> { + for (HttpAuthScheme scheme : httpAuthSchemes.values()) { + if (scheme == null) { + continue; + } + for (ConfigField configField : scheme.getConfigFields()) { + if (configField.type().equals(ConfigField.Type.MAIN)) { + w.writeInline( + "$S: config.$L,", + scheme.getSchemeId().toString(), + configField.name()); + } } } - } - } - ); + }); // no closeBlock needed here, the caller will write the comma // inline with the close of `new DefaultIdentityProviderConfig({})` w.dedent(); @@ -220,28 +225,30 @@ private static void identityProviderConfigProvider(TypeScriptWriter w, /* export interface HttpAuthSchemeInputConfig { httpAuthSchemes?: HttpAuthScheme[]; - + httpAuthSchemeProvider?: WeatherHttpAuthSchemeProvider; - + apiKey?: ApiKeyIdentity | ApiKeyIdentityProvider; - + token?: TokenIdentity | TokenIdentityProvider; - + region?: string | __Provider; - + credentials?: AwsCredentialIdentity | AwsCredentialIdentityProvider; } */ private void generateHttpAuthSchemeInputConfigInterface( - TypeScriptWriter w, - HttpAuthSchemeInputConfigInterfaceCodeSection s + TypeScriptWriter w, + HttpAuthSchemeInputConfigInterfaceCodeSection s ) { w.pushState(s); Map configFields = s.getConfigFields(); Map resolveConfigFunctions = - s.getResolveConfigFunctions(); + s.getResolveConfigFunctions(); String serviceName = CodegenUtils.getServiceName( - s.getSettings(), s.getModel(), s.getSymbolProvider()); + s.getSettings(), + s.getModel(), + s.getSymbolProvider()); w.writeDocs("@public"); w.writeInline("export interface HttpAuthSchemeInputConfig"); if (!resolveConfigFunctions.isEmpty()) { @@ -260,26 +267,26 @@ private void generateHttpAuthSchemeInputConfigInterface( w.addTypeImport("Provider", null, TypeScriptDependency.SMITHY_TYPES); w.writeDocs(""" - A comma-separated list of case-sensitive auth scheme names. - An auth scheme name is a fully qualified auth scheme ID with the namespace prefix trimmed. - For example, the auth scheme with ID aws.auth#sigv4 is named sigv4. - @public"""); + A comma-separated list of case-sensitive auth scheme names. + An auth scheme name is a fully qualified auth scheme ID with the namespace prefix trimmed. + For example, the auth scheme with ID aws.auth#sigv4 is named sigv4. + @public"""); w.write("authSchemePreference?: string[] | Provider;"); w.write(""); w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); w.writeDocs(""" - Configuration of HttpAuthSchemes for a client which provides \ - default identity providers and signers per auth scheme. - @internal"""); + Configuration of HttpAuthSchemes for a client which provides \ + default identity providers and signers per auth scheme. + @internal"""); w.write("httpAuthSchemes?: HttpAuthScheme[];"); w.write(""); String httpAuthSchemeProviderName = serviceName + "HttpAuthSchemeProvider"; w.writeDocs(""" - Configuration of an HttpAuthSchemeProvider for a client which \ - resolves which HttpAuthScheme to use. - @internal"""); + Configuration of an HttpAuthSchemeProvider for a client which \ + resolves which HttpAuthScheme to use. + @internal"""); w.write("httpAuthSchemeProvider?: $L;", httpAuthSchemeProviderName); for (ConfigField configField : configFields.values()) { @@ -297,28 +304,30 @@ private void generateHttpAuthSchemeInputConfigInterface( /* export interface HttpAuthSchemeResolvedConfig { readonly httpAuthSchemes: HttpAuthScheme[]; - + readonly httpAuthSchemeProvider: WeatherHttpAuthSchemeProvider; - + readonly apiKey?: ApiKeyIdentityProvider; - + readonly token?: TokenIdentityProvider; - + readonly region?: __Provider; - + readonly credentials?: AwsCredentialIdentityProvider; } */ private void generateHttpAuthSchemeResolvedConfigInterface( - TypeScriptWriter w, - HttpAuthSchemeResolvedConfigInterfaceCodeSection s + TypeScriptWriter w, + HttpAuthSchemeResolvedConfigInterfaceCodeSection s ) { w.pushState(s); Map configFields = s.getConfigFields(); Map resolveConfigFunctions = - s.getResolveConfigFunctions(); + s.getResolveConfigFunctions(); String serviceName = CodegenUtils.getServiceName( - s.getSettings(), s.getModel(), s.getSymbolProvider()); + s.getSettings(), + s.getModel(), + s.getSymbolProvider()); w.writeDocs("@internal"); w.writeInline("export interface HttpAuthSchemeResolvedConfig"); if (!resolveConfigFunctions.isEmpty()) { @@ -337,24 +346,24 @@ private void generateHttpAuthSchemeResolvedConfigInterface( w.addTypeImport("Provider", null, TypeScriptDependency.SMITHY_TYPES); w.writeDocs(""" - A comma-separated list of case-sensitive auth scheme names. - An auth scheme name is a fully qualified auth scheme ID with the namespace prefix trimmed. - For example, the auth scheme with ID aws.auth#sigv4 is named sigv4. - @public"""); + A comma-separated list of case-sensitive auth scheme names. + An auth scheme name is a fully qualified auth scheme ID with the namespace prefix trimmed. + For example, the auth scheme with ID aws.auth#sigv4 is named sigv4. + @public"""); w.write("readonly authSchemePreference: Provider;\n"); w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); w.writeDocs(""" - Configuration of HttpAuthSchemes for a client which provides \ - default identity providers and signers per auth scheme. - @internal"""); + Configuration of HttpAuthSchemes for a client which provides \ + default identity providers and signers per auth scheme. + @internal"""); w.write("readonly httpAuthSchemes: HttpAuthScheme[];\n"); String httpAuthSchemeProviderName = serviceName + "HttpAuthSchemeProvider"; w.writeDocs(""" - Configuration of an HttpAuthSchemeProvider for a client which \ - resolves which HttpAuthScheme to use. - @internal"""); + Configuration of an HttpAuthSchemeProvider for a client which \ + resolves which HttpAuthScheme to use. + @internal"""); w.write("readonly httpAuthSchemeProvider: $L;", httpAuthSchemeProviderName); for (ConfigField configField : configFields.values()) { if (configField.configFieldWriter().isPresent()) { @@ -383,19 +392,20 @@ private void generateHttpAuthSchemeResolvedConfigInterface( }; */ private void generateResolveHttpAuthSchemeConfigFunction( - TypeScriptWriter w, - ResolveHttpAuthSchemeConfigFunctionCodeSection s + TypeScriptWriter w, + ResolveHttpAuthSchemeConfigFunctionCodeSection s ) { w.pushState(s); Map configFields = s.getConfigFields(); Map resolveConfigFunctions = s.getResolveConfigFunctions(); - Map previousResolvedFunctions = resolveConfigFunctions.entrySet().stream() - .filter(e -> e.getValue().previouslyResolved().isPresent()) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + Map previousResolvedFunctions = resolveConfigFunctions.entrySet() + .stream() + .filter(e -> e.getValue().previouslyResolved().isPresent()) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); w.writeDocs("@internal"); w.writeInline(""" - export const resolveHttpAuthSchemeConfig = ( - config: T & HttpAuthSchemeInputConfig"""); + export const resolveHttpAuthSchemeConfig = ( + config: T & HttpAuthSchemeInputConfig"""); if (!previousResolvedFunctions.isEmpty()) { w.writeInline(" & "); Iterator iter = previousResolvedFunctions.values().iterator(); @@ -409,44 +419,43 @@ private void generateResolveHttpAuthSchemeConfigFunction( } w.write(""); w.write(""" - ): T & HttpAuthSchemeResolvedConfig => {"""); + ): T & HttpAuthSchemeResolvedConfig => {"""); w.indent(); w.pushState(ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.builder() - .service(s.getService()) - .settings(s.getSettings()) - .model(s.getModel()) - .symbolProvider(s.getSymbolProvider()) - .integrations(s.getIntegrations()) - .configFields(configFields) - .build()); + .service(s.getService()) + .settings(s.getSettings()) + .model(s.getModel()) + .symbolProvider(s.getSymbolProvider()) + .integrations(s.getIntegrations()) + .configFields(configFields) + .build()); w.addDependency(TypeScriptDependency.SMITHY_CORE); for (ConfigField configField : configFields.values()) { configField.configFieldWriter().ifPresent(cfw -> cfw.accept(w, configField)); } w.popState(); w.pushState(ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.builder() - .service(s.getService()) - .settings(s.getSettings()) - .model(s.getModel()) - .symbolProvider(s.getSymbolProvider()) - .integrations(s.getIntegrations()) - .configFields(configFields) - .build()); + .service(s.getService()) + .settings(s.getSettings()) + .model(s.getModel()) + .symbolProvider(s.getSymbolProvider()) + .integrations(s.getIntegrations()) + .configFields(configFields) + .build()); Integer i = 0; String configName = "config"; for (ResolveConfigFunction resolveConfigFunction : resolveConfigFunctions.values()) { w.openBlock( - "const config_$L = $T($L", - ");", - i, - resolveConfigFunction.resolveConfigFunction(), - configName, - () -> { - for (String addArg : resolveConfigFunction.addArgs()) { - w.writeInline(", $L", addArg); - } - } - ); + "const config_$L = $T($L", + ");", + i, + resolveConfigFunction.resolveConfigFunction(), + configName, + () -> { + for (String addArg : resolveConfigFunction.addArgs()) { + w.writeInline(", $L", addArg); + } + }); configName = "config_" + i; i++; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningPlugin.java index 9b8a43e258d..7b59bfc2e5a 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpSigningPlugin.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE; @@ -30,10 +29,10 @@ public boolean matchesSettings(TypeScriptSettings settings) { @Override public List getClientPlugins() { return List.of(RuntimeClientPlugin.builder() - .withConventions( - TypeScriptDependency.SMITHY_CORE.dependency, - "HttpSigning", - HAS_MIDDLEWARE) - .build()); + .withConventions( + TypeScriptDependency.SMITHY_CORE.dependency, + "HttpSigning", + HAS_MIDDLEWARE) + .build()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthExtensionConfigurationInterface.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthExtensionConfigurationInterface.java index c91190a0b43..c176a6a32e3 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthExtensionConfigurationInterface.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthExtensionConfigurationInterface.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import software.amazon.smithy.typescript.codegen.Dependency; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java index 8fba2462edb..0dfdda1e0a5 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthRuntimeExtensionIntegration.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.List; @@ -42,8 +41,8 @@ public boolean matchesSettings(TypeScriptSettings settings) { @Override public List getExtensionConfigurationInterfaces( - Model model, - TypeScriptSettings settings + Model model, + TypeScriptSettings settings ) { return List.of(new HttpAuthExtensionConfigurationInterface()); } @@ -55,26 +54,36 @@ public void customize(TypeScriptCodegenContext codegenContext) { } TypeScriptDelegator delegator = codegenContext.writerDelegator(); SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex( - codegenContext.integrations(), - codegenContext.model(), - codegenContext.settings()); + codegenContext.integrations(), + codegenContext.model(), + codegenContext.settings()); ServiceIndex serviceIndex = ServiceIndex.of(codegenContext.model()); TopDownIndex topDownIndex = TopDownIndex.of(codegenContext.model()); String serviceName = CodegenUtils.getServiceName( - codegenContext.settings(), codegenContext.model(), codegenContext.symbolProvider()); + codegenContext.settings(), + codegenContext.model(), + codegenContext.symbolProvider()); ServiceShape serviceShape = codegenContext.settings().getService(codegenContext.model()); Map effectiveAuthSchemes = - AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex); + AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex); Map configFields = AuthUtils.collectConfigFields(effectiveAuthSchemes.values()); generateHttpAuthExtensionConfigurationInterface( - delegator, configFields, serviceName); + delegator, + configFields, + serviceName); generateHttpAuthExtensionRuntimeConfigType( - delegator, configFields, serviceName); + delegator, + configFields, + serviceName); generateGetHttpAuthExtensionConfigurationFunction( - delegator, configFields, serviceName); + delegator, + configFields, + serviceName); generateResolveHttpAuthRuntimeConfigFunction( - delegator, configFields, serviceName); + delegator, + configFields, + serviceName); } /* @@ -87,68 +96,72 @@ public void customize(TypeScriptCodegenContext codegenContext) { TokenIdentity, TokenIdentityProvider } from "@smithy/types"; - + import { WeatherHttpAuthSchemeProvider } from "./httpAuthSchemeProvider"; - + // ... - + export interface HttpAuthExtensionConfiguration { setHttpAuthScheme(httpAuthScheme: HttpAuthScheme): void; httpAuthSchemes(): HttpAuthScheme[]; - + setHttpAuthSchemeProvider(httpAuthSchemeProvider: WeatherHttpAuthSchemeProvider): void; httpAuthSchemeProvider(): WeatherHttpAuthSchemeProvider; - + // @aws.auth#sigv4 setCredentials(credentials: AwsCredentialsIdentity | AwsCredentialIdentityProvider): void; credentials(): AwsCredentialsIdentity | AwsCredentialIdentityProvider | undefined; - + // @httpApiKeyAuth setApiKey(apiKey: ApiKeyIdentity | ApiKeyIdentityProvider): void; apiKey(): ApiKeyIdentity | ApiKeyIdentityProvider | undefined; - + // @httpBearerAuth setToken(token: TokenIdentity| TokenIdentityProvider): void; token(): TokenIdentity | TokenIdentityProvider | undefined; } */ private void generateHttpAuthExtensionConfigurationInterface( - TypeScriptDelegator delegator, - Map configFields, - String serviceName + TypeScriptDelegator delegator, + Map configFields, + String serviceName ) { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_EXTENSION_PATH, w -> { w.openBlock(""" - /** - * @internal - */ - export interface HttpAuthExtensionConfiguration {""", "}", - () -> { - w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); - w.write("setHttpAuthScheme(httpAuthScheme: HttpAuthScheme): void;"); - w.write("httpAuthSchemes(): HttpAuthScheme[];"); + /** + * @internal + */ + export interface HttpAuthExtensionConfiguration {""", + "}", + () -> { + w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); + w.write("setHttpAuthScheme(httpAuthScheme: HttpAuthScheme): void;"); + w.write("httpAuthSchemes(): HttpAuthScheme[];"); - w.addTypeImport(serviceName + "HttpAuthSchemeProvider", null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); - w.write("setHttpAuthSchemeProvider(httpAuthSchemeProvider: $LHttpAuthSchemeProvider): void;", - serviceName); - w.write("httpAuthSchemeProvider(): $LHttpAuthSchemeProvider;", serviceName); + w.addTypeImport(serviceName + "HttpAuthSchemeProvider", + null, + AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); + w.write("setHttpAuthSchemeProvider(httpAuthSchemeProvider: $LHttpAuthSchemeProvider): void;", + serviceName); + w.write("httpAuthSchemeProvider(): $LHttpAuthSchemeProvider;", serviceName); - List mainConfigFields = configFields.values().stream() - .filter(c -> c.type().equals(ConfigField.Type.MAIN)) - .toList(); - for (ConfigField configField : mainConfigFields) { - String capitalizedName = StringUtils.capitalize(configField.name()); - w.write("set$L($L: $T): void;", - capitalizedName, - configField.name(), - configField.inputType()); - w.write("$L(): $T | undefined;", - configField.name(), - configField.inputType()); - } - }); + List mainConfigFields = configFields.values() + .stream() + .filter(c -> c.type().equals(ConfigField.Type.MAIN)) + .toList(); + for (ConfigField configField : mainConfigFields) { + String capitalizedName = StringUtils.capitalize(configField.name()); + w.write("set$L($L: $T): void;", + capitalizedName, + configField.name(), + configField.inputType()); + w.write("$L(): $T | undefined;", + configField.name(), + configField.inputType()); + } + }); }); } @@ -162,13 +175,13 @@ export interface HttpAuthExtensionConfiguration {""", "}", TokenIdentity, TokenIdentityProvider } from "@smithy/types"; - + import { WeatherHttpAuthSchemeProvider } from "./httpAuthSchemeProvider"; - + // ... - + export type HttpAuthRuntimeConfig = Partial<{ httpAuthSchemes: HttpAuthScheme[]; httpAuthSchemeProvider: WeatherHttpAuthSchemeProvider; @@ -181,30 +194,34 @@ export interface HttpAuthExtensionConfiguration {""", "}", }>; */ private void generateHttpAuthExtensionRuntimeConfigType( - TypeScriptDelegator delegator, - Map configFields, - String serviceName + TypeScriptDelegator delegator, + Map configFields, + String serviceName ) { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_EXTENSION_PATH, w -> { w.openBlock(""" - /** - * @internal - */ - export type HttpAuthRuntimeConfig = Partial<{""", "}>;", - () -> { - w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); - w.write("httpAuthSchemes: HttpAuthScheme[];"); - w.addTypeImport(serviceName + "HttpAuthSchemeProvider", null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); - w.write("httpAuthSchemeProvider: $LHttpAuthSchemeProvider;", serviceName); - List mainConfigFields = configFields.values().stream() - .filter(c -> c.type().equals(ConfigField.Type.MAIN)) - .toList(); - for (ConfigField configField : mainConfigFields) { - w.write("$L: $T;", - configField.name(), - configField.inputType()); - } - }); + /** + * @internal + */ + export type HttpAuthRuntimeConfig = Partial<{""", + "}>;", + () -> { + w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); + w.write("httpAuthSchemes: HttpAuthScheme[];"); + w.addTypeImport(serviceName + "HttpAuthSchemeProvider", + null, + AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); + w.write("httpAuthSchemeProvider: $LHttpAuthSchemeProvider;", serviceName); + List mainConfigFields = configFields.values() + .stream() + .filter(c -> c.type().equals(ConfigField.Type.MAIN)) + .toList(); + for (ConfigField configField : mainConfigFields) { + w.write("$L: $T;", + configField.name(), + configField.inputType()); + } + }); }); } @@ -218,13 +235,13 @@ private void generateHttpAuthExtensionRuntimeConfigType( TokenIdentity, TokenIdentityProvider } from "@smithy/types"; - + import { WeatherHttpAuthSchemeProvider } from "./httpAuthSchemeProvider"; - + // ... - + export const getHttpAuthExtensionConfiguration = (runtimeConfig: HttpAuthRuntimeConfig): HttpAuthExtensionConfiguration => { let _httpAuthSchemes = runtimeConfig.httpAuthSchemes!; @@ -273,68 +290,79 @@ private void generateHttpAuthExtensionRuntimeConfigType( }; */ private void generateGetHttpAuthExtensionConfigurationFunction( - TypeScriptDelegator delegator, - Map configFields, - String serviceName + TypeScriptDelegator delegator, + Map configFields, + String serviceName ) { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_EXTENSION_PATH, w -> { w.openBlock(""" - /** - * @internal - */ - export const getHttpAuthExtensionConfiguration = ( - runtimeConfig: HttpAuthRuntimeConfig - ): HttpAuthExtensionConfiguration => {""", "};", - () -> { - w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); - w.write("const _httpAuthSchemes = runtimeConfig.httpAuthSchemes!;"); - w.addTypeImport(serviceName + "HttpAuthSchemeProvider", null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); - w.write("let _httpAuthSchemeProvider = runtimeConfig.httpAuthSchemeProvider!;"); - List mainConfigFields = configFields.values().stream() - .filter(c -> c.type().equals(ConfigField.Type.MAIN)) - .toList(); - for (ConfigField configField : mainConfigFields) { - w.write("let _$L = runtimeConfig.$L;", configField.name(), configField.name()); - } + /** + * @internal + */ + export const getHttpAuthExtensionConfiguration = ( + runtimeConfig: HttpAuthRuntimeConfig + ): HttpAuthExtensionConfiguration => {""", + "};", + () -> { + w.addTypeImport("HttpAuthScheme", null, TypeScriptDependency.SMITHY_TYPES); + w.write("const _httpAuthSchemes = runtimeConfig.httpAuthSchemes!;"); + w.addTypeImport(serviceName + "HttpAuthSchemeProvider", + null, + AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY); + w.write("let _httpAuthSchemeProvider = runtimeConfig.httpAuthSchemeProvider!;"); + List mainConfigFields = configFields.values() + .stream() + .filter(c -> c.type().equals(ConfigField.Type.MAIN)) + .toList(); + for (ConfigField configField : mainConfigFields) { + w.write("let _$L = runtimeConfig.$L;", configField.name(), configField.name()); + } - w.openBlock("return {", "};", - () -> { - w.write(""" - setHttpAuthScheme(httpAuthScheme: HttpAuthScheme): void { - const index = _httpAuthSchemes.findIndex((scheme) => \ - scheme.schemeId === httpAuthScheme.schemeId); - if (index === -1) { - _httpAuthSchemes.push(httpAuthScheme); - } else { - _httpAuthSchemes.splice(index, 1, httpAuthScheme); - } - }, - httpAuthSchemes(): HttpAuthScheme[] { - return _httpAuthSchemes; - },"""); - w.write(""" - setHttpAuthSchemeProvider(httpAuthSchemeProvider: $LHttpAuthSchemeProvider): void { - _httpAuthSchemeProvider = httpAuthSchemeProvider; - }, - httpAuthSchemeProvider(): $LHttpAuthSchemeProvider { - return _httpAuthSchemeProvider; - },""", serviceName, serviceName); - for (ConfigField configField : mainConfigFields) { - String capitalizedName = StringUtils.capitalize(configField.name()); - w.write(""" - set$L($L: $T): void { - _$L = $L; - }, - $L(): $T | undefined { - return _$L; - },""", - capitalizedName, configField.name(), configField.inputType(), - configField.name(), configField.name(), - configField.name(), configField.inputType(), - configField.name()); - } - }); - }); + w.openBlock("return {", + "};", + () -> { + w.write(""" + setHttpAuthScheme(httpAuthScheme: HttpAuthScheme): void { + const index = _httpAuthSchemes.findIndex((scheme) => \ + scheme.schemeId === httpAuthScheme.schemeId); + if (index === -1) { + _httpAuthSchemes.push(httpAuthScheme); + } else { + _httpAuthSchemes.splice(index, 1, httpAuthScheme); + } + }, + httpAuthSchemes(): HttpAuthScheme[] { + return _httpAuthSchemes; + },"""); + w.write(""" + setHttpAuthSchemeProvider(httpAuthSchemeProvider: $LHttpAuthSchemeProvider): void { + _httpAuthSchemeProvider = httpAuthSchemeProvider; + }, + httpAuthSchemeProvider(): $LHttpAuthSchemeProvider { + return _httpAuthSchemeProvider; + },""", + serviceName, + serviceName); + for (ConfigField configField : mainConfigFields) { + String capitalizedName = StringUtils.capitalize(configField.name()); + w.write(""" + set$L($L: $T): void { + _$L = $L; + }, + $L(): $T | undefined { + return _$L; + },""", + capitalizedName, + configField.name(), + configField.inputType(), + configField.name(), + configField.name(), + configField.name(), + configField.inputType(), + configField.name()); + } + }); + }); }); } @@ -352,29 +380,29 @@ private void generateGetHttpAuthExtensionConfigurationFunction( }; */ private void generateResolveHttpAuthRuntimeConfigFunction( - TypeScriptDelegator delegator, - Map configFields, - String serviceName + TypeScriptDelegator delegator, + Map configFields, + String serviceName ) { delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_EXTENSION_PATH, w -> { w.openBlock(""" - /** - * @internal - */ - export const resolveHttpAuthRuntimeConfig = (config: HttpAuthExtensionConfiguration): \ - HttpAuthRuntimeConfig => {""", "};", - () -> { - w.openBlock("return {", "};", () -> { - w.write("httpAuthSchemes: config.httpAuthSchemes(),"); - w.write("httpAuthSchemeProvider: config.httpAuthSchemeProvider(),"); - for (ConfigField configField : configFields.values()) { - if (configField.type().equals(ConfigField.Type.MAIN)) { - w.write("$L: config.$L(),", configField.name(), configField.name()); + /** + * @internal + */ + export const resolveHttpAuthRuntimeConfig = (config: HttpAuthExtensionConfiguration): \ + HttpAuthRuntimeConfig => {""", + "};", + () -> { + w.openBlock("return {", "};", () -> { + w.write("httpAuthSchemes: config.httpAuthSchemes(),"); + w.write("httpAuthSchemeProvider: config.httpAuthSchemeProvider(),"); + for (ConfigField configField : configFields.values()) { + if (configField.type().equals(ConfigField.Type.MAIN)) { + w.write("$L: config.$L(),", configField.name(), configField.name()); + } } - } + }); }); - } - ); }); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java index bbd936eef52..6b2e486c03f 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/HttpAuthTypeScriptIntegration.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.Optional; @@ -35,9 +34,9 @@ default Optional getHttpAuthScheme() { * @param settings settings */ default void customizeSupportedHttpAuthSchemes( - SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex, - Model model, - TypeScriptSettings settings + SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex, + Model model, + TypeScriptSettings settings ) { // pass } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java index 8ce8d9db999..3b6520ceddf 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpApiKeyAuth.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.Optional; @@ -26,27 +25,27 @@ */ @SmithyInternalApi public class SupportHttpApiKeyAuth implements HttpAuthTypeScriptIntegration { - private static final Consumer HTTP_API_KEY_AUTH_SIGNER = w -> - w.write("new $T()", Symbol.builder() - .name("HttpApiKeyAuthSigner") - .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_CORE) - .build()); + private static final Consumer HTTP_API_KEY_AUTH_SIGNER = w -> w.write("new $T()", + Symbol.builder() + .name("HttpApiKeyAuthSigner") + .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_CORE) + .build()); private static final Symbol API_KEY_IDENTITY = Symbol.builder() - .name("ApiKeyIdentity") - .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_TYPES) - .build(); + .name("ApiKeyIdentity") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); private static final Symbol API_KEY_IDENTITY_PROVIDER = Symbol.builder() - .name("ApiKeyIdentityProvider") - .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_TYPES) - .build(); + .name("ApiKeyIdentityProvider") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); private static final Symbol HTTP_API_KEY_LOCATION = Symbol.builder() - .name("HttpApiKeyAuthLocation") - .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_TYPES) - .build(); + .name("HttpApiKeyAuthLocation") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); /** * Integration should be skipped if the `useLegacyAuth` flag is true. @@ -62,55 +61,56 @@ public Optional getHttpAuthScheme() { .schemeId(HttpApiKeyAuthTrait.ID) .applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol()) .addConfigField(ConfigField.builder() - .name("apiKey") - .type(ConfigField.Type.MAIN) - .docs(w -> w.write("The API key to use when making requests.")) - .inputType(Symbol.builder() - .name("ApiKeyIdentity | ApiKeyIdentityProvider") - .addReference(API_KEY_IDENTITY) - .addReference(API_KEY_IDENTITY_PROVIDER) - .build()) - .resolvedType(Symbol.builder() - .name("ApiKeyIdentityProvider") - .addReference(API_KEY_IDENTITY_PROVIDER) + .name("apiKey") + .type(ConfigField.Type.MAIN) + .docs(w -> w.write("The API key to use when making requests.")) + .inputType(Symbol.builder() + .name("ApiKeyIdentity | ApiKeyIdentityProvider") + .addReference(API_KEY_IDENTITY) + .addReference(API_KEY_IDENTITY_PROVIDER) + .build()) + .resolvedType(Symbol.builder() + .name("ApiKeyIdentityProvider") + .addReference(API_KEY_IDENTITY_PROVIDER) + .build()) + .configFieldWriter(ConfigField::defaultMainConfigFieldWriter) .build()) - .configFieldWriter(ConfigField::defaultMainConfigFieldWriter) - .build()) .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() - .name("name") - .type(HttpAuthOptionProperty.Type.SIGNING) - .source(s -> w -> w.write("$S", ((HttpApiKeyAuthTrait) s.trait()).getName())) - .build()) + .name("name") + .type(HttpAuthOptionProperty.Type.SIGNING) + .source(s -> w -> w.write("$S", ((HttpApiKeyAuthTrait) s.trait()).getName())) + .build()) .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() - .name("in") - .type(HttpAuthOptionProperty.Type.SIGNING) - .source(s -> w -> { - Location in = ((HttpApiKeyAuthTrait) s.trait()).getIn(); - switch (in) { - case HEADER: { - w.write("$T.HEADER", HTTP_API_KEY_LOCATION); - break; - } - case QUERY: { - w.write("$T.QUERY", HTTP_API_KEY_LOCATION); - break; + .name("in") + .type(HttpAuthOptionProperty.Type.SIGNING) + .source(s -> w -> { + Location in = ((HttpApiKeyAuthTrait) s.trait()).getIn(); + switch (in) { + case HEADER: { + w.write("$T.HEADER", HTTP_API_KEY_LOCATION); + break; + } + case QUERY: { + w.write("$T.QUERY", HTTP_API_KEY_LOCATION); + break; + } + default: { + throw new CodegenException( + "Encountered unsupported `in` property on " + + "`@httpApiKeyAuth`: " + + in); + } } - default: { - throw new CodegenException( - "Encountered unsupported `in` property on " - + "`@httpApiKeyAuth`: " - + in); - } - } - }) - .build()) + }) + .build()) .addHttpAuthOptionProperty(HttpAuthOptionProperty.builder() - .name("scheme") - .type(HttpAuthOptionProperty.Type.SIGNING) - .source(s -> w -> ((HttpApiKeyAuthTrait) s.trait()).getScheme().ifPresentOrElse( - scheme -> w.write("$S", scheme), - () -> w.write("undefined"))) - .build()) + .name("scheme") + .type(HttpAuthOptionProperty.Type.SIGNING) + .source(s -> w -> ((HttpApiKeyAuthTrait) s.trait()).getScheme() + .ifPresentOrElse( + scheme -> w.write("$S", scheme), + () -> w.write("undefined"))) + .build()) .putDefaultSigner(LanguageTarget.SHARED, HTTP_API_KEY_AUTH_SIGNER) .build()); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpBearerAuth.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpBearerAuth.java index 0879f3e7c5d..3a2be1e6ce6 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpBearerAuth.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportHttpBearerAuth.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.Optional; @@ -24,22 +23,22 @@ */ @SmithyInternalApi public final class SupportHttpBearerAuth implements HttpAuthTypeScriptIntegration { - private static final Consumer HTTP_BEARER_AUTH_SIGNER = w -> - w.write("new $T()", Symbol.builder() - .name("HttpBearerAuthSigner") - .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_CORE) - .build()); + private static final Consumer HTTP_BEARER_AUTH_SIGNER = w -> w.write("new $T()", + Symbol.builder() + .name("HttpBearerAuthSigner") + .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_CORE) + .build()); private static final Symbol TOKEN_IDENTITY = Symbol.builder() - .name("TokenIdentity") - .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_TYPES) - .build(); + .name("TokenIdentity") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); private static final Symbol TOKEN_IDENTITY_PROVIDER = Symbol.builder() - .name("TokenIdentityProvider") - .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_TYPES) - .build(); + .name("TokenIdentityProvider") + .namespace(TypeScriptDependency.SMITHY_TYPES.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_TYPES) + .build(); /** * Integration should be skipped if the `useLegacyAuth` flag is true. @@ -55,20 +54,20 @@ public Optional getHttpAuthScheme() { .schemeId(HttpBearerAuthTrait.ID) .applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol()) .addConfigField(ConfigField.builder() - .name("token") - .type(Type.MAIN) - .docs(w -> w.write("The token used to authenticate requests.")) - .inputType(Symbol.builder() - .name("TokenIdentity | TokenIdentityProvider") - .addReference(TOKEN_IDENTITY) - .addReference(TOKEN_IDENTITY_PROVIDER) - .build()) - .resolvedType(Symbol.builder() - .name("TokenIdentityProvider") - .addReference(TOKEN_IDENTITY_PROVIDER) + .name("token") + .type(Type.MAIN) + .docs(w -> w.write("The token used to authenticate requests.")) + .inputType(Symbol.builder() + .name("TokenIdentity | TokenIdentityProvider") + .addReference(TOKEN_IDENTITY) + .addReference(TOKEN_IDENTITY_PROVIDER) + .build()) + .resolvedType(Symbol.builder() + .name("TokenIdentityProvider") + .addReference(TOKEN_IDENTITY_PROVIDER) + .build()) + .configFieldWriter(ConfigField::defaultMainConfigFieldWriter) .build()) - .configFieldWriter(ConfigField::defaultMainConfigFieldWriter) - .build()) .putDefaultSigner(LanguageTarget.SHARED, HTTP_BEARER_AUTH_SIGNER) .build()); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportNoAuth.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportNoAuth.java index 0c86191c808..21e331fb5f0 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportNoAuth.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/SupportNoAuth.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.integration; import java.util.Optional; @@ -23,13 +22,13 @@ @SmithyInternalApi public final class SupportNoAuth implements HttpAuthTypeScriptIntegration { private static final Consumer NO_AUTH_IDENTITY_PROVIDER_WRITER = - w -> w.write("async () => ({})"); - private static final Consumer NO_AUTH_SIGNER_WRITER = w -> - w.write("new $T()", Symbol.builder() - .name("NoAuthSigner") - .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") - .addDependency(TypeScriptDependency.SMITHY_CORE) - .build()); + w -> w.write("async () => ({})"); + private static final Consumer NO_AUTH_SIGNER_WRITER = w -> w.write("new $T()", + Symbol.builder() + .name("NoAuthSigner") + .namespace(TypeScriptDependency.SMITHY_CORE.getPackageName(), "/") + .addDependency(TypeScriptDependency.SMITHY_CORE) + .build()); /** * Integration should be skipped if the `useLegacyAuth` flag is true. @@ -42,10 +41,10 @@ public boolean matchesSettings(TypeScriptSettings settings) { @Override public Optional getHttpAuthScheme() { return Optional.of(HttpAuthScheme.builder() - .schemeId(NoAuthTrait.ID) - .applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol()) - .putDefaultIdentityProvider(LanguageTarget.SHARED, NO_AUTH_IDENTITY_PROVIDER_WRITER) - .putDefaultSigner(LanguageTarget.SHARED, NO_AUTH_SIGNER_WRITER) - .build()); + .schemeId(NoAuthTrait.ID) + .applicationProtocol(ApplicationProtocol.createDefaultHttpApplicationProtocol()) + .putDefaultIdentityProvider(LanguageTarget.SHARED, NO_AUTH_IDENTITY_PROVIDER_WRITER) + .putDefaultSigner(LanguageTarget.SHARED, NO_AUTH_SIGNER_WRITER) + .build()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeParametersProviderFunctionCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeParametersProviderFunctionCodeSection.java index a650f49d88e..0f007f23b16 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeParametersProviderFunctionCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeParametersProviderFunctionCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.Map; @@ -29,7 +28,7 @@ private DefaultHttpAuthSchemeParametersProviderFunctionCodeSection(Builder build model = SmithyBuilder.requiredState("model", builder.model); symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); httpAuthSchemeParameters = - SmithyBuilder.requiredState("httpAuthSchemeParameters", builder.httpAuthSchemeParameters); + SmithyBuilder.requiredState("httpAuthSchemeParameters", builder.httpAuthSchemeParameters); } public ServiceShape getService() { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeProviderFunctionCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeProviderFunctionCodeSection.java index 8661844d542..3091f0480e0 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeProviderFunctionCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/DefaultHttpAuthSchemeProviderFunctionCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import software.amazon.smithy.codegen.core.SymbolProvider; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java index d83c808f293..2ce13f3fc3e 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.Map; @@ -33,7 +32,7 @@ private HttpAuthOptionFunctionCodeSection(Builder builder) { model = SmithyBuilder.requiredState("model", builder.model); symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); effectiveHttpAuthSchemes = - SmithyBuilder.requiredState("effectiveHttpAuthSchemes", builder.effectiveHttpAuthSchemes); + SmithyBuilder.requiredState("effectiveHttpAuthSchemes", builder.effectiveHttpAuthSchemes); schemeId = SmithyBuilder.requiredState("schemeId", builder.schemeId); httpAuthScheme = builder.httpAuthScheme; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java index a4de6c5649f..11c551e0007 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthOptionFunctionsCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.Map; @@ -30,7 +29,7 @@ private HttpAuthOptionFunctionsCodeSection(Builder builder) { model = SmithyBuilder.requiredState("model", builder.model); symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); effectiveHttpAuthSchemes = - SmithyBuilder.requiredState("effectiveHttpAuthSchemes", builder.effectiveHttpAuthSchemes); + SmithyBuilder.requiredState("effectiveHttpAuthSchemes", builder.effectiveHttpAuthSchemes); } public ServiceShape getService() { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java index fe5eed05ec6..7c6e53c5852 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeInputConfigInterfaceCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.List; @@ -77,8 +76,8 @@ public static class Builder implements SmithyBuilder integrations; - private Map configFields; - private Map resolveConfigFunctions; + private Map configFields; + private Map resolveConfigFunctions; @Override public HttpAuthSchemeInputConfigInterfaceCodeSection build() { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersInterfaceCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersInterfaceCodeSection.java index 6ebe33a3659..79f6c83e91f 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersInterfaceCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersInterfaceCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.Map; @@ -29,7 +28,7 @@ private HttpAuthSchemeParametersInterfaceCodeSection(Builder builder) { model = SmithyBuilder.requiredState("model", builder.model); symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); httpAuthSchemeParameters = - SmithyBuilder.requiredState("httpAuthSchemeParameters", builder.httpAuthSchemeParameters); + SmithyBuilder.requiredState("httpAuthSchemeParameters", builder.httpAuthSchemeParameters); } public ServiceShape getService() { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersProviderInterfaceCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersProviderInterfaceCodeSection.java index 34b6ea0bdcf..ec80322bf28 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersProviderInterfaceCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeParametersProviderInterfaceCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import software.amazon.smithy.codegen.core.SymbolProvider; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeProviderInterfaceCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeProviderInterfaceCodeSection.java index 8fa2cbdf0ff..94898ca3800 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeProviderInterfaceCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeProviderInterfaceCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import software.amazon.smithy.codegen.core.SymbolProvider; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java index 85c95bce832..1cff10f710d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/HttpAuthSchemeResolvedConfigInterfaceCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.List; @@ -77,7 +76,7 @@ public static class Builder implements SmithyBuilder integrations; - private Map configFields; + private Map configFields; private Map resolveConfigFunctions; @Override diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java index cc4eed758d9..c31edf628de 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.List; @@ -77,7 +76,7 @@ public static class Builder implements SmithyBuilder integrations; - private Map configFields; + private Map configFields; private Map resolveConfigFunctions; @Override diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java index f4fa66616e9..d460602a6fb 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.List; @@ -69,7 +68,7 @@ public static class Builder implements SmithyBuilder integrations; - private Map configFields; + private Map configFields; @Override public ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection build() { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java index 2fb3dc52652..20577fc1d3e 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/sections/ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.auth.http.sections; import java.util.List; @@ -69,7 +68,7 @@ public static class Builder implements SmithyBuilder integrations; - private Map configFields; + private Map configFields; @Override public ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection build() { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGenerator.java index 24e1bd4a542..675444380cc 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGenerator.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.documentation; import java.util.Comparator; @@ -48,21 +47,23 @@ private static String write(Node node, int indent) { return indentation + "{ /* empty */ }"; } String membersJoined = objectNode.getMembers() - .entrySet() - .stream() - .sorted(Comparator.comparing(entry -> entry.getKey().getValue())) - .map(entry -> indentation - + " " - + entry.getKey().getValue() - + ": " - + write(entry.getValue(), indent + 2)) - .collect(Collectors.joining(",\n")); + .entrySet() + .stream() + .sorted(Comparator.comparing(entry -> entry.getKey().getValue())) + .map(entry -> indentation + + " " + + entry.getKey().getValue() + + ": " + + write(entry.getValue(), indent + 2)) + .collect(Collectors.joining(",\n")); return buffer - .append("{\n") - .append(membersJoined).append("\n") - .append(indentation).append("}") - .toString(); + .append("{\n") + .append(membersJoined) + .append("\n") + .append(indentation) + .append("}") + .toString(); } case ARRAY -> { ArrayNode arrayNode = node.expectArrayNode(); @@ -70,17 +71,19 @@ private static String write(Node node, int indent) { return indentation + "[]"; } String membersJoined = arrayNode.getElements() - .stream() - .map(elementNode -> indentation - + " " - + write(elementNode, indent + 2)) - .collect(Collectors.joining(",\n")); + .stream() + .map(elementNode -> indentation + + " " + + write(elementNode, indent + 2)) + .collect(Collectors.joining(",\n")); return buffer - .append("[\n") - .append(membersJoined).append("\n") - .append(indentation).append("]") - .toString(); + .append("[\n") + .append(membersJoined) + .append("\n") + .append(indentation) + .append("]") + .toString(); } case STRING -> { StringNode stringNode = node.expectStringNode(); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGenerator.java index 671b50b55fc..75a8b4aeb28 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGenerator.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.documentation; import java.util.Arrays; @@ -45,10 +44,10 @@ public abstract class StructureExampleGenerator { * ``` */ public static String generateStructuralHintDocumentation( - Shape shape, - Model model, - boolean isComment, - boolean isInput + Shape shape, + Model model, + boolean isComment, + boolean isInput ) { StringBuilder buffer = new StringBuilder(); shape(shape, buffer, model, 0, new ShapeTracker(), isInput); @@ -56,30 +55,33 @@ public static String generateStructuralHintDocumentation( // replace non-leading whitespace with single space. String s = Arrays.stream( buffer.toString() - .split("\n")) - .map(line -> line.replaceAll( - "([\\w\\\",:\\[\\{] )\\s+", - "$1") - .replaceAll("\\s+$", "")) - .collect(Collectors.joining((isComment) ? "\n// " : "\n")); + .split("\n")) + .map(line -> line.replaceAll( + "([\\w\\\",:\\[\\{] )\\s+", + "$1") + .replaceAll("\\s+$", "")) + .collect(Collectors.joining((isComment) ? "\n// " : "\n")); return ((isComment) ? "// " : "") + s.replaceAll(",$", ";"); } - private static void structure(StructureShape structureShape, - StringBuilder buffer, - Model model, - int indentation, - ShapeTracker shapeTracker, - boolean isInput) { + private static void structure( + StructureShape structureShape, + StringBuilder buffer, + Model model, + int indentation, + ShapeTracker shapeTracker, + boolean isInput + ) { if (structureShape.getAllMembers().isEmpty()) { append(indentation, buffer, "{},"); checkRequired(indentation, buffer, structureShape); } else { - append(indentation, buffer, - "{" + (shapeTracker.getOccurrenceCount(structureShape) == 1 - ? " // " + structureShape.getId().getName() - : "")); + append(indentation, + buffer, + "{" + (shapeTracker.getOccurrenceCount(structureShape) == 1 + ? " // " + structureShape.getId().getName() + : "")); checkRequired(indentation, buffer, structureShape); structureShape.getAllMembers().values().forEach(member -> { append(indentation + 2, buffer, member.getMemberName() + ": "); @@ -89,15 +91,19 @@ private static void structure(StructureShape structureShape, } } - private static void union(UnionShape unionShape, - StringBuilder buffer, - Model model, - int indentation, - ShapeTracker shapeTracker, - boolean isInput) { - append(indentation, buffer, "{" + (shapeTracker.getOccurrenceCount(unionShape) == 1 - ? " // " + unionShape.getId().getName() - : "// ") + " Union: only one key present"); + private static void union( + UnionShape unionShape, + StringBuilder buffer, + Model model, + int indentation, + ShapeTracker shapeTracker, + boolean isInput + ) { + append(indentation, + buffer, + "{" + (shapeTracker.getOccurrenceCount(unionShape) == 1 + ? " // " + unionShape.getId().getName() + : "// ") + " Union: only one key present"); checkRequired(indentation, buffer, unionShape); unionShape.getAllMembers().values().forEach(member -> { append(indentation + 2, buffer, member.getMemberName() + ": "); @@ -106,12 +112,14 @@ private static void union(UnionShape unionShape, append(indentation, buffer, "},\n"); } - private static void shape(Shape shape, - StringBuilder buffer, - Model model, - int indentation, - ShapeTracker shapeTracker, - boolean isInput) { + private static void shape( + Shape shape, + StringBuilder buffer, + Model model, + int indentation, + ShapeTracker shapeTracker, + boolean isInput + ) { Shape target; if (shape instanceof MemberShape) { target = model.getShape(((MemberShape) shape).getTarget()).get(); @@ -134,21 +142,24 @@ private static void shape(Shape shape, case BLOB: if (isInput) { if (target.hasTrait(StreamingTrait.class)) { - append(indentation, buffer, - "\"MULTIPLE_TYPES_ACCEPTED\", // see \\@smithy/types -> StreamingBlobPayloadInputTypes" - ); + append(indentation, + buffer, + "\"MULTIPLE_TYPES_ACCEPTED\", // see \\@smithy/types -> StreamingBlobPayloadInputTypes"); } else { - append(indentation, buffer, - """ - new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")"""); + append(indentation, + buffer, + """ + new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")"""); } } else { if (target.hasTrait(StreamingTrait.class)) { - append(indentation, buffer, - "\"\", // see \\@smithy/types -> StreamingBlobPayloadOutputTypes"); + append(indentation, + buffer, + "\"\", // see \\@smithy/types -> StreamingBlobPayloadOutputTypes"); } else { - append(indentation, buffer, - "new Uint8Array(),"); + append(indentation, + buffer, + "new Uint8Array(),"); } } break; @@ -185,23 +196,31 @@ private static void shape(Shape shape, case SET: case LIST: - append(indentation, buffer, "[" + (shapeTracker.getOccurrenceCount(target) == 1 - ? " // " + target.getId().getName() - : "")); + append(indentation, + buffer, + "[" + (shapeTracker.getOccurrenceCount(target) == 1 + ? " // " + target.getId().getName() + : "")); checkRequired(indentation, buffer, shape); ListShape list = (ListShape) target; shape(list.getMember(), buffer, model, indentation + 2, shapeTracker, isInput); append(indentation, buffer, "],\n"); break; case MAP: - append(indentation, buffer, "{" + (shapeTracker.getOccurrenceCount(target) == 1 - ? " // " + target.getId().getName() - : "")); + append(indentation, + buffer, + "{" + (shapeTracker.getOccurrenceCount(target) == 1 + ? " // " + target.getId().getName() + : "")); checkRequired(indentation, buffer, shape); append(indentation + 2, buffer, "\"\": "); MapShape map = (MapShape) target; - shape(model.getShape(map.getValue().getTarget()).get(), buffer, model, indentation + 2, - shapeTracker, isInput); + shape(model.getShape(map.getValue().getTarget()).get(), + buffer, + model, + indentation + 2, + shapeTracker, + isInput); append(indentation, buffer, "},\n"); break; @@ -217,19 +236,19 @@ private static void shape(Shape shape, case ENUM: EnumShape enumShape = (EnumShape) target; String enumeration = enumShape.getEnumValues() - .values() - .stream() - .map(s -> "\"" + s + "\"") - .collect(Collectors.joining(" || ")); + .values() + .stream() + .map(s -> "\"" + s + "\"") + .collect(Collectors.joining(" || ")); append(indentation, buffer, enumeration + ","); break; case INT_ENUM: IntEnumShape intEnumShape = (IntEnumShape) target; String intEnumeration = intEnumShape.getEnumValues() - .values() - .stream() - .map(i -> Integer.toString(i)) - .collect(Collectors.joining(" || ")); + .values() + .stream() + .map(i -> Integer.toString(i)) + .collect(Collectors.joining(" || ")); append(indentation, buffer, intEnumeration + ","); break; case OPERATION: @@ -313,9 +332,11 @@ public void mark(Shape shape, int depth) { * @return whether the shape should be truncated. */ public boolean shouldTruncate(Shape shape) { - return (shape instanceof MapShape || shape instanceof UnionShape || shape instanceof StructureShape - || shape instanceof ListShape || shape instanceof SetShape) - && (getOccurrenceCount(shape) > 5 || getOccurrenceDepths(shape) > 2); + return (shape instanceof MapShape || shape instanceof UnionShape + || shape instanceof StructureShape + || shape instanceof ListShape + || shape instanceof SetShape) + && (getOccurrenceCount(shape) > 5 || getOccurrenceDepths(shape) > 2); } /** diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/AddDefaultEndpointRuleSet.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/AddDefaultEndpointRuleSet.java index 092b9baf41f..9e5adffb8af 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/AddDefaultEndpointRuleSet.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/AddDefaultEndpointRuleSet.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.endpointsV2; import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_CONFIG; @@ -28,7 +27,6 @@ import software.amazon.smithy.utils.MapUtils; import software.amazon.smithy.utils.SmithyInternalApi; - /** * This class normalizes models without endpointRuleSet traits to use the same code paths as those with ruleSet, * to make reasoning about models easier and less variable. @@ -36,44 +34,44 @@ @SmithyInternalApi public class AddDefaultEndpointRuleSet implements TypeScriptIntegration { public static final EndpointRuleSetTrait DEFAULT_RULESET = EndpointRuleSetTrait.builder() - .ruleSet(Node.parse(""" - { - "version": "1.0", - "parameters": { - "endpoint": { - "type": "string", - "builtIn": "SDK::Endpoint", - "documentation": "Endpoint used for making requests. Should be formatted as a URI." - } - }, - "rules": [ - { - "conditions": [ + .ruleSet(Node.parse(""" { - "fn": "isSet", - "argv": [ + "version": "1.0", + "parameters": { + "endpoint": { + "type": "string", + "builtIn": "SDK::Endpoint", + "documentation": "Endpoint used for making requests. Should be formatted as a URI." + } + }, + "rules": [ { - "ref": "endpoint" + "conditions": [ + { + "fn": "isSet", + "argv": [ + { + "ref": "endpoint" + } + ] + } + ], + "endpoint": { + "url": { + "ref": "endpoint" + } + }, + "type": "endpoint" + }, + { + "conditions": [], + "error": "(default endpointRuleSet) endpoint is not set - you must configure an endpoint.", + "type": "error" } ] } - ], - "endpoint": { - "url": { - "ref": "endpoint" - } - }, - "type": "endpoint" - }, - { - "conditions": [], - "error": "(default endpointRuleSet) endpoint is not set - you must configure an endpoint.", - "type": "error" - } - ] - } - """)) - .build(); + """)) + .build(); private boolean usesDefaultEndpointRuleset = false; @@ -85,25 +83,24 @@ public List runAfter() { @Override public List getClientPlugins() { RuntimeClientPlugin endpointConfigResolver = RuntimeClientPlugin.builder() - .withConventions( - TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2.dependency, "Endpoint", HAS_CONFIG) - .build(); + .withConventions( + TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2.dependency, + "Endpoint", + HAS_CONFIG) + .build(); if (usesDefaultEndpointRuleset) { return List.of( - endpointConfigResolver, - RuntimeClientPlugin.builder() - .withConventions( - TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2.dependency, - "EndpointRequired", - HAS_CONFIG - ) - .build() - ); + endpointConfigResolver, + RuntimeClientPlugin.builder() + .withConventions( + TypeScriptDependency.MIDDLEWARE_ENDPOINTS_V2.dependency, + "EndpointRequired", + HAS_CONFIG) + .build()); } return List.of( - endpointConfigResolver - ); + endpointConfigResolver); } @Override @@ -115,8 +112,8 @@ public Model preprocessModel(Model model, TypeScriptSettings settings) { usesDefaultEndpointRuleset = true; modelBuilder.removeShape(serviceShape.toShapeId()); modelBuilder.addShape(serviceShape.toBuilder() - .addTrait(DEFAULT_RULESET) - .build()); + .addTrait(DEFAULT_RULESET) + .build()); } return modelBuilder.build(); @@ -124,18 +121,19 @@ public Model preprocessModel(Model model, TypeScriptSettings settings) { @Override public Map> getRuntimeConfigWriters( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - LanguageTarget target + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target ) { if (!settings.generateClient()) { return Collections.emptyMap(); } if (target == LanguageTarget.SHARED) { return MapUtils.of("endpointProvider", writer -> { - writer.addImport("defaultEndpointResolver", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "endpoint/endpointResolver").toString()); + writer.addImport("defaultEndpointResolver", + null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "endpoint/endpointResolver").toString()); writer.write("defaultEndpointResolver"); }); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsParamNameMap.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsParamNameMap.java index fe413f4249c..2c4af5c9b71 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsParamNameMap.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsParamNameMap.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.endpointsV2; import java.util.HashMap; @@ -50,7 +39,7 @@ public static String getLocalName(String endpointsV2ParamName) { String suggestedName = endpointsV2ParamName; if (isTitleCase) { suggestedName = endpointsV2ParamName.substring(0, 1).toLowerCase() - + endpointsV2ParamName.substring(1); + + endpointsV2ParamName.substring(1); } return MAPPING.getOrDefault(endpointsV2ParamName, suggestedName); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java index c05151530e4..96fa6f45538 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.endpointsV2; import java.nio.file.Paths; @@ -44,8 +33,10 @@ public final class EndpointsV2Generator implements Runnable { public static final String ENDPOINT_PARAMETERS_MODULE_NAME = "EndpointParameters"; public static final String ENDPOINT_RESOLVER_MODULE_NAME = "endpointResolver"; public static final String ENDPOINT_PARAMETERS_MODULE = - Paths.get(".", CodegenUtils.SOURCE_FOLDER, EndpointsV2Generator.ENDPOINT_FOLDER, - EndpointsV2Generator.ENDPOINT_PARAMETERS_MODULE_NAME).toString(); + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + EndpointsV2Generator.ENDPOINT_FOLDER, + EndpointsV2Generator.ENDPOINT_PARAMETERS_MODULE_NAME).toString(); public static final Dependency ENDPOINT_PARAMETERS_DEPENDENCY = new Dependency() { @Override public String getPackageName() { @@ -58,8 +49,10 @@ public List getDependencies() { } }; public static final String ENDPOINT_RESOLVER_MODULE = - Paths.get(".", CodegenUtils.SOURCE_FOLDER, EndpointsV2Generator.ENDPOINT_FOLDER, - EndpointsV2Generator.ENDPOINT_RESOLVER_MODULE_NAME).toString(); + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + EndpointsV2Generator.ENDPOINT_FOLDER, + EndpointsV2Generator.ENDPOINT_RESOLVER_MODULE_NAME).toString(); public static final Dependency ENDPOINT_RESOLVER_DEPENDENCY = new Dependency() { @Override public String getPackageName() { @@ -91,7 +84,7 @@ public EndpointsV2Generator( service = settings.getService(model); this.settings = settings; endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class) - .orElseThrow(() -> new RuntimeException("service or model preprocessor missing EndpointRuleSetTrait")); + .orElseThrow(() -> new RuntimeException("service or model preprocessor missing EndpointRuleSetTrait")); ruleSetParameterFinder = new RuleSetParameterFinder(service); } @@ -107,102 +100,100 @@ public void run() { */ private void generateEndpointParameters() { this.delegator.useFileWriter( - Paths.get(CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, ENDPOINT_PARAMETERS_FILE).toString(), - writer -> { - writer.addTypeImport("EndpointParameters", "__EndpointParameters", TypeScriptDependency.SMITHY_TYPES); - writer.addTypeImport("Provider", null, TypeScriptDependency.SMITHY_TYPES); + Paths.get(CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, ENDPOINT_PARAMETERS_FILE).toString(), + writer -> { + writer.addTypeImport("EndpointParameters", + "__EndpointParameters", + TypeScriptDependency.SMITHY_TYPES); + writer.addTypeImport("Provider", null, TypeScriptDependency.SMITHY_TYPES); - writer.writeDocs("@public"); - writer.openBlock( - "export interface ClientInputEndpointParameters {", - "}", - () -> { - Map clientInputParams = ruleSetParameterFinder.getClientContextParams(); - //Omit Endpoint params that should not be a part of the ClientInputEndpointParameters interface - Map builtInParams = ruleSetParameterFinder.getBuiltInParams(); - builtInParams.keySet().removeIf(OmitEndpointParams::isOmitted); - clientInputParams.putAll(builtInParams); + writer.writeDocs("@public"); + writer.openBlock( + "export interface ClientInputEndpointParameters {", + "}", + () -> { + Map clientInputParams = ruleSetParameterFinder.getClientContextParams(); + //Omit Endpoint params that should not be a part of the ClientInputEndpointParameters interface + Map builtInParams = ruleSetParameterFinder.getBuiltInParams(); + builtInParams.keySet().removeIf(OmitEndpointParams::isOmitted); + clientInputParams.putAll(builtInParams); - ObjectNode ruleSet = endpointRuleSetTrait.getRuleSet().expectObjectNode(); - ruleSet.getObjectMember("parameters").ifPresent(parameters -> { - parameters.accept(new RuleSetParametersVisitor(writer, clientInputParams, true)); - }); - } - ); + ObjectNode ruleSet = endpointRuleSetTrait.getRuleSet().expectObjectNode(); + ruleSet.getObjectMember("parameters").ifPresent(parameters -> { + parameters.accept(new RuleSetParametersVisitor(writer, clientInputParams, true)); + }); + }); - writer.write(""); - writer.writeDocs("@public"); - writer.write( - """ - export type ClientResolvedEndpointParameters = Omit & { - defaultSigningName: string; - };""" - ); - writer.write(""); + writer.write(""); + writer.writeDocs("@public"); + writer.write( + """ + export type ClientResolvedEndpointParameters = Omit & { + defaultSigningName: string; + };"""); + writer.write(""); - writer.writeDocs("@internal"); - writer.openBlock( - """ - export const resolveClientEndpointParameters = ( - options: T & ClientInputEndpointParameters - ): T & ClientResolvedEndpointParameters => {""", - "};", - () -> { - writer.openBlock("return Object.assign(options, {", "});", () -> { - ObjectNode ruleSet = endpointRuleSetTrait.getRuleSet().expectObjectNode(); - ruleSet.getObjectMember("parameters").ifPresent(parameters -> { - parameters.accept(new RuleSetParametersVisitor(writer, true)); + writer.writeDocs("@internal"); + writer.openBlock( + """ + export const resolveClientEndpointParameters = ( + options: T & ClientInputEndpointParameters + ): T & ClientResolvedEndpointParameters => {""", + "};", + () -> { + writer.openBlock("return Object.assign(options, {", "});", () -> { + ObjectNode ruleSet = endpointRuleSetTrait.getRuleSet().expectObjectNode(); + ruleSet.getObjectMember("parameters").ifPresent(parameters -> { + parameters.accept(new RuleSetParametersVisitor(writer, true)); + }); + writer.write( + "defaultSigningName: \"$L\",", + settings.getDefaultSigningName()); + }); }); - writer.write( - "defaultSigningName: \"$L\",", - settings.getDefaultSigningName() - ); - }); - } - ); - writer.write(""); + writer.write(""); - writer.writeDocs("@internal"); - writer.openBlock( - "export const commonParams = {", "} as const;", - () -> { - Set paramNames = new HashSet<>(); + writer.writeDocs("@internal"); + writer.openBlock( + "export const commonParams = {", + "} as const;", + () -> { + Set paramNames = new HashSet<>(); - ruleSetParameterFinder.getClientContextParams().forEach((name, type) -> { - if (!paramNames.contains(name)) { - writer.write( - "$L: { type: \"clientContextParams\", name: \"$L\" },", - name, EndpointsParamNameMap.getLocalName(name)); - } - paramNames.add(name); - }); + ruleSetParameterFinder.getClientContextParams().forEach((name, type) -> { + if (!paramNames.contains(name)) { + writer.write( + "$L: { type: \"clientContextParams\", name: \"$L\" },", + name, + EndpointsParamNameMap.getLocalName(name)); + } + paramNames.add(name); + }); - ruleSetParameterFinder.getBuiltInParams().forEach((name, type) -> { - if (!paramNames.contains(name)) { - writer.write( - "$L: { type: \"builtInParams\", name: \"$L\" },", - name, EndpointsParamNameMap.getLocalName(name)); - } - paramNames.add(name); - }); - } - ); + ruleSetParameterFinder.getBuiltInParams().forEach((name, type) -> { + if (!paramNames.contains(name)) { + writer.write( + "$L: { type: \"builtInParams\", name: \"$L\" },", + name, + EndpointsParamNameMap.getLocalName(name)); + } + paramNames.add(name); + }); + }); - writer.write(""); - writer.writeDocs("@internal"); - writer.openBlock( - "export interface EndpointParameters extends __EndpointParameters {", - "}", - () -> { - ObjectNode ruleSet = endpointRuleSetTrait.getRuleSet().expectObjectNode(); - ruleSet.getObjectMember("parameters").ifPresent(parameters -> { - parameters.accept(new RuleSetParametersVisitor(writer)); - }); - } - ); - } - ); + writer.write(""); + writer.writeDocs("@internal"); + writer.openBlock( + "export interface EndpointParameters extends __EndpointParameters {", + "}", + () -> { + ObjectNode ruleSet = endpointRuleSetTrait.getRuleSet().expectObjectNode(); + ruleSet.getObjectMember("parameters").ifPresent(parameters -> { + parameters.accept(new RuleSetParametersVisitor(writer)); + }); + }); + }); } /** @@ -210,50 +201,54 @@ private void generateEndpointParameters() { */ private void generateEndpointResolver() { this.delegator.useFileWriter( - Paths.get(CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, ENDPOINT_RESOLVER_FILE).toString(), - writer -> { - writer.addTypeImport("EndpointV2", null, TypeScriptDependency.SMITHY_TYPES); - writer.addTypeImport("Logger", null, TypeScriptDependency.SMITHY_TYPES); + Paths.get(CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, ENDPOINT_RESOLVER_FILE).toString(), + writer -> { + writer.addTypeImport("EndpointV2", null, TypeScriptDependency.SMITHY_TYPES); + writer.addTypeImport("Logger", null, TypeScriptDependency.SMITHY_TYPES); - writer.addDependency(TypeScriptDependency.UTIL_ENDPOINTS); - writer.addTypeImport("EndpointParams", null, TypeScriptDependency.UTIL_ENDPOINTS); - writer.addImport("resolveEndpoint", null, TypeScriptDependency.UTIL_ENDPOINTS); - writer.addRelativeTypeImport("EndpointParameters", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, - ENDPOINT_PARAMETERS_FILE.replace(".ts", ""))); - writer.addRelativeImport("ruleSet", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, - ENDPOINT_RULESET_FILE.replace(".ts", ""))); + writer.addDependency(TypeScriptDependency.UTIL_ENDPOINTS); + writer.addTypeImport("EndpointParams", null, TypeScriptDependency.UTIL_ENDPOINTS); + writer.addImport("resolveEndpoint", null, TypeScriptDependency.UTIL_ENDPOINTS); + writer.addRelativeTypeImport("EndpointParameters", + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + ENDPOINT_FOLDER, + ENDPOINT_PARAMETERS_FILE.replace(".ts", ""))); + writer.addRelativeImport("ruleSet", + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + ENDPOINT_FOLDER, + ENDPOINT_RULESET_FILE.replace(".ts", ""))); - writer.addImport("EndpointCache", null, TypeScriptDependency.UTIL_ENDPOINTS); - writer.write(""" - const cache = new EndpointCache({ - size: 50, - params: [$L], - }); - """, - ruleSetParameterFinder.getEffectiveParams() - .stream().collect(Collectors.joining("\",\n \"", "\"", "\"")) - ); + writer.addImport("EndpointCache", null, TypeScriptDependency.UTIL_ENDPOINTS); + writer.write(""" + const cache = new EndpointCache({ + size: 50, + params: [$L], + }); + """, + ruleSetParameterFinder.getEffectiveParams() + .stream() + .collect(Collectors.joining("\",\n \"", "\"", "\""))); - writer.writeDocs("@internal"); - writer.write( - """ - export const defaultEndpointResolver = ( - endpointParams: EndpointParameters, - context: { logger?: Logger } = {} - ): EndpointV2 => { - return cache.get(endpointParams as EndpointParams, () => - resolveEndpoint(ruleSet, { - endpointParams: endpointParams as EndpointParams, - logger: context.logger, - }) - ); - }; - """ - ); - } - ); + writer.writeDocs("@internal"); + writer.write( + """ + export const defaultEndpointResolver = ( + endpointParams: EndpointParameters, + context: { logger?: Logger } = {} + ): EndpointV2 => { + return cache.get(endpointParams as EndpointParams, () => + resolveEndpoint(ruleSet, { + endpointParams: endpointParams as EndpointParams, + logger: context.logger, + }) + ); + }; + """); + }); } /** @@ -261,16 +256,14 @@ private void generateEndpointResolver() { */ private void generateEndpointRuleset() { this.delegator.useFileWriter( - Paths.get(CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, ENDPOINT_RULESET_FILE).toString(), - writer -> { - writer.addTypeImport("RuleSetObject", null, TypeScriptDependency.SMITHY_TYPES); + Paths.get(CodegenUtils.SOURCE_FOLDER, ENDPOINT_FOLDER, ENDPOINT_RULESET_FILE).toString(), + writer -> { + writer.addTypeImport("RuleSetObject", null, TypeScriptDependency.SMITHY_TYPES); - writer.writeInline("export const ruleSet: RuleSetObject = "); - new RuleSetSerializer( - endpointRuleSetTrait.getRuleSet(), - writer - ).generate(); - } - ); + writer.writeInline("export const ruleSet: RuleSetObject = "); + new RuleSetSerializer( + endpointRuleSetTrait.getRuleSet(), + writer).generate(); + }); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/OmitEndpointParams.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/OmitEndpointParams.java index 7c94fe61f38..6a28a2e0634 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/OmitEndpointParams.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/OmitEndpointParams.java @@ -2,32 +2,31 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ +package software.amazon.smithy.typescript.codegen.endpointsV2; - package software.amazon.smithy.typescript.codegen.endpointsV2; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; - import java.util.Collections; - import java.util.HashSet; - import java.util.Set; - - /** - * Manages a collection of endpoint parameter names to be omitted from a specific interface. - * While this could be extensible in the future, as of right now, - * this collection is maintaining endpoint params to be omitted from the `ClientInputEndpointParameters` interface. - */ - public final class OmitEndpointParams { - private static final Set OMITTED_PARAMS = new HashSet<>(); +/** + * Manages a collection of endpoint parameter names to be omitted from a specific interface. + * While this could be extensible in the future, as of right now, + * this collection is maintaining endpoint params to be omitted from the `ClientInputEndpointParameters` interface. + */ +public final class OmitEndpointParams { + private static final Set OMITTED_PARAMS = new HashSet<>(); - private OmitEndpointParams() {} + private OmitEndpointParams() {} - public static void addOmittedParams(Set paramNames) { - OMITTED_PARAMS.addAll(paramNames); - } + public static void addOmittedParams(Set paramNames) { + OMITTED_PARAMS.addAll(paramNames); + } - public static boolean isOmitted(String paramName) { - return OMITTED_PARAMS.contains(paramName); - } + public static boolean isOmitted(String paramName) { + return OMITTED_PARAMS.contains(paramName); + } - public static Set getOmittedParams() { - return Collections.unmodifiableSet(OMITTED_PARAMS); - } + public static Set getOmittedParams() { + return Collections.unmodifiableSet(OMITTED_PARAMS); + } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/ParameterGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/ParameterGenerator.java index f7a1bcd12af..d8a5832ad25 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/ParameterGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/ParameterGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.endpointsV2; import java.util.AbstractMap; @@ -48,7 +37,7 @@ public ParameterGenerator(String key, Node param, boolean isInputKey) { this.isInputKey = isInputKey; ObjectNode paramNode = param.asObjectNode() - .orElseThrow(() -> new RuntimeException("param node is not object node.")); + .orElseThrow(() -> new RuntimeException("param node is not object node.")); Optional requiredNode = paramNode.getBooleanMember("required"); requiredNode.ifPresent(booleanNode -> required = booleanNode.getValue()); @@ -107,9 +96,11 @@ public String defaultAsCodeString() { buffer += paramNode.expectBooleanMember("default").getValue() ? "true" : "false"; break; case "stringArray": - buffer += paramNode.expectArrayMember("default").getElements().stream() - .map(element -> element.expectStringNode().getValue()) - .collect(Collectors.joining("`, `", "[`", "`]")); + buffer += paramNode.expectArrayMember("default") + .getElements() + .stream() + .map(element -> element.expectStringNode().getValue()) + .collect(Collectors.joining("`, `", "[`", "`]")); break; default: throw new RuntimeException("Unhandled endpoint param type: " + type.getValue()); @@ -121,9 +112,8 @@ public String defaultAsCodeString() { public Map.Entry getNameAndType() { return new AbstractMap.SimpleEntry<>( - parameterName, - tsParamType - ); + parameterName, + tsParamType); } /** diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java index 2c6235f8823..c234f9ef189 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.endpointsV2; import java.util.ArrayDeque; @@ -62,9 +51,9 @@ public class RuleSetParameterFinder { public RuleSetParameterFinder(ServiceShape service) { this.service = service; - this.ruleset = service.getTrait(EndpointRuleSetTrait.class).orElseThrow( - () -> new RuntimeException("Service does not have EndpointRuleSetTrait.") - ); + this.ruleset = service.getTrait(EndpointRuleSetTrait.class) + .orElseThrow( + () -> new RuntimeException("Service does not have EndpointRuleSetTrait.")); } /** @@ -109,22 +98,23 @@ public List getEffectiveParams() { } else if (arg.isStringNode()) { String argString = arg.expectStringNode().getValue(); URL_PARAMETERS - .matcher(argString) - .results().forEach(matchResult -> { - if (matchResult.groupCount() >= 1) { - if (initialParams.contains(matchResult.group(1))) { - effectiveParams.add(matchResult.group(1)); + .matcher(argString) + .results() + .forEach(matchResult -> { + if (matchResult.groupCount() >= 1) { + if (initialParams.contains(matchResult.group(1))) { + effectiveParams.add(matchResult.group(1)); + } } - } - }); + }); } } while (!conditionQueue.isEmpty()) { Condition condition = conditionQueue.poll(); ArrayNode argv = condition.toNode() - .expectObjectNode() - .expectArrayMember("argv"); + .expectObjectNode() + .expectArrayMember("argv"); for (Node arg : argv) { argQueue.add(arg); } @@ -144,14 +134,15 @@ public List getEffectiveParams() { String urlString = url.toString(); URL_PARAMETERS - .matcher(urlString) - .results().forEach(matchResult -> { - if (matchResult.groupCount() >= 1) { - if (initialParams.contains(matchResult.group(1))) { - effectiveParams.add(matchResult.group(1)); + .matcher(urlString) + .results() + .forEach(matchResult -> { + if (matchResult.groupCount() >= 1) { + if (initialParams.contains(matchResult.group(1))) { + effectiveParams.add(matchResult.group(1)); + } } - } - }); + }); } else if (rule instanceof ErrorRule errorRule) { // no additional use of endpoint parameters in error rules. } @@ -185,19 +176,18 @@ public Map getClientContextParams() { ShapeType shapeType = definition.getType(); if (shapeType.isShapeType(ShapeType.STRING) || shapeType.isShapeType(ShapeType.BOOLEAN)) { map.put( - name, - // "boolean" and "string" are directly usable in TS. - definition.getType().toString().toLowerCase() - ); + name, + // "boolean" and "string" are directly usable in TS. + definition.getType().toString().toLowerCase()); } else if (shapeType.isShapeType(ShapeType.LIST)) { map.put( - name, - "string[]" // Only string lists are supported. + name, + "string[]" // Only string lists are supported. ); } else { throw new RuntimeException("unexpected type " - + definition.getType().toString() - + " received as clientContextParam."); + + definition.getType().toString() + + " received as clientContextParam."); } }); } @@ -221,18 +211,18 @@ public Map getStaticContextParamValues(OperationShape operation) value = definition.getValue().expectBooleanNode().toString(); } else if (definition.getValue().isArrayNode()) { ArrayNode arrayNode = definition.getValue().expectArrayNode(); - value = arrayNode.getElements().stream() - .map(element -> element.expectStringNode().getValue()) - .collect(Collectors.joining("`, `", "[`", "`]")); + value = arrayNode.getElements() + .stream() + .map(element -> element.expectStringNode().getValue()) + .collect(Collectors.joining("`, `", "[`", "`]")); } else { throw new RuntimeException("unexpected type " - + definition.getValue().getType().toString() - + " received as staticContextParam."); + + definition.getValue().getType().toString() + + " received as staticContextParam."); } map.put( - name, - value - ); + name, + value); }); } @@ -255,9 +245,8 @@ public Map getContextParams(Shape operationInput) { ContextParamTrait contextParamTrait = trait.get(); String name = contextParamTrait.getName(); map.put( - name, - member.getMemberName() - ); + name, + member.getMemberName()); } }); } @@ -286,8 +275,8 @@ public Map getOperationContextParamValues(OperationShape operati } // Close all open brackets. - value += ")".repeat((int) ( - value.chars().filter(ch -> ch == '(').count() - value.chars().filter(ch -> ch == ')').count())); + value += ")".repeat((int) (value.chars().filter(ch -> ch == '(').count() + - value.chars().filter(ch -> ch == ')').count())); map.put(name, value); }); @@ -302,7 +291,7 @@ String getJmesPathExpression(String separator, String value, String path) { if (path.startsWith("[") && !path.startsWith("[*]")) { // Process MultiSelect List https://jmespath.org/specification.html#multiselect-list if (value.endsWith("obj")) { - value = value.substring(0, value.length() - 3); + value = value.substring(0, value.length() - 3); } value += "["; @@ -404,9 +393,8 @@ public Void objectNode(ObjectNode node) { if (parameterGenerator.isBuiltIn()) { Map.Entry nameAndType = parameterGenerator.getNameAndType(); map.put( - nameAndType.getKey(), - nameAndType.getValue() - ); + nameAndType.getKey(), + nameAndType.getValue()); } } return null; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParametersVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParametersVisitor.java index 6fd7937246b..0ea12f36ded 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParametersVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParametersVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.endpointsV2; import java.util.HashMap; @@ -38,9 +27,11 @@ public RuleSetParametersVisitor(TypeScriptWriter writer) { this.clientContextParams = new HashMap<>(); } - public RuleSetParametersVisitor(TypeScriptWriter writer, - Map clientContextParams, - boolean useLocalNames) { + public RuleSetParametersVisitor( + TypeScriptWriter writer, + Map clientContextParams, + boolean useLocalNames + ) { this.writer = writer; this.clientContextParams = clientContextParams; this.useLocalNames = useLocalNames; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetSerializer.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetSerializer.java index b3f001f39a8..f173fdc57e2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetSerializer.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetSerializer.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.endpointsV2; import software.amazon.smithy.model.node.ArrayNode; @@ -36,16 +25,15 @@ public RuleSetSerializer(Node ruleSet, TypeScriptWriter writer) { public void generate() { ObjectNode objectNode = ruleSet.expectObjectNode(); writer.openCollapsibleBlock( - "{", - "};", - !objectNode.getMembers().isEmpty(), - () -> { - objectNode.getMembers().forEach((k, v) -> { - writer.writeInline(PropertyAccessor.inlineKey(k.toString()) + ": "); - traverse(v); + "{", + "};", + !objectNode.getMembers().isEmpty(), + () -> { + objectNode.getMembers().forEach((k, v) -> { + writer.writeInline(PropertyAccessor.inlineKey(k.toString()) + ": "); + traverse(v); + }); }); - } - ); } private void traverse(Node node) { @@ -53,26 +41,24 @@ private void traverse(Node node) { ObjectNode objectNode = node.expectObjectNode(); writer.openCollapsibleBlock( - "{", - "},", - !objectNode.getMembers().isEmpty(), - () -> { - objectNode.getMembers().forEach((k, v) -> { - writer.writeInline(PropertyAccessor.inlineKey(k.toString()) + ": "); - traverse(v); + "{", + "},", + !objectNode.getMembers().isEmpty(), + () -> { + objectNode.getMembers().forEach((k, v) -> { + writer.writeInline(PropertyAccessor.inlineKey(k.toString()) + ": "); + traverse(v); + }); }); - } - ); } else if (node.isArrayNode()) { ArrayNode arrayNode = node.expectArrayNode(); writer.openCollapsibleBlock( - "[", - "],", - !arrayNode.getElements().isEmpty(), - () -> { - arrayNode.getElements().forEach(this::traverse); - } - ); + "[", + "],", + !arrayNode.getElements().isEmpty(), + () -> { + arrayNode.getElements().forEach(this::traverse); + }); } else if (node.isBooleanNode()) { writer.write("$L,", node.expectBooleanNode().getValue()); } else if (node.isNumberNode()) { @@ -90,15 +76,13 @@ private void traverse(Node node) { String stringValue = node.expectStringNode().getValue(); if (stringValue.contains("\"")) { writer.write( - "`$L`,", - stringValue - .replaceAll("`", "\\\\`") - ); + "`$L`,", + stringValue + .replaceAll("`", "\\\\`")); } else { writer.write( - "\"$L\",", - stringValue - ); + "\"$L\",", + stringValue); } } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/DefaultExtensionConfigurationInterface.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/DefaultExtensionConfigurationInterface.java index faf3d5b471d..56a7b2f0875 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/DefaultExtensionConfigurationInterface.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/DefaultExtensionConfigurationInterface.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.extensions; import software.amazon.smithy.typescript.codegen.Dependency; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/ExtensionConfigurationInterface.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/ExtensionConfigurationInterface.java index 03bdeae8c29..cfeed00c6ee 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/ExtensionConfigurationInterface.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/ExtensionConfigurationInterface.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.extensions; import software.amazon.smithy.typescript.codegen.Dependency; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/HttpHandlerExtensionConfigurationInterface.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/HttpHandlerExtensionConfigurationInterface.java index b14a540acb2..6c44492a1b8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/HttpHandlerExtensionConfigurationInterface.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/extensions/HttpHandlerExtensionConfigurationInterface.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.extensions; import software.amazon.smithy.typescript.codegen.Dependency; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBaseServiceExceptionClass.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBaseServiceExceptionClass.java index 970e1b1008f..7d4539f68ce 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBaseServiceExceptionClass.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBaseServiceExceptionClass.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.nio.file.Paths; @@ -63,23 +52,25 @@ private void writeAdditionalFiles( writerFactory.accept( Paths.get(CodegenUtils.SOURCE_FOLDER, "models", serviceExceptionName + ".ts").toString(), writer -> { - writer.addImport("ServiceException", "__ServiceException", - TypeScriptDependency.AWS_SMITHY_CLIENT); - writer.addTypeImport("ServiceExceptionOptions", "__ServiceExceptionOptions", - TypeScriptDependency.AWS_SMITHY_CLIENT); - // Export ServiceException information to allow - // documentation inheritance to consume their types - writer.write("export type { __ServiceExceptionOptions };\n"); - writer.write("export { __ServiceException };\n"); - writer.writeDocs("@public\n\nBase exception class for all service exceptions from " - + serviceName + " service."); - writer.openBlock("export class $L extends __ServiceException {", serviceExceptionName); - writer.writeDocs("@internal"); - writer.openBlock("constructor(options: __ServiceExceptionOptions) {"); - writer.write("super(options);"); - writer.write("Object.setPrototypeOf(this, $L.prototype);", serviceExceptionName); - writer.closeBlock("}"); // constructor - writer.closeBlock("}"); // class + writer.addImport("ServiceException", + "__ServiceException", + TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.addTypeImport("ServiceExceptionOptions", + "__ServiceExceptionOptions", + TypeScriptDependency.AWS_SMITHY_CLIENT); + // Export ServiceException information to allow + // documentation inheritance to consume their types + writer.write("export type { __ServiceExceptionOptions };\n"); + writer.write("export { __ServiceException };\n"); + writer.writeDocs("@public\n\nBase exception class for all service exceptions from " + + serviceName + " service."); + writer.openBlock("export class $L extends __ServiceException {", serviceExceptionName); + writer.writeDocs("@internal"); + writer.openBlock("constructor(options: __ServiceExceptionOptions) {"); + writer.write("super(options);"); + writer.write("Object.setPrototypeOf(this, $L.prototype);", serviceExceptionName); + writer.closeBlock("}"); // constructor + writer.closeBlock("}"); // class }); } } @@ -122,7 +113,8 @@ public SymbolProvider decorateSymbolProvider( Symbol serviceExceptionSymbol = Symbol.builder() .name(serviceExceptionName) .namespace(namespace, "/") - .definitionFile(namespace + ".ts").build(); + .definitionFile(namespace + ".ts") + .build(); reference = SymbolReference.builder() .options(SymbolReference.ContextOption.USE) .alias(baseExceptionAlias) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java index 3a21dc919ba..7b9062e9631 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBuiltinPlugins.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE; @@ -21,12 +20,13 @@ public List getClientPlugins() { // Note that order is significant because configurations might // rely on previously resolved values. return List.of( - RuntimeClientPlugin.builder() - .withConventions(TypeScriptDependency.MIDDLEWARE_RETRY.dependency, "Retry") - .build(), - RuntimeClientPlugin.builder() - .withConventions(TypeScriptDependency.MIDDLEWARE_CONTENT_LENGTH.dependency, "ContentLength", - HAS_MIDDLEWARE) - .build()); + RuntimeClientPlugin.builder() + .withConventions(TypeScriptDependency.MIDDLEWARE_RETRY.dependency, "Retry") + .build(), + RuntimeClientPlugin.builder() + .withConventions(TypeScriptDependency.MIDDLEWARE_CONTENT_LENGTH.dependency, + "ContentLength", + HAS_MIDDLEWARE) + .build()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddChecksumRequiredDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddChecksumRequiredDependency.java index 7f3690b027e..7df23af4356 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddChecksumRequiredDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddChecksumRequiredDependency.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE; @@ -64,9 +53,9 @@ public void addConfigInterfaceFields( writer.addTypeImport("Checksum", "__Checksum", TypeScriptDependency.SMITHY_TYPES); writer.addTypeImport("ChecksumConstructor", "__ChecksumConstructor", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs(""" - A constructor for a class implementing the {@link __Checksum} interface - that computes MD5 hashes. - @internal"""); + A constructor for a class implementing the {@link __Checksum} interface + that computes MD5 hashes. + @internal"""); writer.write("md5?: __ChecksumConstructor | __HashConstructor;\n"); } @@ -84,30 +73,36 @@ public Map> getRuntimeConfigWriters( switch (target) { case NODE: return MapUtils.of( - "streamHasher", writer -> { - writer.addDependency(TypeScriptDependency.STREAM_HASHER_NODE); - writer.addImport("fileStreamHasher", "streamHasher", - TypeScriptDependency.STREAM_HASHER_NODE); - writer.write("streamHasher"); - }, - "md5", writer -> { + "streamHasher", + writer -> { + writer.addDependency(TypeScriptDependency.STREAM_HASHER_NODE); + writer.addImport("fileStreamHasher", + "streamHasher", + TypeScriptDependency.STREAM_HASHER_NODE); + writer.write("streamHasher"); + }, + "md5", + writer -> { writer.addDependency(TypeScriptDependency.AWS_SDK_HASH_NODE); writer.addImport("Hash", null, TypeScriptDependency.AWS_SDK_HASH_NODE); writer.write("Hash.bind(null, \"md5\")"); - }); + }); case BROWSER: return MapUtils.of( - "streamHasher", writer -> { - writer.addDependency(TypeScriptDependency.STREAM_HASHER_BROWSER); - writer.addImport("blobHasher", "streamHasher", - TypeScriptDependency.STREAM_HASHER_BROWSER); - writer.write("streamHasher"); - }, - "md5", writer -> { - writer.addDependency(TypeScriptDependency.MD5_BROWSER); - writer.addImport("Md5", null, TypeScriptDependency.MD5_BROWSER); - writer.write("Md5"); - }); + "streamHasher", + writer -> { + writer.addDependency(TypeScriptDependency.STREAM_HASHER_BROWSER); + writer.addImport("blobHasher", + "streamHasher", + TypeScriptDependency.STREAM_HASHER_BROWSER); + writer.write("streamHasher"); + }, + "md5", + writer -> { + writer.addDependency(TypeScriptDependency.MD5_BROWSER); + writer.addImport("Md5", null, TypeScriptDependency.MD5_BROWSER); + writer.write("Md5"); + }); default: return Collections.emptyMap(); } @@ -116,15 +111,14 @@ public Map> getRuntimeConfigWriters( @Override public List getClientPlugins() { return ListUtils.of( - RuntimeClientPlugin.builder() - .withConventions(TypeScriptDependency.BODY_CHECKSUM.dependency, "ApplyMd5BodyChecksum", - HAS_MIDDLEWARE) + RuntimeClientPlugin.builder() + .withConventions(TypeScriptDependency.BODY_CHECKSUM.dependency, + "ApplyMd5BodyChecksum", + HAS_MIDDLEWARE) .operationPredicate((m, s, o) -> hasChecksumRequiredTrait(m, s, o)) - .build() - ); + .build()); } - // return true if operation shape is decorated with `httpChecksumRequired` trait. private static boolean hasChecksumRequiredTrait(Model model, ServiceShape service, OperationShape operation) { return operation.hasTrait(HttpChecksumRequiredTrait.class); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddClientRuntimeConfig.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddClientRuntimeConfig.java index b13b785c10e..cbd0c9ea5d2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddClientRuntimeConfig.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddClientRuntimeConfig.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.nio.file.Paths; @@ -58,99 +57,105 @@ public final class AddClientRuntimeConfig implements TypeScriptIntegration { @Override public void addConfigInterfaceFields( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - TypeScriptWriter writer + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + TypeScriptWriter writer ) { writer.addTypeImport("Provider", "__Provider", TypeScriptDependency.SMITHY_TYPES); writer.addTypeImport("Logger", "__Logger", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs("Value for how many times a request will be made at most in case of retry.") - .write("maxAttempts?: number | __Provider;\n"); + .write("maxAttempts?: number | __Provider;\n"); writer.writeDocs(""" - Specifies which retry algorithm to use. - @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-smithy-util-retry/Enum/RETRY_MODES/ - """) - .write("retryMode?: string | __Provider;\n"); + Specifies which retry algorithm to use. + @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-smithy-util-retry/Enum/RETRY_MODES/ + """) + .write("retryMode?: string | __Provider;\n"); writer.writeDocs("Optional logger for logging debug/info/warn/error.") - .write("logger?: __Logger;\n"); - writer.addRelativeTypeImport("RuntimeExtension", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeExtensions")); + .write("logger?: __Logger;\n"); + writer.addRelativeTypeImport("RuntimeExtension", + null, + Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeExtensions")); writer.writeDocs("Optional extensions") - .write("extensions?: RuntimeExtension[];\n"); + .write("extensions?: RuntimeExtension[];\n"); } @Override public Map> getRuntimeConfigWriters( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - LanguageTarget target + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target ) { switch (target) { case SHARED: return MapUtils.of( - "logger", writer -> { - writer.addImport("NoOpLogger", null, TypeScriptDependency.AWS_SMITHY_CLIENT); - writer.write("new NoOpLogger()"); - } - ); + "logger", + writer -> { + writer.addImport("NoOpLogger", null, TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.write("new NoOpLogger()"); + }); case BROWSER: return MapUtils.of( - "maxAttempts", writer -> { - writer.addDependency(TypeScriptDependency.UTIL_RETRY); - writer.addImport("DEFAULT_MAX_ATTEMPTS", null, TypeScriptDependency.UTIL_RETRY); - writer.write("DEFAULT_MAX_ATTEMPTS"); - }, - "retryMode", writer -> { - writer.addDependency(TypeScriptDependency.UTIL_RETRY); - writer.addImport("DEFAULT_RETRY_MODE", null, TypeScriptDependency.UTIL_RETRY); - writer.write( - "(async () => (await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE)" - ); - } - ); + "maxAttempts", + writer -> { + writer.addDependency(TypeScriptDependency.UTIL_RETRY); + writer.addImport("DEFAULT_MAX_ATTEMPTS", null, TypeScriptDependency.UTIL_RETRY); + writer.write("DEFAULT_MAX_ATTEMPTS"); + }, + "retryMode", + writer -> { + writer.addDependency(TypeScriptDependency.UTIL_RETRY); + writer.addImport("DEFAULT_RETRY_MODE", null, TypeScriptDependency.UTIL_RETRY); + writer.write( + "(async () => (await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE)"); + }); case NODE: return MapUtils.of( - "maxAttempts", writer -> { - writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addImport("loadConfig", "loadNodeConfig", - TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addImport("NODE_MAX_ATTEMPT_CONFIG_OPTIONS", null, - TypeScriptDependency.MIDDLEWARE_RETRY); - writer.write("loadNodeConfig(NODE_MAX_ATTEMPT_CONFIG_OPTIONS, config)"); - }, - "retryMode", writer -> { - writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addImport("loadConfig", "loadNodeConfig", - TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addDependency(TypeScriptDependency.MIDDLEWARE_RETRY); - writer.addImport("NODE_RETRY_MODE_CONFIG_OPTIONS", null, - TypeScriptDependency.MIDDLEWARE_RETRY); - writer.addImport("DEFAULT_RETRY_MODE", null, TypeScriptDependency.UTIL_RETRY); - writer.indent(); - writer.writeInline(""" - loadNodeConfig( - { - ...NODE_RETRY_MODE_CONFIG_OPTIONS, - default: async () => (await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE, - }, - config - )""" - ); - writer.dedent(); - } - ); - default: + "maxAttempts", + writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("loadConfig", + "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("NODE_MAX_ATTEMPT_CONFIG_OPTIONS", + null, + TypeScriptDependency.MIDDLEWARE_RETRY); + writer.write("loadNodeConfig(NODE_MAX_ATTEMPT_CONFIG_OPTIONS, config)"); + }, + "retryMode", + writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("loadConfig", + "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addDependency(TypeScriptDependency.MIDDLEWARE_RETRY); + writer.addImport("NODE_RETRY_MODE_CONFIG_OPTIONS", + null, + TypeScriptDependency.MIDDLEWARE_RETRY); + writer.addImport("DEFAULT_RETRY_MODE", null, TypeScriptDependency.UTIL_RETRY); + writer.indent(); + writer.writeInline( + """ + loadNodeConfig( + { + ...NODE_RETRY_MODE_CONFIG_OPTIONS, + default: async () => (await defaultConfigProvider()).retryMode || DEFAULT_RETRY_MODE, + }, + config + )"""); + writer.dedent(); + }); + default: return Collections.emptyMap(); } } @Override public List getExtensionConfigurationInterfaces( - Model model, - TypeScriptSettings settings + Model model, + TypeScriptSettings settings ) { return List.of(new DefaultExtensionConfigurationInterface(), new HttpHandlerExtensionConfigurationInterface()); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java index bae7d042106..dc8fd805eca 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java @@ -1,18 +1,7 @@ /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Collections; @@ -58,40 +47,49 @@ public Map> getRuntimeConfigWriters( switch (target) { case NODE: return MapUtils.of( - "disableRequestCompression", writer -> { - writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.addImport("loadConfig", "loadNodeConfig", - TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addImport("NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS", null, - TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.write("loadNodeConfig(NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS, config)"); - }, - "requestMinCompressionSizeBytes", writer -> { - writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.addImport("loadConfig", "loadNodeConfig", - TypeScriptDependency.NODE_CONFIG_PROVIDER); - writer.addImport("NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS", null, - TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.write("loadNodeConfig(NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS, config)"); - } - ); + "disableRequestCompression", + writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("loadConfig", + "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS", + null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write("loadNodeConfig(NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS, config)"); + }, + "requestMinCompressionSizeBytes", + writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("loadConfig", + "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS", + null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write( + "loadNodeConfig(NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS, config)"); + }); case BROWSER: return MapUtils.of( - "disableRequestCompression", writer -> { - writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.addImport("DEFAULT_DISABLE_REQUEST_COMPRESSION", null, - TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.write("DEFAULT_DISABLE_REQUEST_COMPRESSION"); - }, - "requestMinCompressionSizeBytes", writer -> { - writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.addImport("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES", null, - TypeScriptDependency.MIDDLEWARE_COMPRESSION); - writer.write("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES"); - } - ); + "disableRequestCompression", + writer -> { + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("DEFAULT_DISABLE_REQUEST_COMPRESSION", + null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write("DEFAULT_DISABLE_REQUEST_COMPRESSION"); + }, + "requestMinCompressionSizeBytes", + writer -> { + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES", + null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES"); + }); default: return Collections.emptyMap(); } @@ -100,24 +98,25 @@ public Map> getRuntimeConfigWriters( @Override public List getClientPlugins() { return ListUtils.of( - RuntimeClientPlugin.builder() - .withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency, - "Compression", RuntimeClientPlugin.Convention.HAS_CONFIG) - .servicePredicate((m, s) -> hasRequestCompressionTrait(m, s)) - .build(), - RuntimeClientPlugin.builder() - .withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency, - "Compression", RuntimeClientPlugin.Convention.HAS_MIDDLEWARE) - .additionalPluginFunctionParamsSupplier((m, s, o) -> getPluginFunctionParams(m, s, o)) - .operationPredicate((m, s, o) -> hasRequestCompressionTrait(o)) - .build() - ); + RuntimeClientPlugin.builder() + .withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency, + "Compression", + RuntimeClientPlugin.Convention.HAS_CONFIG) + .servicePredicate((m, s) -> hasRequestCompressionTrait(m, s)) + .build(), + RuntimeClientPlugin.builder() + .withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency, + "Compression", + RuntimeClientPlugin.Convention.HAS_MIDDLEWARE) + .additionalPluginFunctionParamsSupplier((m, s, o) -> getPluginFunctionParams(m, s, o)) + .operationPredicate((m, s, o) -> hasRequestCompressionTrait(o)) + .build()); } private static Map getPluginFunctionParams( - Model model, - ServiceShape service, - OperationShape operation + Model model, + ServiceShape service, + OperationShape operation ) { Map params = new TreeMap(); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddDefaultsModeDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddDefaultsModeDependency.java index e797d9dc4d6..11721eac25b 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddDefaultsModeDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddDefaultsModeDependency.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import software.amazon.smithy.codegen.core.SymbolProvider; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEventStreamDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEventStreamDependency.java index 99a11996ffa..db704323504 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEventStreamDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEventStreamDependency.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Collections; @@ -47,8 +36,7 @@ public final class AddEventStreamDependency implements TypeScriptIntegration { @Override public List runAfter() { return List.of( - new AddBuiltinPlugins().name() - ); + new AddBuiltinPlugins().name()); } @Override @@ -56,10 +44,10 @@ public List getClientPlugins() { return ListUtils.of( RuntimeClientPlugin.builder() .withConventions(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER.dependency, - "EventStreamSerde", RuntimeClientPlugin.Convention.HAS_CONFIG) + "EventStreamSerde", + RuntimeClientPlugin.Convention.HAS_CONFIG) .servicePredicate(AddEventStreamDependency::hasEventStream) - .build() - ); + .build()); } @Override @@ -74,7 +62,8 @@ public void addConfigInterfaceFields( } writer.addDependency(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_CONFIG_RESOLVER); - writer.addTypeImport("EventStreamSerdeProvider", "__EventStreamSerdeProvider", + writer.addTypeImport("EventStreamSerdeProvider", + "__EventStreamSerdeProvider", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs("The function that provides necessary utilities for generating and parsing event stream"); writer.write("eventStreamSerdeProvider?: __EventStreamSerdeProvider;\n"); @@ -94,14 +83,16 @@ public Map> getRuntimeConfigWriters( case NODE: return MapUtils.of("eventStreamSerdeProvider", writer -> { writer.addDependency(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_NODE); - writer.addImport("eventStreamSerdeProvider", null, + writer.addImport("eventStreamSerdeProvider", + null, TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_NODE); writer.write("eventStreamSerdeProvider"); }); case BROWSER: return MapUtils.of("eventStreamSerdeProvider", writer -> { writer.addDependency(TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_BROWSER); - writer.addImport("eventStreamSerdeProvider", null, + writer.addImport("eventStreamSerdeProvider", + null, TypeScriptDependency.AWS_SDK_EVENTSTREAM_SERDE_BROWSER); writer.write("eventStreamSerdeProvider"); }); @@ -112,7 +103,7 @@ public Map> getRuntimeConfigWriters( @Override public List> interceptors( - TypeScriptCodegenContext codegenContext + TypeScriptCodegenContext codegenContext ) { return List.of(CodeInterceptor.appender(SmithyContextCodeSection.class, (w, s) -> { EventStreamIndex eventStreamIndex = EventStreamIndex.of(s.getModel()); @@ -146,8 +137,7 @@ private static boolean hasEventStream( EventStreamIndex eventStreamIndex = EventStreamIndex.of(model); for (OperationShape operation : operations) { if (eventStreamIndex.getInputInfo(operation).isPresent() - || eventStreamIndex.getOutputInfo(operation).isPresent() - ) { + || eventStreamIndex.getOutputInfo(operation).isPresent()) { return true; } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPlugin.java index 71eb494f7c3..86bd7d1d222 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPlugin.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.nio.file.Paths; @@ -60,40 +59,42 @@ public boolean matchesSettings(TypeScriptSettings settings) { @Override public List getClientPlugins() { return ListUtils.of( - // Add the config if the service uses HTTP API key authorization. - RuntimeClientPlugin.builder() - .inputConfig(Symbol.builder() - .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") - .name("HttpApiKeyAuthInputConfig") - .build()) - .resolvedConfig(Symbol.builder() - .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") - .name("HttpApiKeyAuthResolvedConfig") - .build()) - .resolveFunction(Symbol.builder() - .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") - .name("resolveHttpApiKeyAuthConfig") - .build()) - .servicePredicate((m, s) -> hasEffectiveHttpApiKeyAuthTrait(m, s)) - .build(), - - // Add the middleware to operations that use HTTP API key authorization. - RuntimeClientPlugin.builder() - .pluginFunction(Symbol.builder() - .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") - .name("getHttpApiKeyAuthPlugin") - .build()) - .additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap() {{ - // It's safe to do expectTrait() because the operation predicate ensures that the trait - // exists `in` and `name` are required attributes of the trait, `scheme` is optional. - put("in", s.expectTrait(HttpApiKeyAuthTrait.class).getIn().toString()); - put("name", s.expectTrait(HttpApiKeyAuthTrait.class).getName()); - s.expectTrait(HttpApiKeyAuthTrait.class).getScheme().ifPresent(scheme -> - put("scheme", scheme)); - }}) - .operationPredicate((m, s, o) -> hasEffectiveHttpApiKeyAuthTrait(m, s, o)) - .build() - ); + // Add the config if the service uses HTTP API key authorization. + RuntimeClientPlugin.builder() + .inputConfig(Symbol.builder() + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") + .name("HttpApiKeyAuthInputConfig") + .build()) + .resolvedConfig(Symbol.builder() + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") + .name("HttpApiKeyAuthResolvedConfig") + .build()) + .resolveFunction(Symbol.builder() + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") + .name("resolveHttpApiKeyAuthConfig") + .build()) + .servicePredicate((m, s) -> hasEffectiveHttpApiKeyAuthTrait(m, s)) + .build(), + + // Add the middleware to operations that use HTTP API key authorization. + RuntimeClientPlugin.builder() + .pluginFunction(Symbol.builder() + .namespace("./" + CodegenUtils.SOURCE_FOLDER + "/middleware/" + INTEGRATION_NAME, "/") + .name("getHttpApiKeyAuthPlugin") + .build()) + .additionalPluginFunctionParamsSupplier((m, s, o) -> new HashMap() { + { + // It's safe to do expectTrait() because the operation predicate ensures that the trait + // exists `in` and `name` are required attributes of the trait, `scheme` is optional. + put("in", s.expectTrait(HttpApiKeyAuthTrait.class).getIn().toString()); + put("name", s.expectTrait(HttpApiKeyAuthTrait.class).getName()); + s.expectTrait(HttpApiKeyAuthTrait.class) + .getScheme() + .ifPresent(scheme -> put("scheme", scheme)); + } + }) + .operationPredicate((m, s, o) -> hasEffectiveHttpApiKeyAuthTrait(m, s, o)) + .build()); } @Override @@ -110,9 +111,9 @@ public void customize(TypeScriptCodegenContext codegenContext) { } private void writeAdditionalFiles( - TypeScriptSettings settings, - Model model, - BiConsumer> writerFactory + TypeScriptSettings settings, + Model model, + BiConsumer> writerFactory ) { ServiceShape service = settings.getService(model); @@ -130,10 +131,10 @@ private void writeAdditionalFiles( writerFactory.accept( Paths.get(CodegenUtils.SOURCE_FOLDER, "middleware", INTEGRATION_NAME, "index.ts").toString(), writer -> { - writer.addDependency(TypeScriptDependency.AWS_SDK_UTIL_MIDDLEWARE); - String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.ts"); - writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.ts"); - writer.write("$L", source); + writer.addDependency(TypeScriptDependency.AWS_SDK_UTIL_MIDDLEWARE); + String source = IoUtils.readUtf8Resource(getClass(), "http-api-key-auth.ts"); + writer.write("$L$L", noTouchNoticePrefix, "http-api-key-auth.ts"); + writer.write("$L", source); }); } @@ -151,8 +152,8 @@ private void writeAdditionalExports( // If no operations use it, then the service doesn't use it private static boolean hasEffectiveHttpApiKeyAuthTrait( - Model model, - ServiceShape service + Model model, + ServiceShape service ) { ServiceIndex serviceIndex = ServiceIndex.of(model); TopDownIndex topDownIndex = TopDownIndex.of(model); @@ -168,14 +169,15 @@ private static boolean hasEffectiveHttpApiKeyAuthTrait( } private static boolean hasEffectiveHttpApiKeyAuthTrait( - Model model, - ServiceShape service, - OperationShape operation + Model model, + ServiceShape service, + OperationShape operation ) { if (operation.hasTrait(OptionalAuthTrait.class)) { return false; } return ServiceIndex.of(model) - .getEffectiveAuthSchemes(service, operation).containsKey(HttpApiKeyAuthTrait.ID); + .getEffectiveAuthSchemes(service, operation) + .containsKey(HttpApiKeyAuthTrait.ID); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddProtocolConfig.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddProtocolConfig.java index 442f89cde8b..fee95c54983 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddProtocolConfig.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddProtocolConfig.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Collections; @@ -20,7 +19,6 @@ import software.amazon.smithy.utils.MapUtils; import software.amazon.smithy.utils.SmithyInternalApi; - /** * Adds a protocol implementation to the runtime config. */ @@ -29,10 +27,10 @@ public final class AddProtocolConfig implements TypeScriptIntegration { @Override public void addConfigInterfaceFields( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - TypeScriptWriter writer + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + TypeScriptWriter writer ) { // the {{ protocol?: Protocol }} type field is provided // by the smithy client config interface. @@ -41,27 +39,27 @@ public void addConfigInterfaceFields( } writer - .addTypeImport("ClientProtocol", null, TypeScriptDependency.SMITHY_TYPES) - .addTypeImport("HttpRequest", null, TypeScriptDependency.SMITHY_TYPES) - .addTypeImport("HttpResponse", null, TypeScriptDependency.SMITHY_TYPES) - .writeDocs(""" - The protocol controlling the message type (e.g. HTTP) and format (e.g. JSON) - may be overridden. A default will always be set by the client. - Available options depend on the service's supported protocols and will not be validated by - the client. - @alpha - """) - .write(""" - protocol?: ClientProtocol; - """); + .addTypeImport("ClientProtocol", null, TypeScriptDependency.SMITHY_TYPES) + .addTypeImport("HttpRequest", null, TypeScriptDependency.SMITHY_TYPES) + .addTypeImport("HttpResponse", null, TypeScriptDependency.SMITHY_TYPES) + .writeDocs(""" + The protocol controlling the message type (e.g. HTTP) and format (e.g. JSON) + may be overridden. A default will always be set by the client. + Available options depend on the service's supported protocols and will not be validated by + the client. + @alpha + """) + .write(""" + protocol?: ClientProtocol; + """); } @Override public Map> getRuntimeConfigWriters( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - LanguageTarget target + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target ) { if (!SchemaGenerationAllowlist.allows(settings.getService(), settings)) { return Collections.emptyMap(); @@ -73,13 +71,15 @@ public Map> getRuntimeConfigWriters( case SHARED: if (Objects.equals(settings.getProtocol(), Rpcv2CborTrait.ID)) { return MapUtils.of( - "protocol", writer -> { - writer.addImportSubmodule( - "SmithyRpcV2CborProtocol", null, - TypeScriptDependency.SMITHY_CORE, "/cbor"); - writer.write("new SmithyRpcV2CborProtocol({ defaultNamespace: $S })", namespace); - } - ); + "protocol", + writer -> { + writer.addImportSubmodule( + "SmithyRpcV2CborProtocol", + null, + TypeScriptDependency.SMITHY_CORE, + "/cbor"); + writer.write("new SmithyRpcV2CborProtocol({ defaultNamespace: $S })", namespace); + }); } case BROWSER: case NODE: diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddSdkStreamMixinDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddSdkStreamMixinDependency.java index cb38de0d5ff..19a69b181b1 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddSdkStreamMixinDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddSdkStreamMixinDependency.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Collections; @@ -45,16 +34,17 @@ public final class AddSdkStreamMixinDependency implements TypeScriptIntegration @Override public void addConfigInterfaceFields( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - TypeScriptWriter writer + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + TypeScriptWriter writer ) { if (!hasStreamingBlobDeser(settings, model)) { return; } - writer.addTypeImport("SdkStreamMixinInjector", "__SdkStreamMixinInjector", + writer.addTypeImport("SdkStreamMixinInjector", + "__SdkStreamMixinInjector", TypeScriptDependency.SMITHY_TYPES); writer.writeDocs("The internal function that inject utilities to runtime-specific stream to help users" + " consume the data\n@internal"); @@ -63,21 +53,21 @@ public void addConfigInterfaceFields( @Override public Map> getRuntimeConfigWriters( - TypeScriptSettings settings, - Model model, - SymbolProvider symbolProvider, - LanguageTarget target + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target ) { if (!hasStreamingBlobDeser(settings, model)) { return Collections.emptyMap(); } if (target == LanguageTarget.SHARED) { - return MapUtils.of("sdkStreamMixin", writer -> { - writer.addDependency(TypeScriptDependency.UTIL_STREAM); - writer.addImport("sdkStreamMixin", null, TypeScriptDependency.UTIL_STREAM); - writer.write("sdkStreamMixin"); - }); + return MapUtils.of("sdkStreamMixin", writer -> { + writer.addDependency(TypeScriptDependency.UTIL_STREAM); + writer.addImport("sdkStreamMixin", null, TypeScriptDependency.UTIL_STREAM); + writer.write("sdkStreamMixin"); + }); } else { return Collections.emptyMap(); } @@ -97,8 +87,8 @@ private static boolean hasStreamingBlobDeser(TypeScriptSettings settings, Model public static boolean hasStreamingBlobDeser(TypeScriptSettings settings, Model model, OperationShape operation) { StructureShape ioShapeToDeser = (settings.generateServerSdk()) - ? model.expectShape(operation.getInputShape()).asStructureShape().get() - : model.expectShape(operation.getOutputShape()).asStructureShape().get(); + ? model.expectShape(operation.getInputShape()).asStructureShape().get() + : model.expectShape(operation.getOutputShape()).asStructureShape().get(); for (MemberShape member : ioShapeToDeser.members()) { Shape shape = model.expectShape(member.getTarget()); if (shape instanceof BlobShape && shape.hasTrait(StreamingTrait.class)) { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DefaultReadmeGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DefaultReadmeGenerator.java index e4cabc3b1f5..db7b1c35676 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DefaultReadmeGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DefaultReadmeGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Arrays; @@ -51,7 +40,7 @@ public void customize(TypeScriptCodegenContext codegenContext) { codegenContext.writerDelegator().useFileWriter(README_FILENAME, "", writer -> { ServiceShape service = settings.getService(model); - String resource = IoUtils.readUtf8Resource(getClass(), file); + String resource = IoUtils.readUtf8Resource(getClass(), file); resource = resource.replaceAll(Pattern.quote("${packageName}"), settings.getPackageName()); String clientName = StringUtils.capitalize(service.getId().getName(service)); @@ -61,7 +50,8 @@ public void customize(TypeScriptCodegenContext codegenContext) { String rawDocumentation = service.getTrait(DocumentationTrait.class) .map(DocumentationTrait::getValue) .orElse(""); - String documentation = Arrays.asList(rawDocumentation.split("\n")).stream() + String documentation = Arrays.asList(rawDocumentation.split("\n")) + .stream() .map(StringUtils::trim) .collect(Collectors.joining("\n")); resource = resource.replaceAll(Pattern.quote("${documentation}"), Matcher.quoteReplacement(documentation)); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitor.java index ffb3f32e15c..cd124d46d36 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import software.amazon.smithy.codegen.core.CodegenException; @@ -175,15 +164,19 @@ public String longShape(LongShape shape) { @Override public String floatShape(FloatShape shape) { - context.getWriter().addImport("limitedParseFloat32", "__limitedParseFloat32", - TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport("limitedParseFloat32", + "__limitedParseFloat32", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__limitedParseFloat32(" + dataSource + ")"; } @Override public String doubleShape(DoubleShape shape) { - context.getWriter().addImport("limitedParseDouble", "__limitedParseDouble", - TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport("limitedParseDouble", + "__limitedParseDouble", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__limitedParseDouble(" + dataSource + ")"; } @@ -238,7 +231,8 @@ public String timestampShape(TimestampShape shape) { if (!shape.getId().equals(getMemberShape().getTarget())) { throw new IllegalArgumentException( String.format("Encountered timestamp shape %s that was not the target of member shape %s", - shape.getId(), getMemberShape().getId())); + shape.getId(), + getMemberShape().getId())); } format = httpIndex.determineTimestampFormat(getMemberShape(), Location.DOCUMENT, defaultTimestampFormat); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitor.java index 1f6cd08b9e6..38b39558620 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import software.amazon.smithy.codegen.core.CodegenException; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeDeserVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeDeserVisitor.java index 65166a59fe4..a8ac49d4916 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeDeserVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeDeserVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Set; @@ -316,9 +305,9 @@ protected final void generateDeserFunction( writer.addImport(symbol, symbol.getName()); writer.writeDocs(methodLongName); writer.openBlock("const $L = (\n" - + " output: any,\n" - + " context: __SerdeContext\n" - + "): $T => {", "}", methodName, symbol, () -> functionBody.accept(context, shape)); + + " output: any,\n" + + " context: __SerdeContext\n" + + "): $T => {", "}", methodName, symbol, () -> functionBody.accept(context, shape)); writer.write(""); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeSerVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeSerVisitor.java index c1f4f3b2533..aeb30bd94a7 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeSerVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeSerVisitor.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Set; @@ -312,9 +301,9 @@ private void generateSerFunction( } else { writer.writeDocs(methodLongName); writer.openBlock("const $L = (\n" - + " input: $T,\n" - + " context: __SerdeContext\n" - + "): any => {", "}", methodName, symbol, () -> functionBody.accept(context, shape)); + + " input: $T,\n" + + " context: __SerdeContext\n" + + "): any => {", "}", methodName, symbol, () -> functionBody.accept(context, shape)); writer.write(""); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java index e2449272e2a..67f4f0c9790 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.List; @@ -84,13 +73,14 @@ public static UnionShape getEventStreamOutputShape(GenerationContext context, Op public static MemberShape getEventStreamMember(GenerationContext context, StructureShape struct) { List eventStreamMembers = struct.members() - .stream() - .filter(shape -> { - Shape target = context.getModel().expectShape(shape.getTarget()); - boolean targetStreaming = target.hasTrait(StreamingTrait.class); - boolean targetUnion = target.isUnionShape(); - return targetUnion && targetStreaming; - }).toList(); + .stream() + .filter(shape -> { + Shape target = context.getModel().expectShape(shape.getTarget()); + boolean targetStreaming = target.hasTrait(StreamingTrait.class); + boolean targetUnion = target.isUnionShape(); + return targetUnion && targetStreaming; + }) + .toList(); if (eventStreamMembers.isEmpty()) { throw new CodegenException("No event stream member found in " + struct.getId().toString()); @@ -111,11 +101,11 @@ public static MemberShape getEventStreamMember(GenerationContext context, Struct * Shapes that referred by event will be added. */ public void generateEventStreamSerializers( - GenerationContext context, - ServiceShape service, - String documentContentType, - Runnable serializeInputEventDocumentPayload, - Set documentShapesToSerialize + GenerationContext context, + ServiceShape service, + String documentContentType, + Runnable serializeInputEventDocumentPayload, + Set documentShapesToSerialize ) { Model model = context.getModel(); @@ -123,20 +113,18 @@ public void generateEventStreamSerializers( Set operations = topDownIndex.getContainedOperations(service); TreeSet eventUnionsToSerialize = new TreeSet<>(); TreeSet> eventShapesToMarshall = new TreeSet<>( - (a, b) -> Objects.compare(a.getRight(), b.getRight(), StructureShape::compareTo) - ); + (a, b) -> Objects.compare(a.getRight(), b.getRight(), StructureShape::compareTo)); for (OperationShape operation : operations) { if (hasEventStreamInput(context, operation)) { UnionShape eventsUnion = getEventStreamInputShape(context, operation); eventUnionsToSerialize.add(eventsUnion); eventsUnion.members() - .forEach(member -> { - eventShapesToMarshall.add(Pair.of( - member.getMemberName(), - model.expectShape(member.getTarget()).asStructureShape().get() - )); - }); + .forEach(member -> { + eventShapesToMarshall.add(Pair.of( + member.getMemberName(), + model.expectShape(member.getTarget()).asStructureShape().get())); + }); } } @@ -146,14 +134,13 @@ public void generateEventStreamSerializers( SerdeElisionIndex serdeElisionIndex = SerdeElisionIndex.of(model); eventShapesToMarshall.forEach(memberNameAndEvent -> { generateEventMarshaller( - context, - memberNameAndEvent.getLeft(), - memberNameAndEvent.getRight(), - documentContentType, - serializeInputEventDocumentPayload, - documentShapesToSerialize, - serdeElisionIndex - ); + context, + memberNameAndEvent.getLeft(), + memberNameAndEvent.getRight(), + documentContentType, + serializeInputEventDocumentPayload, + documentShapesToSerialize, + serdeElisionIndex); }); } @@ -169,13 +156,13 @@ public void generateEventStreamSerializers( * load an error code. */ public void generateEventStreamDeserializers( - GenerationContext context, - ServiceShape service, - Set errorShapesToDeserialize, - Set eventShapesToDeserialize, - boolean isErrorCodeInBody, - boolean serdeElisionEnabled, - SerdeElisionIndex serdeElisionIndex + GenerationContext context, + ServiceShape service, + Set errorShapesToDeserialize, + Set eventShapesToDeserialize, + boolean isErrorCodeInBody, + boolean serdeElisionEnabled, + SerdeElisionIndex serdeElisionIndex ) { Model model = context.getModel(); @@ -188,7 +175,8 @@ public void generateEventStreamDeserializers( if (hasEventStreamOutput(context, operation)) { UnionShape eventsUnion = getEventStreamOutputShape(context, operation); eventUnionsToDeserialize.add(eventsUnion); - Set eventShapes = eventsUnion.members().stream() + Set eventShapes = eventsUnion.members() + .stream() .map(member -> model.expectShape(member.getTarget()).asStructureShape().get()) .collect(Collectors.toSet()); eventShapes.forEach(eventShapesToUnmarshall::add); @@ -200,14 +188,13 @@ public void generateEventStreamDeserializers( }); eventShapesToUnmarshall.forEach(event -> { generateEventUnmarshaller( - context, - event, - errorShapesToDeserialize, - eventShapesToDeserialize, - isErrorCodeInBody, - serdeElisionEnabled, - serdeElisionIndex - ); + context, + event, + errorShapesToDeserialize, + eventShapesToDeserialize, + isErrorCodeInBody, + serdeElisionEnabled, + serdeElisionIndex); }); } @@ -222,22 +209,24 @@ private void generateEventStreamSerializer(GenerationContext context, UnionShape writer.writeDocs(methodLongName); writer.openBlock(""" - const $L = ( - input: any, - context: $L - ): any => {""", "}", methodName, getEventStreamSerdeContextType(context, eventsUnion), () -> { + const $L = ( + input: any, + context: $L + ): any => {""", "}", methodName, getEventStreamSerdeContextType(context, eventsUnion), () -> { Symbol materializedSymbol = eventsUnionSymbol.toBuilder() - .putProperty("typeOnly", false) - .build(); - writer.openBlock("const eventMarshallingVisitor = (event: any): __Message => $T.visit(event, {", "});", - materializedSymbol, () -> { - eventsUnion.getAllMembers().forEach((memberName, memberShape) -> { - StructureShape target = model.expectShape(memberShape.getTarget(), StructureShape.class); - String eventSerMethodName = getEventSerFunctionName(context, target); - writer.write("$L: value => $L(value, context),", memberName, eventSerMethodName); + .putProperty("typeOnly", false) + .build(); + writer.openBlock("const eventMarshallingVisitor = (event: any): __Message => $T.visit(event, {", + "});", + materializedSymbol, + () -> { + eventsUnion.getAllMembers().forEach((memberName, memberShape) -> { + StructureShape target = model.expectShape(memberShape.getTarget(), StructureShape.class); + String eventSerMethodName = getEventSerFunctionName(context, target); + writer.write("$L: value => $L(value, context),", memberName, eventSerMethodName); + }); + writer.write("_: value => value as any"); }); - writer.write("_: value => value as any"); - }); writer.write("return context.eventStreamMarshaller.serialize(input, eventMarshallingVisitor);"); }); } @@ -256,7 +245,8 @@ private String getEventStreamSerdeContextType(GenerationContext context, UnionSh writer.addTypeImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES); String contextType = "__SerdeContext"; if (eventsUnion.hasTrait(StreamingTrait.class)) { - writer.addTypeImport("EventStreamSerdeContext", "__EventStreamSerdeContext", + writer.addTypeImport("EventStreamSerdeContext", + "__EventStreamSerdeContext", TypeScriptDependency.SMITHY_TYPES); contextType += " & __EventStreamSerdeContext"; } @@ -269,13 +259,13 @@ private Symbol getSymbol(GenerationContext context, Shape shape) { } public void generateEventMarshaller( - GenerationContext context, - String memberName, - StructureShape event, - String documentContentType, - Runnable serializeInputEventDocumentPayload, - Set documentShapesToSerialize, - SerdeElisionIndex serdeElisionIndex + GenerationContext context, + String memberName, + StructureShape event, + String documentContentType, + Runnable serializeInputEventDocumentPayload, + Set documentShapesToSerialize, + SerdeElisionIndex serdeElisionIndex ) { String methodName = getEventSerFunctionName(context, event); Symbol symbol = getSymbol(context, event); @@ -285,23 +275,26 @@ public void generateEventMarshaller( + " input: $T,\n" + " context: __SerdeContext\n" + "): __Message => {", "}", methodName, symbol, () -> { - writer.openBlock("const headers: __MessageHeaders = {", "}", () -> { - //fix headers required by event stream - writer.write("\":event-type\": { type: \"string\", value: $S },", memberName); - writer.write("\":message-type\": { type: \"string\", value: \"event\" },"); - writeEventContentTypeHeader(context, event, documentContentType); - }); - writeEventHeaders(context, event); - writeEventBody(context, event, serializeInputEventDocumentPayload, - documentShapesToSerialize, serdeElisionIndex); - writer.openBlock("return { headers, body };"); - }); + writer.openBlock("const headers: __MessageHeaders = {", "}", () -> { + //fix headers required by event stream + writer.write("\":event-type\": { type: \"string\", value: $S },", memberName); + writer.write("\":message-type\": { type: \"string\", value: \"event\" },"); + writeEventContentTypeHeader(context, event, documentContentType); + }); + writeEventHeaders(context, event); + writeEventBody(context, + event, + serializeInputEventDocumentPayload, + documentShapesToSerialize, + serdeElisionIndex); + writer.openBlock("return { headers, body };"); + }); } private void writeEventContentTypeHeader( - GenerationContext context, - StructureShape event, - String documentContentType + GenerationContext context, + StructureShape event, + String documentContentType ) { TypeScriptWriter writer = context.getWriter(); Optional payloadMemberOptional = getEventPayloadMember(event); @@ -321,19 +314,24 @@ private void writeEventContentTypeHeader( } private Optional getEventPayloadMember(StructureShape event) { - List payloadMembers = event.getAllMembers().values().stream() + List payloadMembers = event.getAllMembers() + .values() + .stream() .filter(member -> member.hasTrait(EventPayloadTrait.class)) .collect(Collectors.toList()); return payloadMembers.isEmpty() - ? Optional.empty() // implicit payload - : Optional.of(payloadMembers.get(0)); + ? Optional.empty() // implicit payload + : Optional.of(payloadMembers.get(0)); } private void writeEventHeaders(GenerationContext context, StructureShape event) { TypeScriptWriter writer = context.getWriter(); Model model = context.getModel(); - List headerMembers = event.getAllMembers().values().stream() - .filter(member -> member.hasTrait(EventHeaderTrait.class)).collect(Collectors.toList()); + List headerMembers = event.getAllMembers() + .values() + .stream() + .filter(member -> member.hasTrait(EventHeaderTrait.class)) + .collect(Collectors.toList()); for (MemberShape headerMember : headerMembers) { String memberName = headerMember.getMemberName(); Shape target = model.expectShape(headerMember.getTarget()); @@ -341,12 +339,12 @@ private void writeEventHeaders(GenerationContext context, StructureShape event) if (target.isLongShape()) { writer.addImport("Int64", "__Int64", TypeScriptDependency.AWS_SDK_EVENTSTREAM_CODEC); writer.write("headers[$1S] = { type: $2S, value: __Int64.fromNumber(input.$1L) }", - memberName, - getEventHeaderType(target) - ); + memberName, + getEventHeaderType(target)); } else { - writer.write("headers[$1S] = { type: $2S, value: input.$1L }", memberName, - getEventHeaderType(target)); + writer.write("headers[$1S] = { type: $2S, value: input.$1L }", + memberName, + getEventHeaderType(target)); } }); } @@ -376,17 +374,20 @@ private String getEventHeaderType(Shape shape) { * If the event has a member that has an explicit eventPayload trait, return the member. */ private MemberShape getExplicitEventPayloadMember(StructureShape event) { - return event.getAllMembers().values().stream() - .filter(member -> member.hasTrait(EventPayloadTrait.class)) - .collect(Collectors.toList()).get(0); + return event.getAllMembers() + .values() + .stream() + .filter(member -> member.hasTrait(EventPayloadTrait.class)) + .collect(Collectors.toList()) + .get(0); } private void writeEventBody( - GenerationContext context, - StructureShape event, - Runnable serializeInputEventDocumentPayload, - Set documentShapesToSerialize, - SerdeElisionIndex serdeElisionIndex + GenerationContext context, + StructureShape event, + Runnable serializeInputEventDocumentPayload, + Set documentShapesToSerialize, + SerdeElisionIndex serdeElisionIndex ) { TypeScriptWriter writer = context.getWriter(); Optional payloadMemberOptional = getEventPayloadMember(event); @@ -413,7 +414,7 @@ private void writeEventBody( serializeInputEventDocumentPayload.run(); } else { throw new CodegenException(String.format("Unexpected shape type bound to event payload: `%s`", - payloadShape.getType())); + payloadShape.getType())); } }); } else { @@ -451,22 +452,24 @@ private void generateEventStreamDeserializer(GenerationContext context, UnionSha + " output: any,\n" + " context: $L\n" + "): AsyncIterable<$T> => {", "}", methodName, contextType, eventsUnionSymbol, () -> { - writer.openBlock("return context.eventStreamMarshaller.deserialize(", ");", () -> { - writer.write("output,"); - writer.openBlock("async event => {", "}", () -> { - eventsUnion.getAllMembers().forEach((name, member) -> { - StructureShape event = model.expectShape(member.getTarget(), StructureShape.class); - writer.openBlock("if (event[$S] != null) {", "}", name, () -> { - writer.openBlock("return {", "};", () -> { - String eventDeserMethodName = getEventDeserFunctionName(context, event); - writer.write("$1L: await $2L(event[$1S], context),", name, eventDeserMethodName); + writer.openBlock("return context.eventStreamMarshaller.deserialize(", ");", () -> { + writer.write("output,"); + writer.openBlock("async event => {", "}", () -> { + eventsUnion.getAllMembers().forEach((name, member) -> { + StructureShape event = model.expectShape(member.getTarget(), StructureShape.class); + writer.openBlock("if (event[$S] != null) {", "}", name, () -> { + writer.openBlock("return {", "};", () -> { + String eventDeserMethodName = getEventDeserFunctionName(context, event); + writer.write("$1L: await $2L(event[$1S], context),", + name, + eventDeserMethodName); + }); + }); }); + writer.write("return {$$unknown: event as any};"); }); }); - writer.write("return {$$unknown: event as any};"); }); - }); - }); } private String getDeserFunctionName(GenerationContext context, Shape shape) { @@ -479,13 +482,13 @@ public String getEventDeserFunctionName(GenerationContext context, Shape shape) } public void generateEventUnmarshaller( - GenerationContext context, - StructureShape event, - Set errorShapesToDeserialize, - Set eventShapesToDeserialize, - boolean isErrorCodeInBody, - boolean serdeElisionEnabled, - SerdeElisionIndex serdeElisionIndex + GenerationContext context, + StructureShape event, + Set errorShapesToDeserialize, + Set eventShapesToDeserialize, + boolean isErrorCodeInBody, + boolean serdeElisionEnabled, + SerdeElisionIndex serdeElisionIndex ) { String methodName = getEventDeserFunctionName(context, event); Symbol symbol = getSymbol(context, event); @@ -494,23 +497,23 @@ public void generateEventUnmarshaller( + " output: any,\n" + " context: __SerdeContext\n" + "): Promise<$T> => {", "}", methodName, symbol, () -> { - if (event.hasTrait(ErrorTrait.class)) { - generateErrorEventUnmarshaller(context, event, errorShapesToDeserialize, isErrorCodeInBody); - } else { - writer.write("const contents: $L = {} as any;", symbol.getName()); - readEventHeaders(context, event); - readEventBody(context, event, eventShapesToDeserialize, serdeElisionEnabled, serdeElisionIndex); - writer.write("return contents;"); - } - }); + if (event.hasTrait(ErrorTrait.class)) { + generateErrorEventUnmarshaller(context, event, errorShapesToDeserialize, isErrorCodeInBody); + } else { + writer.write("const contents: $L = {} as any;", symbol.getName()); + readEventHeaders(context, event); + readEventBody(context, event, eventShapesToDeserialize, serdeElisionEnabled, serdeElisionIndex); + writer.write("return contents;"); + } + }); } // Writes function content that unmarshall error event with error deserializer private void generateErrorEventUnmarshaller( - GenerationContext context, - StructureShape event, - Set errorShapesToDeserialize, - boolean isErrorCodeInBody + GenerationContext context, + StructureShape event, + Set errorShapesToDeserialize, + boolean isErrorCodeInBody ) { TypeScriptWriter writer = context.getWriter(); // If this is an error event, we need to generate the error deserializer. @@ -519,7 +522,8 @@ private void generateErrorEventUnmarshaller( if (isErrorCodeInBody) { // If error code is in body, parseBody() won't be called inside error deser. So we parse body here. // It's ok to parse body here because body won't be streaming if 'isErrorCodeInBody' is set. - writer.openBlock("const parsedOutput: any = {", "};", + writer.openBlock("const parsedOutput: any = {", + "};", () -> { writer.write("...output,"); writer.write("body: await parseBody(output.body, context)"); @@ -533,29 +537,31 @@ private void generateErrorEventUnmarshaller( // Parse members from event headers. private void readEventHeaders(GenerationContext context, StructureShape event) { TypeScriptWriter writer = context.getWriter(); - List headerMembers = event.getAllMembers().values().stream() - .filter(member -> member.hasTrait(EventHeaderTrait.class)).toList(); + List headerMembers = event.getAllMembers() + .values() + .stream() + .filter(member -> member.hasTrait(EventHeaderTrait.class)) + .toList(); for (MemberShape headerMember : headerMembers) { String memberName = headerMember.getMemberName(); String varName = context.getStringStore().var(memberName); writer.write( - """ - if (output.headers[$1L] !== undefined) { - contents[$1L] = output.headers[$1L].value; - } - """, - varName - ); + """ + if (output.headers[$1L] !== undefined) { + contents[$1L] = output.headers[$1L].value; + } + """, + varName); } } private void readEventBody( - GenerationContext context, - StructureShape event, - Set eventShapesToDeserialize, - boolean serdeElisionEnabled, - SerdeElisionIndex serdeElisionIndex + GenerationContext context, + StructureShape event, + Set eventShapesToDeserialize, + boolean serdeElisionEnabled, + SerdeElisionIndex serdeElisionIndex ) { TypeScriptWriter writer = context.getWriter(); Optional payloadmemberOptional = getEventPayloadMember(event); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java index 010728bfd40..32a8c9dcbb2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.nio.file.Paths; @@ -87,10 +76,22 @@ public abstract class HttpBindingProtocolGenerator implements ProtocolGenerator { private static final Logger LOGGER = Logger.getLogger(HttpBindingProtocolGenerator.class.getName()); - private static final Set REGEX_CHARS = SetUtils.of('.', '*', '+', '?', '^', '$', '{', '}', '(', - ')', '|', '[', ']', '\\'); - private static final ApplicationProtocol APPLICATION_PROTOCOL - = ApplicationProtocol.createDefaultHttpApplicationProtocol(); + private static final Set REGEX_CHARS = SetUtils.of('.', + '*', + '+', + '?', + '^', + '$', + '{', + '}', + '(', + ')', + '|', + '[', + ']', + '\\'); + private static final ApplicationProtocol APPLICATION_PROTOCOL = + ApplicationProtocol.createDefaultHttpApplicationProtocol(); private final Set serializingDocumentShapes = new TreeSet<>(); private final Set deserializingDocumentShapes = new TreeSet<>(); private final Set serializingErrorShapes = new TreeSet<>(); @@ -174,26 +175,24 @@ public void generateSharedComponents(GenerationContext context) { serializingErrorShapes.forEach(error -> generateErrorSerializer(context, error)); ServiceShape service = context.getService(); eventStreamGenerator.generateEventStreamSerializers( - context, - service, - getDocumentContentType(), - () -> { - this.serializeInputEventDocumentPayload(context); - }, - serializingDocumentShapes - ); + context, + service, + getDocumentContentType(), + () -> { + this.serializeInputEventDocumentPayload(context); + }, + serializingDocumentShapes); SerdeElisionIndex serdeElisionIndex = SerdeElisionIndex.of(context.getModel()); // Error shapes that only referred in the error event of an eventstream Set errorEventShapes = new TreeSet<>(); eventStreamGenerator.generateEventStreamDeserializers( - context, - service, - errorEventShapes, - deserializingDocumentShapes, - isErrorCodeInBody, - enableSerdeElision(), - serdeElisionIndex - ); + context, + service, + errorEventShapes, + deserializingDocumentShapes, + isErrorCodeInBody, + enableSerdeElision(), + serdeElisionIndex); errorEventShapes.removeIf(deserializingErrorShapes::contains); errorEventShapes.forEach(error -> generateErrorDeserializer(context, error)); generateDocumentBodyShapeSerializers(context, serializingDocumentShapes); @@ -202,8 +201,7 @@ public void generateSharedComponents(GenerationContext context) { HttpProtocolGeneratorUtils.generateCollectBodyString(context); writer.write( - context.getStringStore().flushVariableDeclarationCode() - ); + context.getStringStore().flushVariableDeclarationCode()); writer.addImport("HttpRequest", "__HttpRequest", TypeScriptDependency.PROTOCOL_HTTP); writer.addImport("HttpResponse", "__HttpResponse", TypeScriptDependency.PROTOCOL_HTTP); @@ -221,7 +219,9 @@ public void generateRequestSerializers(GenerationContext context) { httpTrait -> generateOperationRequestSerializer(context, operation, httpTrait), () -> LOGGER.warning(String.format( "Unable to generate %s protocol request bindings for %s because it does not have an " - + "http binding trait", getName(), operation.getId()))); + + "http binding trait", + getName(), + operation.getId()))); } } @@ -237,7 +237,9 @@ public void generateRequestDeserializers(GenerationContext context) { httpTrait -> generateOperationRequestDeserializer(context, operation, httpTrait), () -> LOGGER.warning(String.format( "Unable to generate %s protocol request bindings for %s because it does not have an " - + "http binding trait", getName(), operation.getId()))); + + "http binding trait", + getName(), + operation.getId()))); } } @@ -254,7 +256,9 @@ public void generateResponseSerializers(GenerationContext context) { httpTrait -> generateOperationResponseSerializer(context, operation, httpTrait), () -> LOGGER.warning(String.format( "Unable to generate %s protocol response bindings for %s because it does not have an " - + "http binding trait", getName(), operation.getId()))); + + "http binding trait", + getName(), + operation.getId()))); } } @@ -276,17 +280,22 @@ public void generateFrameworkErrorSerializer(GenerationContext inputContext) { + " ctx: ServerSerdeContext\n" + "): Promise<$T> => {", "}", responseType, () -> { - writeEmptyEndpoint(context); - - writer.openBlock("switch (input.name) {", "}", () -> { - for (final Shape shape : new TreeSet<>(context.getModel().getShapesWithTrait(HttpErrorTrait.class))) { - StructureShape errorShape = shape.asStructureShape().orElseThrow(IllegalArgumentException::new); - writer.openBlock("case $S: {", "}", errorShape.getId().getName(), () -> { - generateErrorSerializationImplementation(context, errorShape, responseType, bindingIndex); + writeEmptyEndpoint(context); + + writer.openBlock("switch (input.name) {", "}", () -> { + for (final Shape shape : new TreeSet<>( + context.getModel().getShapesWithTrait(HttpErrorTrait.class))) { + StructureShape errorShape = + shape.asStructureShape().orElseThrow(IllegalArgumentException::new); + writer.openBlock("case $S: {", "}", errorShape.getId().getName(), () -> { + generateErrorSerializationImplementation(context, + errorShape, + responseType, + bindingIndex); + }); + } }); - } - }); - }); + }); writer.write(""); } @@ -298,7 +307,8 @@ private void generateServiceMux(GenerationContext context) { Symbol serviceSymbol = context.getSymbolProvider().toSymbol(context.getService()); - writer.openBlock("const mux = new httpbinding.HttpBindingMux<$S, keyof $T>([", "]);", + writer.openBlock("const mux = new httpbinding.HttpBindingMux<$S, keyof $T>([", + "]);", context.getService().getId().getName(), serviceSymbol, () -> { @@ -308,11 +318,12 @@ private void generateServiceMux(GenerationContext context) { httpTrait -> generateUriSpec(context, operation, httpTrait), () -> LOGGER.warning(String.format( "Unable to generate %s uri spec for %s because it does not have an " - + "http binding trait", getName(), operation.getId()))); + + "http binding trait", + getName(), + operation.getId()))); } - } - ); + }); } private void generateOperationMux(GenerationContext context, OperationShape operation) { @@ -320,25 +331,28 @@ private void generateOperationMux(GenerationContext context, OperationShape oper writer.addImport("httpbinding", null, TypeScriptDependency.SERVER_COMMON); - writer.openBlock("const mux = new httpbinding.HttpBindingMux<$S, $S>([", "]);", + writer.openBlock("const mux = new httpbinding.HttpBindingMux<$S, $S>([", + "]);", context.getService().getId().getName(), context.getSymbolProvider().toSymbol(operation).getName(), () -> { HttpTrait httpTrait = operation.expectTrait(HttpTrait.class); generateUriSpec(context, operation, httpTrait); - } - ); + }); } - private void generateUriSpec(GenerationContext context, - OperationShape operation, - HttpTrait httpTrait) { + private void generateUriSpec( + GenerationContext context, + OperationShape operation, + HttpTrait httpTrait + ) { TypeScriptWriter writer = context.getWriter(); String serviceName = context.getService().getId().getName(); String operationName = context.getSymbolProvider().toSymbol(operation).getName(); - writer.openBlock("new httpbinding.UriSpec<$S, $S>(", "),", + writer.openBlock("new httpbinding.UriSpec<$S, $S>(", + "),", serviceName, operationName, () -> { @@ -360,7 +374,8 @@ private void generateUriSpec(GenerationContext context, writer.write("{ type: 'query_literal', key: $S },", e.getKey()); } else { writer.write("{ type: 'query_literal', key: $S, value: $S },", - e.getKey(), e.getValue()); + e.getKey(), + e.getValue()); } } operation.getInput().ifPresent(inputId -> { @@ -384,9 +399,12 @@ public void generateServiceHandlerFactory(GenerationContext context) { Set operations = index.getContainedOperations(context.getService()); SymbolProvider symbolProvider = context.getSymbolProvider(); - writer.addRelativeImport("serializeFrameworkException", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, PROTOCOLS_FOLDER, - ProtocolGenerator.getSanitizedName(getName()))); + writer.addRelativeImport("serializeFrameworkException", + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + PROTOCOLS_FOLDER, + ProtocolGenerator.getSanitizedName(getName()))); writer.addImport("ValidationCustomizer", "__ValidationCustomizer", TypeScriptDependency.SERVER_COMMON); writer.addImport("HttpRequest", "__HttpRequest", TypeScriptDependency.PROTOCOL_HTTP); writer.addImport("HttpResponse", "__HttpResponse", TypeScriptDependency.PROTOCOL_HTTP); @@ -397,38 +415,43 @@ public void generateServiceHandlerFactory(GenerationContext context) { if (context.getSettings().isDisableDefaultValidation()) { writer.write("export const get$L = (service: $T, " - + "customizer: __ValidationCustomizer<$T>): " - + "__ServiceHandler => {", - handlerSymbol.getName(), serviceSymbol, operationsSymbol); + + "customizer: __ValidationCustomizer<$T>): " + + "__ServiceHandler => {", + handlerSymbol.getName(), + serviceSymbol, + operationsSymbol); } else { writer.write("export const get$L = (service: $T): " - + "__ServiceHandler => {", - handlerSymbol.getName(), serviceSymbol); + + "__ServiceHandler => {", + handlerSymbol.getName(), + serviceSymbol); } writer.indent(); generateServiceMux(context); writer.addImport("ServiceException", "__ServiceException", TypeScriptDependency.SERVER_COMMON); writer.openBlock("const serFn: (op: $1T) => __OperationSerializer<$2T, $1T, __ServiceException> = " - + "(op) => {", "};", operationsSymbol, serviceSymbol, () -> { - writer.openBlock("switch (op) {", "}", () -> { - operations.stream() - .filter(o -> o.getTrait(HttpTrait.class).isPresent()) - .forEach(writeOperationCase(writer, symbolProvider)); - }); - }); + + "(op) => {", "};", operationsSymbol, serviceSymbol, () -> { + writer.openBlock("switch (op) {", "}", () -> { + operations.stream() + .filter(o -> o.getTrait(HttpTrait.class).isPresent()) + .forEach(writeOperationCase(writer, symbolProvider)); + }); + }); if (!context.getSettings().isDisableDefaultValidation()) { - writer.addImport("generateValidationSummary", "__generateValidationSummary", - TypeScriptDependency.SERVER_COMMON); - writer.addImport("generateValidationMessage", "__generateValidationMessage", - TypeScriptDependency.SERVER_COMMON); - writer.openBlock("const customizer: __ValidationCustomizer<$T> = (ctx, failures) => {", "};", - operationsSymbol, - () -> { - writeDefaultValidationCustomizer(writer); - } - ); + writer.addImport("generateValidationSummary", + "__generateValidationSummary", + TypeScriptDependency.SERVER_COMMON); + writer.addImport("generateValidationMessage", + "__generateValidationMessage", + TypeScriptDependency.SERVER_COMMON); + writer.openBlock("const customizer: __ValidationCustomizer<$T> = (ctx, failures) => {", + "};", + operationsSymbol, + () -> { + writeDefaultValidationCustomizer(writer); + }); } writer.write("return new $T(service, mux, serFn, serializeFrameworkException, customizer);", handlerSymbol); @@ -441,9 +464,12 @@ public void generateOperationHandlerFactory(GenerationContext context, Operation TypeScriptWriter writer = context.getWriter(); SymbolProvider symbolProvider = context.getSymbolProvider(); - writer.addImport("serializeFrameworkException", null, - Paths.get(".", CodegenUtils.SOURCE_FOLDER, PROTOCOLS_FOLDER, - ProtocolGenerator.getSanitizedName(getName())).toString()); + writer.addImport("serializeFrameworkException", + null, + Paths.get(".", + CodegenUtils.SOURCE_FOLDER, + PROTOCOLS_FOLDER, + ProtocolGenerator.getSanitizedName(getName())).toString()); writer.addImport("HttpRequest", "__HttpRequest", TypeScriptDependency.PROTOCOL_HTTP); writer.addImport("HttpResponse", "__HttpResponse", TypeScriptDependency.PROTOCOL_HTTP); @@ -455,32 +481,40 @@ public void generateOperationHandlerFactory(GenerationContext context, Operation if (context.getSettings().isDisableDefaultValidation()) { writer.write("export const get$L = (operation: __Operation<$T, $T, Context>, " - + "customizer: __ValidationCustomizer<$S>): " - + "__ServiceHandler => {", - operationHandlerSymbol.getName(), inputType, outputType, operationSymbol.getName()); + + "customizer: __ValidationCustomizer<$S>): " + + "__ServiceHandler => {", + operationHandlerSymbol.getName(), + inputType, + outputType, + operationSymbol.getName()); } else { writer.write("export const get$L = (operation: __Operation<$T, $T, Context>): " - + "__ServiceHandler => {", - operationHandlerSymbol.getName(), inputType, outputType); + + "__ServiceHandler => {", + operationHandlerSymbol.getName(), + inputType, + outputType); } writer.indent(); generateOperationMux(context, operation); if (!context.getSettings().isDisableDefaultValidation()) { - writer.addImport("generateValidationSummary", "__generateValidationSummary", - TypeScriptDependency.SERVER_COMMON); - writer.addImport("generateValidationMessage", "__generateValidationMessage", - TypeScriptDependency.SERVER_COMMON); - writer.openBlock("const customizer: __ValidationCustomizer<$S> = (ctx, failures) => {", "};", - operationSymbol.getName(), - () -> { - writeDefaultValidationCustomizer(writer); - } - ); + writer.addImport("generateValidationSummary", + "__generateValidationSummary", + TypeScriptDependency.SERVER_COMMON); + writer.addImport("generateValidationMessage", + "__generateValidationMessage", + TypeScriptDependency.SERVER_COMMON); + writer.openBlock("const customizer: __ValidationCustomizer<$S> = (ctx, failures) => {", + "};", + operationSymbol.getName(), + () -> { + writeDefaultValidationCustomizer(writer); + }); } writer.write("return new $T(operation, mux, new $T(), serializeFrameworkException, customizer);", - operationHandlerSymbol, serializerType); + operationHandlerSymbol, + serializerType); writer.dedent().write("}"); } @@ -525,20 +559,21 @@ public void generateResponseDeserializers(GenerationContext context) { httpTrait -> generateOperationResponseDeserializer(context, operation, httpTrait), () -> LOGGER.warning(String.format( "Unable to generate %s protocol response bindings for %s because it does not have an " - + "http binding trait", getName(), operation.getId()))); + + "http binding trait", + getName(), + operation.getId()))); } SymbolReference responseType = getApplicationProtocol().getResponseType(); Set errorShapes = HttpProtocolGeneratorUtils.generateUnifiedErrorDispatcher( - context, - containedOperations.stream().toList(), - responseType, - this::writeErrorCodeParser, - isErrorCodeInBody, - this::getErrorBodyLocation, - this::getOperationErrors, - getErrorAliases(context, containedOperations) - ); + context, + containedOperations.stream().toList(), + responseType, + this::writeErrorCodeParser, + isErrorCodeInBody, + this::getErrorBodyLocation, + this::getOperationErrors, + getErrorAliases(context, containedOperations)); deserializingErrorShapes.addAll(errorShapes); } @@ -562,28 +597,30 @@ private void generateOperationResponseSerializer( + " input: $T,\n" + " ctx: ServerSerdeContext\n" + "): Promise<$T> => {", "};", methodName, outputType, responseType, () -> { - writeEmptyEndpoint(context, operation); - writeOperationStatusCode(context, operation, bindingIndex, trait); - writeResponseHeaders(context, operation, bindingIndex, - () -> writeDefaultOutputHeaders(context, operation)); - - List bodyBindings = writeResponseBody(context, operation, bindingIndex); - if (!bodyBindings.isEmpty()) { - // Track all shapes bound to the body so their serializers may be generated. - bodyBindings.stream() - .map(HttpBinding::getMember) - .map(member -> context.getModel().expectShape(member.getTarget())) - .forEach(serializingDocumentShapes::add); - } + writeEmptyEndpoint(context, operation); + writeOperationStatusCode(context, operation, bindingIndex, trait); + writeResponseHeaders(context, + operation, + bindingIndex, + () -> writeDefaultOutputHeaders(context, operation)); + + List bodyBindings = writeResponseBody(context, operation, bindingIndex); + if (!bodyBindings.isEmpty()) { + // Track all shapes bound to the body so their serializers may be generated. + bodyBindings.stream() + .map(HttpBinding::getMember) + .map(member -> context.getModel().expectShape(member.getTarget())) + .forEach(serializingDocumentShapes::add); + } - calculateContentLength(context); + calculateContentLength(context); - writer.openBlock("return new $T({", "});", responseType, () -> { - writer.write("headers,"); - writer.write("body,"); - writer.write("statusCode,"); - }); - }); + writer.openBlock("return new $T({", "});", responseType, () -> { + writer.write("headers,"); + writer.write("body,"); + writer.write("statusCode,"); + }); + }); writer.write(""); serializingErrorShapes.addAll(OperationIndex.of(context.getModel()).getErrors(operation, context.getService())); @@ -595,11 +632,11 @@ private void calculateContentLength(GenerationContext context) { writer.addImport("calculateBodyLength", null, TypeScriptDependency.AWS_SDK_UTIL_BODY_LENGTH_NODE); writer.openBlock("if (body && Object.keys(headers).map((str) => str.toLowerCase())" + ".indexOf('content-length') === -1) {", "}", () -> { - writer.write("const length = calculateBodyLength(body);"); - writer.openBlock("if (length !== undefined) {", "}", () -> { - writer.write("headers = { ...headers, 'content-length': String(length) };"); - }); - }); + writer.write("const length = calculateBodyLength(body);"); + writer.openBlock("if (length !== undefined) {", "}", () -> { + writer.write("headers = { ...headers, 'content-length': String(length) };"); + }); + }); } @@ -618,16 +655,18 @@ private void generateErrorSerializer(GenerationContext context, StructureShape e + " input: $T,\n" + " ctx: ServerSerdeContext\n" + "): Promise<$T> => {", "};", methodName, symbol, responseType, () -> { - writeEmptyEndpoint(context); - generateErrorSerializationImplementation(context, error, responseType, bindingIndex); - }); + writeEmptyEndpoint(context); + generateErrorSerializationImplementation(context, error, responseType, bindingIndex); + }); writer.write(""); } - private void generateErrorSerializationImplementation(GenerationContext context, - StructureShape error, - SymbolReference responseType, - HttpBindingIndex bindingIndex) { + private void generateErrorSerializationImplementation( + GenerationContext context, + StructureShape error, + SymbolReference responseType, + HttpBindingIndex bindingIndex + ) { TypeScriptWriter writer = context.getWriter(); writeErrorStatusCode(context, error); writeResponseHeaders(context, error, bindingIndex, () -> writeDefaultErrorHeaders(context, error)); @@ -650,15 +689,14 @@ private void generateErrorSerializationImplementation(GenerationContext context, private void writeEmptyEndpoint(GenerationContext context) { context.getWriter().write(""" - const context: __SerdeContext = { - ...ctx, - endpoint: () => Promise.resolve({ - protocol: '', - hostname: '', - path: '', - }), - };""" - ); + const context: __SerdeContext = { + ...ctx, + endpoint: () => Promise.resolve({ + protocol: '', + hostname: '', + path: '', + }), + };"""); } private void writeEmptyEndpoint(GenerationContext context, OperationShape operation) { @@ -668,17 +706,17 @@ private void writeEmptyEndpoint(GenerationContext context, OperationShape operat // todo: unsupported SSDK feature. contextType += "& any /*event stream context unsupported in ssdk*/"; } - context.getWriter().write(""" - const context: $L = { - ...ctx, - endpoint: () => Promise.resolve({ - protocol: '', - hostname: '', - path: '', - }), - };""", - contextType - ); + context.getWriter() + .write(""" + const context: $L = { + ...ctx, + endpoint: () => Promise.resolve({ + protocol: '', + hostname: '', + path: '', + }), + };""", + contextType); } private void generateOperationRequestSerializer( @@ -707,48 +745,53 @@ private void generateOperationRequestSerializer( writer.writeDocs(methodLongName); writer.openBlock("export const $L = async (\n" - + " input: $T,\n" - + " context: $L\n" - + "): Promise<$T> => {", "};", - methodName, inputType, contextType, requestType, () -> { - - // Get the hostname, path, port, and scheme from client's resolved endpoint. - // Then construct the request from them. The client's resolved endpoint can - // be default one or supplied by users. - - writer.addDependency(TypeScriptDependency.SMITHY_CORE); - writer.addImport("requestBuilder", "rb", TypeScriptDependency.SMITHY_CORE); - writer.write("const b = rb(input, context);"); - - writeRequestHeaders(context, operation, bindingIndex); - writeResolvedPath(context, operation, bindingIndex, trait); - boolean hasQueryComponents = writeRequestQueryString(context, operation, bindingIndex, trait); - - List bodyBindings = writeRequestBody(context, operation, bindingIndex); - if (!bodyBindings.isEmpty()) { - // Track all shapes bound to the body so their serializers may be generated. - bodyBindings.stream() - .map(HttpBinding::getMember) - .map(member -> context.getModel().expectShape(member.getTarget())) - .filter(shape -> !EventStreamGenerator.isEventStreamShape(shape)) - .forEach(serializingDocumentShapes::add); - } + + " input: $T,\n" + + " context: $L\n" + + "): Promise<$T> => {", + "};", + methodName, + inputType, + contextType, + requestType, + () -> { - boolean hasHostPrefix = operation.hasTrait(EndpointTrait.class); - if (hasHostPrefix) { - HttpProtocolGeneratorUtils.writeHostPrefix(context, operation); - writer.write("b.hn(resolvedHostname);"); - } - writer.write("b.m($S)", trait.getMethod()); - writer.write(".h(headers)"); - if (hasQueryComponents) { - writer.write(".q(query)"); - } - // Always set the body, - writer.write(".b(body);"); + // Get the hostname, path, port, and scheme from client's resolved endpoint. + // Then construct the request from them. The client's resolved endpoint can + // be default one or supplied by users. + + writer.addDependency(TypeScriptDependency.SMITHY_CORE); + writer.addImport("requestBuilder", "rb", TypeScriptDependency.SMITHY_CORE); + writer.write("const b = rb(input, context);"); + + writeRequestHeaders(context, operation, bindingIndex); + writeResolvedPath(context, operation, bindingIndex, trait); + boolean hasQueryComponents = writeRequestQueryString(context, operation, bindingIndex, trait); + + List bodyBindings = writeRequestBody(context, operation, bindingIndex); + if (!bodyBindings.isEmpty()) { + // Track all shapes bound to the body so their serializers may be generated. + bodyBindings.stream() + .map(HttpBinding::getMember) + .map(member -> context.getModel().expectShape(member.getTarget())) + .filter(shape -> !EventStreamGenerator.isEventStreamShape(shape)) + .forEach(serializingDocumentShapes::add); + } - writer.write("return b.build();"); - }); + boolean hasHostPrefix = operation.hasTrait(EndpointTrait.class); + if (hasHostPrefix) { + HttpProtocolGeneratorUtils.writeHostPrefix(context, operation); + writer.write("b.hn(resolvedHostname);"); + } + writer.write("b.m($S)", trait.getMethod()); + writer.write(".h(headers)"); + if (hasQueryComponents) { + writer.write(".q(query)"); + } + // Always set the body, + writer.write(".b(body);"); + + writer.write("return b.build();"); + }); writer.write(""); } @@ -794,25 +837,26 @@ private void writeResolvedPath( List labelBindings = bindingIndex.getRequestBindings(operation, Location.LABEL); final Map contextParams = new RuleSetParameterFinder(context.getService()) - .getContextParams(context.getModel().getShape(operation.getInputShape()).get()); + .getContextParams(context.getModel().getShape(operation.getInputShape()).get()); // Always write the bound path, but only the actual segments. writer.write("b.bp(\"$L\");", - "/" + trait.getUri().getSegments().stream() - .filter(segment -> { - String content = segment.getContent(); - boolean isContextParam = contextParams.containsKey(content); - - // If the endpoint also contains the uri segment, e.g. Bucket, we - // do not want to include it in the operation URI to be resolved. - // We use this logic plus a temporary control-list, since it is not yet known - // how many services and param names will have this issue. - return !(isContextParam - && contextParamDeduplicationParamControlSet.contains(content)); - }) - .map(Segment::toString) - .collect(Collectors.joining("/")) - ); + "/" + trait.getUri() + .getSegments() + .stream() + .filter(segment -> { + String content = segment.getContent(); + boolean isContextParam = contextParams.containsKey(content); + + // If the endpoint also contains the uri segment, e.g. Bucket, we + // do not want to include it in the operation URI to be resolved. + // We use this logic plus a temporary control-list, since it is not yet known + // how many services and param names will have this issue. + return !(isContextParam + && contextParamDeduplicationParamControlSet.contains(content)); + }) + .map(Segment::toString) + .collect(Collectors.joining("/"))); // Handle any label bindings. if (!labelBindings.isEmpty()) { @@ -825,21 +869,19 @@ private void writeResolvedPath( Shape target = model.expectShape(binding.getMember().getTarget()); String labelValueProvider = "() => " + getInputValue( - context, - binding.getLocation(), - "input." + memberName + "!", - binding.getMember(), - target - ); + context, + binding.getLocation(), + "input." + memberName + "!", + binding.getMember(), + target); // Get the correct label to use. Segment uriLabel = uriLabels.stream().filter(s -> s.getContent().equals(memberName)).findFirst().get(); writer.write("b.p('$L', $L, '$L', $L)", - memberName, - labelValueProvider, - uriLabel.toString(), - uriLabel.isGreedyLabel() ? "true" : "false" - ); + memberName, + labelValueProvider, + uriLabel.toString(), + uriLabel.isGreedyLabel() ? "true" : "false"); } } } @@ -893,7 +935,8 @@ private void writeRequestQueryParam( SymbolProvider symbolProvider = context.getSymbolProvider(); String memberName = symbolProvider.toMemberName(binding.getMember()); - writer.addImport("extendedEncodeURIComponent", "__extendedEncodeURIComponent", + writer.addImport("extendedEncodeURIComponent", + "__extendedEncodeURIComponent", TypeScriptDependency.AWS_SMITHY_CLIENT); Shape target = model.expectShape(binding.getMember().getTarget()); @@ -904,27 +947,25 @@ private void writeRequestQueryParam( String idempotencyComponent = ""; if (isIdempotencyToken && !isRequired) { writer - .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); + .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); idempotencyComponent = " ?? generateIdempotencyToken()"; } String memberAssertionComponent = (idempotencyComponent.isEmpty() ? "!" : ""); String queryValue = getInputValue( - context, - binding.getLocation(), - "input[" + context.getStringStore().var(memberName) + "]" + memberAssertionComponent, - binding.getMember(), - target - ); + context, + binding.getLocation(), + "input[" + context.getStringStore().var(memberName) + "]" + memberAssertionComponent, + binding.getMember(), + target); String simpleAccessExpression = "input[" - + context.getStringStore().var(memberName) - + "]" + memberAssertionComponent; + + context.getStringStore().var(memberName) + + "]" + memberAssertionComponent; boolean isSimpleAccessExpression = Objects.equals( - simpleAccessExpression, - queryValue - ); + simpleAccessExpression, + queryValue); writer.addImport("expectNonNull", "__expectNonNull", TypeScriptDependency.AWS_SMITHY_CLIENT); @@ -932,29 +973,27 @@ private void writeRequestQueryParam( String value = isRequired ? "__expectNonNull($L, `" + memberName + "`)" : "$L"; // simple undefined check writer.write( - "[$L]: [," + value + idempotencyComponent + "],", - context.getStringStore().var(binding.getLocationName()), - queryValue - ); + "[$L]: [," + value + idempotencyComponent + "],", + context.getStringStore().var(binding.getLocationName()), + queryValue); } else { if (isRequired) { // __expectNonNull is immediately invoked and not inside a function. writer.write( - "[$L]: [__expectNonNull(input.$L, `$L`) != null, () => $L],", - context.getStringStore().var(binding.getLocationName()), - memberName, - memberName, - queryValue // no idempotency token default for required members + "[$L]: [__expectNonNull(input.$L, `$L`) != null, () => $L],", + context.getStringStore().var(binding.getLocationName()), + memberName, + memberName, + queryValue // no idempotency token default for required members ); } else { // undefined check with lazy eval writer.write( - "[$L]: [() => input.$L !== void 0, () => ($L)$L],", - context.getStringStore().var(binding.getLocationName()), - memberName, - queryValue, - idempotencyComponent - ); + "[$L]: [() => input.$L !== void 0, () => ($L)$L],", + context.getStringStore().var(binding.getLocationName()), + memberName, + queryValue, + idempotencyComponent); } } } @@ -988,8 +1027,8 @@ private void writeRequestHeaders( } else { writer.addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT); opening = normalHeaderCount > 0 - ? "const headers: any = map({}, isSerializableHeaderValue, {" - : "const headers: any = map({"; + ? "const headers: any = map({}, isSerializableHeaderValue, {" + : "const headers: any = map({"; closing = "});"; } @@ -1019,23 +1058,22 @@ private void writeRequestHeaders( private void writeNormalHeader(GenerationContext context, HttpBinding binding) { String memberLocation = "input[" - + context.getStringStore().var(context.getSymbolProvider().toMemberName(binding.getMember())) - + "]"; + + context.getStringStore().var(context.getSymbolProvider().toMemberName(binding.getMember())) + + "]"; Shape target = context.getModel().expectShape(binding.getMember().getTarget()); String headerKey = binding.getLocationName().toLowerCase(Locale.US); String headerValue = getInputValue( - context, - binding.getLocation(), - memberLocation + "!", - binding.getMember(), - target - ); + context, + binding.getLocation(), + memberLocation + "!", + binding.getMember(), + target); boolean headerAssertion = headerValue.endsWith("!"); String headerBaseValue = (headerAssertion - ? headerValue.substring(0, headerValue.length() - 1) - : headerValue); + ? headerValue.substring(0, headerValue.length() - 1) + : headerValue); boolean isIdempotencyToken = binding.getMember().hasTrait(IdempotencyTokenTrait.class); if (!Objects.equals(memberLocation + "!", headerValue)) { @@ -1045,42 +1083,42 @@ private void writeNormalHeader(GenerationContext context, HttpBinding binding) { defaultValue = " || " + s.substring(s.indexOf(": ") + 2, s.length() - 1); } else if (isIdempotencyToken) { context.getWriter() - .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); + .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); defaultValue = " ?? generateIdempotencyToken()"; } String headerValueExpression = headerAssertion && !defaultValue.isEmpty() - ? headerBaseValue + defaultValue - : headerValue + defaultValue; + ? headerBaseValue + defaultValue + : headerValue + defaultValue; // evaluated value has a function or method call attached context.getWriter() - .addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT); - headerBuffer.put(headerKey, String.format( - "[%s]: [() => isSerializableHeaderValue(%s), () => %s],", - context.getStringStore().var(headerKey), - memberLocation + defaultValue, - headerValueExpression - )); + .addImport("isSerializableHeaderValue", null, TypeScriptDependency.AWS_SMITHY_CLIENT); + headerBuffer.put(headerKey, + String.format( + "[%s]: [() => isSerializableHeaderValue(%s), () => %s],", + context.getStringStore().var(headerKey), + memberLocation + defaultValue, + headerValueExpression)); } else { String constructedHeaderValue = (headerAssertion - ? headerBaseValue - : headerValue); + ? headerBaseValue + : headerValue); if (headerBuffer.containsKey(headerKey)) { String s = headerBuffer.get(headerKey); constructedHeaderValue += " || " + s.substring(s.indexOf(": ") + 2, s.length() - 1); } else if (isIdempotencyToken) { context.getWriter() - .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); + .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); constructedHeaderValue += " ?? generateIdempotencyToken()"; } else { constructedHeaderValue = headerValue; } - headerBuffer.put(headerKey, String.format( - "[%s]: %s,", - context.getStringStore().var(headerKey), - constructedHeaderValue - )); + headerBuffer.put(headerKey, + String.format( + "[%s]: %s,", + context.getStringStore().var(headerKey), + constructedHeaderValue)); } } @@ -1091,20 +1129,26 @@ private void writePrefixHeaders(GenerationContext context, HttpBinding binding) MapShape prefixMap = model.expectShape(binding.getMember().getTarget()).asMapShape().get(); Shape target = model.expectShape(prefixMap.getValue().getTarget()); // Iterate through each entry in the member. - writer.openBlock("...($1L !== undefined) && Object.keys($1L).reduce(", "),", memberLocation, - () -> { - writer.openBlock("(acc: any, suffix: string) => {", "}, {}", - () -> { - // Use a ! since we already validated the input member is defined above. - String headerValue = getInputValue(context, binding.getLocation(), - memberLocation + "![suffix]", binding.getMember(), target); - // Append the prefix to key. - writer.write("acc[`$L$${suffix.toLowerCase()}`] = $L;", - binding.getLocationName().toLowerCase(Locale.US), headerValue); - writer.write("return acc;"); - }); - } - ); + writer.openBlock("...($1L !== undefined) && Object.keys($1L).reduce(", + "),", + memberLocation, + () -> { + writer.openBlock("(acc: any, suffix: string) => {", + "}, {}", + () -> { + // Use a ! since we already validated the input member is defined above. + String headerValue = getInputValue(context, + binding.getLocation(), + memberLocation + "![suffix]", + binding.getMember(), + target); + // Append the prefix to key. + writer.write("acc[`$L$${suffix.toLowerCase()}`] = $L;", + binding.getLocationName().toLowerCase(Locale.US), + headerValue); + writer.write("return acc;"); + }); + }); } private void writeResponseHeaders( @@ -1149,8 +1193,7 @@ private void writeContentTypeHeader(GenerationContext context, Shape operationOr optionalContentType.ifPresent(contentType -> { // context.getWriter().write("'content-type': $S,", contentType) headerBuffer.put("content-type", - "'content-type': '" + contentType + "'," - ); + "'content-type': '" + contentType + "',"); }); } @@ -1316,7 +1359,11 @@ protected String getInputValue( throw new CodegenException(String.format( "Unsupported %s binding of %s to %s in %s using the %s protocol", - bindingType, member.getMemberName(), target.getType(), member.getContainer(), getName())); + bindingType, + member.getMemberName(), + target.getType(), + member.getContainer(), + getName())); } /** @@ -1407,8 +1454,11 @@ private String getCollectionInputParam( switch (bindingType) { case HEADER: if (collectionTarget.isStringShape()) { - context.getWriter().addImport( - "quoteHeader", "__quoteHeader", TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport( + "quoteHeader", + "__quoteHeader", + TypeScriptDependency.AWS_SMITHY_CLIENT); return iteratedParam + ".map(__quoteHeader).join(', ')"; } return iteratedParam + ".join(', ')"; @@ -1478,13 +1528,16 @@ private String getMapInputParam( MemberShape mapMember = target.getValue(); SymbolProvider symbolProvider = context.getSymbolProvider(); - String valueString = getInputValue(context, bindingType, "value", mapMember, + String valueString = getInputValue(context, + bindingType, + "value", + mapMember, model.expectShape(mapMember.getTarget())); return "Object.entries(" + dataSource + " || {}).reduce(" - + "(acc: any, [key, value]: [string, " + symbolProvider.toSymbol(mapMember) + "]) => {" - + "acc[key] = " + valueString + ";" - + "return acc;" - + "}, {})"; + + "(acc: any, [key, value]: [string, " + symbolProvider.toSymbol(mapMember) + "]) => {" + + "acc[key] = " + valueString + ";" + + "return acc;" + + "}, {})"; } /** @@ -1543,8 +1596,7 @@ private String getTimestampInputParam( * @param context The generation context. * @param operation The operation whose input is being generated. */ - protected void writeDefaultInputHeaders(GenerationContext context, OperationShape operation) { - } + protected void writeDefaultInputHeaders(GenerationContext context, OperationShape operation) {} /** * Writes any additional HTTP output headers required by the protocol implementation. @@ -1564,8 +1616,7 @@ protected void writeDefaultInputHeaders(GenerationContext context, OperationShap * @param context The generation context. * @param operation The operation whose output is being generated. */ - protected void writeDefaultOutputHeaders(GenerationContext context, OperationShape operation) { - } + protected void writeDefaultOutputHeaders(GenerationContext context, OperationShape operation) {} /** * Writes any additional HTTP error headers required by the protocol implementation. @@ -1585,8 +1636,7 @@ protected void writeDefaultOutputHeaders(GenerationContext context, OperationSha * @param context The generation context. * @param error The error which is being generated. */ - protected void writeDefaultErrorHeaders(GenerationContext context, StructureShape error) { - } + protected void writeDefaultErrorHeaders(GenerationContext context, StructureShape error) {} /** * Writes the code needed to serialize a protocol input document. @@ -1786,13 +1836,20 @@ private void genericSerializePayload(GenerationContext context, HttpBinding payl // Because documents can be set to a null value, handle setting that as the body // instead of using toString, as `null.toString()` will fail. if (target.isDocumentShape()) { - writer.openBlock("if (input.$L === null) {", "} else {", memberName, + writer.openBlock("if (input.$L === null) {", + "} else {", + memberName, () -> writer.write("body = \"null\";")); writer.indent(); } - writer.write("body = $L;", getInputValue( - context, Location.PAYLOAD, "input." + memberName, payloadBinding.getMember(), target)); + writer.write("body = $L;", + getInputValue( + context, + Location.PAYLOAD, + "input." + memberName, + payloadBinding.getMember(), + target)); if (target.isDocumentShape()) { writer.dedent(); writer.write("}"); @@ -1824,23 +1881,23 @@ private void generateOperationRequestDeserializer( + " output: $T,\n" + " context: $L\n" + "): Promise<$T> => {", "};", methodName, requestType, contextType, inputType, () -> { - handleContentType(context, operation, bindingIndex); - handleAccept(context, operation, bindingIndex); - // Start deserializing the response. - writer.openBlock("const contents: any = map({", "});", () -> { - readRequestHeaders(context, operation, bindingIndex, "output"); - }); - readQueryString(context, operation, bindingIndex); - readPath(context, operation, bindingIndex, trait); - readHost(context, operation); - List documentBindings = readRequestBody(context, operation, bindingIndex); - // Track all shapes bound to the document so their deserializers may be generated. - documentBindings.forEach(binding -> { - Shape target = model.expectShape(binding.getMember().getTarget()); - deserializingDocumentShapes.add(target); - }); - writer.write("return contents;"); - }); + handleContentType(context, operation, bindingIndex); + handleAccept(context, operation, bindingIndex); + // Start deserializing the response. + writer.openBlock("const contents: any = map({", "});", () -> { + readRequestHeaders(context, operation, bindingIndex, "output"); + }); + readQueryString(context, operation, bindingIndex); + readPath(context, operation, bindingIndex, trait); + readHost(context, operation); + List documentBindings = readRequestBody(context, operation, bindingIndex); + // Track all shapes bound to the document so their deserializers may be generated. + documentBindings.forEach(binding -> { + Shape target = model.expectShape(binding.getMember().getTarget()); + deserializingDocumentShapes.add(target); + }); + writer.write("return contents;"); + }); writer.write(""); } @@ -1878,7 +1935,8 @@ private void handleContentType( "__UnsupportedMediaTypeException", TypeScriptDependency.SERVER_COMMON); Optional optionalContentType = bindingIndex.determineRequestContentType( - operation, getDocumentContentType()); + operation, + getDocumentContentType()); writer.write("const contentTypeHeaderKey: string | undefined = Object.keys(output.headers)" + ".find(key => key.toLowerCase() === 'content-type');"); writer.openBlock("if (contentTypeHeaderKey != null) {", "};", () -> { @@ -1929,7 +1987,8 @@ private void handleAccept( TypeScriptWriter writer = context.getWriter(); Optional optionalContentType = bindingIndex.determineResponseContentType( - operation, getDocumentContentType()); + operation, + getDocumentContentType()); writer.addImport("NotAcceptableException", "__NotAcceptableException", TypeScriptDependency.SERVER_COMMON); @@ -1941,7 +2000,9 @@ private void handleAccept( String contentType = optionalContentType.orElse(getDocumentContentType()); // Validate that the content type matches the protocol default, or what's modeled if there's // a modeled type. - writer.openBlock("if (!__acceptMatches(accept, $S)) {", "};", contentType, + writer.openBlock("if (!__acceptMatches(accept, $S)) {", + "};", + contentType, () -> writer.write("throw new __NotAcceptableException();")); }); } @@ -1950,7 +2011,7 @@ private boolean bodyIsBlobWithoutMediaType( GenerationContext context, Collection bindings ) { - for (HttpBinding binding : bindings) { + for (HttpBinding binding : bindings) { if (binding.getLocation() == Location.PAYLOAD) { Shape target = context.getModel().expectShape(binding.getMember().getTarget()); return !target.hasTrait(MediaTypeTrait.class) && target.isBlobShape(); @@ -1994,25 +2055,29 @@ private void readDirectQueryBindings(GenerationContext context, List { - writer.openBlock("if (query[$S].length === 1) {", "}", - binding.getLocationName(), - () -> { - writer.write("queryValue = query[$S][0];", binding.getLocationName()); - } - ); - writer.openBlock("else {", "}", () -> { - writer.write("throw new __SerializationException();"); + writer.openBlock("if (Array.isArray(query[$S])) {", + "}", + binding.getLocationName(), + () -> { + writer.openBlock("if (query[$S].length === 1) {", + "}", + binding.getLocationName(), + () -> { + writer.write("queryValue = query[$S][0];", binding.getLocationName()); + }); + writer.openBlock("else {", "}", () -> { + writer.write("throw new __SerializationException();"); + }); }); - }); writer.openBlock("else {", "}", () -> { writer.write("queryValue = query[$S] as string;", binding.getLocationName()); }); } - String queryValue = getOutputValue(context, binding.getLocation(), "queryValue", - binding.getMember(), target); + String queryValue = getOutputValue(context, + binding.getLocation(), + "queryValue", + binding.getMember(), + target); writer.write("contents.$L = $L;", memberName, queryValue); }); } @@ -2021,7 +2086,9 @@ private void readDirectQueryBindings(GenerationContext context, List { - writer.openBlock("if (value.length === 1) {", "}", + writer.openBlock("if (value.length === 1) {", + "}", () -> { writer.write("queryValue = value[0];"); }); @@ -2052,8 +2124,11 @@ private void readMappedQueryBindings(GenerationContext context, HttpBinding mapp writer.openBlock("else {", "}", () -> { writer.write("queryValue = value as string;"); }); - parsedValue = getOutputValue(context, mappedBinding.getLocation(), - "queryValue", target.getValue(), valueShape); + parsedValue = getOutputValue(context, + mappedBinding.getLocation(), + "queryValue", + target.getValue(), + valueShape); } writer.write("parsedQuery[key] = $L;", parsedValue); }); @@ -2104,8 +2179,11 @@ private void readPath( // since this is in the path, we should decode early String dataSource = String.format("decodeURIComponent(parsedPath.groups.%s)", binding.getLocationName()); - String labelValue = getOutputValue(context, binding.getLocation(), - dataSource, binding.getMember(), target); + String labelValue = getOutputValue(context, + binding.getLocation(), + dataSource, + binding.getMember(), + target); writer.write("contents.$L = $L;", memberName, labelValue); } }); @@ -2138,8 +2216,11 @@ private void readHost(GenerationContext context, OperationShape operation) { if (member.hasTrait(HostLabelTrait.class)) { Shape target = context.getModel().expectShape(member.getTarget()); String memberName = context.getSymbolProvider().toMemberName(member); - String labelValue = getOutputValue(context, Location.LABEL, - "parsedHost.groups." + member.getMemberName(), member, target); + String labelValue = getOutputValue(context, + Location.LABEL, + "parsedHost.groups." + member.getMemberName(), + member, + target); writer.write("contents.$L = $L;", memberName, labelValue); } } @@ -2166,40 +2247,49 @@ private void generateOperationResponseDeserializer( String errorMethodName = "de_CommandError"; // Add the normalized output type. Symbol outputType = symbol.expectProperty("outputType", Symbol.class); - String contextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), writer, - context.getModel(), operation); + String contextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), + writer, + context.getModel(), + operation); // Handle the general response. writer.writeDocs(methodLongName); writer.openBlock("export const $L = async (\n" - + " output: $T,\n" - + " context: $L\n" - + "): Promise<$T> => {", "};", - methodName, responseType, contextType, outputType, () -> { - // Redirect error deserialization to the dispatcher if we receive an error range - // status code that's not the modeled code (300 or higher). This allows for - // returning other 2XX codes that don't match the defined value. - writer.openBlock("if (output.statusCode !== $L && output.statusCode >= 300) {", "}", trait.getCode(), - () -> writer.write("return $L(output, context);", errorMethodName)); - - // Start deserializing the response. - writer.openBlock("const contents: any = map({", "});", () -> { - writer.write("$$metadata: deserializeMetadata(output),"); - - readResponseHeaders(context, operation, bindingIndex, "output"); - }); + + " output: $T,\n" + + " context: $L\n" + + "): Promise<$T> => {", + "};", + methodName, + responseType, + contextType, + outputType, + () -> { + // Redirect error deserialization to the dispatcher if we receive an error range + // status code that's not the modeled code (300 or higher). This allows for + // returning other 2XX codes that don't match the defined value. + writer.openBlock("if (output.statusCode !== $L && output.statusCode >= 300) {", + "}", + trait.getCode(), + () -> writer.write("return $L(output, context);", errorMethodName)); + + // Start deserializing the response. + writer.openBlock("const contents: any = map({", "});", () -> { + writer.write("$$metadata: deserializeMetadata(output),"); + + readResponseHeaders(context, operation, bindingIndex, "output"); + }); - List documentBindings = readResponseBody(context, operation, bindingIndex); - // Track all shapes bound to the document so their deserializers may be generated. - documentBindings.forEach(binding -> { - Shape target = model.expectShape(binding.getMember().getTarget()); - if (!EventStreamGenerator.isEventStreamShape(target)) { - deserializingDocumentShapes.add(target); - } - }); + List documentBindings = readResponseBody(context, operation, bindingIndex); + // Track all shapes bound to the document so their deserializers may be generated. + documentBindings.forEach(binding -> { + Shape target = model.expectShape(binding.getMember().getTarget()); + if (!EventStreamGenerator.isEventStreamShape(target)) { + deserializingDocumentShapes.add(target); + } + }); - writer.write("return contents;"); - }); + writer.write("return contents;"); + }); writer.write(""); } @@ -2217,42 +2307,47 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape writer.writeDocs(errorDeserMethodLongName); writer.openBlock("const $L = async (\n" - + " $L: any,\n" - + " context: __SerdeContext\n" - + "): Promise<$T> => {", "};", - errorDeserMethodName, outputName, errorSymbol, () -> { - writer.openBlock("const contents: any = map({", "});", () -> { - readResponseHeaders(context, error, bindingIndex, outputName); - }); + + " $L: any,\n" + + " context: __SerdeContext\n" + + "): Promise<$T> => {", + "};", + errorDeserMethodName, + outputName, + errorSymbol, + () -> { + writer.openBlock("const contents: any = map({", "});", () -> { + readResponseHeaders(context, error, bindingIndex, outputName); + }); - List documentBindings = readErrorResponseBody(context, error, bindingIndex); - // Track all shapes bound to the document so their deserializers may be generated. - documentBindings.forEach(binding -> { - Shape target = model.expectShape(binding.getMember().getTarget()); - deserializingDocumentShapes.add(target); - }); + List documentBindings = readErrorResponseBody(context, error, bindingIndex); + // Track all shapes bound to the document so their deserializers may be generated. + documentBindings.forEach(binding -> { + Shape target = model.expectShape(binding.getMember().getTarget()); + deserializingDocumentShapes.add(target); + }); - // todo: unsupported ssdk feature. - String serverSdkInfix = context.getSettings().generateServerSdk() - ? ": any /* $metadata unsupported on ssdk error */" - : ""; - - Symbol materializedErrorSymbol = errorSymbol.toBuilder() - .putProperty("typeOnly", false) - .build(); - writer.openBlock("const exception$L = new $T({", "});", - serverSdkInfix, - materializedErrorSymbol, - () -> { - writer.write("$$metadata: deserializeMetadata($L),", outputName); - writer.write("...contents"); - } - ); - String errorLocation = this.getErrorBodyLocation(context, outputName + ".body"); - writer.addImport("decorateServiceException", "__decorateServiceException", - TypeScriptDependency.AWS_SMITHY_CLIENT); - writer.write("return __decorateServiceException(exception, $L);", errorLocation); - }); + // todo: unsupported ssdk feature. + String serverSdkInfix = context.getSettings().generateServerSdk() + ? ": any /* $metadata unsupported on ssdk error */" + : ""; + + Symbol materializedErrorSymbol = errorSymbol.toBuilder() + .putProperty("typeOnly", false) + .build(); + writer.openBlock("const exception$L = new $T({", + "});", + serverSdkInfix, + materializedErrorSymbol, + () -> { + writer.write("$$metadata: deserializeMetadata($L),", outputName); + writer.write("...contents"); + }); + String errorLocation = this.getErrorBodyLocation(context, outputName + ".body"); + writer.addImport("decorateServiceException", + "__decorateServiceException", + TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.write("return __decorateServiceException(exception, $L);", errorLocation); + }); writer.write(""); } @@ -2322,19 +2417,21 @@ private void readNormalHeaders( String headerName = binding.getLocationName().toLowerCase(Locale.US); Shape target = context.getModel().expectShape(binding.getMember().getTarget()); String headerValue = getOutputValue( - context, binding.getLocation(), - outputName + ".headers[" + context.getStringStore().var(headerName) + "]", - binding.getMember(), target - ); + context, + binding.getLocation(), + outputName + ".headers[" + context.getStringStore().var(headerName) + "]", + binding.getMember(), + target); String checkedValue = outputName + ".headers[" + context.getStringStore().var(headerName) + "]"; if (checkedValue.equals(headerValue)) { writer.write("[$L]: [, $L],", context.getStringStore().var(memberName), headerValue); } else { writer.write( - "[$L]: [() => void 0 !== $L, () => $L],", - context.getStringStore().var(memberName), checkedValue, headerValue - ); + "[$L]: [() => void 0 !== $L, () => $L],", + context.getStringStore().var(memberName), + checkedValue, + headerValue); } } } @@ -2366,17 +2463,20 @@ private void readPrefixHeaders( writer.openBlock("$L: [, ", "],", memberName, () -> { String headerLocation = binding.getLocationName().toLowerCase(Locale.US); writer.write( - "Object.keys($L.headers).filter(header => header.startsWith('$L'))", - outputName, - headerLocation - ); + "Object.keys($L.headers).filter(header => header.startsWith('$L'))", + outputName, + headerLocation); writer.indent().openBlock(".reduce((acc, header) => {", "}, {} as any)", () -> { MapShape prefixMap = model.expectShape(binding.getMember().getTarget()).asMapShape().get(); Shape target = model.expectShape(prefixMap.getValue().getTarget()); - String headerValue = getOutputValue(context, binding.getLocation(), - outputName + ".headers[header]", binding.getMember(), target); + String headerValue = getOutputValue(context, + binding.getLocation(), + outputName + ".headers[header]", + binding.getMember(), + target); writer.write("acc[header.substring($L)] = $L;", - headerLocation.length(), headerValue); + headerLocation.length(), + headerValue); writer.write("return acc;"); }); }); @@ -2403,7 +2503,8 @@ private List readResponseBody( documentBindings.sort(Comparator.comparing(HttpBinding::getMemberName)); List payloadBindings = bindingIndex.getResponseBindings(operationOrError, Location.PAYLOAD); List responseCodeBindings = bindingIndex.getResponseBindings( - operationOrError, Location.RESPONSE_CODE); + operationOrError, + Location.RESPONSE_CODE); return readBody(context, operationOrError, documentBindings, payloadBindings, responseCodeBindings, true); } @@ -2517,17 +2618,26 @@ private HttpBinding readPayload( if (!isStreaming && target instanceof UnionShape) { writer.openBlock( - "if (Object.keys(data ?? {}).length) {", - "}", - () -> { - importUnionDeserializer(writer); - writer.write("contents.$L = __expectUnion($L);", binding.getMemberName(), getOutputValue(context, - Location.PAYLOAD, "data", binding.getMember(), target)); - } - ); + "if (Object.keys(data ?? {}).length) {", + "}", + () -> { + importUnionDeserializer(writer); + writer.write("contents.$L = __expectUnion($L);", + binding.getMemberName(), + getOutputValue(context, + Location.PAYLOAD, + "data", + binding.getMember(), + target)); + }); } else { - writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context, - Location.PAYLOAD, "data", binding.getMember(), target)); + writer.write("contents.$L = $L;", + binding.getMemberName(), + getOutputValue(context, + Location.PAYLOAD, + "data", + binding.getMember(), + target)); } return binding; @@ -2630,8 +2740,13 @@ private String getOutputValue( HttpBindingIndex httpIndex = HttpBindingIndex.of(context.getModel()); Format format = httpIndex.determineTimestampFormat(member, bindingType, getDocumentTimestampFormat()); return HttpProtocolGeneratorUtils.getTimestampOutputParam( - context.getWriter(), dataSource, bindingType, member, format, - requiresNumericEpochSecondsInPayload(), context.getSettings().generateClient()); + context.getWriter(), + dataSource, + bindingType, + member, + format, + requiresNumericEpochSecondsInPayload(), + context.getSettings().generateClient()); } else if (target instanceof BlobShape) { return getBlobOutputParam(bindingType, dataSource); } else if (target instanceof CollectionShape) { @@ -2642,7 +2757,11 @@ private String getOutputValue( throw new CodegenException(String.format( "Unsupported %s binding of %s to %s in %s using the %s protocol", - bindingType, member.getMemberName(), target.getType(), member.getContainer(), getName())); + bindingType, + member.getMemberName(), + target.getType(), + member.getContainer(), + getName())); } /** @@ -2691,7 +2810,10 @@ private String getStringOutputParam( } return HttpProtocolGeneratorUtils.getStringOutputParam( - context, target, dataSource, !isGuaranteedString(bindingType)); + context, + target, + dataSource, + !isGuaranteedString(bindingType)); } private boolean isGuaranteedString(Location bindingType) { @@ -2746,8 +2868,11 @@ private String getCollectionOutputParam( if (trimParameterValue) { outputValueDataSource += ".trim()"; } - String collectionTargetValue = getOutputValue(context, bindingType, outputValueDataSource, - targetMember, collectionTarget); + String collectionTargetValue = getOutputValue(context, + bindingType, + outputValueDataSource, + targetMember, + collectionTarget); String outputParam; switch (bindingType) { case QUERY_PARAMS: @@ -2859,29 +2984,47 @@ private String getNumberOutputParam( case HEADER: switch (target.getType()) { case DOUBLE: - context.getWriter().addImport( - "strictParseDouble", "__strictParseDouble", TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport( + "strictParseDouble", + "__strictParseDouble", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__strictParseDouble(" + dataSource + ")"; case FLOAT: - context.getWriter().addImport( - "strictParseFloat", "__strictParseFloat", TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport( + "strictParseFloat", + "__strictParseFloat", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__strictParseFloat(" + dataSource + ")"; case LONG: - context.getWriter().addImport( - "strictParseLong", "__strictParseLong", TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport( + "strictParseLong", + "__strictParseLong", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__strictParseLong(" + dataSource + ")"; case INT_ENUM: case INTEGER: - context.getWriter().addImport( - "strictParseInt32", "__strictParseInt32", TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport( + "strictParseInt32", + "__strictParseInt32", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__strictParseInt32(" + dataSource + ")"; case SHORT: - context.getWriter().addImport( - "strictParseShort", "__strictParseShort", TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport( + "strictParseShort", + "__strictParseShort", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__strictParseShort(" + dataSource + ")"; case BYTE: - context.getWriter().addImport( - "strictParseByte", "__strictParseByte", TypeScriptDependency.AWS_SMITHY_CLIENT); + context.getWriter() + .addImport( + "strictParseByte", + "__strictParseByte", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__strictParseByte(" + dataSource + ")"; default: throw new CodegenException("Unexpected number shape `" + target.getType() + "`"); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java index 41e3310ada6..9d3fd4bf557 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.nio.file.Paths; @@ -81,20 +70,20 @@ public static String getTimestampInputParam( ) { switch (format) { case DATE_TIME: - context.getWriter().addImport( - "serializeDateTime", - "__serializeDateTime", - TypeScriptDependency.AWS_SMITHY_CLIENT - ); + context.getWriter() + .addImport( + "serializeDateTime", + "__serializeDateTime", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__serializeDateTime(" + dataSource + ")"; case EPOCH_SECONDS: return "(" + dataSource + ".getTime() / 1_000)"; case HTTP_DATE: - context.getWriter().addImport( - "dateToUtcString", - "__dateToUtcString", - TypeScriptDependency.AWS_SMITHY_CLIENT - ); + context.getWriter() + .addImport( + "dateToUtcString", + "__dateToUtcString", + TypeScriptDependency.AWS_SMITHY_CLIENT); return "__dateToUtcString(" + dataSource + ")"; default: throw new CodegenException("Unexpected timestamp format `" + format + "` on " + shape); @@ -116,13 +105,15 @@ public static String getTimestampInputParam( * @param isClient true if generating a client. * @return Returns a value or expression of the output timestamp. */ - public static String getTimestampOutputParam(TypeScriptWriter writer, - String dataSource, - Location bindingType, - Shape shape, - Format format, - boolean requireNumericEpochSecondsInPayload, - boolean isClient) { + public static String getTimestampOutputParam( + TypeScriptWriter writer, + String dataSource, + Location bindingType, + Shape shape, + Format format, + boolean requireNumericEpochSecondsInPayload, + boolean isClient + ) { // This has always explicitly wrapped the dataSource in "new Date(..)", so it could never generate // an expression that evaluates to null. Codegen relies on this. writer.addImport("expectNonNull", "__expectNonNull", TypeScriptDependency.AWS_SMITHY_CLIENT); @@ -130,21 +121,25 @@ public static String getTimestampOutputParam(TypeScriptWriter writer, case DATE_TIME: // Clients should be able to handle offsets and normalize the datetime to an offset of zero. if (isClient) { - writer.addImport("parseRfc3339DateTimeWithOffset", "__parseRfc3339DateTimeWithOffset", - TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.addImport("parseRfc3339DateTimeWithOffset", + "__parseRfc3339DateTimeWithOffset", + TypeScriptDependency.AWS_SMITHY_CLIENT); return String.format("__expectNonNull(__parseRfc3339DateTimeWithOffset(%s))", dataSource); } else { - writer.addImport("parseRfc3339DateTime", "__parseRfc3339DateTime", - TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.addImport("parseRfc3339DateTime", + "__parseRfc3339DateTime", + TypeScriptDependency.AWS_SMITHY_CLIENT); return String.format("__expectNonNull(__parseRfc3339DateTime(%s))", dataSource); } case HTTP_DATE: - writer.addImport("parseRfc7231DateTime", "__parseRfc7231DateTime", - TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.addImport("parseRfc7231DateTime", + "__parseRfc7231DateTime", + TypeScriptDependency.AWS_SMITHY_CLIENT); return String.format("__expectNonNull(__parseRfc7231DateTime(%s))", dataSource); case EPOCH_SECONDS: - writer.addImport("parseEpochTimestamp", "__parseEpochTimestamp", - TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.addImport("parseEpochTimestamp", + "__parseEpochTimestamp", + TypeScriptDependency.AWS_SMITHY_CLIENT); String modifiedDataSource = dataSource; if (requireNumericEpochSecondsInPayload && (bindingType == Location.DOCUMENT || bindingType == Location.PAYLOAD)) { @@ -200,10 +195,12 @@ public static String getStringInputParam(GenerationContext context, Shape shape, * only be false if the value is guaranteed to be a string already. * @return Returns a value or expression of the output string. */ - public static String getStringOutputParam(GenerationContext context, - Shape shape, - String dataSource, - boolean useExpect) { + public static String getStringOutputParam( + GenerationContext context, + Shape shape, + String dataSource, + boolean useExpect + ) { // Handle media type generation, defaulting to a standard String. Optional mediaTypeTrait = shape.getTrait(MediaTypeTrait.class); if (mediaTypeTrait.isPresent()) { @@ -251,12 +248,14 @@ public static void generateMetadataDeserializer(GenerationContext context, Symbo TypeScriptWriter writer = context.getWriter(); writer.addTypeImport("ResponseMetadata", "__ResponseMetadata", TypeScriptDependency.SMITHY_TYPES); - writer.openBlock("const deserializeMetadata = (output: $T): __ResponseMetadata => ({", "});", responseType, + writer.openBlock("const deserializeMetadata = (output: $T): __ResponseMetadata => ({", + "});", + responseType, () -> { writer.write("httpStatusCode: output.statusCode,"); writer.write("requestId: output.headers[\"x-amzn-requestid\"] ??" - + " output.headers[\"x-amzn-request-id\"] ??" - + " output.headers[\"x-amz-request-id\"],"); + + " output.headers[\"x-amzn-request-id\"] ??" + + " output.headers[\"x-amz-request-id\"],"); writer.write("extendedRequestId: output.headers[\"x-amz-id-2\"],"); writer.write("cfId: output.headers[\"x-amz-cf-id\"],"); }); @@ -291,15 +290,14 @@ public static void writeRetryableTrait(TypeScriptWriter writer, StructureShape e if (retryableTrait.isPresent()) { String textAfterBlock = String.format("}%s", separator); writer.openCollapsibleBlock( - "$$retryable = {", - textAfterBlock, - retryableTrait.get().getThrottling(), - () -> { - if (retryableTrait.get().getThrottling()) { - writer.write("throttling: true,"); - } - } - ); + "$$retryable = {", + textAfterBlock, + retryableTrait.get().getThrottling(), + () -> { + if (retryableTrait.get().getThrottling()) { + writer.write("throttling: true,"); + } + }); } } @@ -318,14 +316,14 @@ public static void writeRetryableTrait(TypeScriptWriter writer, StructureShape e * @return A set of all error structure shapes for the operation that were dispatched to. */ public static Set generateUnifiedErrorDispatcher( - GenerationContext context, - List operations, - SymbolReference responseType, - Consumer errorCodeGenerator, - boolean shouldParseErrorBody, - BiFunction bodyErrorLocationModifier, - BiFunction, Map> operationErrorsToShapes, - Map> errorAliases + GenerationContext context, + List operations, + SymbolReference responseType, + Consumer errorCodeGenerator, + boolean shouldParseErrorBody, + BiFunction bodyErrorLocationModifier, + BiFunction, Map> operationErrorsToShapes, + Map> errorAliases ) { TypeScriptWriter writer = context.getWriter(); SymbolProvider symbolProvider = context.getSymbolProvider(); @@ -333,84 +331,86 @@ public static Set generateUnifiedErrorDispatcher( String errorMethodName = "de_CommandError"; String errorMethodLongName = "deserialize_" - + ProtocolGenerator.getSanitizedName(context.getProtocolName()) - + "CommandError"; + + ProtocolGenerator.getSanitizedName(context.getProtocolName()) + + "CommandError"; writer.writeDocs(errorMethodLongName); writer.openBlock("const $L = async (\n" - + " output: $T,\n" - + " context: __SerdeContext,\n" - + "): Promise => {", "}", errorMethodName, responseType, () -> { - // Prepare error response for parsing error code. If error code needs to be parsed from response body - // then we collect body and parse it to JS object, otherwise leave the response body as is. - if (shouldParseErrorBody) { - writer.openBlock("const parsedOutput: any = {", "};", - () -> { - writer.write("...output,"); - writer.write("body: await parseErrorBody(output.body, context)"); - }); - } + + " output: $T,\n" + + " context: __SerdeContext,\n" + + "): Promise => {", "}", errorMethodName, responseType, () -> { + // Prepare error response for parsing error code. If error code needs to be parsed from response body + // then we collect body and parse it to JS object, otherwise leave the response body as is. + if (shouldParseErrorBody) { + writer.openBlock("const parsedOutput: any = {", + "};", + () -> { + writer.write("...output,"); + writer.write("body: await parseErrorBody(output.body, context)"); + }); + } - // Error responses must be at least BaseException interface - errorCodeGenerator.accept(context); + // Error responses must be at least BaseException interface + errorCodeGenerator.accept(context); - Runnable defaultErrorHandler = () -> { - if (shouldParseErrorBody) { - // Body is already parsed above - writer.write("const parsedBody = parsedOutput.body;"); - } else { - // Body is not parsed above, so parse it here - writer.write("const parsedBody = await parseBody(output.body, context);"); - } + Runnable defaultErrorHandler = () -> { + if (shouldParseErrorBody) { + // Body is already parsed above + writer.write("const parsedBody = parsedOutput.body;"); + } else { + // Body is not parsed above, so parse it here + writer.write("const parsedBody = await parseBody(output.body, context);"); + } - // Get the protocol specific error location for retrieving contents. - String errorLocation = bodyErrorLocationModifier.apply(context, "parsedBody"); - writer.openBlock("return throwDefaultError({", "}) as never;", () -> { - writer.write("output,"); - if (errorLocation.equals("parsedBody")) { - writer.write("parsedBody,"); - } else { - writer.write("parsedBody: $L,", errorLocation); - } - writer.write("errorCode"); - }); - }; + // Get the protocol specific error location for retrieving contents. + String errorLocation = bodyErrorLocationModifier.apply(context, "parsedBody"); + writer.openBlock("return throwDefaultError({", "}) as never;", () -> { + writer.write("output,"); + if (errorLocation.equals("parsedBody")) { + writer.write("parsedBody,"); + } else { + writer.write("parsedBody: $L,", errorLocation); + } + writer.write("errorCode"); + }); + }; - Map operationNamesToShapes = operationErrorsToShapes.apply(context, operations); + Map operationNamesToShapes = operationErrorsToShapes.apply(context, operations); - if (!operationNamesToShapes.isEmpty()) { - writer.openBlock("switch (errorCode) {", "}", () -> { - // Generate the case statement for each error, invoking the specific deserializer. + if (!operationNamesToShapes.isEmpty()) { + writer.openBlock("switch (errorCode) {", "}", () -> { + // Generate the case statement for each error, invoking the specific deserializer. - operationNamesToShapes.forEach((name, errorId) -> { - StructureShape error = context.getModel().expectShape(errorId).asStructureShape().get(); - // Track errors bound to the operation so their deserializers may be generated. - errorShapes.add(error); - Symbol errorSymbol = symbolProvider.toSymbol(error); - String errorDeserMethodName = ProtocolGenerator.getDeserFunctionShortName(errorSymbol) + "Res"; - // Dispatch to the error deserialization function. - String outputParam = shouldParseErrorBody ? "parsedOutput" : "output"; - writer.write("case $S:", name); - writer.write("case $S:", errorId.toString()); - for (String alias : errorAliases.getOrDefault(errorId.toString(), new TreeSet<>())) { - if (!Objects.equals(name, alias) && !Objects.equals(errorId.toString(), alias)) { - writer.write("case $S:", alias); - } - } - writer.indent() - .write("throw await $L($L, context);", errorDeserMethodName, outputParam) - .dedent(); - }); + operationNamesToShapes.forEach((name, errorId) -> { + StructureShape error = context.getModel().expectShape(errorId).asStructureShape().get(); + // Track errors bound to the operation so their deserializers may be generated. + errorShapes.add(error); + Symbol errorSymbol = symbolProvider.toSymbol(error); + String errorDeserMethodName = + ProtocolGenerator.getDeserFunctionShortName(errorSymbol) + "Res"; + // Dispatch to the error deserialization function. + String outputParam = shouldParseErrorBody ? "parsedOutput" : "output"; + writer.write("case $S:", name); + writer.write("case $S:", errorId.toString()); + for (String alias : errorAliases.getOrDefault(errorId.toString(), new TreeSet<>())) { + if (!Objects.equals(name, alias) && !Objects.equals(errorId.toString(), alias)) { + writer.write("case $S:", alias); + } + } + writer.indent() + .write("throw await $L($L, context);", errorDeserMethodName, outputParam) + .dedent(); + }); - // Build a generic error the best we can for ones we don't know about. - writer.write("default:").indent(); - defaultErrorHandler.run(); - writer.dedent(); + // Build a generic error the best we can for ones we don't know about. + writer.write("default:").indent(); + defaultErrorHandler.run(); + writer.dedent(); + }); + } else { + defaultErrorHandler.run(); + } }); - } else { - defaultErrorHandler.run(); - } - }); writer.write(""); return errorShapes; @@ -434,8 +434,9 @@ public static void writeHostPrefix(GenerationContext context, OperationShape ope writer.write("resolvedHostname = $S + resolvedHostname;", trait.getHostPrefix().toString()); if (operation.getInput().isPresent()) { List prefixLabels = trait.getHostPrefix().getLabels(); - StructureShape inputShape = context.getModel().expectShape(operation.getInput() - .get(), StructureShape.class); + StructureShape inputShape = context.getModel() + .expectShape(operation.getInput() + .get(), StructureShape.class); for (SmithyPattern.Segment label : prefixLabels) { MemberShape member = inputShape.getMember(label.getContent()).get(); String memberName = symbolProvider.toMemberName(member); @@ -443,7 +444,8 @@ public static void writeHostPrefix(GenerationContext context, OperationShape ope writer.write("throw new Error('Empty value provided for input host prefix: $L.');", memberName); }); writer.write("resolvedHostname = resolvedHostname.replace(\"{$L}\", input.$L!)", - label.getContent(), memberName); + label.getContent(), + memberName); } } writer.openBlock("if (!__isValidHostname(resolvedHostname)) {", "}", () -> { @@ -458,16 +460,18 @@ public static void writeHostPrefix(GenerationContext context, OperationShape ope public static SymbolReference getClientBaseException(GenerationContext context) { ServiceShape service = context.getService(); SymbolProvider symbolProvider = context.getSymbolProvider(); - String serviceName = symbolProvider.toSymbol(service).getName() - .replaceAll("(Client)$", ""); + String serviceName = symbolProvider.toSymbol(service) + .getName() + .replaceAll("(Client)$", ""); String serviceExceptionName = CodegenUtils.getSyntheticBaseExceptionName( - serviceName, context.getModel() - ); + serviceName, + context.getModel()); String namespace = Paths.get(".", "src", "models", serviceExceptionName).toString(); Symbol serviceExceptionSymbol = Symbol.builder() - .name(serviceExceptionName) - .namespace(namespace, "/") - .definitionFile(namespace + ".ts").build(); + .name(serviceExceptionName) + .namespace(namespace, "/") + .definitionFile(namespace + ".ts") + .build(); return SymbolReference.builder() .options(SymbolReference.ContextOption.USE) .alias("__BaseException") @@ -483,15 +487,17 @@ public static SymbolReference getClientBaseException(GenerationContext context) * @return map of error names to {@link ShapeId} */ public static Map getOperationErrors(GenerationContext context, OperationShape operation) { - return operation.getErrors().stream() - .collect(Collectors.toMap( - shapeId -> shapeId.getName(context.getService()), - Function.identity(), - (x, y) -> { - if (!x.equals(y)) { - throw new CodegenException(String.format("conflicting error shape ids: %s, %s", x, y)); - } - return x; - }, TreeMap::new)); + return operation.getErrors() + .stream() + .collect(Collectors.toMap( + shapeId -> shapeId.getName(context.getService()), + Function.identity(), + (x, y) -> { + if (!x.equals(y)) { + throw new CodegenException(String.format("conflicting error shape ids: %s, %s", x, y)); + } + return x; + }, + TreeMap::new)); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java index 9d89d23cb60..fdb0c7f9055 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Set; @@ -44,8 +33,8 @@ public abstract class HttpRpcProtocolGenerator implements ProtocolGenerator { public static final Logger LOGGER = Logger.getLogger(HttpRpcProtocolGenerator.class.getName()); - private static final ApplicationProtocol APPLICATION_PROTOCOL - = ApplicationProtocol.createDefaultHttpApplicationProtocol(); + private static final ApplicationProtocol APPLICATION_PROTOCOL = + ApplicationProtocol.createDefaultHttpApplicationProtocol(); protected final Set serializingDocumentShapes = new TreeSet<>(); protected final Set deserializingDocumentShapes = new TreeSet<>(); @@ -103,27 +92,25 @@ public void generateSharedComponents(GenerationContext context) { ServiceShape service = context.getService(); deserializingErrorShapes.forEach(error -> generateErrorDeserializer(context, error)); eventStreamGenerator.generateEventStreamSerializers( - context, - service, - getDocumentContentType(), - () -> { - TypeScriptWriter writer = context.getWriter(); - serializeEventStreamBodyToBytes(writer); - }, - serializingDocumentShapes - ); + context, + service, + getDocumentContentType(), + () -> { + TypeScriptWriter writer = context.getWriter(); + serializeEventStreamBodyToBytes(writer); + }, + serializingDocumentShapes); // Error shapes that only referred in the error event of an eventstream Set errorEventShapes = new TreeSet<>(); SerdeElisionIndex serdeElisionIndex = SerdeElisionIndex.of(context.getModel()); eventStreamGenerator.generateEventStreamDeserializers( - context, - service, - errorEventShapes, - deserializingDocumentShapes, - isErrorCodeInBody, - enableSerdeElision(), - serdeElisionIndex - ); + context, + service, + errorEventShapes, + deserializingDocumentShapes, + isErrorCodeInBody, + enableSerdeElision(), + serdeElisionIndex); errorEventShapes.removeIf(deserializingErrorShapes::contains); errorEventShapes.forEach(error -> generateErrorDeserializer(context, error)); generateDocumentBodyShapeSerializers(context, serializingDocumentShapes); @@ -146,44 +133,44 @@ public void generateSharedComponents(GenerationContext context) { writer.addTypeImport("HeaderBag", "__HeaderBag", TypeScriptDependency.SMITHY_TYPES); Symbol requestSymbol = requestType.getSymbol() - .toBuilder() - .putProperty("typeOnly", false) - .build(); + .toBuilder() + .putProperty("typeOnly", false) + .build(); writer.openBlock("const buildHttpRpcRequest = async (\n" - + " context: __SerdeContext,\n" - + " headers: __HeaderBag,\n" - + " path: string,\n" - + " resolvedHostname: string | undefined,\n" - + " body: any,\n" - + "): Promise<$T> => {", "};", requestType, () -> { - // Get the hostname, port, and scheme from client's resolved endpoint. Then construct the request from - // them. The client's resolved endpoint can be default one or supplied by users. - writer.write("const {hostname, protocol = \"https\", port, path: basePath} = await context.endpoint();"); - writer.openBlock("const contents: any = {", "};", () -> { - writer.write("protocol,"); - writer.write("hostname,"); - writer.write("port,"); - writer.write("method: \"POST\","); - writer.write("path: basePath.endsWith(\"/\") ? basePath.slice(0, -1) + path : basePath + path,"); - writer.write("headers,"); - }); - writer.openBlock("if (resolvedHostname !== undefined) {", "}", () -> { - writer.write("contents.hostname = resolvedHostname;"); - }); - writer.openBlock("if (body !== undefined) {", "}", () -> { - writer.write("contents.body = body;"); + + " context: __SerdeContext,\n" + + " headers: __HeaderBag,\n" + + " path: string,\n" + + " resolvedHostname: string | undefined,\n" + + " body: any,\n" + + "): Promise<$T> => {", "};", requestType, () -> { + // Get the hostname, port, and scheme from client's resolved endpoint. Then construct the request from + // them. The client's resolved endpoint can be default one or supplied by users. + writer.write( + "const {hostname, protocol = \"https\", port, path: basePath} = await context.endpoint();"); + writer.openBlock("const contents: any = {", "};", () -> { + writer.write("protocol,"); + writer.write("hostname,"); + writer.write("port,"); + writer.write("method: \"POST\","); + writer.write( + "path: basePath.endsWith(\"/\") ? basePath.slice(0, -1) + path : basePath + path,"); + writer.write("headers,"); + }); + writer.openBlock("if (resolvedHostname !== undefined) {", "}", () -> { + writer.write("contents.hostname = resolvedHostname;"); + }); + writer.openBlock("if (body !== undefined) {", "}", () -> { + writer.write("contents.body = body;"); + }); + writer.write("return new $T(contents);", requestSymbol); }); - writer.write("return new $T(contents);", requestSymbol); - } - ); // Write common request header to be shared by all requests writeSharedRequestHeaders(context); writer.write(""); writer.write( - context.getStringStore().flushVariableDeclarationCode() - ); + context.getStringStore().flushVariableDeclarationCode()); writer.addImport("HttpRequest", "__HttpRequest", TypeScriptDependency.PROTOCOL_HTTP); writer.addImport("HttpResponse", "__HttpResponse", TypeScriptDependency.PROTOCOL_HTTP); @@ -237,15 +224,14 @@ public void generateResponseDeserializers(GenerationContext context) { SymbolReference responseType = getApplicationProtocol().getResponseType(); Set errorShapes = HttpProtocolGeneratorUtils.generateUnifiedErrorDispatcher( - context, - containedOperations.stream().toList(), - responseType, - this::writeErrorCodeParser, - isErrorCodeInBody, - this::getErrorBodyLocation, - this::getOperationErrors, - getErrorAliases(context, containedOperations) - ); + context, + containedOperations.stream().toList(), + responseType, + this::writeErrorCodeParser, + isErrorCodeInBody, + this::getErrorBodyLocation, + this::getOperationErrors, + getErrorAliases(context, containedOperations)); deserializingErrorShapes.addAll(errorShapes); } @@ -268,26 +254,29 @@ protected void generateOperationSerializer(GenerationContext context, OperationS writer.writeDocs(methodLongName); writer.openBlock("export const $L = async (\n" - + " input: $T,\n" - + " context: $L\n" - + "): Promise<$T> => {", "};", - methodName, inputType, serdeContextType, requestType, - () -> { - writeRequestHeaders(context, operation); - boolean hasRequestBody = writeRequestBody(context, operation); - boolean hasHostPrefix = operation.hasTrait(EndpointTrait.class); - - if (hasHostPrefix) { - HttpProtocolGeneratorUtils.writeHostPrefix(context, operation); - } - - // Construct the request with the operation's path and optional hostname and body. - writer.write("return buildHttpRpcRequest(context, headers, $S, $L, $L);", - getOperationPath(context, operation), - hasHostPrefix ? "resolvedHostname" : "undefined", - hasRequestBody ? "body" : "undefined"); - } - ); + + " input: $T,\n" + + " context: $L\n" + + "): Promise<$T> => {", + "};", + methodName, + inputType, + serdeContextType, + requestType, + () -> { + writeRequestHeaders(context, operation); + boolean hasRequestBody = writeRequestBody(context, operation); + boolean hasHostPrefix = operation.hasTrait(EndpointTrait.class); + + if (hasHostPrefix) { + HttpProtocolGeneratorUtils.writeHostPrefix(context, operation); + } + + // Construct the request with the operation's path and optional hostname and body. + writer.write("return buildHttpRpcRequest(context, headers, $S, $L, $L);", + getOperationPath(context, operation), + hasHostPrefix ? "resolvedHostname" : "undefined", + hasRequestBody ? "body" : "undefined"); + }); writer.write(""); } @@ -365,15 +354,17 @@ protected void writeSharedRequestHeaders(GenerationContext context) { protected boolean writeRequestBody(GenerationContext context, OperationShape operation) { if (operation.getInput().isPresent()) { // If there's an input present, we know it's a structure. - StructureShape inputShape = context.getModel().expectShape(operation.getInput().get()) - .asStructureShape().get(); + StructureShape inputShape = context.getModel() + .expectShape(operation.getInput().get()) + .asStructureShape() + .get(); TypeScriptWriter writer = context.getWriter(); // Write the default `body` property. writer.write("let body: any;"); if (EventStreamGenerator.hasEventStreamInput(context, operation)) { MemberShape eventStreamMember = EventStreamGenerator.getEventStreamMember( - context, inputShape - ); + context, + inputShape); Shape target = context.getModel().expectShape(eventStreamMember.getTarget()); Symbol targetSymbol = context.getSymbolProvider().toSymbol(target); String serFunctionName = ProtocolGenerator.getSerFunctionShortName(targetSymbol); @@ -463,37 +454,42 @@ protected void generateOperationDeserializer(GenerationContext context, Operatio String methodName = ProtocolGenerator.getDeserFunctionShortName(symbol); String methodLongName = ProtocolGenerator.getDeserFunctionName(symbol, getName()); String errorMethodName = "de_CommandError"; - String serdeContextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), writer, - context.getModel(), operation); + String serdeContextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), + writer, + context.getModel(), + operation); // Add the normalized output type. Symbol outputType = symbol.expectProperty("outputType", Symbol.class); // Handle the general response. writer.writeDocs(methodLongName); writer.openBlock("export const $L = async (\n" - + " output: $T,\n" - + " context: $L\n" - + "): Promise<$T> => {", "};", - methodName, responseType, serdeContextType, outputType, - () -> { - // Redirect error deserialization to the dispatcher - writer.openBlock("if (output.statusCode >= 300) {", "}", () -> { - writer.write("return $L(output, context);", errorMethodName); - }); + + " output: $T,\n" + + " context: $L\n" + + "): Promise<$T> => {", + "};", + methodName, + responseType, + serdeContextType, + outputType, + () -> { + // Redirect error deserialization to the dispatcher + writer.openBlock("if (output.statusCode >= 300) {", "}", () -> { + writer.write("return $L(output, context);", errorMethodName); + }); - // Start deserializing the response. - readResponseBody(context, operation); + // Start deserializing the response. + readResponseBody(context, operation); - // Build the response with typing and metadata. - writer.openBlock("const response: $T = {", "};", outputType, () -> { - writer.write("$$metadata: deserializeMetadata(output),"); - operation.getOutput().ifPresent(outputId -> { - writer.write("...contents,"); + // Build the response with typing and metadata. + writer.openBlock("const response: $T = {", "};", outputType, () -> { + writer.write("$$metadata: deserializeMetadata(output),"); + operation.getOutput().ifPresent(outputId -> { + writer.write("...contents,"); + }); }); + writer.write("return response;"); }); - writer.write("return response;"); - } - ); writer.write(""); } @@ -511,42 +507,43 @@ protected void generateErrorDeserializer(GenerationContext context, StructureSha writer.writeDocs(errorDeserMethodLongName); writer.openBlock("const $L = async (\n" - + " $L: any,\n" - + " context: __SerdeContext\n" - + "): Promise<$T> => {", "};", errorDeserMethodName, outputReference, errorSymbol, () -> { - // First deserialize the body properly. - if (isErrorCodeInBody) { - // Body is already parsed in the error dispatcher, simply assign the body. - writer.write("const body = $L.body", outputReference); - } else { - // The dispatcher defers parsing the body in cases where protocols do not have - // their error code in the body, so we handle that parsing before deserializing - // the error shape here. - writer.write("const body = parseBody($L.body, context);", outputReference); - } + + " $L: any,\n" + + " context: __SerdeContext\n" + + "): Promise<$T> => {", "};", errorDeserMethodName, outputReference, errorSymbol, () -> { + // First deserialize the body properly. + if (isErrorCodeInBody) { + // Body is already parsed in the error dispatcher, simply assign the body. + writer.write("const body = $L.body", outputReference); + } else { + // The dispatcher defers parsing the body in cases where protocols do not have + // their error code in the body, so we handle that parsing before deserializing + // the error shape here. + writer.write("const body = parseBody($L.body, context);", outputReference); + } - if (SerdeElisionIndex.of(context.getModel()).mayElide(error) && enableSerdeElision()) { - writer.addImport("_json", null, TypeScriptDependency.AWS_SMITHY_CLIENT); - writer.write("const deserialized: any = _json($L);", - getErrorBodyLocation(context, "body")); - } else { - writer.write("const deserialized: any = $L($L, context);", - ProtocolGenerator.getDeserFunctionShortName(errorSymbol), - getErrorBodyLocation(context, "body")); - } + if (SerdeElisionIndex.of(context.getModel()).mayElide(error) && enableSerdeElision()) { + writer.addImport("_json", null, TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.write("const deserialized: any = _json($L);", + getErrorBodyLocation(context, "body")); + } else { + writer.write("const deserialized: any = $L($L, context);", + ProtocolGenerator.getDeserFunctionShortName(errorSymbol), + getErrorBodyLocation(context, "body")); + } - // Then load it into the object with additional error and response properties. - Symbol materializedError = errorSymbol.toBuilder() - .putProperty("typeOnly", false) - .build(); - writer.openBlock("const exception = new $T({", "});", materializedError, () -> { - writer.write("$$metadata: deserializeMetadata($L),", outputReference); - writer.write("...deserialized"); - }); - writer.addImport("decorateServiceException", "__decorateServiceException", - TypeScriptDependency.AWS_SMITHY_CLIENT); - writer.write("return __decorateServiceException(exception, body);"); - }); + // Then load it into the object with additional error and response properties. + Symbol materializedError = errorSymbol.toBuilder() + .putProperty("typeOnly", false) + .build(); + writer.openBlock("const exception = new $T({", "});", materializedError, () -> { + writer.write("$$metadata: deserializeMetadata($L),", outputReference); + writer.write("...deserialized"); + }); + writer.addImport("decorateServiceException", + "__decorateServiceException", + TypeScriptDependency.AWS_SMITHY_CLIENT); + writer.write("return __decorateServiceException(exception, body);"); + }); writer.write(""); } @@ -560,15 +557,14 @@ protected void readResponseBody(GenerationContext context, OperationShape operat StructureShape outputShape = context.getModel().expectShape(outputId).asStructureShape().get(); if (EventStreamGenerator.hasEventStreamOutput(context, operation)) { MemberShape eventStreamMember = EventStreamGenerator.getEventStreamMember( - context, outputShape - ); + context, + outputShape); Shape target = context.getModel().expectShape(eventStreamMember.getTarget()); Symbol targetSymbol = context.getSymbolProvider().toSymbol(target); writer.write( - "const contents = { $L: $L(output.body, context) };", - eventStreamMember.getMemberName(), - ProtocolGenerator.getDeserFunctionShortName(targetSymbol) - ); + "const contents = { $L: $L(output.body, context) };", + eventStreamMember.getMemberName(), + ProtocolGenerator.getDeserFunctionShortName(targetSymbol)); } else { // We only need to load the body and prepare a contents object if there is a response. writer.write("const data: any = await parseBody(output.body, context)"); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGenerator.java index 4c1c29aab00..e3b8ec72991 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGenerator.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Collection; @@ -118,9 +107,11 @@ default ApplicationProtocol resolveApplicationProtocol( .collect(Collectors.joining(", ")); throw new CodegenException(String.format( "All of the protocols generated for a service must be runtime compatible, but " - + "protocol `%s` is incompatible with other application protocols: [%s]. Please pick a " - + "set of compatible protocols using the `protocols` option when generating %s.", - getProtocol().getName(), protocolNames, service.getId())); + + "protocol `%s` is incompatible with other application protocols: [%s]. Please pick a " + + "set of compatible protocols using the `protocols` option when generating %s.", + getProtocol().getName(), + protocolNames, + service.getId())); } return other; @@ -131,8 +122,7 @@ default ApplicationProtocol resolveApplicationProtocol( * * @param context Serde context. */ - default void generateSharedComponents(GenerationContext context) { - } + default void generateSharedComponents(GenerationContext context) {} /** * Generates the code used to serialize the shapes of a service @@ -317,8 +307,7 @@ default Map getOperationErrors(GenerationContext context, Colle Map errors = new LinkedHashMap<>(); for (OperationShape operation : operations) { errors.putAll( - getOperationErrors(context, operation) - ); + getOperationErrors(context, operation)); } return errors; } @@ -327,7 +316,8 @@ default Map getOperationErrors(GenerationContext context, Colle * @return map of fully qualified shape id to aliases and/or short names that should map to the same error. */ default Map> getErrorAliases( - GenerationContext context, Collection operations + GenerationContext context, + Collection operations ) { return Collections.emptyMap(); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java index 1c69fa9a952..f019727bae8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Collections; @@ -82,8 +71,9 @@ private RuntimeClientPlugin(Builder builder) { if (!(allNull || allSet)) { throw new IllegalStateException( "If any of inputConfig, resolvedConfig, or resolveFunction are set, then all of " - + "inputConfig, resolvedConfig, and resolveFunction must be set: inputConfig: " - + inputConfig + ", resolvedConfig: " + resolvedConfig + ", resolveFunction: " + resolveFunction); + + "inputConfig, resolvedConfig, and resolveFunction must be set: inputConfig: " + + inputConfig + ", resolvedConfig: " + resolvedConfig + ", resolveFunction: " + + resolveFunction); } if (destroyFunction != null && resolvedConfig == null) { @@ -211,9 +201,9 @@ public Optional getResolveFunction() { * for a parameter, and value is the value for a parameter. */ public Map getAdditionalResolveFunctionParameters( - Model model, - ServiceShape service, - OperationShape operation + Model model, + ServiceShape service, + OperationShape operation ) { if (additionalResolveFunctionParamsSupplier != null) { return additionalResolveFunctionParamsSupplier.apply(model, service, operation); @@ -259,9 +249,9 @@ public Optional getPluginFunction() { * for a parameter, and value is the value for a parameter. */ public Map getAdditionalPluginFunctionParameters( - Model model, - ServiceShape service, - OperationShape operation + Model model, + ServiceShape service, + OperationShape operation ) { if (additionalPluginFunctionParamsSupplier != null) { return additionalPluginFunctionParamsSupplier.apply(model, service, operation); @@ -282,9 +272,9 @@ public Map getAdditionalPluginFunctionParameters( * for a parameter, and value is the value for a parameter. */ public Map getAdditionalPluginFunctionParameterWriterConsumers( - Model model, - ServiceShape service, - OperationShape operation + Model model, + ServiceShape service, + OperationShape operation ) { if (additionalPluginFunctionParamsSupplier != null) { return additionalPluginFunctionParamsSupplier.apply(model, service, operation); @@ -398,12 +388,12 @@ public Builder toBuilder() { @Override public String toString() { return "RuntimeClientPlugin{" - + "inputConfig=" + inputConfig - + ", resolvedConfig=" + resolvedConfig - + ", resolveFunction=" + resolveFunction - + ", pluginFunction=" + pluginFunction - + ", destroyFunction=" + destroyFunction - + '}'; + + "inputConfig=" + inputConfig + + ", resolvedConfig=" + resolvedConfig + + ", resolveFunction=" + resolveFunction + + ", pluginFunction=" + pluginFunction + + ", destroyFunction=" + destroyFunction + + '}'; } @Override @@ -416,14 +406,14 @@ public boolean equals(Object o) { RuntimeClientPlugin that = (RuntimeClientPlugin) o; return Objects.equals(inputConfig, that.inputConfig) - && Objects.equals(resolvedConfig, that.resolvedConfig) - && Objects.equals(resolveFunction, that.resolveFunction) - && Objects.equals(additionalResolveFunctionParamsSupplier, that.additionalResolveFunctionParamsSupplier) - && Objects.equals(pluginFunction, that.pluginFunction) - && Objects.equals(additionalPluginFunctionParamsSupplier, that.additionalPluginFunctionParamsSupplier) - && Objects.equals(destroyFunction, that.destroyFunction) - && servicePredicate.equals(that.servicePredicate) - && operationPredicate.equals(that.operationPredicate); + && Objects.equals(resolvedConfig, that.resolvedConfig) + && Objects.equals(resolveFunction, that.resolveFunction) + && Objects.equals(additionalResolveFunctionParamsSupplier, that.additionalResolveFunctionParamsSupplier) + && Objects.equals(pluginFunction, that.pluginFunction) + && Objects.equals(additionalPluginFunctionParamsSupplier, that.additionalPluginFunctionParamsSupplier) + && Objects.equals(destroyFunction, that.destroyFunction) + && servicePredicate.equals(that.servicePredicate) + && operationPredicate.equals(that.operationPredicate); } @Override @@ -578,13 +568,12 @@ public Builder resolveFunction(Symbol resolveFunction) { * @see #getResolveFunction() */ public Builder resolveFunction( - Symbol resolveFunction, - FunctionParamsSupplier additionalResolveFunctionParamsSupplier + Symbol resolveFunction, + FunctionParamsSupplier additionalResolveFunctionParamsSupplier ) { return resolveFunction( - SymbolReference.builder().symbol(resolveFunction).build(), - additionalResolveFunctionParamsSupplier - ); + SymbolReference.builder().symbol(resolveFunction).build(), + additionalResolveFunctionParamsSupplier); } /** @@ -600,7 +589,7 @@ public Builder resolveFunction( * @see #getResolveFunction() */ public Builder additionalResolveFunctionParamsSupplier( - FunctionParamsSupplier additionalResolveFunctionParamsSupplier + FunctionParamsSupplier additionalResolveFunctionParamsSupplier ) { this.additionalResolveFunctionParamsSupplier = additionalResolveFunctionParamsSupplier; return this; @@ -660,13 +649,12 @@ public Builder pluginFunction(Symbol pluginFunction) { * @see #getPluginFunction() */ public Builder pluginFunction( - Symbol pluginFunction, - FunctionParamsSupplier additionalPluginFunctionParamsSupplier + Symbol pluginFunction, + FunctionParamsSupplier additionalPluginFunctionParamsSupplier ) { return pluginFunction( - SymbolReference.builder().symbol(pluginFunction).build(), - additionalPluginFunctionParamsSupplier - ); + SymbolReference.builder().symbol(pluginFunction).build(), + additionalPluginFunctionParamsSupplier); } /** @@ -679,7 +667,7 @@ public Builder pluginFunction( * @see #getPluginFunction() */ public Builder additionalPluginFunctionParamsSupplier( - FunctionParamsSupplier additionalPluginFunctionParamsSupplier + FunctionParamsSupplier additionalPluginFunctionParamsSupplier ) { this.additionalPluginFunctionParamsSupplier = additionalPluginFunctionParamsSupplier; return this; @@ -808,7 +796,7 @@ public Builder withAdditionalClientParams(Map writ * Enables access to the writer for adding imports/dependencies. */ public Builder withAdditionalOperationParams( - Map writeAdditionalOperationParams + Map writeAdditionalOperationParams ) { // enforce consistent sorting during codegen. this.writeAdditionalOperationParams = new TreeMap<>(writeAdditionalOperationParams); @@ -867,14 +855,17 @@ public Builder withConventions( switch (convention) { case HAS_CONFIG: inputConfig(Convention.createTypeSymbol( - packageName, version, pluginName + "InputConfig") - ); + packageName, + version, + pluginName + "InputConfig")); resolvedConfig(Convention.createTypeSymbol( - packageName, version, pluginName + "ResolvedConfig") - ); + packageName, + version, + pluginName + "ResolvedConfig")); resolveFunction(Convention.createSymbol( - packageName, version, "resolve" + pluginName + "Config") - ); + packageName, + version, + "resolve" + pluginName + "Config")); break; case HAS_MIDDLEWARE: pluginFunction(Convention.createSymbol(packageName, version, "get" + pluginName + "Plugin")); @@ -951,11 +942,11 @@ private static Symbol createSymbol(String packageName, String version, String na private static Symbol createTypeSymbol(String packageName, String version, String name) { return Symbol.builder() - .namespace(packageName, "/") - .name(name) - .putProperty("typeOnly", true) - .addDependency(TypeScriptDependency.NORMAL_DEPENDENCY, packageName, version) - .build(); + .namespace(packageName, "/") + .name(name) + .putProperty("typeOnly", true) + .addDependency(TypeScriptDependency.NORMAL_DEPENDENCY, packageName, version) + .build(); } } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/TypeScriptIntegration.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/TypeScriptIntegration.java index 75dda21d0e1..cb69fd9f01f 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/TypeScriptIntegration.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/TypeScriptIntegration.java @@ -1,18 +1,7 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import java.util.Collections; @@ -227,7 +216,7 @@ default Map> getRuntimeConfigWriters( SymbolProvider symbolProvider, LanguageTarget target ) { - return Collections.emptyMap(); + return Collections.emptyMap(); } /** @@ -258,8 +247,8 @@ default Map> getRuntimeConfigWriters( * @return list of client configuration interface */ default List getExtensionConfigurationInterfaces( - Model model, - TypeScriptSettings settings + Model model, + TypeScriptSettings settings ) { return Collections.emptyList(); } @@ -269,10 +258,10 @@ default List getExtensionConfigurationInterface */ @SmithyInternalApi default void prepareCustomizations( - TypeScriptWriter writer, - LanguageTarget target, - TypeScriptSettings settings, - Model model + TypeScriptWriter writer, + LanguageTarget target, + TypeScriptSettings settings, + Model model ) { return; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndex.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndex.java index e64fa170cc4..340cc6f3439 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndex.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndex.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.knowledge; import java.util.HashMap; @@ -30,12 +29,16 @@ public class SerdeElisionIndex implements KnowledgeIndex { private final Map elisionBinding = new HashMap<>(); private final Map mutatingTraits = MapUtils.of( - "jsonName", JsonNameTrait.ID, - "streaming", StreamingTrait.ID, - "mediaType", MediaTypeTrait.ID, - "sparse", SparseTrait.ID, - "idempotencyToken", IdempotencyTokenTrait.ID - ); + "jsonName", + JsonNameTrait.ID, + "streaming", + StreamingTrait.ID, + "mediaType", + MediaTypeTrait.ID, + "sparse", + SparseTrait.ID, + "idempotencyToken", + IdempotencyTokenTrait.ID); public SerdeElisionIndex(Model model) { for (Shape shape : model.toSet()) { @@ -69,7 +72,7 @@ private boolean hasMutatingTraits(Shape shape, Model model) { } } Selector selector = Selector.parse( - "[id = '" + shape.getId() + "']" + " ~> [trait|" + entry.getKey() + "]"); + "[id = '" + shape.getId() + "']" + " ~> [trait|" + entry.getKey() + "]"); if (!selector.select(model).isEmpty()) { return true; } @@ -93,16 +96,25 @@ private boolean hasIncompatibleTypes(Shape shape, Model model, int depth) { case SET: return hasIncompatibleTypes(target.asSetShape().get().getMember(), model, depth + 1); case STRUCTURE: - return target.asStructureShape().get().getAllMembers().values().stream().anyMatch( - s -> hasIncompatibleTypes(s, model, depth + 1) - ); + return target.asStructureShape() + .get() + .getAllMembers() + .values() + .stream() + .anyMatch( + s -> hasIncompatibleTypes(s, model, depth + 1)); case UNION: - return target.asUnionShape().get().getAllMembers().values().stream().anyMatch( - s -> hasIncompatibleTypes(s, model, depth + 1) - ); + return target.asUnionShape() + .get() + .getAllMembers() + .values() + .stream() + .anyMatch( + s -> hasIncompatibleTypes(s, model, depth + 1)); case MAP: return hasIncompatibleTypes(model.getShape(target.asMapShape().get().getValue().getTarget()).get(), - model, depth + 1); + model, + depth + 1); case BIG_DECIMAL: case BIG_INTEGER: case BLOB: diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/ServiceClosure.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/ServiceClosure.java index 9e0017cc0de..388b9acfb7d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/ServiceClosure.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/ServiceClosure.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.knowledge; import java.util.ArrayList; @@ -96,8 +95,8 @@ public final class ServiceClosure implements KnowledgeIndex { private final Set scanned = new HashSet<>(); private ServiceClosure( - Model model, - ServiceShape service + Model model, + ServiceShape service ) { this.model = model; this.service = service; @@ -280,7 +279,7 @@ private void scan(Collection shapes) { }); } case BYTE, INT_ENUM, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BIG_INTEGER, BIG_DECIMAL, BOOLEAN, STRING, - TIMESTAMP, DOCUMENT, ENUM, BLOB -> { + TIMESTAMP, DOCUMENT, ENUM, BLOB -> { if (shape.isEnumShape() || shape.isIntEnumShape() || shape.hasTrait(EnumTrait.class)) { enums.add(shape); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/AddProtocols.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/AddProtocols.java index c1f5287de5c..d17c8ed590b 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/AddProtocols.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/AddProtocols.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols; import java.util.List; @@ -21,7 +20,6 @@ public class AddProtocols implements TypeScriptIntegration { @Override public List getProtocolGenerators() { return ListUtils.of( - new SmithyRpcV2Cbor() - ); + new SmithyRpcV2Cbor()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/ProtocolPriorityConfig.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/ProtocolPriorityConfig.java index 17a5fa91b32..e46570cffaa 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/ProtocolPriorityConfig.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/ProtocolPriorityConfig.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols; import java.util.ArrayList; @@ -12,7 +11,6 @@ import java.util.Objects; import software.amazon.smithy.model.shapes.ShapeId; - /** * Allows customization of protocol selection for specific services or a global default ordering. */ @@ -21,11 +19,11 @@ public final class ProtocolPriorityConfig { private final List customDefaultPriority; public ProtocolPriorityConfig( - Map> serviceProtocolPriorityCustomizations, - List customDefaultPriority + Map> serviceProtocolPriorityCustomizations, + List customDefaultPriority ) { this.serviceProtocolPriorityCustomizations = - Objects.requireNonNullElseGet(serviceProtocolPriorityCustomizations, HashMap::new); + Objects.requireNonNullElseGet(serviceProtocolPriorityCustomizations, HashMap::new); this.customDefaultPriority = customDefaultPriority; } @@ -35,8 +33,7 @@ public ProtocolPriorityConfig( */ public List getProtocolPriority(ShapeId serviceShapeId) { return serviceProtocolPriorityCustomizations.getOrDefault( - serviceShapeId, - customDefaultPriority != null ? new ArrayList<>(customDefaultPriority) : null - ); + serviceShapeId, + customDefaultPriority != null ? new ArrayList<>(customDefaultPriority) : null); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/SmithyProtocolUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/SmithyProtocolUtils.java index c9c5d45a753..992b67dc277 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/SmithyProtocolUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/SmithyProtocolUtils.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols; import java.util.Set; @@ -20,7 +19,6 @@ import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; import software.amazon.smithy.utils.SmithyInternalApi; - /** * Utility methods for generating Smithy protocols. */ @@ -42,9 +40,9 @@ private SmithyProtocolUtils() {} * @param visitor A ShapeVisitor that generates a serde function for shapes. */ public static void generateDocumentBodyShapeSerde( - ProtocolGenerator.GenerationContext context, - Set shapes, - ShapeVisitor visitor + ProtocolGenerator.GenerationContext context, + Set shapes, + ShapeVisitor visitor ) { Walker shapeWalker = new Walker(NeighborProviderIndex.of(context.getModel()).getProvider()); Set shapesToGenerate = new TreeSet<>(shapes); @@ -54,16 +52,16 @@ public static void generateDocumentBodyShapeSerde( public static void generateProtocolTests(ProtocolGenerator generator, ProtocolGenerator.GenerationContext context) { new HttpProtocolTestGenerator(context, - generator, - SmithyProtocolUtils::filterProtocolTests, - SmithyProtocolUtils::filterMalformedRequestTests).run(); + generator, + SmithyProtocolUtils::filterProtocolTests, + SmithyProtocolUtils::filterMalformedRequestTests).run(); } private static boolean filterProtocolTests( - ServiceShape service, - OperationShape operation, - HttpMessageTestCase testCase, - TypeScriptSettings settings + ServiceShape service, + OperationShape operation, + HttpMessageTestCase testCase, + TypeScriptSettings settings ) { if (testCase.getTags().contains("defaults")) { return true; @@ -77,9 +75,9 @@ private static boolean filterProtocolTests( // TODO(cbor): enable test when it's working with vitest 3.x if (settings.generateSchemas() - && (testCase.getId().equals("RpcV2CborInvalidGreetingError") - || testCase.getId().equals("RpcV2CborComplexError") - || testCase.getId().equals("RpcV2CborEmptyComplexError"))) { + && (testCase.getId().equals("RpcV2CborInvalidGreetingError") + || testCase.getId().equals("RpcV2CborComplexError") + || testCase.getId().equals("RpcV2CborEmptyComplexError"))) { return true; } @@ -87,10 +85,10 @@ private static boolean filterProtocolTests( } private static boolean filterMalformedRequestTests( - ServiceShape service, - OperationShape operation, - HttpMalformedRequestTestCase testCase, - TypeScriptSettings settings + ServiceShape service, + OperationShape operation, + HttpMalformedRequestTestCase testCase, + TypeScriptSettings settings ) { // Handling overflow/underflow of longs in JS is extraordinarily tricky. // Numbers are actually all 62-bit floats, and so any integral number is diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitor.java index 5c2b2085dbf..b1b0459f557 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitor.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols.cbor; import software.amazon.smithy.model.knowledge.HttpBinding; @@ -27,8 +26,10 @@ public class CborMemberDeserVisitor extends DocumentMemberDeserVisitor { * @param dataSource The in-code location of the data to provide an output of * ({@code output.foo}, {@code entry}, etc.) */ - public CborMemberDeserVisitor(ProtocolGenerator.GenerationContext context, - String dataSource) { + public CborMemberDeserVisitor( + ProtocolGenerator.GenerationContext context, + String dataSource + ) { super(context, dataSource, TimestampFormatTrait.Format.EPOCH_SECONDS); this.context = context; context.getWriter().addImport("_json", null, TypeScriptDependency.AWS_SMITHY_CLIENT); @@ -77,13 +78,12 @@ public String bigDecimalShape(BigDecimalShape shape) { @Override public String timestampShape(TimestampShape shape) { return HttpProtocolGeneratorUtils.getTimestampOutputParam( - context.getWriter(), - dataSource, - HttpBinding.Location.DOCUMENT, - shape, - TimestampFormatTrait.Format.EPOCH_SECONDS, - requiresNumericEpochSecondsInPayload(), - context.getSettings().generateClient() - ); + context.getWriter(), + dataSource, + HttpBinding.Location.DOCUMENT, + shape, + TimestampFormatTrait.Format.EPOCH_SECONDS, + requiresNumericEpochSecondsInPayload(), + context.getSettings().generateClient()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitor.java index b13c84ed068..cf7d36a4997 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitor.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols.cbor; import software.amazon.smithy.model.shapes.BigDecimalShape; @@ -29,8 +28,10 @@ public class CborMemberSerVisitor extends DocumentMemberSerVisitor { * @param dataSource The in-code location of the data to provide an input of * ({@code input.foo}, {@code entry}, etc.) */ - public CborMemberSerVisitor(ProtocolGenerator.GenerationContext context, - String dataSource) { + public CborMemberSerVisitor( + ProtocolGenerator.GenerationContext context, + String dataSource + ) { super(context, dataSource, TimestampFormatTrait.Format.EPOCH_SECONDS); this.context = context; this.serdeElisionEnabled = true; @@ -81,9 +82,12 @@ public String bigIntegerShape(BigIntegerShape shape) { */ @Override public String bigDecimalShape(BigDecimalShape shape) { - context.getWriter().addImportSubmodule( - "nv", "__nv", TypeScriptDependency.SMITHY_CORE, "/serde" - ); + context.getWriter() + .addImportSubmodule( + "nv", + "__nv", + TypeScriptDependency.SMITHY_CORE, + "/serde"); return "__nv(" + dataSource + ")"; } @@ -92,12 +96,12 @@ public String bigDecimalShape(BigDecimalShape shape) { */ @Override public String timestampShape(TimestampShape shape) { - context.getWriter().addImportSubmodule( - "dateToTag", - "__dateToTag", - TypeScriptDependency.SMITHY_CORE, - SmithyCoreSubmodules.CBOR - ); + context.getWriter() + .addImportSubmodule( + "dateToTag", + "__dateToTag", + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); return "__dateToTag(" + dataSource + ")"; } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitor.java index e053d3f6087..6715d40d230 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitor.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols.cbor; import java.util.Map; @@ -25,7 +24,6 @@ import software.amazon.smithy.typescript.codegen.util.PropertyAccessor; import software.amazon.smithy.typescript.codegen.validation.UnaryFunctionCall; - public class CborShapeDeserVisitor extends DocumentShapeDeserVisitor { public CborShapeDeserVisitor(ProtocolGenerator.GenerationContext context) { @@ -50,28 +48,26 @@ protected void deserializeCollection(ProtocolGenerator.GenerationContext context writer.write("const collection = (output || [])$L", filterExpression); } else { writer.openBlock( - "const collection = (output || [])$L.map((entry: any) => {", - "});", - filterExpression, () -> { - if (filterExpression.isEmpty()) { - writer.openBlock("if (entry === null) {", "}", () -> { - if (!shape.hasTrait(SparseTrait.ID)) { - writer.write( - "throw new TypeError('All elements of the non-sparse list $S must be non-null.');", - shape.getId() - ); - } else { - writer.write("return null as any;"); - } - }); - } + "const collection = (output || [])$L.map((entry: any) => {", + "});", + filterExpression, + () -> { + if (filterExpression.isEmpty()) { + writer.openBlock("if (entry === null) {", "}", () -> { + if (!shape.hasTrait(SparseTrait.ID)) { + writer.write( + "throw new TypeError('All elements of the non-sparse list $S must be non-null.');", + shape.getId()); + } else { + writer.write("return null as any;"); + } + }); + } - writer.write("return $L$L;", - target.accept(getMemberVisitor("entry")), - usesExpect(target) ? " as any" : "" - ); - } - ); + writer.write("return $L$L;", + target.accept(getMemberVisitor("entry")), + usesExpect(target) ? " as any" : ""); + }); } writer.write("return collection;"); @@ -80,8 +76,8 @@ protected void deserializeCollection(ProtocolGenerator.GenerationContext context @Override protected void deserializeDocument(ProtocolGenerator.GenerationContext context, DocumentShape shape) { context.getWriter().write(""" - return output; // document. - """); + return output; // document. + """); } @Override @@ -91,28 +87,26 @@ protected void deserializeMap(ProtocolGenerator.GenerationContext context, MapSh SymbolProvider symbolProvider = context.getSymbolProvider(); writer.openBlock("return Object.entries(output).reduce((acc: $T, [key, value]: [string, any]) => {", - "", - symbolProvider.toSymbol(shape), - () -> { - writer.openBlock("if (value !== null) {", "}", () -> { - writer.write("acc[key as $T] = $L$L", - symbolProvider.toSymbol(shape.getKey()), - target.accept(getMemberVisitor("value")), - usesExpect(target) ? " as any;" : ";" - ); - }); - - if (shape.hasTrait(SparseTrait.ID)) { - writer.write("else {") - .indent(); - writer.write("acc[key as $T] = null as any;", symbolProvider.toSymbol(shape.getKey())) - .dedent(); - writer.write("}"); - } + "", + symbolProvider.toSymbol(shape), + () -> { + writer.openBlock("if (value !== null) {", "}", () -> { + writer.write("acc[key as $T] = $L$L", + symbolProvider.toSymbol(shape.getKey()), + target.accept(getMemberVisitor("value")), + usesExpect(target) ? " as any;" : ";"); + }); + + if (shape.hasTrait(SparseTrait.ID)) { + writer.write("else {") + .indent(); + writer.write("acc[key as $T] = null as any;", symbolProvider.toSymbol(shape.getKey())) + .dedent(); + writer.write("}"); + } - writer.write("return acc;"); - } - ); + writer.write("return acc;"); + }); writer.writeInline("}, {} as $T);", symbolProvider.toSymbol(shape)); } @@ -145,14 +139,12 @@ protected void deserializeStructure(ProtocolGenerator.GenerationContext context, boolean isUnaryCall = UnaryFunctionCall.check(functionExpression); if (isUnaryCall) { writer.write("'$1L': $2L,", - memberName, - UnaryFunctionCall.toRef(functionExpression) - ); + memberName, + UnaryFunctionCall.toRef(functionExpression)); } else { writer.write("'$1L': (_: any) => $2L,", - memberName, - functionExpression - ); + memberName, + functionExpression); } } } @@ -171,8 +163,7 @@ protected void deserializeUnion(ProtocolGenerator.GenerationContext context, Uni Shape target = model.expectShape(memberShape.getTarget()); String memberValue = target.accept( - getMemberVisitor(PropertyAccessor.getFrom("output", memberName)) - ); + getMemberVisitor(PropertyAccessor.getFrom("output", memberName))); if (usesExpect(target)) { writer.openBlock("if ($L !== undefined) {", "}", memberValue, () -> { @@ -180,18 +171,18 @@ protected void deserializeUnion(ProtocolGenerator.GenerationContext context, Uni }); } else { writer.openBlock( - "if ($1L != null) {", "}", - PropertyAccessor.getFrom("output", memberName), - () -> { - writer.write(""" - return { - $L: $L - } - """, - memberName, memberValue - ); - } - ); + "if ($1L != null) {", + "}", + PropertyAccessor.getFrom("output", memberName), + () -> { + writer.write(""" + return { + $L: $L + } + """, + memberName, + memberValue); + }); } }); writer.write("return { $$unknown: Object.entries(output)[0] };"); @@ -199,12 +190,12 @@ protected void deserializeUnion(ProtocolGenerator.GenerationContext context, Uni private CborMemberDeserVisitor getMemberVisitor(String dataSource) { return new CborMemberDeserVisitor( - getContext(), dataSource - ); + getContext(), + dataSource); } private boolean usesExpect(Shape shape) { return shape.isStringShape() || shape.isBooleanShape() - || (shape instanceof NumberShape && !shape.isBigDecimalShape() && !shape.isBigIntegerShape()); + || (shape instanceof NumberShape && !shape.isBigDecimalShape() && !shape.isBigIntegerShape()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitor.java index 73e3889f466..6ebbbfc7161 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitor.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols.cbor; import java.util.Map; @@ -29,7 +28,6 @@ import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; import software.amazon.smithy.typescript.codegen.validation.UnaryFunctionCall; - public class CborShapeSerVisitor extends DocumentShapeSerVisitor { /** * The service model's timestampFormat is ignored in RPCv2 CBOR protocol. @@ -69,8 +67,8 @@ protected void serializeCollection(ProtocolGenerator.GenerationContext context, @Override protected void serializeDocument(ProtocolGenerator.GenerationContext context, DocumentShape shape) { context.getWriter().write(""" - return input; // document. - """); + return input; // document. + """); } @Override @@ -81,31 +79,31 @@ protected void serializeMap(ProtocolGenerator.GenerationContext context, MapShap Symbol keySymbol = symbolProvider.toSymbol(shape.getKey()); String entryKeyType = keySymbol.toString().equals("string") - ? "string" - : symbolProvider.toSymbol(shape.getKey()) + "| string"; + ? "string" + : symbolProvider.toSymbol(shape.getKey()) + "| string"; writer.openBlock("return Object.entries(input).reduce((acc: Record, " - + "[key, value]: [$1L, any]) => {", "}, {});", entryKeyType, - () -> { - writer.write(""" - if (value !== null) { - acc[key] = $L; - } - """, - target.accept(getMemberVisitor("value")) - ); - - if (shape.hasTrait(SparseTrait.ID)) { + + "[key, value]: [$1L, any]) => {", + "}, {});", + entryKeyType, + () -> { writer.write(""" - else { - acc[key] = null as any; - } - """); - } + if (value !== null) { + acc[key] = $L; + } + """, + target.accept(getMemberVisitor("value"))); + + if (shape.hasTrait(SparseTrait.ID)) { + writer.write(""" + else { + acc[key] = null as any; + } + """); + } - writer.write("return acc;"); - } - ); + writer.write("return acc;"); + }); } @Override @@ -118,17 +116,19 @@ protected void serializeStructure(ProtocolGenerator.GenerationContext context, S Shape target = context.getModel().expectShape(memberShape.getTarget()); String valueExpression = (memberShape.hasTrait(TimestampFormatTrait.class) - ? HttpProtocolGeneratorUtils.getTimestampInputParam( - context, "_", memberShape, TIMESTAMP_FORMAT - ) - : target.accept(getMemberVisitor("_"))); + ? HttpProtocolGeneratorUtils.getTimestampInputParam( + context, + "_", + memberShape, + TIMESTAMP_FORMAT) + : target.accept(getMemberVisitor("_"))); String valueProvider = "_ => " + valueExpression; boolean isUnaryCall = UnaryFunctionCall.check(valueExpression); if (memberShape.hasTrait(IdempotencyTokenTrait.class)) { writer - .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); + .addImport("v4", "generateIdempotencyToken", TypeScriptDependency.SMITHY_UUID); writer.write("'$L': [true, _ => _ ?? generateIdempotencyToken()],", memberName); } else { if (valueProvider.equals("_ => _")) { @@ -153,8 +153,10 @@ protected void serializeUnion(ProtocolGenerator.GenerationContext context, Union Map members = new TreeMap<>(shape.getAllMembers()); members.forEach((memberName, memberShape) -> { Shape target = model.expectShape(memberShape.getTarget()); - writer.write("$L: value => ({ $S: $L }),", memberName, memberName, - target.accept(getMemberVisitor("value"))); + writer.write("$L: value => ({ $S: $L }),", + memberName, + memberName, + target.accept(getMemberVisitor("value"))); }); writer.write("_: (name, value) => ({ [name]: value } as any)"); }); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/SmithyRpcV2Cbor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/SmithyRpcV2Cbor.java index fdf2a2d40ff..23664e937a9 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/SmithyRpcV2Cbor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocols/cbor/SmithyRpcV2Cbor.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.protocols.cbor; import java.util.Set; @@ -50,42 +49,44 @@ public SmithyRpcV2Cbor() { public void generateSharedComponents(GenerationContext context) { TypeScriptWriter writer = context.getWriter(); - writer.addImportSubmodule("parseCborBody", "parseBody", - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR - ) - .addImportSubmodule("parseCborErrorBody", "parseErrorBody", - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR - ) - .addImportSubmodule("loadSmithyRpcV2CborErrorCode", null, - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR - ); + writer.addImportSubmodule("parseCborBody", + "parseBody", + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR) + .addImportSubmodule("parseCborErrorBody", + "parseErrorBody", + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR) + .addImportSubmodule("loadSmithyRpcV2CborErrorCode", + null, + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); ServiceShape service = context.getService(); deserializingErrorShapes.forEach(error -> generateErrorDeserializer(context, error)); eventStreamGenerator.generateEventStreamSerializers( - context, - service, - getDocumentContentType(), - () -> { - writer.addImportSubmodule( - "cbor", null, - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR - ); - writer.write("body = cbor.serialize(body);"); - }, - serializingDocumentShapes - ); + context, + service, + getDocumentContentType(), + () -> { + writer.addImportSubmodule( + "cbor", + null, + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); + writer.write("body = cbor.serialize(body);"); + }, + serializingDocumentShapes); Set errorEventShapes = new TreeSet<>(); SerdeElisionIndex serdeElisionIndex = SerdeElisionIndex.of(context.getModel()); eventStreamGenerator.generateEventStreamDeserializers( - context, - service, - errorEventShapes, - deserializingDocumentShapes, - true, - enableSerdeElision(), - serdeElisionIndex - ); + context, + service, + errorEventShapes, + deserializingDocumentShapes, + true, + enableSerdeElision(), + serdeElisionIndex); errorEventShapes.removeIf(deserializingErrorShapes::contains); errorEventShapes.forEach(error -> generateErrorDeserializer(context, error)); generateDocumentBodyShapeSerializers(context, serializingDocumentShapes); @@ -107,15 +108,15 @@ public void generateSharedComponents(GenerationContext context) { writer.addTypeImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES); writer.addTypeImport("HeaderBag", "__HeaderBag", TypeScriptDependency.SMITHY_TYPES); writer.addImportSubmodule( - "buildHttpRpcRequest", null, - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR - ); + "buildHttpRpcRequest", + null, + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); writeSharedRequestHeaders(context); writer.write(""); writer.write( - context.getStringStore().flushVariableDeclarationCode() - ); + context.getStringStore().flushVariableDeclarationCode()); } @Override @@ -126,8 +127,8 @@ public ShapeId getProtocol() { @Override public void generateProtocolTests(GenerationContext generationContext) { SmithyProtocolUtils.generateProtocolTests( - this, generationContext - ); + this, + generationContext); } @Override @@ -138,23 +139,19 @@ protected String getDocumentContentType() { @Override protected void generateDocumentBodyShapeSerializers(GenerationContext generationContext, Set shapes) { SmithyProtocolUtils.generateDocumentBodyShapeSerde( - generationContext, - shapes, - new CborShapeSerVisitor( - generationContext - ) - ); + generationContext, + shapes, + new CborShapeSerVisitor( + generationContext)); } @Override protected void generateDocumentBodyShapeDeserializers(GenerationContext generationContext, Set shapes) { SmithyProtocolUtils.generateDocumentBodyShapeSerde( - generationContext, - shapes, - new CborShapeDeserVisitor( - generationContext - ) - ); + generationContext, + shapes, + new CborShapeDeserVisitor( + generationContext)); } @Override @@ -168,46 +165,49 @@ protected void generateOperationDeserializer(GenerationContext context, Operatio String methodName = ProtocolGenerator.getDeserFunctionShortName(symbol); String methodLongName = ProtocolGenerator.getDeserFunctionName(symbol, getName()); String errorMethodName = "de_CommandError"; - String serdeContextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), writer, - context.getModel(), operation); + String serdeContextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), + writer, + context.getModel(), + operation); Symbol outputType = symbol.expectProperty("outputType", Symbol.class); writer.writeDocs(methodLongName); writer.openBlock(""" - export const $L = async ( - output: $T, - context: $L - ): Promise<$T> => {""", "};", - methodName, responseType, serdeContextType, outputType, - () -> { - writer.addImportSubmodule( - "checkCborResponse", "cr", - TypeScriptDependency.SMITHY_CORE, - SmithyCoreSubmodules.CBOR - ); - writer.write("cr(output);"); - - writer.write(""" - if (output.statusCode >= 300) { - return $L(output, context); - } - """, - errorMethodName - ); - - readResponseBody(context, operation); - - writer.write(""" - const response: $T = { - $$metadata: deserializeMetadata(output), $L - }; - return response; - """, - outputType, - operation.getOutput().map((o) -> "...contents,").orElse("") - ); - } - ); + export const $L = async ( + output: $T, + context: $L + ): Promise<$T> => {""", + "};", + methodName, + responseType, + serdeContextType, + outputType, + () -> { + writer.addImportSubmodule( + "checkCborResponse", + "cr", + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); + writer.write("cr(output);"); + + writer.write(""" + if (output.statusCode >= 300) { + return $L(output, context); + } + """, + errorMethodName); + + readResponseBody(context, operation); + + writer.write(""" + const response: $T = { + $$metadata: deserializeMetadata(output), $L + }; + return response; + """, + outputType, + operation.getOutput().map((o) -> "...contents,").orElse("")); + }); writer.write(""); } @@ -224,18 +224,21 @@ protected String getOperationPath(GenerationContext generationContext, Operation } @Override - protected void serializeInputDocument(GenerationContext generationContext, - OperationShape operationShape, - StructureShape inputStructure) { + protected void serializeInputDocument( + GenerationContext generationContext, + OperationShape operationShape, + StructureShape inputStructure + ) { TypeScriptWriter writer = generationContext.getWriter(); writer.addImportSubmodule( - "cbor", null, - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR - ); - writer.write("body = cbor.serialize($L);", inputStructure.accept( - new CborMemberSerVisitor(generationContext, "input")) - ); + "cbor", + null, + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); + writer.write("body = cbor.serialize($L);", + inputStructure.accept( + new CborMemberSerVisitor(generationContext, "input"))); } @Override @@ -243,24 +246,26 @@ protected void writeErrorCodeParser(GenerationContext generationContext) { TypeScriptWriter writer = generationContext.getWriter(); writer.addImportSubmodule( - "loadSmithyRpcV2CborErrorCode", null, - TypeScriptDependency.SMITHY_CORE, SmithyCoreSubmodules.CBOR - ); + "loadSmithyRpcV2CborErrorCode", + null, + TypeScriptDependency.SMITHY_CORE, + SmithyCoreSubmodules.CBOR); writer.write("const errorCode = loadSmithyRpcV2CborErrorCode(output, parsedOutput.body);"); } @Override - protected void deserializeOutputDocument(GenerationContext generationContext, - OperationShape operationShape, - StructureShape outputStructure) { + protected void deserializeOutputDocument( + GenerationContext generationContext, + OperationShape operationShape, + StructureShape outputStructure + ) { TypeScriptWriter writer = generationContext.getWriter(); - writer.write("contents = $L;", outputStructure.accept( - new CborMemberDeserVisitor( - generationContext, - "data" - ) - )); + writer.write("contents = $L;", + outputStructure.accept( + new CborMemberDeserVisitor( + generationContext, + "data"))); } @Override @@ -270,9 +275,9 @@ protected void writeSharedRequestHeaders(GenerationContext context) { writer.openBlock("const SHARED_HEADERS: __HeaderBag = {", "};", () -> { writer.write("'content-type': $S,", getDocumentContentType()); writer.write(""" - "smithy-protocol": "rpc-v2-cbor", - "accept": "application/cbor", - """); + "smithy-protocol": "rpc-v2-cbor", + "accept": "application/cbor", + """); }); } @@ -299,17 +304,17 @@ protected void writeRequestHeaders(GenerationContext context, OperationShape ope if (hasEventStreamOutput) { writer.write(""" - headers.accept = "application/vnd.amazon.eventstream"; - """); + headers.accept = "application/vnd.amazon.eventstream"; + """); } if (hasEventStreamInput) { writer.write(""" - headers["content-type"] = "application/vnd.amazon.eventstream"; - """); + headers["content-type"] = "application/vnd.amazon.eventstream"; + """); } else if (inputIsEmpty) { writer.write(""" - delete headers["content-type"]; - """); + delete headers["content-type"]; + """); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerationAllowlist.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerationAllowlist.java index a81919ac885..5e1d5b4c4ff 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerationAllowlist.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerationAllowlist.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.schema; import java.util.HashSet; @@ -12,7 +11,6 @@ import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.utils.SmithyInternalApi; - /** * * Controls rollout of schema generation. diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerator.java index e08bacbcd47..41ed4cdaaa8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerator.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.schema; import java.nio.file.Paths; @@ -51,12 +50,15 @@ public class SchemaGenerator implements Runnable { private final ServiceClosure closure; private final ReservedWords reservedWords = new ReservedWordsBuilder() - .loadWords(Objects.requireNonNull(TypeScriptClientCodegenPlugin.class.getResource("reserved-words.txt"))) - .build(); - - public SchemaGenerator(Model model, - FileManifest fileManifest, - TypeScriptSettings settings, SymbolProvider symbolProvider) { + .loadWords(Objects.requireNonNull(TypeScriptClientCodegenPlugin.class.getResource("reserved-words.txt"))) + .build(); + + public SchemaGenerator( + Model model, + FileManifest fileManifest, + TypeScriptSettings settings, + SymbolProvider symbolProvider + ) { this.model = model; this.fileManifest = fileManifest; closure = ServiceClosure.of(model, settings.getService(model)); @@ -64,7 +66,7 @@ public SchemaGenerator(Model model, this.settings = settings; this.symbolProvider = symbolProvider; writer.write(""" - /* eslint no-var: 0 */"""); + /* eslint no-var: 0 */"""); } /** @@ -90,9 +92,8 @@ public void run() { boolean hasContent = !writer.toString().matches("/\\* eslint no-var: 0 \\*/[\\s\\n]+$"); if (hasContent) { fileManifest.writeFile( - Paths.get(CodegenUtils.SOURCE_FOLDER, SCHEMAS_FOLDER, "schemas_0.ts").toString(), - stringConstants + "\n" + writer - ); + Paths.get(CodegenUtils.SOURCE_FOLDER, SCHEMAS_FOLDER, "schemas_0.ts").toString(), + stringConstants + "\n" + writer); } } @@ -120,15 +121,14 @@ private void writeSimpleSchema(Shape shape) { writer.addTypeImport("StaticSimpleSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.openBlock(""" export var $L: StaticSimpleSchema = [0, $L, $L,""", - "", - getShapeVariableName(shape), - store.var(shape.getId().getNamespace(), "n"), - store.var(shape.getId().getName()), - () -> { - writeTraits(shape); - writer.writeInline(", $L];", resolveSimpleSchema(shape, shape)); - } - ); + "", + getShapeVariableName(shape), + store.var(shape.getId().getNamespace(), "n"), + store.var(shape.getId().getName()), + () -> { + writeTraits(shape); + writer.writeInline(", $L];", resolveSimpleSchema(shape, shape)); + }); } } @@ -139,35 +139,31 @@ private void writeStructureSchema(StructureShape shape) { String exceptionCtorSymbolName = "__" + symbolName; writer.addTypeImport("StaticErrorSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.addRelativeImport( - symbolName, - exceptionCtorSymbolName, - Paths.get("..", "models", "errors") - ); + symbolName, + exceptionCtorSymbolName, + Paths.get("..", "models", "errors")); writer.openBlock(""" - export var $L: StaticErrorSchema = [-3, $L, $L,""", - "];", - getShapeVariableName(shape), - store.var(shape.getId().getNamespace(), "n"), - store.var(shape.getId().getName()), - () -> doWithMembers(shape) - ); + export var $L: StaticErrorSchema = [-3, $L, $L,""", + "];", + getShapeVariableName(shape), + store.var(shape.getId().getNamespace(), "n"), + store.var(shape.getId().getName()), + () -> doWithMembers(shape)); writer.addImportSubmodule("TypeRegistry", null, TypeScriptDependency.SMITHY_CORE, "/schema"); writer.write(""" - TypeRegistry.for($L).registerError($L, $L);""", - store.var(shape.getId().getNamespace(), "n"), - getShapeVariableName(shape), - exceptionCtorSymbolName - ); + TypeRegistry.for($L).registerError($L, $L);""", + store.var(shape.getId().getNamespace(), "n"), + getShapeVariableName(shape), + exceptionCtorSymbolName); } else { writer.addTypeImport("StaticStructureSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.openBlock(""" - export var $L: StaticStructureSchema = [3, $L, $L,""", - "];", - getShapeVariableName(shape), - store.var(shape.getId().getNamespace(), "n"), - store.var(shape.getId().getName()), - () -> doWithMembers(shape) - ); + export var $L: StaticStructureSchema = [3, $L, $L,""", + "];", + getShapeVariableName(shape), + store.var(shape.getId().getNamespace(), "n"), + store.var(shape.getId().getName()), + () -> doWithMembers(shape)); } }); } @@ -178,33 +174,30 @@ private void writeStructureSchema(StructureShape shape) { private void writeBaseError() { String serviceName = CodegenUtils.getServiceName(settings, model, symbolProvider); String serviceExceptionName = CodegenUtils.getSyntheticBaseExceptionName( - serviceName, model - ); + serviceName, + model); String namespace = settings.getService(model).getId().getNamespace(); String exceptionCtorSymbolName = "__" + serviceExceptionName; writer.addTypeImport("StaticErrorSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.addRelativeImport( - serviceExceptionName, - exceptionCtorSymbolName, - Paths.get("..", "models", serviceExceptionName) - ); + serviceExceptionName, + exceptionCtorSymbolName, + Paths.get("..", "models", serviceExceptionName)); String syntheticNamespace = store.var("smithy.ts.sdk.synthetic." + namespace); writer.write(""" export var $L: StaticErrorSchema = [-3, $L, $S, 0, [], []];""", - serviceExceptionName, - syntheticNamespace, - serviceExceptionName - ); + serviceExceptionName, + syntheticNamespace, + serviceExceptionName); writer.addImportSubmodule("TypeRegistry", null, TypeScriptDependency.SMITHY_CORE, "/schema"); writer.write(""" - TypeRegistry.for($L).registerError($L, $L);""", - syntheticNamespace, - serviceExceptionName, - exceptionCtorSymbolName - ); + TypeRegistry.for($L).registerError($L, $L);""", + syntheticNamespace, + serviceExceptionName, + exceptionCtorSymbolName); } private void writeUnionSchema(UnionShape shape) { @@ -212,12 +205,11 @@ private void writeUnionSchema(UnionShape shape) { writer.addTypeImport("StaticStructureSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.openBlock(""" export var $L: StaticStructureSchema = [3, $L, $L,""", - "];", - getShapeVariableName(shape), - store.var(shape.getId().getNamespace(), "n"), - store.var(shape.getId().getName()), - () -> doWithMembers(shape) - ); + "];", + getShapeVariableName(shape), + store.var(shape.getId().getNamespace(), "n"), + store.var(shape.getId().getName()), + () -> doWithMembers(shape)); }); } @@ -257,15 +249,13 @@ private void writeListSchema(CollectionShape shape) { writer.addTypeImport("StaticListSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.openBlock(""" export var $L: StaticListSchema = [1, $L, $L,""", - "];", - getShapeVariableName(shape), - store.var(shape.getId().getNamespace(), "n"), - store.var(shape.getId().getName()), - () -> this.doWithMember( - shape, - shape.getMember() - ) - ); + "];", + getShapeVariableName(shape), + store.var(shape.getId().getNamespace(), "n"), + store.var(shape.getId().getName()), + () -> this.doWithMember( + shape, + shape.getMember())); }); } @@ -274,16 +264,14 @@ private void writeMapSchema(MapShape shape) { writer.addTypeImport("StaticMapSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.openBlock(""" export var $L: StaticMapSchema = [2, $L, $L,""", - "];", - getShapeVariableName(shape), - store.var(shape.getId().getNamespace(), "n"), - store.var(shape.getId().getName()), - () -> this.doWithMember( - shape, - shape.getKey(), - shape.getValue() - ) - ); + "];", + getShapeVariableName(shape), + store.var(shape.getId().getNamespace(), "n"), + store.var(shape.getId().getName()), + () -> this.doWithMember( + shape, + shape.getKey(), + shape.getValue())); }); } @@ -295,13 +283,12 @@ private void doWithMember(Shape shape, MemberShape memberShape) { String ref = resolveSchema(shape, memberShape); if (elision.traits.hasSchemaTraits(memberShape)) { writer.openBlock( - ", [$L, ", - "]", - ref, - () -> { - writeTraits(memberShape); - } - ); + ", [$L, ", + "]", + ref, + () -> { + writeTraits(memberShape); + }); } else { writer.write(", $L", ref); } @@ -316,21 +303,19 @@ private void doWithMember(Shape shape, MemberShape keyShape, MemberShape memberS String valueRef = resolveSchema(shape, memberShape); if (elision.traits.hasSchemaTraits(memberShape) || elision.traits.hasSchemaTraits(keyShape)) { writer.openBlock( - ", [$L, ", - "]", - keyRef, - () -> { - writeTraits(keyShape); - } - ); + ", [$L, ", + "]", + keyRef, + () -> { + writeTraits(keyShape); + }); writer.openBlock( - ", [$L, ", - "]", - valueRef, - () -> { - writeTraits(memberShape); - } - ); + ", [$L, ", + "]", + valueRef, + () -> { + writeTraits(memberShape); + }); } else { writer.write(", $L, $L", keyRef, valueRef); } @@ -339,27 +324,25 @@ private void doWithMember(Shape shape, MemberShape keyShape, MemberShape memberS private void writeOperationSchema(OperationShape shape) { writer.addTypeImport("StaticOperationSchema", null, TypeScriptDependency.SMITHY_TYPES); writer.openBlock(""" - export var $L: StaticOperationSchema = [9, $L, $L,""", - "];", - getShapeVariableName(shape), - store.var(shape.getId().getNamespace(), "n"), - store.var(shape.getId().getName()), - () -> { - writeTraits(shape); - writer.write(""" - , () => $L, () => $L""", - getShapeVariableName(model.expectShape(shape.getInputShape())), - getShapeVariableName(model.expectShape(shape.getOutputShape())) - ); - } - ); + export var $L: StaticOperationSchema = [9, $L, $L,""", + "];", + getShapeVariableName(shape), + store.var(shape.getId().getNamespace(), "n"), + store.var(shape.getId().getName()), + () -> { + writeTraits(shape); + writer.write(""" + , () => $L, () => $L""", + getShapeVariableName(model.expectShape(shape.getInputShape())), + getShapeVariableName(model.expectShape(shape.getOutputShape()))); + }); } private void writeTraits(Shape shape) { String traitCode = new SchemaTraitWriter( - shape, elision, - store - ).toString(); + shape, + elision, + store).toString(); writer.writeInline(traitCode.replace("$", "$$")); } @@ -369,19 +352,18 @@ private void writeTraits(Shape shape) { */ private void checkedWriteSchema(Shape shape, Runnable schemaWriteFn) { if (shape.getId().getNamespace().equals("smithy.api") - && shape.getId().getName().equals("Unit")) { + && shape.getId().getName().equals("Unit")) { // special signal value for operation input/output. writer.write(""" - export var __Unit = "unit" as const;"""); + export var __Unit = "unit" as const;"""); } else if (!elision.isReferenceSchema(shape) && !elision.traits.hasSchemaTraits(shape)) { String sentinel = this.resolveSchema(model.expectShape(ShapeId.from("smithy.api#Unit")), shape); writer.write( - """ - export var $L = $L;""", - getShapeVariableName(shape), - sentinel - ); + """ + export var $L = $L;""", + getShapeVariableName(shape), + sentinel); } else { schemaWriteFn.run(); } @@ -501,8 +483,7 @@ private String resolveSimpleSchemaNestedContainer(Shape context, Shape shape) { } default -> { throw new IllegalArgumentException( - "call to resolveSimpleSchemaNestedContainer with incompatible shape type." - ); + "call to resolveSimpleSchemaNestedContainer with incompatible shape type."); } } if (contained.isMemberShape()) { @@ -512,9 +493,9 @@ private String resolveSimpleSchemaNestedContainer(Shape context, Shape shape) { if (contained.isListShape() || contained.isMapShape()) { String schemaVarName = store.var(shape.getId().getName()); return staticTypePrefix - + store.var(shape.getId().getNamespace(), "n") + ", " + schemaVarName + ", 0, " - + keySchema - + this.resolveSimpleSchema(context, contained) + "]"; + + store.var(shape.getId().getNamespace(), "n") + ", " + schemaVarName + ", 0, " + + keySchema + + this.resolveSimpleSchema(context, contained) + "]"; } else { return sentinel + " | " + this.resolveSimpleSchema(context, contained); } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndex.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndex.java index 6c11f24a99a..8ad0e43d401 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndex.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndex.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.schema; import software.amazon.smithy.model.Model; @@ -47,11 +46,11 @@ public boolean isReferenceSchema(Shape shape) { ShapeType type = targetShape.getType(); switch (type) { case STRING, - BOOLEAN, - BYTE, DOUBLE, FLOAT, SHORT, INTEGER, LONG, - ENUM, INT_ENUM, - BIG_INTEGER, BIG_DECIMAL, - TIMESTAMP, BLOB, DOCUMENT -> { + BOOLEAN, + BYTE, DOUBLE, FLOAT, SHORT, INTEGER, LONG, + ENUM, INT_ENUM, + BIG_INTEGER, BIG_DECIMAL, + TIMESTAMP, BLOB, DOCUMENT -> { return false; } case LIST, SET, MAP -> { diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtension.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtension.java index 44235470e6d..170e03f1acf 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtension.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtension.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.schema; import java.util.HashMap; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndex.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndex.java index f74dc915fb8..27147c3ef7c 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndex.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndex.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.schema; import java.util.HashMap; @@ -49,50 +48,47 @@ @SmithyInternalApi public final class SchemaTraitFilterIndex implements KnowledgeIndex { private static final Set EXCLUDED_TRAITS = SetUtils.of( - // excluded due to special schema handling. - TimestampFormatTrait.ID - ); + // excluded due to special schema handling. + TimestampFormatTrait.ID); /** * All of these are added by scanning the ProtocolDefinition and AuthDefinition meta traits. * The hard coded initial list is shown as an example of what this set contains. */ private final Set includedTraits = new HashSet<>( - // (wrapped for mutability) - SetUtils.of( - SparseTrait.ID, // Shape serde - SensitiveTrait.ID, - IdempotencyTokenTrait.ID, - JsonNameTrait.ID, // Shape serde - MediaTypeTrait.ID, // JSON shape serde - XmlAttributeTrait.ID, // XML shape serde - XmlFlattenedTrait.ID, // XML shape serde - XmlNameTrait.ID, // XML shape serde - XmlNamespaceTrait.ID, // XML shape serde - StreamingTrait.ID, // HttpBindingProtocol handles streaming + payload members. - EndpointTrait.ID, // HttpProtocol - ErrorTrait.ID, // set by the ServiceException runtime classes. - RequiresLengthTrait.ID, // unhandled - - EventHeaderTrait.ID, // @smithy/core/event-streams::EventStreamSerde - EventPayloadTrait.ID, // @smithy/core/event-streams::EventStreamSerde - - // afaict, HttpErrorTrait is ignored by the client. The discriminator selects the error structure - // but the actual HTTP response status code is used with no particular comparison - // with the trait's error code. - HttpErrorTrait.ID, - // the following HTTP traits are handled by HTTP binding protocol base class. - HttpTrait.ID, - HttpHeaderTrait.ID, - HttpQueryTrait.ID, - HttpLabelTrait.ID, - HttpPayloadTrait.ID, - HttpPrefixHeadersTrait.ID, - HttpQueryParamsTrait.ID, - HttpResponseCodeTrait.ID, - HostLabelTrait.ID - ) - ); + // (wrapped for mutability) + SetUtils.of( + SparseTrait.ID, // Shape serde + SensitiveTrait.ID, + IdempotencyTokenTrait.ID, + JsonNameTrait.ID, // Shape serde + MediaTypeTrait.ID, // JSON shape serde + XmlAttributeTrait.ID, // XML shape serde + XmlFlattenedTrait.ID, // XML shape serde + XmlNameTrait.ID, // XML shape serde + XmlNamespaceTrait.ID, // XML shape serde + StreamingTrait.ID, // HttpBindingProtocol handles streaming + payload members. + EndpointTrait.ID, // HttpProtocol + ErrorTrait.ID, // set by the ServiceException runtime classes. + RequiresLengthTrait.ID, // unhandled + + EventHeaderTrait.ID, // @smithy/core/event-streams::EventStreamSerde + EventPayloadTrait.ID, // @smithy/core/event-streams::EventStreamSerde + + // afaict, HttpErrorTrait is ignored by the client. The discriminator selects the error structure + // but the actual HTTP response status code is used with no particular comparison + // with the trait's error code. + HttpErrorTrait.ID, + // the following HTTP traits are handled by HTTP binding protocol base class. + HttpTrait.ID, + HttpHeaderTrait.ID, + HttpQueryTrait.ID, + HttpLabelTrait.ID, + HttpPayloadTrait.ID, + HttpPrefixHeadersTrait.ID, + HttpQueryParamsTrait.ID, + HttpResponseCodeTrait.ID, + HostLabelTrait.ID)); private final Map cache = new HashMap<>(); private final Model model; @@ -149,21 +145,23 @@ private boolean hasSchemaTraits(Shape shape, int depth) { return false; } boolean hasSchemaTraits = shape.getAllTraits() - .values() - .stream() - .map(Trait::toShapeId) - .anyMatch(this::includeTrait); + .values() + .stream() + .map(Trait::toShapeId) + .anyMatch(this::includeTrait); if (hasSchemaTraits) { cache.put(shape, true); return true; } - boolean membersHaveSchemaTraits = shape.getAllMembers().values().stream() - .anyMatch(ms -> hasSchemaTraits(ms, depth + 1)); + boolean membersHaveSchemaTraits = shape.getAllMembers() + .values() + .stream() + .anyMatch(ms -> hasSchemaTraits(ms, depth + 1)); boolean targetHasSchemaTraits = shape.asMemberShape() - .map(ms -> hasSchemaTraits(model.expectShape(ms.getTarget()), depth + 1)) - .orElse(false); + .map(ms -> hasSchemaTraits(model.expectShape(ms.getTarget()), depth + 1)) + .orElse(false); cache.put(shape, membersHaveSchemaTraits || targetHasSchemaTraits); return cache.get(shape); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGenerator.java index 29240a33847..a1e6002c3aa 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGenerator.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.schema; import java.util.Objects; @@ -42,7 +41,6 @@ import software.amazon.smithy.utils.SetUtils; import software.amazon.smithy.utils.SmithyInternalApi; - /** * Creates the string representing a trait's data. * For presence-based trait, essentially boolean, a 1 or 2 will be used. @@ -51,22 +49,21 @@ public class SchemaTraitGenerator { private static final String ANNOTATION_TRAIT_VALUE = "1"; private static final Set ANNOTATION_TRAITS = SetUtils.of( - XmlAttributeTrait.ID, - XmlFlattenedTrait.ID, - EventHeaderTrait.ID, - EventPayloadTrait.ID, - StreamingTrait.ID, - RequiresLengthTrait.ID, - HttpLabelTrait.ID, - HttpPayloadTrait.ID, - HttpQueryParamsTrait.ID, - HttpResponseCodeTrait.ID, - HttpChecksumRequiredTrait.ID, - HostLabelTrait.ID, - SparseTrait.ID, - SensitiveTrait.ID, - IdempotencyTokenTrait.ID - ); + XmlAttributeTrait.ID, + XmlFlattenedTrait.ID, + EventHeaderTrait.ID, + EventPayloadTrait.ID, + StreamingTrait.ID, + RequiresLengthTrait.ID, + HttpLabelTrait.ID, + HttpPayloadTrait.ID, + HttpQueryParamsTrait.ID, + HttpResponseCodeTrait.ID, + HttpChecksumRequiredTrait.ID, + HostLabelTrait.ID, + SparseTrait.ID, + SensitiveTrait.ID, + IdempotencyTokenTrait.ID); /** * Data traits are traits with one or more fields of data. @@ -75,22 +72,20 @@ public class SchemaTraitGenerator { * for the fields' data. */ private static final Set DATA_TRAITS = SetUtils.of( - HttpErrorTrait.ID, - HttpTrait.ID, - EndpointTrait.ID, - XmlNamespaceTrait.ID - ); + HttpErrorTrait.ID, + HttpTrait.ID, + EndpointTrait.ID, + XmlNamespaceTrait.ID); private static final Set STRING_TRAITS = SetUtils.of( - TimestampFormatTrait.ID, - JsonNameTrait.ID, - MediaTypeTrait.ID, - XmlNameTrait.ID, - HttpHeaderTrait.ID, - HttpQueryTrait.ID, - HttpPrefixHeadersTrait.ID, - ErrorTrait.ID - ); + TimestampFormatTrait.ID, + JsonNameTrait.ID, + MediaTypeTrait.ID, + XmlNameTrait.ID, + HttpHeaderTrait.ID, + HttpQueryTrait.ID, + HttpPrefixHeadersTrait.ID, + ErrorTrait.ID); public String serializeTraitData(Trait trait, StringStore stringStore) { if (trait instanceof TimestampFormatTrait) { @@ -103,25 +98,23 @@ public String serializeTraitData(Trait trait, StringStore stringStore) { } else if (DATA_TRAITS.contains(trait.toShapeId())) { if (trait instanceof EndpointTrait endpointTrait) { return """ - ["%s"] - """.formatted(endpointTrait.getHostPrefix()); + ["%s"] + """.formatted(endpointTrait.getHostPrefix()); } else if (trait instanceof XmlNamespaceTrait xmlNamespaceTrait) { return """ - [%s, %s] - """.formatted( - stringStore.var(xmlNamespaceTrait.getPrefix().orElse("")), - stringStore.var(xmlNamespaceTrait.getUri()) - ); + [%s, %s] + """.formatted( + stringStore.var(xmlNamespaceTrait.getPrefix().orElse("")), + stringStore.var(xmlNamespaceTrait.getUri())); } else if (trait instanceof HttpErrorTrait httpError) { return Objects.toString(httpError.getCode()); } else if (trait instanceof HttpTrait httpTrait) { return """ - ["%s", "%s", %s] - """.formatted( - httpTrait.getMethod(), - httpTrait.getUri(), - httpTrait.getCode() - ); + ["%s", "%s", %s] + """.formatted( + httpTrait.getMethod(), + httpTrait.getUri(), + httpTrait.getCode()); } } else if (SchemaTraitExtension.INSTANCE.contains(trait)) { return SchemaTraitExtension.INSTANCE.render(trait); @@ -129,14 +122,14 @@ public String serializeTraitData(Trait trait, StringStore stringStore) { if (trait instanceof StringTrait stringTrait) { return """ - /* unhandled trait \s""" + "`" + trait.getClass().getSimpleName() + "` */ " - + stringStore.var(stringTrait.getValue()); + /* unhandled trait \s""" + "`" + trait.getClass().getSimpleName() + "` */ " + + stringStore.var(stringTrait.getValue()); } else if (trait instanceof AnnotationTrait) { return """ - /* unhandled trait \s""" + "`" + trait.getClass().getSimpleName() + "` */ " - + ANNOTATION_TRAIT_VALUE; + /* unhandled trait \s""" + "`" + trait.getClass().getSimpleName() + "` */ " + + ANNOTATION_TRAIT_VALUE; } return """ - /* unhandled trait \s""" + "`" + trait.getClass().getSimpleName() + "` */ void 0"; + /* unhandled trait \s""" + "`" + trait.getClass().getSimpleName() + "` */ void 0"; } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriter.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriter.java index f75c78aac4c..0ae940d9e78 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriter.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriter.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.schema; import java.util.List; @@ -19,27 +18,25 @@ import software.amazon.smithy.model.traits.Trait; import software.amazon.smithy.typescript.codegen.util.StringStore; - class SchemaTraitWriter { private final Shape shape; private final SchemaReferenceIndex elision; private final StringStore stringStore; private final StringBuilder buffer = new StringBuilder(); private final List compressTraits = List.of( - HttpLabelTrait.ID, - IdempotentTrait.ID, - IdempotencyTokenTrait.ID, - SensitiveTrait.ID, - HttpPayloadTrait.ID, - HttpResponseCodeTrait.ID, - HttpQueryParamsTrait.ID - ); + HttpLabelTrait.ID, + IdempotentTrait.ID, + IdempotencyTokenTrait.ID, + SensitiveTrait.ID, + HttpPayloadTrait.ID, + HttpResponseCodeTrait.ID, + HttpQueryParamsTrait.ID); private final SchemaTraitGenerator traitGenerator = new SchemaTraitGenerator(); SchemaTraitWriter( - Shape shape, - SchemaReferenceIndex elision, - StringStore stringStore + Shape shape, + SchemaReferenceIndex elision, + StringStore stringStore ) { this.shape = shape; this.elision = elision; @@ -62,11 +59,11 @@ public String toString() { private boolean mayUseCompressedTraits() { return shape.getAllTraits() - .values() - .stream() - .map(Trait::toShapeId) - .filter(elision.traits::includeTrait) - .allMatch(compressTraits::contains); + .values() + .stream() + .map(Trait::toShapeId) + .filter(elision.traits::includeTrait) + .allMatch(compressTraits::contains); } private void writeTraitsBitVector() { @@ -87,11 +84,9 @@ private void writeTraitsObject() { return; } buffer.append(""" - [%s]: %s,\s""".formatted( + [%s]: %s,\s""".formatted( stringStore.var(shapeId.getName()), - traitGenerator.serializeTraitData(trait, stringStore) - ) - ); + traitGenerator.serializeTraitData(trait, stringStore))); }); buffer.deleteCharAt(buffer.length() - 1); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientBodyExtraCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientBodyExtraCodeSection.java index 3f833427829..bae99d412de 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientBodyExtraCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientBodyExtraCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConfigCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConfigCodeSection.java index d9c2a6d535b..136fe7bd75d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConfigCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConfigCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConstructorCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConstructorCodeSection.java index e373519c1fc..65b3fb6c7a3 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConstructorCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientConstructorCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientDestroyCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientDestroyCodeSection.java index 99c045637c4..cd257864bec 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientDestroyCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientDestroyCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientPropertiesCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientPropertiesCodeSection.java index b179368c0cf..d5685aeb544 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientPropertiesCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/ClientPropertiesCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandBodyExtraCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandBodyExtraCodeSection.java index 8e5b7393694..3a3972b386a 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandBodyExtraCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandBodyExtraCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandConstructorCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandConstructorCodeSection.java index b3d2137b847..08e877105d8 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandConstructorCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandConstructorCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandContextCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandContextCodeSection.java index ee7a0316eff..a9c63031020 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandContextCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandContextCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandPropertiesCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandPropertiesCodeSection.java index 3ef42d6390d..6782e1f94d2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandPropertiesCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/CommandPropertiesCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/PreCommandClassCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/PreCommandClassCodeSection.java index 06b3aa6317f..4c1c2893c2d 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/PreCommandClassCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/PreCommandClassCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java index 83b2af1db77..754fd6a2cdf 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.sections; import java.util.List; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/ClientWriterConsumer.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/ClientWriterConsumer.java index 62290dcfe7b..6ddfb226059 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/ClientWriterConsumer.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/ClientWriterConsumer.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.util; import software.amazon.smithy.typescript.codegen.TypeScriptWriter; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/CommandWriterConsumer.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/CommandWriterConsumer.java index 1eed5a9db8a..5325c3b6f34 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/CommandWriterConsumer.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/CommandWriterConsumer.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.util; import software.amazon.smithy.typescript.codegen.TypeScriptWriter; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessor.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessor.java index b12c4aa4f5f..7ece9e98d0c 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessor.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessor.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.util; import java.util.regex.Pattern; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/StringStore.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/StringStore.java index 3d3625d7153..64371fd2bc1 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/StringStore.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/StringStore.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.util; import java.nio.file.Path; @@ -120,8 +119,8 @@ private String assignPreferredKey(String literal, String preferredPrefix) { */ private String allocateVariable(String literal) { String[] sections = Arrays.stream(literal.split("[-_\\s]")) - .filter(s -> !s.isEmpty()) - .toArray(String[]::new); + .filter(s -> !s.isEmpty()) + .toArray(String[]::new); StringBuilder v = new StringBuilder("_"); Queue deconfliction = new LinkedList<>(); if (sections.length > 1) { @@ -183,8 +182,8 @@ public static final class WithSchemaWriter extends StringStore { private final StringStore store; private WithSchemaWriter( - TypeScriptWriter writer, - StringStore store + TypeScriptWriter writer, + StringStore store ) { this.writer = writer; this.store = store; @@ -194,8 +193,9 @@ private WithSchemaWriter( public String var(String literal) { String var = store.var(literal); writer.addRelativeImport( - var, null, Path.of("./schemas_0") - ); + var, + null, + Path.of("./schemas_0")); return var; } @@ -203,8 +203,9 @@ public String var(String literal) { public String var(String literal, String preferredPrefix) { String var = store.var(literal, preferredPrefix); writer.addRelativeImport( - var, null, Path.of("./schemas_0") - ); + var, + null, + Path.of("./schemas_0")); return var; } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ImportFrom.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ImportFrom.java index d70fe9fec0f..fbac2d5b783 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ImportFrom.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ImportFrom.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.validation; import java.util.Set; @@ -15,24 +14,23 @@ @SmithyInternalApi public class ImportFrom { public static final Set NODE_NATIVE_DEPENDENCIES = SetUtils.of( - "buffer", - "child_process", - "crypto", - "dns", - "events", - "fs", - "http", - "http2", - "https", - "os", - "path", - "process", - "stream", - "tls", - "url", - "util", - "zlib" - ); + "buffer", + "child_process", + "crypto", + "dns", + "events", + "fs", + "http", + "http2", + "https", + "os", + "path", + "process", + "stream", + "tls", + "url", + "util", + "zlib"); private final String from; @@ -49,7 +47,7 @@ public ImportFrom(String importTargetExpression) { public boolean isNodejsNative() { String[] packageNameSegments = from.split("/"); return from.startsWith("node:") - || NODE_NATIVE_DEPENDENCIES.contains(packageNameSegments[0]); + || NODE_NATIVE_DEPENDENCIES.contains(packageNameSegments[0]); } /** diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/LongValidator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/LongValidator.java index 8a2511a8cfc..55bf09446e2 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/LongValidator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/LongValidator.java @@ -1,18 +1,7 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.validation; import java.util.List; @@ -45,17 +34,19 @@ public LongValidator(TypeScriptSettings settings) { @Override public List validate(Model model) { ServiceShape service = model.expectShape(settings.getService(), ServiceShape.class); - Set longs = new Walker(model).walkShapes(service).stream() + Set longs = new Walker(model).walkShapes(service) + .stream() .flatMap(shape -> OptionalUtils.stream(shape.asLongShape())) .collect(Collectors.toSet()); return longs.stream() - .map(shape -> warning(shape, "JavaScript numbers are all IEEE-754 double-precision floats. As a " - + "consequence of this, the maximum safe value for integral numbers is 2^53 - 1. Since a " - + "long shape can have values up to 2^63 - 1, there is a significant range of values that " - + "cannot be safely represented in JavaScript. If possible, use the int shape. If values " - + "outside of the safe range of JavaScript integrals are needed, it is recommended to use a " - + "string shape instead.")) + .map(shape -> warning(shape, + "JavaScript numbers are all IEEE-754 double-precision floats. As a " + + "consequence of this, the maximum safe value for integral numbers is 2^53 - 1. Since a " + + "long shape can have values up to 2^63 - 1, there is a significant range of values that " + + "cannot be safely represented in JavaScript. If possible, use the int shape. If values " + + "outside of the safe range of JavaScript integrals are needed, it is recommended to use a " + + "string shape instead.")) .collect(Collectors.toList()); } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java index bfaa256760f..10e4d0ffb1e 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLast.java @@ -1,18 +1,7 @@ /* - * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.validation; public abstract class ReplaceLast { @@ -27,8 +16,8 @@ public static String in(String original, String target, String replacement) { int lastPosition = original.lastIndexOf(target); if (lastPosition >= 0) { return original.substring(0, lastPosition) - + replacement - + original.substring(lastPosition + target.length()); + + replacement + + original.substring(lastPosition + target.length()); } return original; } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinder.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinder.java index a9a4d018853..0c2c878ae00 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinder.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinder.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.validation; import java.util.HashMap; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCall.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCall.java index f27b6a95f21..89b56a309af 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCall.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCall.java @@ -2,7 +2,6 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.validation; import java.util.regex.Pattern; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ApplicationProtocolTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ApplicationProtocolTest.java index e748546a450..5ef618b2de1 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ApplicationProtocolTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ApplicationProtocolTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import org.junit.jupiter.api.Assertions; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CodegenUtilsTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CodegenUtilsTest.java index 13c914cf8fb..3f28ae77f33 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CodegenUtilsTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CodegenUtilsTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import org.junit.jupiter.api.Assertions; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java index a9270cf6880..f0416b7a545 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -16,56 +20,54 @@ public class CommandGeneratorTest { @Test public void writesOperationSchemaRef() { testCommandCodegen( - "output-structure.smithy", - new String[] {".sc("} - ); + "output-structure.smithy", + new String[] {".sc("}); } @Test public void writesOperationContextParamValues() { testCommandCodegen( - "endpointsV2/endpoints-operation-context-params.smithy", - new String[] { - """ -opContextParamIdentifier: { type: "operationContextParams", get: (input?: any) => input?.fooString }""", - """ -opContextParamSubExpression: { type: "operationContextParams", get: (input?: any) => input?.fooObj?.bar }""", - """ -opContextParamWildcardExpressionList: { type: "operationContextParams", get: (input?: any) => input?.fooList }""", - """ -opContextParamWildcardExpressionListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListList.flat() }""", - """ -opContextParamWildcardExpressionListObj: { type: "operationContextParams", get: (input?: any) => input?.fooListObj?.map((obj: any) => obj?.key) }""", - """ -opContextParamWildcardExpressionListObjListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListObjList?.map((obj: any) => obj?.key).flat() }""", - """ -opContextParamWildcardExpressionHash: { type: "operationContextParams", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }""", - """ -opContextParamMultiSelectList: { type: "operationContextParams", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooObject?.bar,obj?.fooString].filter((i) => i)) }""", - """ -opContextParamMultiSelectListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooList].filter((i) => i)).flat() }""", - """ -opContextParamKeys: { type: "operationContextParams", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }""", - } - ); + "endpointsV2/endpoints-operation-context-params.smithy", + new String[] { + """ + opContextParamIdentifier: { type: "operationContextParams", get: (input?: any) => input?.fooString }""", + """ + opContextParamSubExpression: { type: "operationContextParams", get: (input?: any) => input?.fooObj?.bar }""", + """ + opContextParamWildcardExpressionList: { type: "operationContextParams", get: (input?: any) => input?.fooList }""", + """ + opContextParamWildcardExpressionListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListList.flat() }""", + """ + opContextParamWildcardExpressionListObj: { type: "operationContextParams", get: (input?: any) => input?.fooListObj?.map((obj: any) => obj?.key) }""", + """ + opContextParamWildcardExpressionListObjListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListObjList?.map((obj: any) => obj?.key).flat() }""", + """ + opContextParamWildcardExpressionHash: { type: "operationContextParams", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }""", + """ + opContextParamMultiSelectList: { type: "operationContextParams", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooObject?.bar,obj?.fooString].filter((i) => i)) }""", + """ + opContextParamMultiSelectListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooList].filter((i) => i)).flat() }""", + """ + opContextParamKeys: { type: "operationContextParams", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }""", + }); } private void testCommandCodegen(String filename, String[] expectedTypeArray) { MockManifest manifest = new MockManifest(); PluginContext context = PluginContext.builder() - .pluginClassLoader(getClass().getClassLoader()) - .model(Model.assembler() - .addImport(getClass().getResource(filename)) - .discoverModels() - .assemble() - .unwrap()) - .fileManifest(manifest) - .settings(Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()) - .build(); + .pluginClassLoader(getClass().getClassLoader()) + .model(Model.assembler() + .addImport(getClass().getResource(filename)) + .discoverModels() + .assemble() + .unwrap()) + .fileManifest(manifest) + .settings(Node.objectNodeBuilder() + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()) + .build(); new TypeScriptCodegenPlugin().execute(context); String contents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "//commands/GetFooCommand.ts").get(); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/DefaultDefaultReadmeGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/DefaultDefaultReadmeGeneratorTest.java index b89d9a6a965..1ca07a53f35 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/DefaultDefaultReadmeGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/DefaultDefaultReadmeGeneratorTest.java @@ -1,5 +1,15 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static software.amazon.smithy.typescript.codegen.integration.DefaultReadmeGenerator.README_FILENAME; + +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -9,13 +19,6 @@ import software.amazon.smithy.model.node.Node; import software.amazon.smithy.typescript.codegen.integration.DefaultReadmeGenerator; -import java.util.ArrayList; -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static software.amazon.smithy.typescript.codegen.integration.DefaultReadmeGenerator.README_FILENAME; - class DefaultDefaultReadmeGeneratorTest { private TypeScriptSettings settings; @@ -29,12 +32,13 @@ class DefaultDefaultReadmeGeneratorTest { @BeforeEach void setup() { - settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .withMember("createDefaultReadme", Node.from(true)) - .build()); + settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .withMember("createDefaultReadme", Node.from(true)) + .build()); manifest = new MockManifest(); symbolProvider = new SymbolVisitor(model, settings); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/EnumGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/EnumGeneratorTest.java index 8da99ab72f8..3ee21c1c929 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/EnumGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/EnumGeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -26,10 +30,11 @@ public void generatesNamedEnums() { .addImport(getClass().getResource("simple-service.smithy")) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); Symbol symbol = new SymbolVisitor(model, settings).toSymbol(shape); new EnumGenerator(shape, symbol, writer).run(); @@ -51,10 +56,11 @@ public void generatesUnnamedEnums() { .addImport(getClass().getResource("simple-service.smithy")) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); Symbol symbol = new SymbolVisitor(model, settings).toSymbol(shape); new EnumGenerator(shape, symbol, writer).run(); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ImportDeclarationsTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ImportDeclarationsTest.java index 33129d927fc..6a3064bc0ad 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ImportDeclarationsTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ImportDeclarationsTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -115,10 +119,10 @@ public void canImportDefaultImportWithIgnore() { declarations.addIgnoredDefaultImport("foo", "@types/foo", "I want to"); String result = declarations.toString(); - assertThat(result, containsString("// @ts-ignore: I want to\nimport foo from \"@types/foo\"; // eslint-disable-line")); + assertThat(result, + containsString("// @ts-ignore: I want to\nimport foo from \"@types/foo\"; // eslint-disable-line")); } - @Test public void canImportDefaultImportWithNamedImport() { ImportDeclarations declarations = new ImportDeclarations("/foo/bar"); @@ -164,36 +168,43 @@ public void importOrdering() { // https://projects.haykranen.nl/java/ declarations.addTypeImport( - "DecoratorContainerSchemaListenerTransactionRepository", null, "../../java8"); + "DecoratorContainerSchemaListenerTransactionRepository", + null, + "../../java8"); declarations.addTypeImport( - "AuthenticationExpressionDecoratorContainerSchemaListenerTransactionRepository", null, "../../java11"); + "AuthenticationExpressionDecoratorContainerSchemaListenerTransactionRepository", + null, + "../../java11"); declarations.addTypeImport( - "ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenerTransactions", null, "../../java15"); + "ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenerTransactions", + null, + "../../java15"); // uses multiline format as line width 120 is breached. declarations.addTypeImport( - "ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenersTransactions", null, "../../java18"); - + "ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenersTransactions", + null, + "../../java18"); String result = declarations.toString(); - assertEquals(""" - import { getEndpointPlugin } from "@smithy/middleware-endpoint"; - import { Command as $Command } from "@smithy/smithy-client"; - import type { MetadataBearer as __MetadataBearer } from "@smithy/types"; - - import type { AuthenticationExpressionDecoratorContainerSchemaListenerTransactionRepository } from "../../java11"; - import type { ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenerTransactions } from "../../java15"; - import type { - ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenersTransactions, - } from "../../java18"; - import type { DecoratorContainerSchemaListenerTransactionRepository } from "../../java8"; - import { commonParams } from "../endpoint/EndpointParameters"; - import type { FractionalSecondsOutput } from "../models/models_0"; - import type { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; - import { FractionalSeconds } from "../schemas/schemas_0"; - - """, - result - ); + assertEquals( + """ + import { getEndpointPlugin } from "@smithy/middleware-endpoint"; + import { Command as $Command } from "@smithy/smithy-client"; + import type { MetadataBearer as __MetadataBearer } from "@smithy/types"; + + import type { AuthenticationExpressionDecoratorContainerSchemaListenerTransactionRepository } from "../../java11"; + import type { ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenerTransactions } from "../../java15"; + import type { + ResolverVisitorAuthenticationExpressionDecoratorContainerSchemaListenersTransactions, + } from "../../java18"; + import type { DecoratorContainerSchemaListenerTransactionRepository } from "../../java8"; + import { commonParams } from "../endpoint/EndpointParameters"; + import type { FractionalSecondsOutput } from "../models/models_0"; + import type { RpcV2ProtocolClientResolvedConfig, ServiceInputTypes, ServiceOutputTypes } from "../RpcV2ProtocolClient"; + import { FractionalSeconds } from "../schemas/schemas_0"; + + """, + result); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IndexGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IndexGeneratorTest.java index f39d2ce4ba2..4403ff2ca06 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IndexGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IndexGeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -12,12 +16,16 @@ public class IndexGeneratorTest { @Test public void writesIndex() { - Model model = Model.assembler().addImport(getClass().getResource("simple-service-with-operation.smithy")).assemble().unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + Model model = Model.assembler() + .addImport(getClass().getResource("simple-service-with-operation.smithy")) + .assemble() + .unwrap(); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider symbolProvider = new SymbolVisitor(model, settings); TypeScriptWriter writer = new TypeScriptWriter(""); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IntEnumGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IntEnumGeneratorTest.java index 7af9f8f63a5..f97a04d16d5 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IntEnumGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IntEnumGeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -24,10 +28,11 @@ public void generatesIntEnums() { .addImport(getClass().getResource("simple-service.smithy")) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); Symbol symbol = new SymbolVisitor(model, settings).toSymbol(shape); new IntEnumGenerator(shape, symbol, writer).run(); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/PackageJsonGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/PackageJsonGeneratorTest.java index 2cdda9dff7e..95d3b32a2ff 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/PackageJsonGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/PackageJsonGeneratorTest.java @@ -1,5 +1,17 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -10,19 +22,13 @@ import software.amazon.smithy.model.node.Node; import software.amazon.smithy.model.node.ObjectNode; -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Stream; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.not; -import static org.junit.jupiter.api.Assertions.assertTrue; - class PackageJsonGeneratorTest { @ParameterizedTest @MethodSource("providePackageDescriptionTestCases") - void expectPackageDescriptionUpdatedByArtifactType(TypeScriptSettings.ArtifactType artifactType, String expectedDescription) { + void expectPackageDescriptionUpdatedByArtifactType( + TypeScriptSettings.ArtifactType artifactType, + String expectedDescription + ) { Model model = Model.assembler() .addImport(getClass().getResource("simple-service.smithy")) .assemble() @@ -63,13 +69,16 @@ void expectPackageBrowserFieldToBeMerged() { .withMember("packageDescription", Node.from("example description")) .build(); - final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, settings, + final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, + settings, TypeScriptSettings.ArtifactType.CLIENT); var pjson = typeScriptSettings.getPackageJson(); pjson = pjson.withMember("browser", Node.objectNode().withMember("example-browser", Node.from("example"))); - pjson = pjson.withMember("react-native", Node.objectNode().withMember("example-react-native", - Node.from("example"))); + pjson = pjson.withMember("react-native", + Node.objectNode() + .withMember("example-react-native", + Node.from("example"))); typeScriptSettings.setPackageJson(pjson); PackageJsonGenerator.writePackageJson(typeScriptSettings, manifest, new HashMap<>()); @@ -112,7 +121,8 @@ void expectTestScriptAndTestConfigToBeAdded() { .withMember("packageDescription", Node.from("example description")) .build(); - final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, settings, + final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, + settings, TypeScriptSettings.ArtifactType.CLIENT); Map> deps = new HashMap<>(); @@ -149,7 +159,8 @@ void expectTypeDocToNotBeAdded() { .withMember("packageDescription", Node.from("example description")) .build(); - final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, settings, + final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, + settings, TypeScriptSettings.ArtifactType.CLIENT); Map> deps = new HashMap<>(); @@ -182,7 +193,8 @@ void expectTypeDocToBeAddedWithGenerateTypeDoc() { .withMember("generateTypeDoc", true) .build(); - final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, settings, + final TypeScriptSettings typeScriptSettings = TypeScriptSettings.from(model, + settings, TypeScriptSettings.ArtifactType.CLIENT); Map> deps = new HashMap<>(); @@ -201,7 +213,6 @@ void expectTypeDocToBeAddedWithGenerateTypeDoc() { private static Stream providePackageDescriptionTestCases() { return Stream.of( Arguments.of(TypeScriptSettings.ArtifactType.SSDK, "example server"), - Arguments.of(TypeScriptSettings.ArtifactType.CLIENT, "example client") - ); + Arguments.of(TypeScriptSettings.ArtifactType.CLIENT, "example client")); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGeneratorTest.java index b6ddfa9f76d..7cfa60742ce 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/RuntimeConfigGeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -8,7 +12,6 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.build.MockManifest; @@ -59,15 +62,20 @@ public Map> getRuntimeConfigWriters( } }); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider symbolProvider = new SymbolVisitor(model, settings); TypeScriptDelegator delegator = new TypeScriptDelegator(manifest, symbolProvider); RuntimeConfigGenerator generator = new RuntimeConfigGenerator( - settings, model, symbolProvider, delegator, integrations, + settings, + model, + symbolProvider, + delegator, + integrations, ApplicationProtocol.createDefaultHttpApplicationProtocol()); generator.generate(LanguageTarget.NODE); generator.generate(LanguageTarget.BROWSER); @@ -81,7 +89,8 @@ public Map> getRuntimeConfigWriters( Assertions.assertTrue(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.shared.ts")); // Does the runtimeConfig.shared.ts file expand the template properties properly? - String runtimeConfigSharedContents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.shared.ts").get(); + String runtimeConfigSharedContents = + manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.shared.ts").get(); assertThat(runtimeConfigSharedContents, containsString("export const getRuntimeConfig = (config: ExampleClientConfig) =>")); assertThat(runtimeConfigSharedContents, containsString("apiVersion: \"1.0.0\",")); @@ -91,23 +100,25 @@ public Map> getRuntimeConfigWriters( // Does the runtimeConfig.ts file expand the template properties properly? String runtimeConfigContents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(); assertThat(runtimeConfigContents, - containsString("import type { ExampleClientConfig } from \"./ExampleClient\";")); + containsString("import type { ExampleClientConfig } from \"./ExampleClient\";")); assertThat(runtimeConfigSharedContents, containsString("export const getRuntimeConfig = (config: ExampleClientConfig) =>")); assertThat(runtimeConfigContents, containsString("config?.syn ?? syn: 'ack2',")); assertThat(runtimeConfigSharedContents, containsString("config?.foo ?? foo: 'bar',")); // Does the runtimeConfig.browser.ts file expand the template properties properly? - String runtimeConfigBrowserContents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(); + String runtimeConfigBrowserContents = + manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(); assertThat(runtimeConfigBrowserContents, - containsString("import type { ExampleClientConfig } from \"./ExampleClient\";")); + containsString("import type { ExampleClientConfig } from \"./ExampleClient\";")); assertThat(runtimeConfigSharedContents, containsString("export const getRuntimeConfig = (config: ExampleClientConfig) =>")); assertThat(runtimeConfigContents, containsString("config?.syn ?? syn: 'ack2',")); assertThat(runtimeConfigSharedContents, containsString("config?.foo ?? foo: 'bar',")); // Does the runtimeConfig.native.ts file expand the browser template properties properly? - String runtimeConfigNativeContents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.native.ts").get(); + String runtimeConfigNativeContents = + manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.native.ts").get(); assertThat(runtimeConfigNativeContents, containsString("import type { ExampleClientConfig } from \"./ExampleClient\";")); assertThat(runtimeConfigNativeContents, diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGeneratorTest.java index 108ad32d8fb..927dbf59d5a 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ServiceBareBonesClientGeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -21,11 +25,12 @@ public void hasHooksForService() { @Test public void addsCustomIntegrationDependencyFields() { Model model = Model.assembler().addImport(getClass().getResource("simple-service.smithy")).assemble().unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); TypeScriptWriter writer = new TypeScriptWriter("./foo"); SymbolProvider symbolProvider = new SymbolVisitor(model, settings); ApplicationProtocol applicationProtocol = ApplicationProtocol.createDefaultHttpApplicationProtocol(); @@ -44,12 +49,18 @@ public void addConfigInterfaceFields( } }); - new ServiceBareBonesClientGenerator(settings, model, symbolProvider, writer, integrations, - Collections.emptyList(), applicationProtocol).run(); + new ServiceBareBonesClientGenerator(settings, + model, + symbolProvider, + writer, + integrations, + Collections.emptyList(), + applicationProtocol).run(); - assertThat(writer.toString(), containsString(" /**\n" - + " * Hello!\n" - + " */\n" - + " syn?: string;")); + assertThat(writer.toString(), + containsString(" /**\n" + + " * Hello!\n" + + " */\n" + + " syn?: string;")); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java index 7ef0967521f..7e65dec8c24 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java @@ -1,8 +1,13 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; + import org.junit.jupiter.api.Test; import software.amazon.smithy.build.MockManifest; import software.amazon.smithy.build.PluginContext; @@ -19,173 +24,174 @@ public class StructureGeneratorTest { public void properlyGeneratesEmptyMessageMemberOfException() { testErrorStructureCodegen("error-test-empty.smithy", """ - export class Err extends __BaseException { - readonly name = "Err" as const; - readonly $fault = "client" as const; - /** - * @internal - */ - constructor(opts: __ExceptionOptionType) { - super({ - name: "Err", - $fault: "client", - ...opts, - }); - Object.setPrototypeOf(this, Err.prototype); - } - } - """); + export class Err extends __BaseException { + readonly name = "Err" as const; + readonly $fault = "client" as const; + /** + * @internal + */ + constructor(opts: __ExceptionOptionType) { + super({ + name: "Err", + $fault: "client", + ...opts, + }); + Object.setPrototypeOf(this, Err.prototype); + } + } + """); } @Test public void properlyGeneratesOptionalMessageMemberOfException() { testErrorStructureCodegen("error-test-optional-message.smithy", """ - export class Err extends __BaseException { - readonly name = "Err" as const; - readonly $fault = "client" as const; - /** - * @internal - */ - constructor(opts: __ExceptionOptionType) { - super({ - name: "Err", - $fault: "client", - ...opts, - }); - Object.setPrototypeOf(this, Err.prototype); - } - } - """); + export class Err extends __BaseException { + readonly name = "Err" as const; + readonly $fault = "client" as const; + /** + * @internal + */ + constructor(opts: __ExceptionOptionType) { + super({ + name: "Err", + $fault: "client", + ...opts, + }); + Object.setPrototypeOf(this, Err.prototype); + } + } + """); } @Test public void properlyGeneratesRequiredMessageMemberOfException() { testErrorStructureCodegen("error-test-required-message.smithy", """ - export class Err extends __BaseException { - readonly name = "Err" as const; - readonly $fault = "client" as const; - /** - * @internal - */ - constructor(opts: __ExceptionOptionType) { - super({ - name: "Err", - $fault: "client", - ...opts, - }); - Object.setPrototypeOf(this, Err.prototype); - } - } - """); + export class Err extends __BaseException { + readonly name = "Err" as const; + readonly $fault = "client" as const; + /** + * @internal + */ + constructor(opts: __ExceptionOptionType) { + super({ + name: "Err", + $fault: "client", + ...opts, + }); + Object.setPrototypeOf(this, Err.prototype); + } + } + """); } @Test public void properlyGeneratesOptionalNonMessageMemberOfException() { testErrorStructureCodegen("error-test-optional-member-no-message.smithy", """ - export class Err extends __BaseException { - readonly name = "Err" as const; - readonly $fault = "client" as const; - foo?: string | undefined; - /** - * @internal - */ - constructor(opts: __ExceptionOptionType) { - super({ - name: "Err", - $fault: "client", - ...opts, - }); - Object.setPrototypeOf(this, Err.prototype); - this.foo = opts.foo; - } - } - """); + export class Err extends __BaseException { + readonly name = "Err" as const; + readonly $fault = "client" as const; + foo?: string | undefined; + /** + * @internal + */ + constructor(opts: __ExceptionOptionType) { + super({ + name: "Err", + $fault: "client", + ...opts, + }); + Object.setPrototypeOf(this, Err.prototype); + this.foo = opts.foo; + } + } + """); } @Test public void properlyGeneratesRequiredNonMessageMemberOfException() { testErrorStructureCodegen("error-test-required-member-no-message.smithy", """ - export class Err extends __BaseException { - readonly name = "Err" as const; - readonly $fault = "client" as const; - foo: string | undefined; - /** - * @internal - */ - constructor(opts: __ExceptionOptionType) { - super({ - name: "Err", - $fault: "client", - ...opts, - }); - Object.setPrototypeOf(this, Err.prototype); - this.foo = opts.foo; - } - } - """); + export class Err extends __BaseException { + readonly name = "Err" as const; + readonly $fault = "client" as const; + foo: string | undefined; + /** + * @internal + */ + constructor(opts: __ExceptionOptionType) { + super({ + name: "Err", + $fault: "client", + ...opts, + }); + Object.setPrototypeOf(this, Err.prototype); + this.foo = opts.foo; + } + } + """); } @Test public void generatesEmptyRetryableTrait() { testErrorStructureCodegen("error-test-retryable.smithy", """ - export class Err extends __BaseException { - readonly name = "Err" as const; - readonly $fault = "client" as const; - $retryable = {}; - /** - * @internal - */ - constructor(opts: __ExceptionOptionType) { - super({ - name: "Err", - $fault: "client", - ...opts, - }); - Object.setPrototypeOf(this, Err.prototype); - } - } - """); + export class Err extends __BaseException { + readonly name = "Err" as const; + readonly $fault = "client" as const; + $retryable = {}; + /** + * @internal + */ + constructor(opts: __ExceptionOptionType) { + super({ + name: "Err", + $fault: "client", + ...opts, + }); + Object.setPrototypeOf(this, Err.prototype); + } + } + """); } @Test public void generatesRetryableTraitWithThrottling() { testErrorStructureCodegen("error-test-retryable-throttling.smithy", """ - export class Err extends __BaseException { - readonly name = "Err" as const; - readonly $fault = "client" as const; - $retryable = { - throttling: true, - }; - /** - * @internal - */ - constructor(opts: __ExceptionOptionType) { - super({ - name: "Err", - $fault: "client", - ...opts, - }); - Object.setPrototypeOf(this, Err.prototype); - } - } - """); + export class Err extends __BaseException { + readonly name = "Err" as const; + readonly $fault = "client" as const; + $retryable = { + throttling: true, + }; + /** + * @internal + */ + constructor(opts: __ExceptionOptionType) { + super({ + name: "Err", + $fault: "client", + ...opts, + }); + Object.setPrototypeOf(this, Err.prototype); + } + } + """); } @Test public void properlyGeneratesRequiredMessageMemberNotBackwardCompatible() { testStructureCodegenBase("test-required-member.smithy", """ - export interface GetFooOutput { - someRequiredMember: string; - } - """, - RequiredMemberMode.STRICT, true); + export interface GetFooOutput { + someRequiredMember: string; + } + """, + RequiredMemberMode.STRICT, + true); } private String testStructureCodegen(String file, String includedString) { @@ -200,7 +206,8 @@ private String testStructureCodegenBase( String file, String testString, RequiredMemberMode requiredMemberMode, - boolean assertContains) { + boolean assertContains + ) { Model model = Model.assembler() .addImport(getClass().getResource(file)) .assemble() @@ -246,10 +253,11 @@ public void generatesNonErrorStructures() { .addImport(getClass().getResource("simple-service.smithy")); struct.getAllMembers().values().forEach(assembler::addShape); Model model = assembler.assemble().unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); TypeScriptWriter writer = new TypeScriptWriter("./foo"); new StructureGenerator(model, new SymbolVisitor(model, settings), writer, struct).run(); @@ -276,10 +284,11 @@ public void generatesNonErrorStructuresThatExtendOtherInterfaces() { OperationShape operation = OperationShape.builder().id("com.foo#Operation").output(struct).build(); assembler.addShape(operation); Model model = assembler.assemble().unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); TypeScriptWriter writer = new TypeScriptWriter("./foo"); new StructureGenerator(model, new SymbolVisitor(model, settings), writer, struct).run(); @@ -287,4 +296,4 @@ public void generatesNonErrorStructuresThatExtendOtherInterfaces() { assertThat(output, containsString("export interface Bar {")); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolDecoratorIntegration.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolDecoratorIntegration.java index b5c58533685..91e3641638c 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolDecoratorIntegration.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolDecoratorIntegration.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import software.amazon.smithy.codegen.core.Symbol; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolProviderTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolProviderTest.java index 236e04dc74d..765a5db1c41 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolProviderTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/SymbolProviderTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -30,10 +34,11 @@ public void createsSymbols() { .addShape(shape) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol symbol = provider.toSymbol(shape); @@ -52,10 +57,11 @@ public void createsSymbolsIntoTargetNamespace() { .addShapes(shape1, shape2) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol symbol1 = provider.toSymbol(shape1); Symbol symbol2 = provider.toSymbol(shape2); @@ -80,10 +86,11 @@ public void escapesReservedWords() { .addShape(shape) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol symbol = provider.toSymbol(shape); @@ -102,10 +109,11 @@ public void doesNotEscapeBuiltins() { .addShapes(struct, member) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol structSymbol = provider.toSymbol(struct); @@ -133,10 +141,11 @@ public void escapesRecursiveSymbols() { .addShapes(list, listMember, record) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol listSymbol = provider.toSymbol(list); @@ -150,10 +159,11 @@ public void createsCommandModules() { .addImport(getClass().getResource("output-structure.smithy")) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); Shape command = model.expectShape(ShapeId.from("smithy.example#GetFoo")); SymbolProvider provider = new SymbolVisitor(model, settings); @@ -166,8 +176,10 @@ public void createsCommandModules() { @Test public void usesLazyJsonStringForJsonMediaType() { - StringShape jsonString = StringShape.builder().id("foo.bar#jsonString") - .addTrait(new MediaTypeTrait("application/json")).build(); + StringShape jsonString = StringShape.builder() + .id("foo.bar#jsonString") + .addTrait(new MediaTypeTrait("application/json")) + .build(); MemberShape member = MemberShape.builder().id("foo.bar#test$a").target(jsonString).build(); StructureShape struct = StructureShape.builder() .id("foo.bar#test") @@ -178,10 +190,11 @@ public void usesLazyJsonStringForJsonMediaType() { .addShapes(struct, member, jsonString) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol memberSymbol = provider.toSymbol(member); @@ -206,10 +219,11 @@ public void omitsUnknownStringEnumVariant() { .addShapes(struct, member, stringShape) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol memberSymbol = provider.toSymbol(member); @@ -234,10 +248,11 @@ public void omitsUnknownNumberIntEnumVariant() { .addShapes(struct, member, shape) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings); Symbol memberSymbol = provider.toSymbol(member); @@ -254,10 +269,11 @@ public void placesResourceShapeIntoInitialBucket() { .addShapes(shape1, shape2) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider provider = new SymbolVisitor(model, settings, 1); Symbol symbol1 = provider.toSymbol(shape1); Symbol symbol2 = provider.toSymbol(shape2); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPluginTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPluginTest.java index 249ed3db80c..811d131c864 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPluginTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPluginTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -25,10 +29,10 @@ public void generatesRuntimeConfigFiles() { .model(model) .fileManifest(manifest) .settings(Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()) + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()) .build(); new TypeScriptCodegenPlugin().execute(context); @@ -44,7 +48,7 @@ public void generatesRuntimeConfigFiles() { String packageJsonContents = manifest.getFileString("package.json").get(); ObjectNode packageJson = Node.parse(packageJsonContents).expectObjectNode(); assertThat(packageJson.expectObjectMember("browser").getStringMember("./dist-es/runtimeConfig"), - equalTo(Optional.of(Node.from("./dist-es/runtimeConfig.browser")))); + equalTo(Optional.of(Node.from("./dist-es/runtimeConfig.browser")))); } @Test @@ -92,10 +96,11 @@ public void generatesServiceClients() { assertTrue(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/Example.ts")); assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/Example.ts").get(), - containsString("export class Example extends ExampleClient")); + containsString("export class Example extends ExampleClient")); assertTrue(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/ExampleClient.ts")); - assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/ExampleClient.ts").get(), containsString("export class ExampleClient")); + assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/ExampleClient.ts").get(), + containsString("export class ExampleClient")); } @Test diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegatorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegatorTest.java index 24e0f4db0dd..6fabc9680f1 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegatorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDelegatorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDependencyTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDependencyTest.java index 2a4c22536be..23310b6ba8c 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDependencyTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptDependencyTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -8,7 +12,6 @@ import static org.hamcrest.Matchers.startsWith; import java.util.List; - import org.junit.jupiter.api.Test; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.codegen.core.SymbolDependency; @@ -26,7 +29,7 @@ public void createsSymbols() { @Test public void getsUnconditionalDependencies() { assertThat(TypeScriptDependency.getUnconditionalDependencies(), - hasItem(TypeScriptDependency.SMITHY_TYPES.dependency)); + hasItem(TypeScriptDependency.SMITHY_TYPES.dependency)); } @Test diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitorTest.java index f62ac1a36d1..67ae1d23b4d 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitorTest.java @@ -1,12 +1,16 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; -import org.junit.jupiter.api.Test; -import software.amazon.smithy.jmespath.JmespathExpression; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static software.amazon.smithy.typescript.codegen.TypeScriptWriter.CODEGEN_INDICATOR; +import org.junit.jupiter.api.Test; +import software.amazon.smithy.jmespath.JmespathExpression; + public class TypeScriptJmesPathVisitorTest { private String generateTypescriptInterpretation(String path) { @@ -38,35 +42,40 @@ public void createsSimpleTwoLevelIndex() { public void createsDeepIndex() { String result = generateTypescriptInterpretation("foo.bar.car.gar.foo.bar.car"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n return result.foo.bar.car.gar.foo.bar.car;\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n return result.foo.bar.car.gar.foo.bar.car;\n}\n")); } @Test public void createsListProfile() { String result = generateTypescriptInterpretation("foo.bar[].car"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.foo.bar);\n let projection_3 = flat_1.map((element_2: any) => {\n return element_2.car;\n });\n return projection_3;\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.foo.bar);\n let projection_3 = flat_1.map((element_2: any) => {\n return element_2.car;\n });\n return projection_3;\n}\n")); } @Test public void createsLengthEqualityCheckProfile() { String result = generateTypescriptInterpretation("length(items) == `0`"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n return (result.items.length == 0.0);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n return (result.items.length == 0.0);\n}\n")); } @Test public void createsLengthLessCheckProfile() { String result = generateTypescriptInterpretation("length(items) < `0`"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n return (result.items.length < 0.0);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n return (result.items.length < 0.0);\n}\n")); } @Test public void createsLengthGreaterCheckProfile() { String result = generateTypescriptInterpretation("length(items) > `0`"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n return (result.items.length > 0.0);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n return (result.items.length > 0.0);\n}\n")); } @Test @@ -74,90 +83,107 @@ public void createsDeepLengthCheckProfile() { String result = generateTypescriptInterpretation("length(items.foo.deep[]) == `0`"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items.foo.deep);\n return (flat_1.length == 0.0);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items.foo.deep);\n return (flat_1.length == 0.0);\n}\n")); } @Test public void createsDoubleLengthChecksProfile() { - String result = generateTypescriptInterpretation("length(set.items[].bar[].gar.foo.items[].item) == length(bar.foos[].foo)"); + String result = generateTypescriptInterpretation( + "length(set.items[].bar[].gar.foo.items[].item) == length(bar.foos[].foo)"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.set.items);\n let projection_3 = flat_1.map((element_2: any) => {\n return element_2.bar;\n });\n let flat_4: any[] = [].concat(...projection_3);\n let projection_6 = flat_4.map((element_5: any) => {\n return element_5.gar.foo.items;\n });\n let flat_7: any[] = [].concat(...projection_6);\n let projection_9 = flat_7.map((element_8: any) => {\n return element_8.item;\n });\n let flat_10: any[] = [].concat(...result.bar.foos);\n let projection_12 = flat_10.map((element_11: any) => {\n return element_11.foo;\n });\n return (projection_9.length == projection_12.length);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.set.items);\n let projection_3 = flat_1.map((element_2: any) => {\n return element_2.bar;\n });\n let flat_4: any[] = [].concat(...projection_3);\n let projection_6 = flat_4.map((element_5: any) => {\n return element_5.gar.foo.items;\n });\n let flat_7: any[] = [].concat(...projection_6);\n let projection_9 = flat_7.map((element_8: any) => {\n return element_8.item;\n });\n let flat_10: any[] = [].concat(...result.bar.foos);\n let projection_12 = flat_10.map((element_11: any) => {\n return element_11.foo;\n });\n return (projection_9.length == projection_12.length);\n}\n")); } @Test public void createWrapAroundProfile() { String result = generateTypescriptInterpretation("length(items[-1]) == `0`"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n return (result.items[result.items.length - 1].length == 0.0);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n return (result.items[result.items.length - 1].length == 0.0);\n}\n")); } @Test public void createContainsProfile() { String result = generateTypescriptInterpretation("contains(items[].State, `false`)"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n let projection_3 = flat_1.map((element_2: any) => {\n return element_2.State;\n });\n return projection_3.includes(false);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n let projection_3 = flat_1.map((element_2: any) => {\n return element_2.State;\n });\n return projection_3.includes(false);\n}\n")); } @Test public void createWildcardIndex() { String result = generateTypescriptInterpretation("foo.*.bar"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let objectProjection_2 = Object.values(result.foo).map((element_1: any) => {\n return element_1.bar;\n });\n return objectProjection_2;\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let objectProjection_2 = Object.values(result.foo).map((element_1: any) => {\n return element_1.bar;\n });\n return objectProjection_2;\n}\n")); } @Test public void createFilterIndex() { String result = generateTypescriptInterpretation("items[?foo=='awesome'][]"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let filterRes_2 = result.items.filter((element_1: any) => {\n return (element_1.foo == \"awesome\");\n });\n let flat_3: any[] = [].concat(...filterRes_2);\n return flat_3;\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let filterRes_2 = result.items.filter((element_1: any) => {\n return (element_1.foo == \"awesome\");\n });\n let flat_3: any[] = [].concat(...filterRes_2);\n return flat_3;\n}\n")); } @Test public void createMultiIndex() { String result = generateTypescriptInterpretation("items[].[`4` > `0`, `1` == `0`][]"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n let projection_3 = flat_1.map((element_2: any) => {\n let result_4 = [];\n result_4.push((4.0 > 0.0));\n result_4.push((1.0 == 0.0));\n element_2 = result_4;\n return element_2;\n });\n let flat_5: any[] = [].concat(...projection_3);\n return flat_5;\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n let projection_3 = flat_1.map((element_2: any) => {\n let result_4 = [];\n result_4.push((4.0 > 0.0));\n result_4.push((1.0 == 0.0));\n element_2 = result_4;\n return element_2;\n });\n let flat_5: any[] = [].concat(...projection_3);\n return flat_5;\n}\n")); } @Test public void createLengthFilterInstancesIndex() { - String result = generateTypescriptInterpretation("length(Instances[?LifecycleState==\"InService\"]) >= MinSize"); + String result = + generateTypescriptInterpretation("length(Instances[?LifecycleState==\"InService\"]) >= MinSize"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let filterRes_2 = result.Instances.filter((element_1: any) => {\n return (element_1.LifecycleState == element_1.InService);\n });\n return (filterRes_2.length >= result.MinSize);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let filterRes_2 = result.Instances.filter((element_1: any) => {\n return (element_1.LifecycleState == element_1.InService);\n });\n return (filterRes_2.length >= result.MinSize);\n}\n")); } @Test public void createComplexLengthFilterContainsIndex() { - String result = generateTypescriptInterpretation("contains(AutoScalingGroups[].[length(Instances[?LifecycleState=='InService']) >= MinSize][], `false`)"); + String result = generateTypescriptInterpretation( + "contains(AutoScalingGroups[].[length(Instances[?LifecycleState=='InService']) >= MinSize][], `false`)"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.AutoScalingGroups);\n let projection_3 = flat_1.map((element_2: any) => {\n let filterRes_5 = element_2.Instances.filter((element_4: any) => {\n return (element_4.LifecycleState == \"InService\");\n });\n let result_6 = [];\n result_6.push((filterRes_5.length >= element_2.MinSize));\n element_2 = result_6;\n return element_2;\n });\n let flat_7: any[] = [].concat(...projection_3);\n return flat_7.includes(false);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.AutoScalingGroups);\n let projection_3 = flat_1.map((element_2: any) => {\n let filterRes_5 = element_2.Instances.filter((element_4: any) => {\n return (element_4.LifecycleState == \"InService\");\n });\n let result_6 = [];\n result_6.push((filterRes_5.length >= element_2.MinSize));\n element_2 = result_6;\n return element_2;\n });\n let flat_7: any[] = [].concat(...projection_3);\n return flat_7.includes(false);\n}\n")); } @Test public void createNotIndex() { String result = generateTypescriptInterpretation("!(length(items) == `0`)"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n return (!(result.items.length == 0.0));\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n return (!(result.items.length == 0.0));\n}\n")); } @Test public void createOrIndex() { String result = generateTypescriptInterpretation("length(items[]) == `0` || length(foo) > `0`"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n return (((flat_1.length == 0.0) || (result.foo.length > 0.0)) && ((result.foo.length > 0.0) || (flat_1.length == 0.0))) ;\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n return (((flat_1.length == 0.0) || (result.foo.length > 0.0)) && ((result.foo.length > 0.0) || (flat_1.length == 0.0))) ;\n}\n")); } @Test public void createAndIndex() { String result = generateTypescriptInterpretation("length(items[]) == `0` && length(foo) > `0`"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n return ((flat_1.length == 0.0) && (result.foo.length > 0.0));\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let flat_1: any[] = [].concat(...result.items);\n return ((flat_1.length == 0.0) && (result.foo.length > 0.0));\n}\n")); } @Test public void createComplexAndNotIndex() { - String result = generateTypescriptInterpretation("(length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`)"); + String result = generateTypescriptInterpretation( + "(length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`)"); assertThat(result, - equalTo(CODEGEN_INDICATOR + "let returnComparator = () => {\n let filterRes_2 = result.services.filter((element_1: any) => {\n return (!((element_1.deployments.length == 1.0) && (element_1.runningCount == element_1.desiredCount)));\n });\n return (filterRes_2.length == 0.0);\n}\n")); + equalTo(CODEGEN_INDICATOR + + "let returnComparator = () => {\n let filterRes_2 = result.services.filter((element_1: any) => {\n return (!((element_1.deployments.length == 1.0) && (element_1.runningCount == element_1.desiredCount)));\n });\n return (filterRes_2.length == 0.0);\n}\n")); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptSettingsTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptSettingsTest.java index 7e5fd136838..750b2dcd44c 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptSettingsTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptSettingsTest.java @@ -1,5 +1,15 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + import java.util.LinkedHashSet; import java.util.List; import java.util.stream.Stream; @@ -19,12 +29,6 @@ import software.amazon.smithy.typescript.codegen.protocols.ProtocolPriorityConfig; import software.amazon.smithy.utils.MapUtils; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) public class TypeScriptSettingsTest { // these are mock protocol names. @@ -36,10 +40,14 @@ public class TypeScriptSettingsTest { ShapeId query = ShapeId.from("namespace#query"); ShapeId serviceQuery = ShapeId.from("namespace#serviceQuery"); LinkedHashSet protocolShapeIds = new LinkedHashSet<>( - List.of( - json1_0, json1_1, restJson1, rpcv2Cbor, restXml, query, serviceQuery - ) - ); + List.of( + json1_0, + json1_1, + restJson1, + rpcv2Cbor, + restXml, + query, + serviceQuery)); @Test public void resolvesDefaultService() { @@ -47,10 +55,11 @@ public void resolvesDefaultService() { .addImport(getClass().getResource("simple-service.smithy")) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); assertThat(settings.getService(), equalTo(ShapeId.from("smithy.example#Example"))); } @@ -61,10 +70,11 @@ public void defaultsToYarn() { .addImport(getClass().getResource("simple-service.smithy")) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); assertEquals(TypeScriptSettings.PackageManager.YARN, settings.getPackageManager()); } @@ -75,19 +85,22 @@ public void canBeConfiguredToNpm() { .addImport(getClass().getResource("simple-service.smithy")) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .withMember("packageManager", Node.from("npm")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .withMember("packageManager", Node.from("npm")) + .build()); assertEquals(TypeScriptSettings.PackageManager.NPM, settings.getPackageManager()); } - @ParameterizedTest @MethodSource("providePackageDescriptionTestCases") - void expectPackageDescriptionUpdatedByArtifactType(TypeScriptSettings.ArtifactType artifactType, String expectedDescription) { + void expectPackageDescriptionUpdatedByArtifactType( + TypeScriptSettings.ArtifactType artifactType, + String expectedDescription + ) { Model model = Model.assembler() .addImport(getClass().getResource("simple-service.smithy")) .assemble() @@ -107,14 +120,15 @@ void expectPackageDescriptionUpdatedByArtifactType(TypeScriptSettings.ArtifactTy private static Stream providePackageDescriptionTestCases() { return Stream.of( Arguments.of(TypeScriptSettings.ArtifactType.SSDK, "example server"), - Arguments.of(TypeScriptSettings.ArtifactType.CLIENT, "example client") - ); + Arguments.of(TypeScriptSettings.ArtifactType.CLIENT, "example client")); } @Test - public void resolveServiceProtocolSelectJson(@Mock Model model, - @Mock ServiceShape service, - @Mock ServiceIndex serviceIndex) { + public void resolveServiceProtocolSelectJson( + @Mock Model model, + @Mock ServiceShape service, + @Mock ServiceIndex serviceIndex + ) { TypeScriptSettings subject = new TypeScriptSettings(); when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); ShapeId serviceShapeId = ShapeId.from("namespace#Service"); @@ -122,18 +136,21 @@ public void resolveServiceProtocolSelectJson(@Mock Model model, // spec case 1. when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( - rpcv2Cbor, null, - json1_0, null - )); + rpcv2Cbor, + null, + json1_0, + null)); ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); // JS customization has JSON at higher default priority than CBOR. assertEquals(json1_0, protocol); } @Test - public void resolveServiceProtocolSelectOnlyOption(@Mock Model model, - @Mock ServiceShape service, - @Mock ServiceIndex serviceIndex) { + public void resolveServiceProtocolSelectOnlyOption( + @Mock Model model, + @Mock ServiceShape service, + @Mock ServiceIndex serviceIndex + ) { TypeScriptSettings subject = new TypeScriptSettings(); when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); ShapeId serviceShapeId = ShapeId.from("namespace#Service"); @@ -141,16 +158,18 @@ public void resolveServiceProtocolSelectOnlyOption(@Mock Model model, // spec case 2. when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( - rpcv2Cbor, null - )); + rpcv2Cbor, + null)); ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); assertEquals(rpcv2Cbor, protocol); } @Test - public void resolveServiceProtocolSelectJsonOverQueryAndCbor(@Mock Model model, - @Mock ServiceShape service, - @Mock ServiceIndex serviceIndex) { + public void resolveServiceProtocolSelectJsonOverQueryAndCbor( + @Mock Model model, + @Mock ServiceShape service, + @Mock ServiceIndex serviceIndex + ) { TypeScriptSettings subject = new TypeScriptSettings(); when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); ShapeId serviceShapeId = ShapeId.from("namespace#Service"); @@ -158,19 +177,23 @@ public void resolveServiceProtocolSelectJsonOverQueryAndCbor(@Mock Model model, // spec case 3. when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( - rpcv2Cbor, null, - json1_0, null, - query, null - )); + rpcv2Cbor, + null, + json1_0, + null, + query, + null)); ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); // JS customization has JSON at higher default priority than CBOR. assertEquals(json1_0, protocol); } @Test - public void resolveServiceProtocolSelectJsonOverQuery(@Mock Model model, - @Mock ServiceShape service, - @Mock ServiceIndex serviceIndex) { + public void resolveServiceProtocolSelectJsonOverQuery( + @Mock Model model, + @Mock ServiceShape service, + @Mock ServiceIndex serviceIndex + ) { TypeScriptSettings subject = new TypeScriptSettings(); when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); ShapeId serviceShapeId = ShapeId.from("namespace#Service"); @@ -178,17 +201,20 @@ public void resolveServiceProtocolSelectJsonOverQuery(@Mock Model model, // spec case 4. when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( - json1_0, null, - query, null - )); + json1_0, + null, + query, + null)); ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); assertEquals(json1_0, protocol); } @Test - public void resolveServiceProtocolSelectQueryWhenSingularOption(@Mock Model model, - @Mock ServiceShape service, - @Mock ServiceIndex serviceIndex) { + public void resolveServiceProtocolSelectQueryWhenSingularOption( + @Mock Model model, + @Mock ServiceShape service, + @Mock ServiceIndex serviceIndex + ) { TypeScriptSettings subject = new TypeScriptSettings(); when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); ShapeId serviceShapeId = ShapeId.from("namespace#Service"); @@ -196,16 +222,18 @@ public void resolveServiceProtocolSelectQueryWhenSingularOption(@Mock Model mode // spec case 5. when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( - query, null - )); + query, + null)); ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); assertEquals(query, protocol); } @Test - public void resolveServiceProtocolSelectServiceCustomPriority(@Mock Model model, - @Mock ServiceShape service, - @Mock ServiceIndex serviceIndex) { + public void resolveServiceProtocolSelectServiceCustomPriority( + @Mock Model model, + @Mock ServiceShape service, + @Mock ServiceIndex serviceIndex + ) { TypeScriptSettings subject = new TypeScriptSettings(); when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); ShapeId serviceShapeId = ShapeId.from("namespace#Service"); @@ -213,31 +241,41 @@ public void resolveServiceProtocolSelectServiceCustomPriority(@Mock Model model, // service override, non-spec when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( - json1_0, null, - json1_1, null, - restJson1, null, - rpcv2Cbor, null, - restXml, null, - query, null, - serviceQuery, null - )); + json1_0, + null, + json1_1, + null, + restJson1, + null, + rpcv2Cbor, + null, + restXml, + null, + query, + null, + serviceQuery, + null)); subject.setProtocolPriority(new ProtocolPriorityConfig( - MapUtils.of( - serviceShapeId, - List.of( - serviceQuery, rpcv2Cbor, json1_1, restJson1, restXml, query - ) - ), - null - )); + MapUtils.of( + serviceShapeId, + List.of( + serviceQuery, + rpcv2Cbor, + json1_1, + restJson1, + restXml, + query)), + null)); ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); assertEquals(serviceQuery, protocol); } @Test - public void resolveServiceProtocolSelectDefaultCustomPriority(@Mock Model model, - @Mock ServiceShape service, - @Mock ServiceIndex serviceIndex) { + public void resolveServiceProtocolSelectDefaultCustomPriority( + @Mock Model model, + @Mock ServiceShape service, + @Mock ServiceIndex serviceIndex + ) { TypeScriptSettings subject = new TypeScriptSettings(); when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); ShapeId serviceShapeId = ShapeId.from("namespace#Service"); @@ -245,20 +283,28 @@ public void resolveServiceProtocolSelectDefaultCustomPriority(@Mock Model model, // global default override when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( - json1_0, null, - json1_1, null, - restJson1, null, - rpcv2Cbor, null, - restXml, null, - query, null, - serviceQuery, null - )); + json1_0, + null, + json1_1, + null, + restJson1, + null, + rpcv2Cbor, + null, + restXml, + null, + query, + null, + serviceQuery, + null)); subject.setProtocolPriority(new ProtocolPriorityConfig( - null, - List.of( - rpcv2Cbor, json1_1, restJson1, restXml, query - ) - )); + null, + List.of( + rpcv2Cbor, + json1_1, + restJson1, + restXml, + query))); ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); assertEquals(rpcv2Cbor, protocol); } @@ -266,42 +312,40 @@ public void resolveServiceProtocolSelectDefaultCustomPriority(@Mock Model model, @Test public void parseProtocolPriorityJson() { Model model = Model.assembler() - .addImport(getClass().getResource("simple-service.smithy")) - .assemble() - .unwrap(); + .addImport(getClass().getResource("simple-service.smithy")) + .assemble() + .unwrap(); ObjectNode settings = Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .withMember("serviceProtocolPriority", Node.parse( - """ - { - "namespace#Service1": ["namespace#Protocol1", "namespace#Protocol2"], - "namespace#Service2": ["namespace#Protocol2", "namespace#Protocol1"] - } - """)) - .withMember("defaultProtocolPriority", Node.parse( - """ - ["namespace#Protocol3", "namespace#Protocol4"] - """ - )) - .build(); + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .withMember("serviceProtocolPriority", + Node.parse( + """ + { + "namespace#Service1": ["namespace#Protocol1", "namespace#Protocol2"], + "namespace#Service2": ["namespace#Protocol2", "namespace#Protocol1"] + } + """)) + .withMember("defaultProtocolPriority", + Node.parse( + """ + ["namespace#Protocol3", "namespace#Protocol4"] + """)) + .build(); final TypeScriptSettings subject = TypeScriptSettings.from(model, settings); assertEquals( - ShapeId.from("namespace#Protocol2"), - subject.getProtocolPriority().getProtocolPriority(ShapeId.from("namespace#Service1")).get(1) - ); + ShapeId.from("namespace#Protocol2"), + subject.getProtocolPriority().getProtocolPriority(ShapeId.from("namespace#Service1")).get(1)); assertEquals( - ShapeId.from("namespace#Protocol2"), - subject.getProtocolPriority().getProtocolPriority(ShapeId.from("namespace#Service2")).get(0) - ); + ShapeId.from("namespace#Protocol2"), + subject.getProtocolPriority().getProtocolPriority(ShapeId.from("namespace#Service2")).get(0)); assertEquals( - ShapeId.from("namespace#Protocol4"), - subject.getProtocolPriority().getProtocolPriority(ShapeId.from("namespace#Service5")).get(1) - ); + ShapeId.from("namespace#Protocol4"), + subject.getProtocolPriority().getProtocolPriority(ShapeId.from("namespace#Service5")).get(1)); } @Test diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptUtilsTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptUtilsTest.java index b4a756287ca..6d29fdabe18 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptUtilsTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptUtilsTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptWriterTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptWriterTest.java index f7b54118521..a878d130cb2 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptWriterTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/TypeScriptWriterTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; import static org.hamcrest.MatcherAssert.assertThat; @@ -28,12 +32,12 @@ public void doesNotAddNewlineBetweenManagedAndExplicitImports() { String result = writer.toString(); assertEquals(""" - %simport { Bar as __Bar } from "@smithy/types"; - - import { Baz } from "./hello"; - import { Qux as __Qux } from "./qux"; - import { Foo } from "baz"; - """.formatted(CODEGEN_INDICATOR), result); + %simport { Bar as __Bar } from "@smithy/types"; + + import { Baz } from "./hello"; + import { Qux as __Qux } from "./qux"; + import { Foo } from "baz"; + """.formatted(CODEGEN_INDICATOR), result); } @Test @@ -76,7 +80,7 @@ public void addImportSubmodule() { String result = writer.toString(); assertEquals(""" - %simport { symbol as __symbol } from "@smithy/core/submodule"; - """.formatted(CODEGEN_INDICATOR).trim(), result.trim()); + %simport { symbol as __symbol } from "@smithy/core/submodule"; + """.formatted(CODEGEN_INDICATOR).trim(), result.trim()); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/UnionGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/UnionGeneratorTest.java index 3a4cef19deb..4a001cf519f 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/UnionGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/UnionGeneratorTest.java @@ -1,7 +1,9 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; @@ -37,75 +39,76 @@ public void generatesTaggedUnions() { .addShapes(unionShape, memberA, memberB, memberC) .assemble() .unwrap(); - TypeScriptSettings settings = TypeScriptSettings.from(model, Node.objectNodeBuilder() - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()); + TypeScriptSettings settings = TypeScriptSettings.from(model, + Node.objectNodeBuilder() + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()); SymbolProvider symbolProvider = new SymbolVisitor(model, settings); TypeScriptWriter writer = new TypeScriptWriter("./Example"); new UnionGenerator(model, symbolProvider, writer, unionShape).run(); String output = writer.toString(); assertEquals(""" - // smithy-typescript generated code - /** - * @public - */ - export type Example = - | Example.AMember - | Example.BMember - | Example.CMember - | Example.$UnknownMember; - - /** - * @public - */ - export namespace Example { - export interface AMember { - A: string; - B?: never; - C?: never; - $unknown?: never; - } - - export interface BMember { - A?: never; - B: number; - C?: never; - $unknown?: never; - } - - export interface CMember { - A?: never; - B?: never; - C: boolean; - $unknown?: never; - } - - /** - * @public - */ - export interface $UnknownMember { - A?: never; - B?: never; - C?: never; - $unknown: [string, any]; - } - - export interface Visitor { - A: (value: string) => T; - B: (value: number) => T; - C: (value: boolean) => T; - _: (name: string, value: any) => T; - } - - export const visit = (value: Example, visitor: Visitor): T => { - if (value.A !== undefined) return visitor.A(value.A); - if (value.B !== undefined) return visitor.B(value.B); - if (value.C !== undefined) return visitor.C(value.C); - return visitor._(value.$unknown[0], value.$unknown[1]); - }; - } - """, output); + // smithy-typescript generated code + /** + * @public + */ + export type Example = + | Example.AMember + | Example.BMember + | Example.CMember + | Example.$UnknownMember; + + /** + * @public + */ + export namespace Example { + export interface AMember { + A: string; + B?: never; + C?: never; + $unknown?: never; + } + + export interface BMember { + A?: never; + B: number; + C?: never; + $unknown?: never; + } + + export interface CMember { + A?: never; + B?: never; + C: boolean; + $unknown?: never; + } + + /** + * @public + */ + export interface $UnknownMember { + A?: never; + B?: never; + C?: never; + $unknown: [string, any]; + } + + export interface Visitor { + A: (value: string) => T; + B: (value: number) => T; + C: (value: boolean) => T; + _: (name: string, value: any) => T; + } + + export const visit = (value: Example, visitor: Visitor): T => { + if (value.A !== undefined) return visitor.A(value.A); + if (value.B !== undefined) return visitor.B(value.B); + if (value.C !== undefined) return visitor.C(value.C); + return visitor._(value.$unknown[0], value.$unknown[1]); + }; + } + """, output); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGeneratorTest.java index 87f363b9006..0a01ea08692 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/DocumentationExampleGeneratorTest.java @@ -1,41 +1,45 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.documentation; +import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; import software.amazon.smithy.model.node.ObjectNode; -import static org.junit.jupiter.api.Assertions.*; - class DocumentationExampleGeneratorTest { ObjectNode input = ObjectNode.builder() - .withMember("Key", "example-key") - .withMember("Bucket", "example-key") - .build(); + .withMember("Key", "example-key") + .withMember("Bucket", "example-key") + .build(); ObjectNode output = ObjectNode.builder() - .withMember("Config", - ObjectNode.builder() - .withMember("Temperature", 30) - .build()) - .build(); + .withMember("Config", + ObjectNode.builder() + .withMember("Temperature", 30) + .build()) + .build(); @Test void inputToJavaScriptObject() { String example = DocumentationExampleGenerator.inputToJavaScriptObject(input); assertEquals(""" - { - Bucket: "example-key", - Key: "example-key" - }""", example); + { + Bucket: "example-key", + Key: "example-key" + }""", example); } @Test void outputToJavaScriptObject() { String example = DocumentationExampleGenerator.inputToJavaScriptObject(output); assertEquals(""" - { - Config: { - Temperature: 30 - } - }""", example); + { + Config: { + Temperature: 30 + } + }""", example); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGeneratorTest.java index 83ce12608a8..c7a74bfd768 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGeneratorTest.java @@ -1,10 +1,13 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.documentation; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import java.util.List; - import org.junit.jupiter.api.Test; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.BlobShape; @@ -18,240 +21,243 @@ public class StructureExampleGeneratorTest { StringShape string = StringShape.builder() - .id("foo.bar#string") - .build(); + .id("foo.bar#string") + .build(); BlobShape blob = BlobShape.builder() - .id("foo.bar#blob") - .build(); + .id("foo.bar#blob") + .build(); BlobShape streamingBlob = BlobShape.builder() - .id("foo.bar#streamingBlob") - .traits( - List.of( - new StreamingTrait() - ) - ) - .build(); + .id("foo.bar#streamingBlob") + .traits( + List.of( + new StreamingTrait())) + .build(); ListShape list = ListShape.builder() - .id("foo.bar#list") - .member(string.getId()) - .build(); + .id("foo.bar#list") + .member(string.getId()) + .build(); MapShape map = MapShape.builder() - .id("foo.bar#map") - .key(MemberShape.builder() - .id("foo.bar#map$member") - .target(string.getId()) - .build()) - .value(MemberShape.builder() - .id("foo.bar#map$member") - .target(string.getId()) - .build()) - .build(); + .id("foo.bar#map") + .key(MemberShape.builder() + .id("foo.bar#map$member") + .target(string.getId()) + .build()) + .value(MemberShape.builder() + .id("foo.bar#map$member") + .target(string.getId()) + .build()) + .build(); MemberShape memberForString = MemberShape.builder() - .id("foo.bar#structure$string") - .target(string.getId()) - .build(); + .id("foo.bar#structure$string") + .target(string.getId()) + .build(); MemberShape memberForBlob = MemberShape.builder() - .id("foo.bar#blobStructure$blob") - .target(blob.getId()) - .build(); + .id("foo.bar#blobStructure$blob") + .target(blob.getId()) + .build(); MemberShape memberForStreamingBlob = MemberShape.builder() - .id("foo.bar#blobStructure$streamingBlob") - .target(streamingBlob.getId()) - .build(); + .id("foo.bar#blobStructure$streamingBlob") + .target(streamingBlob.getId()) + .build(); MemberShape memberForList = MemberShape.builder() - .id("foo.bar#structure$list") - .target(list.getId()) - .build(); + .id("foo.bar#structure$list") + .target(list.getId()) + .build(); MemberShape memberForMap = MemberShape.builder() - .id("foo.bar#structure$map") - .target(map.getId()) - .build(); + .id("foo.bar#structure$map") + .target(map.getId()) + .build(); StructureShape structure = StructureShape.builder() - .id("foo.bar#structure") - .members( - List.of( - memberForString, memberForList, memberForMap, - MemberShape.builder() - .id("foo.bar#structure$list2") - .target(list.getId()) - .build(), - MemberShape.builder() - .id("foo.bar#structure$list3") - .target(list.getId()) - .build(), - MemberShape.builder() - .id("foo.bar#structure$list4") - .target(list.getId()) - .build(), - MemberShape.builder() - .id("foo.bar#structure$list5") - .target(list.getId()) - .build(), - MemberShape.builder() - .id("foo.bar#structure$list6") - .target(list.getId()) - .build(), - MemberShape.builder() - .id("foo.bar#structure$list7") - .target(list.getId()) - .build(), - MemberShape.builder() - .id("foo.bar#structure$structure") - .target("foo.bar#structure") - .build())) - .build(); - + .id("foo.bar#structure") + .members( + List.of( + memberForString, + memberForList, + memberForMap, + MemberShape.builder() + .id("foo.bar#structure$list2") + .target(list.getId()) + .build(), + MemberShape.builder() + .id("foo.bar#structure$list3") + .target(list.getId()) + .build(), + MemberShape.builder() + .id("foo.bar#structure$list4") + .target(list.getId()) + .build(), + MemberShape.builder() + .id("foo.bar#structure$list5") + .target(list.getId()) + .build(), + MemberShape.builder() + .id("foo.bar#structure$list6") + .target(list.getId()) + .build(), + MemberShape.builder() + .id("foo.bar#structure$list7") + .target(list.getId()) + .build(), + MemberShape.builder() + .id("foo.bar#structure$structure") + .target("foo.bar#structure") + .build())) + .build(); StructureShape blobStructure = StructureShape.builder() - .id("foo.bar#blobStructure") - .members( - List.of( - memberForBlob, memberForStreamingBlob - ) - ) - .build(); + .id("foo.bar#blobStructure") + .members( + List.of( + memberForBlob, + memberForStreamingBlob)) + .build(); private Model model = Model.builder() - .addShapes( - string, list, map, structure, - memberForString, memberForList, memberForMap, - blob, streamingBlob - ) - .build(); + .addShapes( + string, + list, + map, + structure, + memberForString, + memberForList, + memberForMap, + blob, + streamingBlob) + .build(); @Test public void generatesStructuralHintDocumentation_map() { assertThat( - StructureExampleGenerator.generateStructuralHintDocumentation(map, model, false, true), - equalTo(""" - { // map - "": "STRING_VALUE", - };""")); + StructureExampleGenerator.generateStructuralHintDocumentation(map, model, false, true), + equalTo(""" + { // map + "": "STRING_VALUE", + };""")); } @Test public void generatesStructuralHintDocumentation_structure() { assertThat( - StructureExampleGenerator.generateStructuralHintDocumentation(structure, model, false, true), - equalTo(""" - { // structure - string: "STRING_VALUE", - list: [ // list - "STRING_VALUE", - ], - map: { // map - "": "STRING_VALUE", - }, - list2: [ - "STRING_VALUE", - ], - list3: [ - "STRING_VALUE", - ], - list4: [ - "STRING_VALUE", - ], - list5: [ - "STRING_VALUE", - ], - list6: "", - list7: "", - structure: { - string: "STRING_VALUE", - list: "", - map: { - "": "STRING_VALUE", - }, - list2: "", - list3: "", - list4: "", - list5: "", - list6: "", - list7: "", - structure: "", - }, - };""")); + StructureExampleGenerator.generateStructuralHintDocumentation(structure, model, false, true), + equalTo(""" + { // structure + string: "STRING_VALUE", + list: [ // list + "STRING_VALUE", + ], + map: { // map + "": "STRING_VALUE", + }, + list2: [ + "STRING_VALUE", + ], + list3: [ + "STRING_VALUE", + ], + list4: [ + "STRING_VALUE", + ], + list5: [ + "STRING_VALUE", + ], + list6: "", + list7: "", + structure: { + string: "STRING_VALUE", + list: "", + map: { + "": "STRING_VALUE", + }, + list2: "", + list3: "", + list4: "", + list5: "", + list6: "", + list7: "", + structure: "", + }, + };""")); } @Test public void generatesStructuralHintDocumentation_structure_asComment() { assertThat( - StructureExampleGenerator.generateStructuralHintDocumentation(structure, model, true, true), - equalTo(""" - // { // structure - // string: "STRING_VALUE", - // list: [ // list - // "STRING_VALUE", - // ], - // map: { // map - // "": "STRING_VALUE", - // }, - // list2: [ - // "STRING_VALUE", - // ], - // list3: [ - // "STRING_VALUE", - // ], - // list4: [ - // "STRING_VALUE", - // ], - // list5: [ - // "STRING_VALUE", - // ], - // list6: "", - // list7: "", - // structure: { - // string: "STRING_VALUE", - // list: "", - // map: { - // "": "STRING_VALUE", - // }, - // list2: "", - // list3: "", - // list4: "", - // list5: "", - // list6: "", - // list7: "", - // structure: "", - // }, - // };""")); + StructureExampleGenerator.generateStructuralHintDocumentation(structure, model, true, true), + equalTo(""" + // { // structure + // string: "STRING_VALUE", + // list: [ // list + // "STRING_VALUE", + // ], + // map: { // map + // "": "STRING_VALUE", + // }, + // list2: [ + // "STRING_VALUE", + // ], + // list3: [ + // "STRING_VALUE", + // ], + // list4: [ + // "STRING_VALUE", + // ], + // list5: [ + // "STRING_VALUE", + // ], + // list6: "", + // list7: "", + // structure: { + // string: "STRING_VALUE", + // list: "", + // map: { + // "": "STRING_VALUE", + // }, + // list2: "", + // list3: "", + // list4: "", + // list5: "", + // list6: "", + // list7: "", + // structure: "", + // }, + // };""")); } @Test public void generatesStructuralHintDocumentation_list() { assertThat( - StructureExampleGenerator.generateStructuralHintDocumentation(list, model, false, true), - equalTo(""" - [ // list - "STRING_VALUE", - ];""")); + StructureExampleGenerator.generateStructuralHintDocumentation(list, model, false, true), + equalTo(""" + [ // list + "STRING_VALUE", + ];""")); } @Test public void generateStructuralHintDocumentation_blob() { assertThat( - StructureExampleGenerator.generateStructuralHintDocumentation(blobStructure, model, false, true), - equalTo(""" - { // blobStructure - blob: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("") - streamingBlob: "MULTIPLE_TYPES_ACCEPTED", // see \\@smithy/types -> StreamingBlobPayloadInputTypes - };""")); + StructureExampleGenerator.generateStructuralHintDocumentation(blobStructure, model, false, true), + equalTo(""" + { // blobStructure + blob: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("") + streamingBlob: "MULTIPLE_TYPES_ACCEPTED", // see \\@smithy/types -> StreamingBlobPayloadInputTypes + };""")); assertThat( - StructureExampleGenerator.generateStructuralHintDocumentation(blobStructure, model, false, false), - equalTo(""" - { // blobStructure - blob: new Uint8Array(), - streamingBlob: "", // see \\@smithy/types -> StreamingBlobPayloadOutputTypes - };""")); + StructureExampleGenerator.generateStructuralHintDocumentation(blobStructure, model, false, false), + equalTo(""" + { // blobStructure + blob: new Uint8Array(), + streamingBlob: "", // see \\@smithy/types -> StreamingBlobPayloadOutputTypes + };""")); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2GeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2GeneratorTest.java index 2a1a9550674..3bde79d8a95 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2GeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2GeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.endpointsV2; import static org.hamcrest.MatcherAssert.assertThat; @@ -21,114 +25,113 @@ public void containsTrailingSemicolon() { String ruleset = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/endpoint/ruleset.ts").get(); assertEquals( - """ -// smithy-typescript generated code -import type { RuleSetObject } from "@smithy/types"; - -export const ruleSet: RuleSetObject = { - version: "1.3", - parameters: { - Region: { - type: "String", - documentation: "The region to dispatch this request, eg. `us-east-1`.", - }, - Stage: { - type: "String", - required: true, - default: "production", - }, - Endpoint: { - builtIn: "SDK::Endpoint", - type: "String", - required: false, - documentation: "Override the endpoint used to send this request", - }, - }, - rules: [ - { - conditions: [ - { - fn: "isSet", - argv: [ - { - ref: "Endpoint", - }, - ], - }, - { - fn: "parseURL", - argv: [ - { - ref: "Endpoint", - }, - ], - assign: "url", - }, - ], - endpoint: { - url: { - ref: "Endpoint", - }, - properties: {}, - headers: {}, - }, - type: "endpoint", - }, - { - documentation: "Template the region into the URI when region is set", - conditions: [ - { - fn: "isSet", - argv: [ - { - ref: "Region", - }, - ], - }, - ], - type: "tree", - rules: [ - { - conditions: [ - { - fn: "stringEquals", - argv: [ - { - ref: "Stage", - }, - "staging", - ], - }, - ], - endpoint: { - url: "https://{Region}.staging.example.com/2023-01-01", - properties: {}, - headers: {}, - }, - type: "endpoint", - }, - { - conditions: [], - endpoint: { - url: "https://{Region}.example.com/2023-01-01", - properties: {}, - headers: {}, - }, - type: "endpoint", - }, - ], - }, - { - documentation: "Fallback when region is unset", - conditions: [], - error: "Region must be set to resolve a valid endpoint", - type: "error", - }, - ], -}; -""", - ruleset - ); + """ + // smithy-typescript generated code + import type { RuleSetObject } from "@smithy/types"; + + export const ruleSet: RuleSetObject = { + version: "1.3", + parameters: { + Region: { + type: "String", + documentation: "The region to dispatch this request, eg. `us-east-1`.", + }, + Stage: { + type: "String", + required: true, + default: "production", + }, + Endpoint: { + builtIn: "SDK::Endpoint", + type: "String", + required: false, + documentation: "Override the endpoint used to send this request", + }, + }, + rules: [ + { + conditions: [ + { + fn: "isSet", + argv: [ + { + ref: "Endpoint", + }, + ], + }, + { + fn: "parseURL", + argv: [ + { + ref: "Endpoint", + }, + ], + assign: "url", + }, + ], + endpoint: { + url: { + ref: "Endpoint", + }, + properties: {}, + headers: {}, + }, + type: "endpoint", + }, + { + documentation: "Template the region into the URI when region is set", + conditions: [ + { + fn: "isSet", + argv: [ + { + ref: "Region", + }, + ], + }, + ], + type: "tree", + rules: [ + { + conditions: [ + { + fn: "stringEquals", + argv: [ + { + ref: "Stage", + }, + "staging", + ], + }, + ], + endpoint: { + url: "https://{Region}.staging.example.com/2023-01-01", + properties: {}, + headers: {}, + }, + type: "endpoint", + }, + { + conditions: [], + endpoint: { + url: "https://{Region}.example.com/2023-01-01", + properties: {}, + headers: {}, + }, + type: "endpoint", + }, + ], + }, + { + documentation: "Fallback when region is unset", + conditions: [], + error: "Region must be set to resolve a valid endpoint", + type: "error", + }, + ], + }; + """, + ruleset); } @Test @@ -137,56 +140,60 @@ public void containsExtraContextParameter() { String ruleset = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/endpoint/ruleset.ts").get(); - assertThat(ruleset, containsString( - """ - }, - Stage: { - type: "String", - required: true, - default: "production", - }, - """)); - - String endpointParameters = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/endpoint/EndpointParameters.ts").get(); - - assertThat(endpointParameters, containsString( - """ - return Object.assign(options, { - stage: options.stage ?? "production", - defaultSigningName: "", - }); - """)); - assertThat(endpointParameters, containsString( - """ - export interface ClientInputEndpointParameters { - region?: string | undefined | Provider; - stage?: string | undefined | Provider; - endpoint?:""")); + assertThat(ruleset, + containsString( + """ + }, + Stage: { + type: "String", + required: true, + default: "production", + }, + """)); + + String endpointParameters = + manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/endpoint/EndpointParameters.ts").get(); + + assertThat(endpointParameters, + containsString( + """ + return Object.assign(options, { + stage: options.stage ?? "production", + defaultSigningName: "", + }); + """)); + assertThat(endpointParameters, + containsString( + """ + export interface ClientInputEndpointParameters { + region?: string | undefined | Provider; + stage?: string | undefined | Provider; + endpoint?:""")); } private MockManifest testEndpoints(String filename) { MockManifest manifest = new MockManifest(); PluginContext context = PluginContext.builder() - .pluginClassLoader(getClass().getClassLoader()) - .model(Model.assembler() - .addImport(getClass().getResource(filename)) - .discoverModels() - .assemble() - .unwrap()) - .fileManifest(manifest) - .settings(Node.objectNodeBuilder() - .withMember("service", Node.from("smithy.example#Example")) - .withMember("package", Node.from("example")) - .withMember("packageVersion", Node.from("1.0.0")) - .build()) - .build(); + .pluginClassLoader(getClass().getClassLoader()) + .model(Model.assembler() + .addImport(getClass().getResource(filename)) + .discoverModels() + .assemble() + .unwrap()) + .fileManifest(manifest) + .settings(Node.objectNodeBuilder() + .withMember("service", Node.from("smithy.example#Example")) + .withMember("package", Node.from("example")) + .withMember("packageVersion", Node.from("1.0.0")) + .build()) + .build(); new TypeScriptCodegenPlugin().execute(context); assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/endpoint/EndpointParameters.ts"), - is(true)); + is(true)); assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/endpoint/endpointResolver.ts"), - is(true)); + is(true)); String contents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/endpoint/ruleset.ts").get(); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinderTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinderTest.java index ea4392f248a..7d87c8561e2 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinderTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinderTest.java @@ -1,8 +1,14 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.endpointsV2; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + import java.util.List; import java.util.Optional; - import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -12,9 +18,6 @@ import software.amazon.smithy.rulesengine.language.EndpointRuleSet; import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class RuleSetParameterFinderTest { RuleSetParameterFinder subject; @@ -113,7 +116,6 @@ class RuleSetParameterFinderTest { } """); - @Test void getEffectiveParams(@Mock ServiceShape serviceShape, @Mock EndpointRuleSetTrait endpointRuleSetTrait) { EndpointRuleSet endpointRuleSet = EndpointRuleSet.fromNode(ruleSet); @@ -123,7 +125,8 @@ void getEffectiveParams(@Mock ServiceShape serviceShape, @Mock EndpointRuleSetTr List effectiveParams = subject.getEffectiveParams(); - assertEquals(List.of("BasicParameter", "NestedParameter", "ShorthandParameter", "UrlOnlyParameter"), effectiveParams); + assertEquals(List.of("BasicParameter", "NestedParameter", "ShorthandParameter", "UrlOnlyParameter"), + effectiveParams); } @Test @@ -132,27 +135,25 @@ void getJmesPathExpression(@Mock ServiceShape serviceShape, @Mock EndpointRuleSe subject = new RuleSetParameterFinder(serviceShape); assertEquals( - """ - Object.keys(input?.RequestItems ?? {})""", - subject.getJmesPathExpression("?.", "input", "keys(RequestItems)") - ); + """ + Object.keys(input?.RequestItems ?? {})""", + subject.getJmesPathExpression("?.", "input", "keys(RequestItems)")); assertEquals( - """ - input?.TableCreationParameters?.TableName""", - subject.getJmesPathExpression("?.", "input", "TableCreationParameters.TableName") - ); + """ + input?.TableCreationParameters?.TableName""", + subject.getJmesPathExpression("?.", "input", "TableCreationParameters.TableName")); assertEquals( - """ - input?.TransactItems?.map((obj: any) => obj?.Get?.TableName""", - subject.getJmesPathExpression("?.", "input", "TransactItems[*].Get.TableName") - ); + """ + input?.TransactItems?.map((obj: any) => obj?.Get?.TableName""", + subject.getJmesPathExpression("?.", "input", "TransactItems[*].Get.TableName")); assertEquals( - """ - input?.TransactItems?.map((obj: any) => [obj?.ConditionCheck?.TableName,obj?.Put?.TableName,obj?.Delete?.TableName,obj?.Update?.TableName].filter((i) => i)).flat()""", - subject.getJmesPathExpression("?.", "input", "TransactItems[*].[ConditionCheck.TableName, Put.TableName, Delete.TableName, Update.TableName][]") - ); + """ + input?.TransactItems?.map((obj: any) => [obj?.ConditionCheck?.TableName,obj?.Put?.TableName,obj?.Delete?.TableName,obj?.Update?.TableName].filter((i) => i)).flat()""", + subject.getJmesPathExpression("?.", + "input", + "TransactItems[*].[ConditionCheck.TableName, Put.TableName, Delete.TableName, Update.TableName][]")); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPluginTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPluginTest.java index c28295d8708..31c580cbec3 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPluginTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/AddHttpApiKeyAuthPluginTest.java @@ -1,24 +1,13 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.typescript.codegen.integration; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; import org.junit.jupiter.api.Test; import software.amazon.smithy.build.MockManifest; @@ -61,7 +50,8 @@ private void testInjects(String filename, String extra) { assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(), containsString("from \"../middleware/HttpApiKeyAuth\"")); - String generatedGetFooCommand = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(); + String generatedGetFooCommand = + manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(); assertThat(generatedGetFooCommand, containsString("getHttpApiKeyAuthPlugin(config")); assertThat(generatedGetFooCommand, containsString(extra)); @@ -80,8 +70,7 @@ private void testInjects(String filename, String extra) { containsString("from \"./middleware/HttpApiKeyAuth\"")); } - private MockManifest generate(String filename) - { + private MockManifest generate(String filename) { MockManifest manifest = new MockManifest(); PluginContext context = PluginContext.builder() .pluginClassLoader(getClass().getClassLoader()) @@ -135,6 +124,6 @@ private void testDoesNotInject(String filename) { // 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\""))); + not(containsString("from \"./middleware/HttpApiKeyAuth\""))); } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitorTest.java index 80b762db823..412c2706f78 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.integration; import static org.hamcrest.MatcherAssert.assertThat; @@ -39,8 +43,8 @@ import software.amazon.smithy.model.traits.TimestampFormatTrait; import software.amazon.smithy.model.traits.TimestampFormatTrait.Format; import software.amazon.smithy.typescript.codegen.TypeScriptSettings; -import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.TypeScriptSettings.ArtifactType; +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext; import software.amazon.smithy.utils.ListUtils; @@ -87,7 +91,7 @@ public static Collection validMemberTargetTypes() { String delegate = "de_Foo" + "(" + DATA_SOURCE + ", context)"; - return ListUtils.of(new Object[][]{ + return ListUtils.of(new Object[][] { {BooleanShape.builder().id(id).build(), "__expectBoolean(" + DATA_SOURCE + ")", source}, {ByteShape.builder().id(id).build(), "__expectByte(" + DATA_SOURCE + ")", source}, {DoubleShape.builder().id(id).build(), "__limitedParseDouble(" + DATA_SOURCE + ")", source}, @@ -97,9 +101,9 @@ public static Collection validMemberTargetTypes() { {ShortShape.builder().id(id).build(), "__expectShort(" + DATA_SOURCE + ")", source}, {StringShape.builder().id(id).build(), "__expectString(" + DATA_SOURCE + ")", source}, { - StringShape.builder().id(id).addTrait(new MediaTypeTrait("foo+json")).build(), - "__LazyJsonString.from(" + DATA_SOURCE + ")", - source + StringShape.builder().id(id).addTrait(new MediaTypeTrait("foo+json")).build(), + "__LazyJsonString.from(" + DATA_SOURCE + ")", + source }, {BlobShape.builder().id(id).build(), "context.base64Decoder(" + DATA_SOURCE + ")", source}, {DocumentShape.builder().id(id).build(), delegate, source}, @@ -108,24 +112,24 @@ public static Collection validMemberTargetTypes() { {MapShape.builder().id(id).key(key).value(value).build(), delegate, source}, {StructureShape.builder().id(id).build(), delegate, source}, { - TimestampShape.builder().id(id).build(), - "__expectNonNull(__parseEpochTimestamp(" + DATA_SOURCE + "))", - source + TimestampShape.builder().id(id).build(), + "__expectNonNull(__parseEpochTimestamp(" + DATA_SOURCE + "))", + source }, { - TimestampShape.builder().id(id).build(), - "__expectNonNull(__parseRfc3339DateTime(" + DATA_SOURCE + "))", - source.toBuilder().addTrait(new TimestampFormatTrait(TimestampFormatTrait.DATE_TIME)).build() + TimestampShape.builder().id(id).build(), + "__expectNonNull(__parseRfc3339DateTime(" + DATA_SOURCE + "))", + source.toBuilder().addTrait(new TimestampFormatTrait(TimestampFormatTrait.DATE_TIME)).build() }, { - TimestampShape.builder().id(id).build(), - "__expectNonNull(__parseRfc7231DateTime(" + DATA_SOURCE + "))", - source.toBuilder().addTrait(new TimestampFormatTrait(TimestampFormatTrait.HTTP_DATE)).build() + TimestampShape.builder().id(id).build(), + "__expectNonNull(__parseRfc7231DateTime(" + DATA_SOURCE + "))", + source.toBuilder().addTrait(new TimestampFormatTrait(TimestampFormatTrait.HTTP_DATE)).build() }, { - UnionShape.builder().id(id).addMember(member).build(), - "de_Foo(__expectUnion(" + DATA_SOURCE + "), context)", - source + UnionShape.builder().id(id).addMember(member).build(), + "de_Foo(__expectUnion(" + DATA_SOURCE + "), context)", + source }, }); } @@ -164,11 +168,15 @@ private static final class MockProvider implements SymbolProvider { public Symbol toSymbol(Shape shape) { if (shape instanceof CollectionShape) { MemberShape member = MemberShape.builder().id(id + "$member").target(id + "Target").build(); - return collectionMock.toBuilder().putProperty("shape", - ListShape.builder().id(id).member(member).build()).build(); + return collectionMock.toBuilder() + .putProperty("shape", + ListShape.builder().id(id).member(member).build()) + .build(); } - return mock.toBuilder().putProperty("shape", - StructureShape.builder().id(id).build()).build(); + return mock.toBuilder() + .putProperty("shape", + StructureShape.builder().id(id).build()) + .build(); } } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitorTest.java index c40d09aec5d..72ada18e730 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.integration; import static org.hamcrest.MatcherAssert.assertThat; @@ -70,7 +74,7 @@ public static Collection validMemberTargetTypes() { MemberShape value = MemberShape.builder().id(id + "$value").target(targetId).build(); String delegate = "se_Foo(" + DATA_SOURCE + ", context)"; - return ListUtils.of(new Object[][]{ + return ListUtils.of(new Object[][] { {BooleanShape.builder().id(id).build(), DATA_SOURCE}, {BigDecimalShape.builder().id(id).build(), "String(" + DATA_SOURCE + ")"}, {BigIntegerShape.builder().id(id).build(), "String(" + DATA_SOURCE + ")"}, @@ -82,8 +86,8 @@ public static Collection validMemberTargetTypes() { {ShortShape.builder().id(id).build(), DATA_SOURCE}, {StringShape.builder().id(id).build(), DATA_SOURCE}, { - StringShape.builder().id(id).addTrait(new MediaTypeTrait("foo+json")).build(), - "__LazyJsonString.from(" + DATA_SOURCE + ")" + StringShape.builder().id(id).addTrait(new MediaTypeTrait("foo+json")).build(), + "__LazyJsonString.from(" + DATA_SOURCE + ")" }, {BlobShape.builder().id(id).build(), "context.base64Encoder(" + DATA_SOURCE + ")"}, {DocumentShape.builder().id(id).build(), delegate}, @@ -129,11 +133,15 @@ private static final class MockProvider implements SymbolProvider { public Symbol toSymbol(Shape shape) { if (shape instanceof CollectionShape) { MemberShape member = MemberShape.builder().id(id + "$member").target(id + "Target").build(); - return collectionMock.toBuilder().putProperty("shape", - ListShape.builder().id(id).member(member).build()).build(); + return collectionMock.toBuilder() + .putProperty("shape", + ListShape.builder().id(id).member(member).build()) + .build(); } - return mock.toBuilder().putProperty("shape", - StructureShape.builder().id(id).build()).build(); + return mock.toBuilder() + .putProperty("shape", + StructureShape.builder().id(id).build()) + .build(); } } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGeneratorTest.java index 9298633e0fe..63b4360a4ae 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGeneratorTest.java @@ -1,5 +1,12 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.integration; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -11,22 +18,18 @@ import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.shapes.StructureShape; import software.amazon.smithy.model.shapes.UnionShape; -import software.amazon.smithy.model.traits.HttpPayloadTrait; import software.amazon.smithy.model.traits.StreamingTrait; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class EventStreamGeneratorTest { @Test void getEventStreamMember( - @Mock ProtocolGenerator.GenerationContext context, - @Mock Model model, - @Mock StructureShape struct, - @Mock MemberShape eventStreamMember1, - @Mock ShapeId streamingMember1ShapeId, - @Mock UnionShape streamingTarget1 + @Mock ProtocolGenerator.GenerationContext context, + @Mock Model model, + @Mock StructureShape struct, + @Mock MemberShape eventStreamMember1, + @Mock ShapeId streamingMember1ShapeId, + @Mock UnionShape streamingTarget1 ) { when(struct.members()).thenReturn(List.of(eventStreamMember1)); when(eventStreamMember1.getTarget()).thenReturn(streamingMember1ShapeId); @@ -37,50 +40,46 @@ void getEventStreamMember( when(streamingTarget1.isUnionShape()).thenReturn(true); MemberShape eventStreamMember = EventStreamGenerator.getEventStreamMember( - context, - struct - ); + context, + struct); assertEquals(eventStreamMember1, eventStreamMember); } @Test void getEventStreamMemberTooFew( - @Mock ProtocolGenerator.GenerationContext context, - @Mock StructureShape struct + @Mock ProtocolGenerator.GenerationContext context, + @Mock StructureShape struct ) { when(struct.members()).thenReturn(List.of()); when(struct.getId()).thenReturn(ShapeId.from("namespace#Shape")); try { MemberShape eventStreamMember = EventStreamGenerator.getEventStreamMember( - context, - struct - ); + context, + struct); } catch (CodegenException e) { assertEquals( - "No event stream member found in " + struct.getId().toString(), - e.getMessage() - ); + "No event stream member found in " + struct.getId().toString(), + e.getMessage()); } } @Test void getEventStreamMemberTooMany( - @Mock ProtocolGenerator.GenerationContext context, - @Mock Model model, - @Mock StructureShape struct, - @Mock MemberShape eventStreamMember1, - @Mock ShapeId streamingMember1ShapeId, - @Mock UnionShape streamingTarget1, - @Mock MemberShape eventStreamMember2, - @Mock ShapeId streamingMember2ShapeId, - @Mock UnionShape streamingTarget2 + @Mock ProtocolGenerator.GenerationContext context, + @Mock Model model, + @Mock StructureShape struct, + @Mock MemberShape eventStreamMember1, + @Mock ShapeId streamingMember1ShapeId, + @Mock UnionShape streamingTarget1, + @Mock MemberShape eventStreamMember2, + @Mock ShapeId streamingMember2ShapeId, + @Mock UnionShape streamingTarget2 ) { when(struct.members()).thenReturn(List.of( - eventStreamMember1, - eventStreamMember2 - )); + eventStreamMember1, + eventStreamMember2)); when(context.getModel()).thenReturn(model); when(struct.getId()).thenReturn(ShapeId.from("namespace#Shape")); @@ -96,14 +95,12 @@ void getEventStreamMemberTooMany( try { MemberShape eventStreamMember = EventStreamGenerator.getEventStreamMember( - context, - struct - ); + context, + struct); } catch (CodegenException e) { assertEquals( - "More than one event stream member in " + struct.getId().toString(), - e.getMessage() - ); + "More than one event stream member in " + struct.getId().toString(), + e.getMessage()); } } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtilsTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtilsTest.java index 77b63881e50..d88006cd817 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtilsTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtilsTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.integration; import static org.hamcrest.MatcherAssert.assertThat; @@ -29,11 +33,14 @@ public void givesCorrectTimestampSerialization() { TimestampShape shape = TimestampShape.builder().id("com.smithy.example#Foo").build(); assertThat("__serializeDateTime(" + DATA_SOURCE + ")", - equalTo(HttpProtocolGeneratorUtils.getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.DATE_TIME))); + equalTo(HttpProtocolGeneratorUtils + .getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.DATE_TIME))); assertThat("(" + DATA_SOURCE + ".getTime() / 1_000)", - equalTo(HttpProtocolGeneratorUtils.getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.EPOCH_SECONDS))); + equalTo(HttpProtocolGeneratorUtils + .getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.EPOCH_SECONDS))); assertThat("__dateToUtcString(" + DATA_SOURCE + ")", - equalTo(HttpProtocolGeneratorUtils.getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.HTTP_DATE))); + equalTo(HttpProtocolGeneratorUtils + .getTimestampInputParam(mockContext, DATA_SOURCE, shape, Format.HTTP_DATE))); } @Test @@ -42,15 +49,45 @@ public void givesCorrectTimestampDeserialization() { TypeScriptWriter writer = new TypeScriptWriter("foo"); assertThat("__expectNonNull(__parseRfc3339DateTimeWithOffset(" + DATA_SOURCE + "))", - equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.DATE_TIME, false, true))); + equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, + DATA_SOURCE, + Location.DOCUMENT, + shape, + Format.DATE_TIME, + false, + true))); assertThat("__expectNonNull(__parseRfc3339DateTime(" + DATA_SOURCE + "))", - equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.DATE_TIME, false, false))); + equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, + DATA_SOURCE, + Location.DOCUMENT, + shape, + Format.DATE_TIME, + false, + false))); assertThat("__expectNonNull(__parseEpochTimestamp(__expectNumber(" + DATA_SOURCE + ")))", - equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.EPOCH_SECONDS, true, false))); + equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, + DATA_SOURCE, + Location.DOCUMENT, + shape, + Format.EPOCH_SECONDS, + true, + false))); assertThat("__expectNonNull(__parseEpochTimestamp(" + DATA_SOURCE + "))", - equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.EPOCH_SECONDS, false, false))); + equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, + DATA_SOURCE, + Location.DOCUMENT, + shape, + Format.EPOCH_SECONDS, + false, + false))); assertThat("__expectNonNull(__parseRfc7231DateTime(" + DATA_SOURCE + "))", - equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, DATA_SOURCE, Location.DOCUMENT, shape, Format.HTTP_DATE, false, false))); + equalTo(HttpProtocolGeneratorUtils.getTimestampOutputParam(writer, + DATA_SOURCE, + Location.DOCUMENT, + shape, + Format.HTTP_DATE, + false, + false))); } @Test @@ -72,10 +109,12 @@ public void writesCorrectHostPrefix() { assertThat(writer.toString(), containsString("let { hostname: resolvedHostname } = await context.endpoint();")); assertThat(writer.toString(), containsString("if (context.disableHostPrefix !== true) {")); assertThat(writer.toString(), containsString("resolvedHostname = \"{foo}.data.\" + resolvedHostname;")); - assertThat(writer.toString(), containsString("resolvedHostname = resolvedHostname.replace(\"{foo}\", input.foo!)")); + assertThat(writer.toString(), + containsString("resolvedHostname = resolvedHostname.replace(\"{foo}\", input.foo!)")); assertThat(writer.toString(), containsString("if (!__isValidHostname(resolvedHostname)) {")); - assertThat(writer.toString(), containsString( - "throw new Error(\"ValidationError: prefixed hostname must be hostname compatible.")); + assertThat(writer.toString(), + containsString( + "throw new Error(\"ValidationError: prefixed hostname must be hostname compatible.")); } private static final class MockProvider implements SymbolProvider { @@ -87,8 +126,10 @@ private static final class MockProvider implements SymbolProvider { @Override public Symbol toSymbol(Shape shape) { - return mock.toBuilder().putProperty("shape", - StructureShape.builder().id(id).build()).build(); + return mock.toBuilder() + .putProperty("shape", + StructureShape.builder().id(id).build()) + .build(); } } } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGeneratorTest.java index 6c64b5d90dd..e1b4ea44bce 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGeneratorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.integration; import static org.hamcrest.MatcherAssert.assertThat; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java index cbc1f5bce90..d0213a72f88 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.integration; import static org.hamcrest.MatcherAssert.assertThat; @@ -5,7 +9,6 @@ import java.util.HashMap; import java.util.Map; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.codegen.core.Symbol; @@ -81,20 +84,26 @@ public void allowsConfigurableSettingsPredicate() { TypeScriptSettings createDefaultReadmeTrueSettings = new TypeScriptSettings(); createDefaultReadmeTrueSettings.setCreateDefaultReadme(true); - assertThat(createDefaultReadmeFlagPlugin.matchesSettings(model, service, createDefaultReadmeTrueSettings), equalTo(true)); + assertThat(createDefaultReadmeFlagPlugin.matchesSettings(model, service, createDefaultReadmeTrueSettings), + equalTo(true)); TypeScriptSettings createDefaultReadmeFalseSettings = new TypeScriptSettings(); - assertThat(createDefaultReadmeFlagPlugin.matchesSettings(model, service, createDefaultReadmeFalseSettings), equalTo(false)); + assertThat(createDefaultReadmeFlagPlugin.matchesSettings(model, service, createDefaultReadmeFalseSettings), + equalTo(false)); } @Test public void configuresWithDefaultConventions() { - Map resolveFunctionParams = new HashMap() {{ + Map resolveFunctionParams = new HashMap() { + { put("resolveFunctionParam", "resolveFunctionParam"); - }}; - Map pluginFunctionParams = new HashMap() {{ + } + }; + Map pluginFunctionParams = new HashMap() { + { put("pluginFunctionParam", "pluginFunctionParam"); - }}; + } + }; RuntimeClientPlugin plugin = RuntimeClientPlugin.builder() .withConventions("foo/baz", "1.0.0", "Foo") @@ -121,35 +130,38 @@ public void configuresWithDefaultConventions() { equalTo(pluginFunctionParams)); assertThat(plugin.getInputConfig().get().getSymbol().getDependencies().get(0).getPackageName(), - equalTo("foo/baz")); + equalTo("foo/baz")); assertThat(plugin.getResolvedConfig().get().getSymbol().getDependencies().get(0).getPackageName(), - equalTo("foo/baz")); + equalTo("foo/baz")); assertThat(plugin.getResolveFunction().get().getSymbol().getDependencies().get(0).getPackageName(), - equalTo("foo/baz")); + equalTo("foo/baz")); assertThat(plugin.getPluginFunction().get().getSymbol().getDependencies().get(0).getPackageName(), - equalTo("foo/baz")); + equalTo("foo/baz")); assertThat(plugin.getInputConfig().get().getSymbol().getDependencies().get(0).getVersion(), - equalTo("1.0.0")); + equalTo("1.0.0")); assertThat(plugin.getResolvedConfig().get().getSymbol().getDependencies().get(0).getVersion(), - equalTo("1.0.0")); + equalTo("1.0.0")); assertThat(plugin.getResolveFunction().get().getSymbol().getDependencies().get(0).getVersion(), - equalTo("1.0.0")); + equalTo("1.0.0")); assertThat(plugin.getPluginFunction().get().getSymbol().getDependencies().get(0).getVersion(), - equalTo("1.0.0")); + equalTo("1.0.0")); } @Test public void allConfigSymbolsMustBeSetIfAnyAreSet() { - Assertions.assertThrows(IllegalStateException.class, () -> RuntimeClientPlugin.builder() - .inputConfig(Symbol.builder().namespace("foo", "/").name("abc").build()).build()); + Assertions.assertThrows(IllegalStateException.class, + () -> RuntimeClientPlugin.builder() + .inputConfig(Symbol.builder().namespace("foo", "/").name("abc").build()) + .build()); } @Test public void destroyFunctionRequiresResolvedConfig() { - Assertions.assertThrows(IllegalStateException.class, () -> RuntimeClientPlugin.builder() - .withConventions("foo/baz", "1.0.0", "Foo", RuntimeClientPlugin.Convention.HAS_DESTROY) - .build()); + Assertions.assertThrows(IllegalStateException.class, + () -> RuntimeClientPlugin.builder() + .withConventions("foo/baz", "1.0.0", "Foo", RuntimeClientPlugin.Convention.HAS_DESTROY) + .build()); } @Test diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndexTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndexTest.java index d868afe6a72..976605fe2de 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndexTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/knowledge/SerdeElisionIndexTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.knowledge; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -59,19 +63,25 @@ public void cannotElideUnsupportedTypes() { @Test public void cannotElideNestedUnsupportedTypes() { - model = model.toBuilder().addShapes( - // Shim set shapes into 2.0 model. - SetShape.builder().id("foo.bar#BigDecimalSet").member(ShapeId.from("foo.bar#BigDecimal")).build(), - SetShape.builder().id("foo.bar#BigIntegerSet").member(ShapeId.from("foo.bar#BigInteger")).build(), - SetShape.builder().id("foo.bar#BlobSet").member(ShapeId.from("foo.bar#Blob")).build(), - SetShape.builder().id("foo.bar#DocumentSet").member(ShapeId.from("foo.bar#Document")).build(), - SetShape.builder().id("foo.bar#TimestampSet").member(ShapeId.from("foo.bar#Timestamp")).build(), - SetShape.builder().id("foo.bar#DoubleSet").member(ShapeId.from("foo.bar#Double")).build(), - SetShape.builder().id("foo.bar#FloatSet").member(ShapeId.from("foo.bar#Float")).build() - ).build(); + model = model.toBuilder() + .addShapes( + // Shim set shapes into 2.0 model. + SetShape.builder() + .id("foo.bar#BigDecimalSet") + .member(ShapeId.from("foo.bar#BigDecimal")) + .build(), + SetShape.builder() + .id("foo.bar#BigIntegerSet") + .member(ShapeId.from("foo.bar#BigInteger")) + .build(), + SetShape.builder().id("foo.bar#BlobSet").member(ShapeId.from("foo.bar#Blob")).build(), + SetShape.builder().id("foo.bar#DocumentSet").member(ShapeId.from("foo.bar#Document")).build(), + SetShape.builder().id("foo.bar#TimestampSet").member(ShapeId.from("foo.bar#Timestamp")).build(), + SetShape.builder().id("foo.bar#DoubleSet").member(ShapeId.from("foo.bar#Double")).build(), + SetShape.builder().id("foo.bar#FloatSet").member(ShapeId.from("foo.bar#Float")).build()) + .build(); SerdeElisionIndex index = SerdeElisionIndex.of(model); - assertFalse(index.mayElide(model.getShape(ShapeId.from("foo.bar#BigDecimalList")).get())); assertFalse(index.mayElide(model.getShape(ShapeId.from("foo.bar#BigIntegerList")).get())); assertFalse(index.mayElide(model.getShape(ShapeId.from("foo.bar#BlobList")).get())); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitorTest.java index 9076e5d9a32..ba8080f1d6c 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberDeserVisitorTest.java @@ -1,5 +1,12 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.protocols.cbor; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -12,47 +19,40 @@ import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class CborMemberDeserVisitorTest { private CborMemberDeserVisitor subject; @BeforeEach void setup( - @Mock ProtocolGenerator.GenerationContext context, - @Mock Model model, - @Mock TypeScriptWriter typeScriptWriter, - @Mock TypeScriptSettings settings + @Mock ProtocolGenerator.GenerationContext context, + @Mock Model model, + @Mock TypeScriptWriter typeScriptWriter, + @Mock TypeScriptSettings settings ) { when(context.getModel()).thenReturn(model); when(context.getWriter()).thenReturn(typeScriptWriter); when(context.getSettings()).thenReturn(settings); subject = new CborMemberDeserVisitor( - context, - "data" - ); + context, + "data"); } @Test void blobShape(@Mock BlobShape blobShape) { // no decoder for blob in cbor. assertEquals( - "data", - subject.blobShape( - blobShape - ) - ); + "data", + subject.blobShape( + blobShape)); } @Test void timestampShape(@Mock TimestampShape timestampShape) { // protocol always uses this timestamp format. assertEquals( - "__expectNonNull(__parseEpochTimestamp(data))", - subject.timestampShape(timestampShape) - ); + "__expectNonNull(__parseEpochTimestamp(data))", + subject.timestampShape(timestampShape)); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitorTest.java index dadb9b0c0b5..b1899694747 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborMemberSerVisitorTest.java @@ -1,5 +1,13 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.protocols.cbor; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.when; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -13,10 +21,6 @@ import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.lenient; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class CborMemberSerVisitorTest { @@ -24,51 +28,46 @@ class CborMemberSerVisitorTest { @BeforeEach void setup( - @Mock ProtocolGenerator.GenerationContext context, - @Mock Model model, - @Mock TypeScriptWriter typeScriptWriter + @Mock ProtocolGenerator.GenerationContext context, + @Mock Model model, + @Mock TypeScriptWriter typeScriptWriter ) { when(context.getModel()).thenReturn(model); lenient().when(context.getWriter()).thenReturn(typeScriptWriter); subject = new CborMemberSerVisitor( - context, - "data" - ); + context, + "data"); } @Test void blobShape(@Mock BlobShape blob) { // no encoder for blob in cbor. assertEquals( - "data", - subject.blobShape(blob) - ); + "data", + subject.blobShape(blob)); } @Test void floatShape(@Mock FloatShape floatShape) { // no serializer function for float in cbor. assertEquals( - "data", - subject.floatShape(floatShape) - ); + "data", + subject.floatShape(floatShape)); } @Test void doubleShape(@Mock DoubleShape doubleShape) { // no serializer function for double in cbor. assertEquals( - "data", - subject.doubleShape(doubleShape) - ); + "data", + subject.doubleShape(doubleShape)); } @Test void timestampShape(@Mock TimestampShape timestampShape) { assertEquals( - "__dateToTag(data)", - subject.timestampShape(timestampShape) - ); + "__dateToTag(data)", + subject.timestampShape(timestampShape)); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitorTest.java index 80775da1c75..7ec95e996fa 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeDeserVisitorTest.java @@ -1,5 +1,15 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.protocols.cbor; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -22,12 +32,6 @@ import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; import software.amazon.smithy.utils.MapUtils; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.lenient; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class CborShapeDeserVisitorTest { @@ -39,76 +43,74 @@ class CborShapeDeserVisitorTest { @BeforeEach void setUp( - @Mock TypeScriptSettings typeScriptSettings + @Mock TypeScriptSettings typeScriptSettings ) { lenient().when(context.getWriter()).thenReturn(writer); lenient().when(context.getSettings()).thenReturn(typeScriptSettings); lenient().when(typeScriptSettings.generateServerSdk()) - .thenReturn(false); + .thenReturn(false); subject = new CborShapeDeserVisitor( - context - ); + context); } @Test void deserializeCollection( - @Mock CollectionShape collectionShape, - @Mock MemberShape memberShape, - @Mock ShapeId shapeId, - @Mock Shape target, - @Mock Model model + @Mock CollectionShape collectionShape, + @Mock MemberShape memberShape, + @Mock ShapeId shapeId, + @Mock Shape target, + @Mock Model model ) { when(collectionShape.getMember()).thenReturn(memberShape); when(memberShape.getTarget()).thenReturn(shapeId); when(context.getModel()).thenReturn(model); when(model.expectShape(any(ShapeId.class))) - .thenReturn(target); + .thenReturn(target); when(target.accept(any())).thenReturn("entry"); subject.deserializeCollection(context, collectionShape); verify(writer).write( - "const collection = (output || [])$L", - ".filter((e: any) => e != null)" - ); + "const collection = (output || [])$L", + ".filter((e: any) => e != null)"); } @Test void deserializeDocument( - @Mock DocumentShape documentShape + @Mock DocumentShape documentShape ) { subject.deserializeDocument(context, documentShape); verify(writer).write( - """ - return output; // document. """ - ); + return output; // document. + """); } @Test - void deserializeMap(@Mock MapShape mapShape, - @Mock MemberShape valueShape, - @Mock ShapeId shapeId, - @Mock SymbolProvider symbolProvider, - @Mock Symbol symbol, - @Mock Shape target, - @Mock Model model) { + void deserializeMap( + @Mock MapShape mapShape, + @Mock MemberShape valueShape, + @Mock ShapeId shapeId, + @Mock SymbolProvider symbolProvider, + @Mock Symbol symbol, + @Mock Shape target, + @Mock Model model + ) { when(mapShape.getValue()).thenReturn(valueShape); when(valueShape.getTarget()).thenReturn(shapeId); when(context.getSymbolProvider()).thenReturn(symbolProvider); when(context.getModel()).thenReturn(model); when(model.expectShape(shapeId)).thenReturn(target); when(symbolProvider.toSymbol(mapShape)) - .thenReturn(symbol); + .thenReturn(symbol); subject.deserializeMap(context, mapShape); verify(writer).openBlock( - eq("return Object.entries(output).reduce((acc: $T, [key, value]: [string, any]) => {"), - eq(""), - eq(symbol), - any() - ); + eq("return Object.entries(output).reduce((acc: $T, [key, value]: [string, any]) => {"), + eq(""), + eq(symbol), + any()); } @Test @@ -116,26 +118,27 @@ void deserializeStructure(@Mock StructureShape structureShape) { subject.deserializeStructure(context, structureShape); verify(writer).addImport( - "take", null, TypeScriptDependency.AWS_SMITHY_CLIENT - ); + "take", + null, + TypeScriptDependency.AWS_SMITHY_CLIENT); verify(writer).openBlock( - eq("return take(output, {"), - eq("}) as any;"), - any() - ); + eq("return take(output, {"), + eq("}) as any;"), + any()); } @Test - void deserializeUnion(@Mock UnionShape unionShape, - @Mock MemberShape mapMember, - @Mock ShapeId shapeId, - @Mock Shape target, - @Mock Model model) { + void deserializeUnion( + @Mock UnionShape unionShape, + @Mock MemberShape mapMember, + @Mock ShapeId shapeId, + @Mock Shape target, + @Mock Model model + ) { when(unionShape.getAllMembers()).thenReturn( - MapUtils.of( - "member", mapMember - ) - ); + MapUtils.of( + "member", + mapMember)); when(mapMember.getTarget()).thenReturn(shapeId); when(context.getModel()).thenReturn(model); @@ -144,14 +147,12 @@ void deserializeUnion(@Mock UnionShape unionShape, subject.deserializeUnion(context, unionShape); verify(writer).openBlock( - eq("if ($1L != null) {"), - eq("}"), - eq("output.member"), - any() - ); + eq("if ($1L != null) {"), + eq("}"), + eq("output.member"), + any()); verify(writer).write( - "return { $$unknown: Object.entries(output)[0] };" - ); + "return { $$unknown: Object.entries(output)[0] };"); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitorTest.java index 6554a1770d3..e970794d335 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/protocols/cbor/CborShapeSerVisitorTest.java @@ -1,5 +1,15 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.protocols.cbor; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -21,12 +31,6 @@ import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.lenient; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class CborShapeSerVisitorTest { @@ -39,10 +43,10 @@ class CborShapeSerVisitorTest { @BeforeEach void setUp( - @Mock Model model, - @Mock Shape shape, - @Mock SymbolProvider symbolProvider, - @Mock Symbol symbol + @Mock Model model, + @Mock Shape shape, + @Mock SymbolProvider symbolProvider, + @Mock Symbol symbol ) { lenient().when(context.getWriter()).thenReturn(writer); lenient().when(context.getSymbolProvider()).thenReturn(symbolProvider); @@ -53,96 +57,89 @@ void setUp( lenient().when(context.getModel()).thenReturn(model); lenient().when(model.expectShape(any(ShapeId.class))).thenReturn(shape); lenient().when(shape.accept(any(CborMemberSerVisitor.class))) - .thenReturn("entry"); + .thenReturn("entry"); subject = new CborShapeSerVisitor( - context - ); + context); } @Test void serializeCollection( - @Mock CollectionShape collectionShape, - @Mock MemberShape memberShape, - @Mock ShapeId shapeId + @Mock CollectionShape collectionShape, + @Mock MemberShape memberShape, + @Mock ShapeId shapeId ) { when(collectionShape.getMember()).thenReturn(memberShape); when(memberShape.getTarget()).thenReturn(shapeId); subject.serializeCollection( - context, - collectionShape - ); + context, + collectionShape); verify(writer).write( - "return input$L;", - ".filter((e: any) => e != null)" - ); + "return input$L;", + ".filter((e: any) => e != null)"); } @Test void serializeDocument(@Mock DocumentShape documentShape) { subject.serializeDocument( - context, - documentShape - ); + context, + documentShape); verify(writer).write( - """ - return input; // document. """ - ); + return input; // document. + """); } @Test - void serializeMap(@Mock MapShape mapShape, - @Mock MemberShape valueShape, - @Mock ShapeId shapeId) { + void serializeMap( + @Mock MapShape mapShape, + @Mock MemberShape valueShape, + @Mock ShapeId shapeId + ) { when(mapShape.getValue()).thenReturn(valueShape); when(valueShape.getTarget()).thenReturn(shapeId); subject.serializeMap( - context, - mapShape - ); + context, + mapShape); verify(writer).openBlock( - eq("return Object.entries(input).reduce((acc: Record, [key, value]: [$1L, any]) => {"), - eq("}, {});"), - eq("string"), - any() - ); + eq("return Object.entries(input).reduce((acc: Record, [key, value]: [$1L, any]) => {"), + eq("}, {});"), + eq("string"), + any()); } @Test void serializeStructure(@Mock StructureShape structureShape) { subject.serializeStructure( - context, - structureShape - ); + context, + structureShape); verify(writer).addImport("take", null, TypeScriptDependency.AWS_SMITHY_CLIENT); verify(writer).openBlock( - eq("return take(input, {"), - eq("});"), - any() - ); + eq("return take(input, {"), + eq("});"), + any()); } @Test - void serializeUnion(@Mock UnionShape unionShape, - @Mock ServiceShape service, - @Mock ShapeId shapeId) { + void serializeUnion( + @Mock UnionShape unionShape, + @Mock ServiceShape service, + @Mock ShapeId shapeId + ) { when(context.getService()).thenReturn(service); when(unionShape.getId()).thenReturn(shapeId); when(shapeId.getName(service)).thenReturn("name"); subject.serializeUnion( - context, - unionShape - ); + context, + unionShape); verify(writer).openBlock( - eq("return $L.visit(input, {"), - eq("});"), - eq("name"), - any() - ); + eq("return $L.visit(input, {"), + eq("});"), + eq("name"), + any()); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndexTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndexTest.java index 2151d0be4b9..86810e20fef 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndexTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaReferenceIndexTest.java @@ -1,10 +1,18 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.schema; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Set; import java.util.function.Function; import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.CollectionShape; import software.amazon.smithy.model.shapes.MapShape; @@ -13,10 +21,6 @@ import software.amazon.smithy.model.shapes.StructureShape; import software.amazon.smithy.typescript.codegen.knowledge.SerdeElisionIndexTest; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; - class SchemaReferenceIndexTest { private static Model model; private static SchemaReferenceIndex subject; @@ -24,31 +28,30 @@ class SchemaReferenceIndexTest { @BeforeAll public static void before() { model = Model.assembler() - .addImport(SerdeElisionIndexTest.class.getResource("serde-elision.smithy")) - .assemble() - .unwrap(); + .addImport(SerdeElisionIndexTest.class.getResource("serde-elision.smithy")) + .assemble() + .unwrap(); subject = new SchemaReferenceIndex(model); } @Test void isReferenceSchema() { Stream simpleShapes = Stream.of( - model.getStringShapes().stream(), - model.getBooleanShapes().stream(), - model.getByteShapes().stream(), - model.getDoubleShapes().stream(), - model.getFloatShapes().stream(), - model.getShortShapes().stream(), - model.getIntegerShapes().stream(), - model.getLongShapes().stream(), - model.getEnumShapes().stream(), - model.getIntEnumShapes().stream(), - model.getBigIntegerShapes().stream(), - model.getBigDecimalShapes().stream(), - model.getTimestampShapes().stream(), - model.getBlobShapes().stream(), - model.getDocumentShapes().stream() - ).flatMap(Function.identity()); + model.getStringShapes().stream(), + model.getBooleanShapes().stream(), + model.getByteShapes().stream(), + model.getDoubleShapes().stream(), + model.getFloatShapes().stream(), + model.getShortShapes().stream(), + model.getIntegerShapes().stream(), + model.getLongShapes().stream(), + model.getEnumShapes().stream(), + model.getIntEnumShapes().stream(), + model.getBigIntegerShapes().stream(), + model.getBigDecimalShapes().stream(), + model.getTimestampShapes().stream(), + model.getBlobShapes().stream(), + model.getDocumentShapes().stream()).flatMap(Function.identity()); simpleShapes.forEach(booleanShape -> { assertFalse(subject.isReferenceSchema(booleanShape)); }); @@ -59,20 +62,19 @@ void isReferenceSchema() { }); Stream collectionShapes = Stream.of( - model.getSetShapes().stream(), - model.getListShapes().stream(), - model.getMapShapes().stream() - ).flatMap(Function.identity()); + model.getSetShapes().stream(), + model.getListShapes().stream(), + model.getMapShapes().stream()).flatMap(Function.identity()); collectionShapes.forEach(shape -> { boolean isRef; if (shape instanceof CollectionShape collection) { isRef = subject.isReferenceSchema(collection.getMember()); } else if (shape instanceof MapShape map) { - isRef = subject.isReferenceSchema(map.getValue()); + isRef = subject.isReferenceSchema(map.getValue()); } else { throw new UnsupportedOperationException("Unexpected shape type"); } assertEquals(isRef, subject.isReferenceSchema(shape)); }); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtensionTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtensionTest.java index 7f85e0d1a5f..265320ba6ba 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtensionTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitExtensionTest.java @@ -1,10 +1,14 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.schema; +import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; import software.amazon.smithy.model.traits.JsonNameTrait; -import static org.junit.jupiter.api.Assertions.*; - class SchemaTraitExtensionTest { private static final SchemaTraitExtension subject = SchemaTraitExtension.INSTANCE; @@ -31,8 +35,7 @@ void render() { throw new UnsupportedOperationException("wrong trait type"); }); assertEquals( - "test__test", - subject.render(trait) - ); + "test__test", + subject.render(trait)); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndexTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndexTest.java index b36eab1f51b..dd3cb668b61 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndexTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitFilterIndexTest.java @@ -1,5 +1,12 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.schema; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Set; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import software.amazon.smithy.model.Model; @@ -34,10 +41,6 @@ import software.amazon.smithy.typescript.codegen.knowledge.SerdeElisionIndexTest; import software.amazon.smithy.utils.SetUtils; -import java.util.Set; - -import static org.junit.jupiter.api.Assertions.*; - class SchemaTraitFilterIndexTest { private static Model model; private static SchemaTraitFilterIndex subject; @@ -45,13 +48,12 @@ class SchemaTraitFilterIndexTest { @BeforeAll public static void before() { model = Model.assembler() - .addImport(SerdeElisionIndexTest.class.getResource("serde-elision.smithy")) - .assemble() - .unwrap(); + .addImport(SerdeElisionIndexTest.class.getResource("serde-elision.smithy")) + .assemble() + .unwrap(); subject = new SchemaTraitFilterIndex(model); } - @Test void hasSchemaTraits() { Set sparseShapes = model.getShapesWithTrait(SparseTrait.class); @@ -60,54 +62,50 @@ void hasSchemaTraits() { for (Shape sparseShape : sparseShapes) { assertTrue(subject.hasSchemaTraits(sparseShape)); assertTrue( - subject.includeTrait( - sparseShape.getTrait(SparseTrait.class).get().toShapeId() - ) - ); + subject.includeTrait( + sparseShape.getTrait(SparseTrait.class).get().toShapeId())); } } @Test void includeTrait() { Set excludedShapes = SetUtils.of( - TimestampFormatTrait.ID - ); + TimestampFormatTrait.ID); for (ShapeId excludedShape : excludedShapes) { String presence = subject.includeTrait(excludedShape) - ? "should not be included" - : excludedShape.getName(); + ? "should not be included" + : excludedShape.getName(); assertEquals(excludedShape.getName(), presence); } Set includedTraits = SetUtils.of( - SparseTrait.ID, - SensitiveTrait.ID, - IdempotencyTokenTrait.ID, - JsonNameTrait.ID, - MediaTypeTrait.ID, - XmlAttributeTrait.ID, - XmlFlattenedTrait.ID, - XmlNameTrait.ID, - XmlNamespaceTrait.ID, - EventHeaderTrait.ID, - EventPayloadTrait.ID, - StreamingTrait.ID, - RequiresLengthTrait.ID, - EndpointTrait.ID, - HttpErrorTrait.ID, - HttpHeaderTrait.ID, - HttpQueryTrait.ID, - HttpLabelTrait.ID, - HttpPayloadTrait.ID, - HttpPrefixHeadersTrait.ID, - HttpQueryParamsTrait.ID, - HttpResponseCodeTrait.ID, - HostLabelTrait.ID, - ErrorTrait.ID, - HttpTrait.ID - ); + SparseTrait.ID, + SensitiveTrait.ID, + IdempotencyTokenTrait.ID, + JsonNameTrait.ID, + MediaTypeTrait.ID, + XmlAttributeTrait.ID, + XmlFlattenedTrait.ID, + XmlNameTrait.ID, + XmlNamespaceTrait.ID, + EventHeaderTrait.ID, + EventPayloadTrait.ID, + StreamingTrait.ID, + RequiresLengthTrait.ID, + EndpointTrait.ID, + HttpErrorTrait.ID, + HttpHeaderTrait.ID, + HttpQueryTrait.ID, + HttpLabelTrait.ID, + HttpPayloadTrait.ID, + HttpPrefixHeadersTrait.ID, + HttpQueryParamsTrait.ID, + HttpResponseCodeTrait.ID, + HostLabelTrait.ID, + ErrorTrait.ID, + HttpTrait.ID); for (ShapeId includedTrait : includedTraits) { String presence = subject.includeTrait(includedTrait) ? includedTrait.getName() : "is missing"; assertEquals(includedTrait.getName(), presence); } } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGeneratorTest.java index ce07beb638d..0fa67d0868b 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitGeneratorTest.java @@ -1,5 +1,12 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.schema; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; import org.junit.jupiter.api.Test; import software.amazon.smithy.model.pattern.UriPattern; import software.amazon.smithy.model.traits.EndpointTrait; @@ -12,10 +19,6 @@ import software.amazon.smithy.model.traits.XmlNamespaceTrait; import software.amazon.smithy.typescript.codegen.util.StringStore; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - class SchemaTraitGeneratorTest { private static final SchemaTraitGenerator subject = new SchemaTraitGenerator(); @@ -26,29 +29,31 @@ public void test() { } private static final List testCases = List.of( - // timestamp - new TestPair("", new TimestampFormatTrait("date-time")), - // strings - new TestPair("_jN", new JsonNameTrait("jsonName")), - - // annotations - new TestPair("1", new XmlAttributeTrait()), - - // data traits - new TestPair("[\"prefix\"]\n", EndpointTrait.builder() - .hostPrefix("prefix") - .build()), - new TestPair("[_p, _h]\n", XmlNamespaceTrait.builder() - .prefix("prefix") - .uri("https://localhost") - .build()), - new TestPair("404", new HttpErrorTrait(404)), - new TestPair("[\"GET\", \"/uri-pattern\", 200]\n", HttpTrait.builder() - .method("GET") - .uri(UriPattern.parse("/uri-pattern")) - .code(200) - .build()) - ); + // timestamp + new TestPair("", new TimestampFormatTrait("date-time")), + // strings + new TestPair("_jN", new JsonNameTrait("jsonName")), + + // annotations + new TestPair("1", new XmlAttributeTrait()), + + // data traits + new TestPair("[\"prefix\"]\n", + EndpointTrait.builder() + .hostPrefix("prefix") + .build()), + new TestPair("[_p, _h]\n", + XmlNamespaceTrait.builder() + .prefix("prefix") + .uri("https://localhost") + .build()), + new TestPair("404", new HttpErrorTrait(404)), + new TestPair("[\"GET\", \"/uri-pattern\", 200]\n", + HttpTrait.builder() + .method("GET") + .uri(UriPattern.parse("/uri-pattern")) + .code(200) + .build())); @Test void serializeTraitData() { @@ -56,4 +61,4 @@ void serializeTraitData() { testCase.test(); } } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriterTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriterTest.java index 20a4270d9aa..c170bf27914 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriterTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/schema/SchemaTraitWriterTest.java @@ -1,5 +1,11 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.schema; +import static org.junit.jupiter.api.Assertions.*; + import java.util.Set; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -9,8 +15,6 @@ import software.amazon.smithy.typescript.codegen.knowledge.SerdeElisionIndexTest; import software.amazon.smithy.typescript.codegen.util.StringStore; -import static org.junit.jupiter.api.Assertions.*; - class SchemaTraitWriterTest { private static Model model; private static SchemaReferenceIndex schemaReferenceIndex; @@ -19,15 +23,14 @@ class SchemaTraitWriterTest { @BeforeAll public static void before() { model = Model.assembler() - .addImport(SerdeElisionIndexTest.class.getResource("serde-elision.smithy")) - .assemble() - .unwrap(); + .addImport(SerdeElisionIndexTest.class.getResource("serde-elision.smithy")) + .assemble() + .unwrap(); schemaReferenceIndex = new SchemaReferenceIndex(model); subject = new SchemaTraitWriter( - null, - schemaReferenceIndex, - new StringStore() - ); + null, + schemaReferenceIndex, + new StringStore()); } @Test @@ -36,13 +39,12 @@ void testToString() { assertEquals(1, streamingShapes.size()); for (Shape streamingShape : streamingShapes) { subject = new SchemaTraitWriter( - streamingShape, - schemaReferenceIndex, - new StringStore() - ); + streamingShape, + schemaReferenceIndex, + new StringStore()); String codeGeneration = subject.toString(); assertEquals(""" - { [_s]: 1 }""", codeGeneration); + { [_s]: 1 }""", codeGeneration); } } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessorTest.java index 468c218d5ac..72ba0bc01d9 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/PropertyAccessorTest.java @@ -1,9 +1,13 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.util; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + class PropertyAccessorTest { @Test void getFrom() { @@ -21,4 +25,4 @@ void getFromQuoted() { void getFromExtraQuoted() { assertEquals("output[`file\"system\"id`]", PropertyAccessor.getFrom("output", "file\"system\"id")); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/StringStoreTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/StringStoreTest.java index bcacbf659d3..3ae062b4319 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/StringStoreTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/util/StringStoreTest.java @@ -1,124 +1,126 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.util; -import org.junit.jupiter.api.Test; -import java.util.Objects; - import static org.junit.jupiter.api.Assertions.*; +import java.util.Objects; +import org.junit.jupiter.api.Test; + class StringStoreTest { @Test void var() { StringStore subject = new StringStore(); String sourceCode = """ - const array = [ - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s, - %s - ]; - """.formatted( - subject.var("SomeObject"), - subject.var("some_object"), - subject.var("SomeObject"), - subject.var("some_object"), - subject.var("_"), - subject.var("__"), - subject.var("___"), - subject.var("_internal"), - subject.var("__internal"), - subject.var("___internal"), - subject.var("_internal_"), - subject.var("__internal__"), - subject.var("___internal__"), - subject.var("_two--words"), - subject.var("__twoWords__"), - subject.var("___TwoWords__"), - subject.var("$Symbol"), - subject.var("%Symbol"), - subject.var(" !)(@*#&$^% "), - subject.var(" !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^"), - subject.var("**Ack**Ack**"), - subject.var("Spaces Are Cool"), - subject.var("__why Would &&& YouName $something this...") - ); + const array = [ + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s, + %s + ]; + """.formatted( + subject.var("SomeObject"), + subject.var("some_object"), + subject.var("SomeObject"), + subject.var("some_object"), + subject.var("_"), + subject.var("__"), + subject.var("___"), + subject.var("_internal"), + subject.var("__internal"), + subject.var("___internal"), + subject.var("_internal_"), + subject.var("__internal__"), + subject.var("___internal__"), + subject.var("_two--words"), + subject.var("__twoWords__"), + subject.var("___TwoWords__"), + subject.var("$Symbol"), + subject.var("%Symbol"), + subject.var(" !)(@*#&$^% "), + subject.var(" !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^"), + subject.var("**Ack**Ack**"), + subject.var("Spaces Are Cool"), + subject.var("__why Would &&& YouName $something this...")); String[] expected = """ - const _ = "_"; - const _AA = "**Ack**Ack**"; - const _S = "$Symbol"; - const _SAC = "Spaces Are Cool"; - const _SO = "SomeObject"; - const _S_ = " !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^"; - const _Sy = "%Symbol"; - const _TW = "___TwoWords__"; - const __ = "__"; - const ___ = "___"; - const ____ = " !)(@*#&$^% "; - const _i = "_internal"; - const _in = "__internal"; - const _int = "___internal"; - const _inte = "_internal_"; - const _inter = "__internal__"; - const _intern = "___internal__"; - const _so = "some_object"; - const _tW = "__twoWords__"; - const _tw = "_two--words"; - const _wWYt = "__why Would &&& YouName $something this..."; - - const array = [ - _SO, - _so, - _SO, - _so, - _, - __, - ___, - _i, - _in, - _int, - _inte, - _inter, - _intern, - _tw, - _tW, - _TW, - _S, - _Sy, - ____, - _S_, - _AA, - _SAC, - _wWYt - ]; - """.split("\n"); + const _ = "_"; + const _AA = "**Ack**Ack**"; + const _S = "$Symbol"; + const _SAC = "Spaces Are Cool"; + const _SO = "SomeObject"; + const _S_ = " !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^"; + const _Sy = "%Symbol"; + const _TW = "___TwoWords__"; + const __ = "__"; + const ___ = "___"; + const ____ = " !)(@*#&$^% "; + const _i = "_internal"; + const _in = "__internal"; + const _int = "___internal"; + const _inte = "_internal_"; + const _inter = "__internal__"; + const _intern = "___internal__"; + const _so = "some_object"; + const _tW = "__twoWords__"; + const _tw = "_two--words"; + const _wWYt = "__why Would &&& YouName $something this..."; + + const array = [ + _SO, + _so, + _SO, + _so, + _, + __, + ___, + _i, + _in, + _int, + _inte, + _inter, + _intern, + _tw, + _tW, + _TW, + _S, + _Sy, + ____, + _S_, + _AA, + _SAC, + _wWYt + ]; + """.split("\n"); String[] actual = (subject.flushVariableDeclarationCode() + "\n" - + sourceCode).split("\n"); + + sourceCode).split("\n"); for (int i = 0; i < expected.length; ++i) { assertEquals( - Objects.toString(i) + ": " + expected[i].trim(), - Objects.toString(i) + ": " + actual[i].trim() - ); + Objects.toString(i) + ": " + expected[i].trim(), + Objects.toString(i) + ": " + actual[i].trim()); } } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ImportFromTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ImportFromTest.java index 087b1a8ff8f..a2f7f600b68 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ImportFromTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ImportFromTest.java @@ -1,101 +1,82 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.validation; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + class ImportFromTest { @Test void isNodejsNative() { assertTrue( - new ImportFrom("node:buffer").isNodejsNative() - ); + new ImportFrom("node:buffer").isNodejsNative()); assertTrue( - new ImportFrom("stream").isNodejsNative() - ); + new ImportFrom("stream").isNodejsNative()); assertFalse( - new ImportFrom("@smithy/util").isNodejsNative() - ); + new ImportFrom("@smithy/util").isNodejsNative()); assertFalse( - new ImportFrom("../file").isNodejsNative() - ); + new ImportFrom("../file").isNodejsNative()); } @Test void isNamespaced() { assertTrue( - new ImportFrom("@smithy/util/submodule").isNamespaced() - ); + new ImportFrom("@smithy/util/submodule").isNamespaced()); assertTrue( - new ImportFrom("@smithy/util").isNamespaced() - ); + new ImportFrom("@smithy/util").isNamespaced()); assertFalse( - new ImportFrom("node:stream").isNamespaced() - ); + new ImportFrom("node:stream").isNamespaced()); assertFalse( - new ImportFrom("fs/promises").isNamespaced() - ); + new ImportFrom("fs/promises").isNamespaced()); } @Test void isRelative() { assertTrue( - new ImportFrom("/file/path").isRelative() - ); + new ImportFrom("/file/path").isRelative()); assertTrue( - new ImportFrom("./file/path").isRelative() - ); + new ImportFrom("./file/path").isRelative()); assertTrue( - new ImportFrom("../../../../file/path").isRelative() - ); + new ImportFrom("../../../../file/path").isRelative()); assertFalse( - new ImportFrom("@smithy/util").isRelative() - ); + new ImportFrom("@smithy/util").isRelative()); assertFalse( - new ImportFrom("fs/promises").isRelative() - ); + new ImportFrom("fs/promises").isRelative()); } @Test void isDeclarablePackageImport() { assertTrue( - new ImportFrom("@smithy/util/submodule").isDeclarablePackageImport() - ); + new ImportFrom("@smithy/util/submodule").isDeclarablePackageImport()); assertTrue( - new ImportFrom("@smithy/util").isDeclarablePackageImport() - ); + new ImportFrom("@smithy/util").isDeclarablePackageImport()); assertTrue( - new ImportFrom("smithy_pkg").isDeclarablePackageImport() - ); + new ImportFrom("smithy_pkg").isDeclarablePackageImport()); assertTrue( - new ImportFrom("smithy_pkg/array").isDeclarablePackageImport() - ); + new ImportFrom("smithy_pkg/array").isDeclarablePackageImport()); assertFalse( - new ImportFrom("node:buffer").isDeclarablePackageImport() - ); + new ImportFrom("node:buffer").isDeclarablePackageImport()); assertFalse( - new ImportFrom("../pkg/pkg").isDeclarablePackageImport() - ); + new ImportFrom("../pkg/pkg").isDeclarablePackageImport()); } @Test void getPackageName() { assertEquals( - new ImportFrom("smithy_pkg/array").getPackageName(), - "smithy_pkg" - ); + new ImportFrom("smithy_pkg/array").getPackageName(), + "smithy_pkg"); assertEquals( - new ImportFrom("@smithy/util/submodule").getPackageName(), - "@smithy/util" - ); + new ImportFrom("@smithy/util/submodule").getPackageName(), + "@smithy/util"); assertEquals( - new ImportFrom("node:fs/promises").getPackageName(), - "fs" - ); + new ImportFrom("node:fs/promises").getPackageName(), + "fs"); assertEquals( - new ImportFrom("smithy_pkg").getPackageName(), - "smithy_pkg" - ); + new ImportFrom("smithy_pkg").getPackageName(), + "smithy_pkg"); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/LongValidatorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/LongValidatorTest.java index d6e966fc3ce..466d96b0a30 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/LongValidatorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/LongValidatorTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.validation; import static org.hamcrest.MatcherAssert.assertThat; diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java index eaa5152f3a0..a3f2a7acd84 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/ReplaceLastTest.java @@ -1,9 +1,13 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.validation; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + class ReplaceLastTest { @Test @@ -13,4 +17,4 @@ public void replaceLast() { assertEquals(ReplaceLast.in("welcometothecity", "e", "is"), "welcometothiscity"); } -} \ No newline at end of file +} diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinderTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinderTest.java index f81dcd47f91..72a1c054cb7 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinderTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/SensitiveDataFinderTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.validation; import static org.hamcrest.MatcherAssert.assertThat; @@ -16,129 +20,136 @@ public class SensitiveDataFinderTest { StringShape sensitiveString = StringShape.builder() - .addTrait(new SensitiveTrait()) - .id("foo.bar#sensitiveString") - .build(); + .addTrait(new SensitiveTrait()) + .id("foo.bar#sensitiveString") + .build(); StringShape dullString = StringShape.builder() - .id("foo.bar#dullString") - .build(); + .id("foo.bar#dullString") + .build(); MemberShape memberWithSensitiveData = MemberShape.builder() - .id("foo.bar#sensitive$member") - .target(sensitiveString.getId()) - .build(); + .id("foo.bar#sensitive$member") + .target(sensitiveString.getId()) + .build(); MemberShape memberWithDullData = MemberShape.builder() - .id("foo.bar#dull$member") - .target(dullString.getId()) - .build(); + .id("foo.bar#dull$member") + .target(dullString.getId()) + .build(); MemberShape listMemberWithSensitiveData = MemberShape.builder() - .id("foo.bar#listSensitive$member") - .target(sensitiveString.getId()) - .build(); + .id("foo.bar#listSensitive$member") + .target(sensitiveString.getId()) + .build(); MemberShape listMemberWithDullData = MemberShape.builder() - .id("foo.bar#listDull$member") - .target(dullString.getId()) - .build(); + .id("foo.bar#listDull$member") + .target(dullString.getId()) + .build(); MemberShape mapMemberWithSensitiveKeyData = MemberShape.builder() - .id("foo.bar#mapSensitiveKey$member") - .target(sensitiveString.getId()) - .build(); + .id("foo.bar#mapSensitiveKey$member") + .target(sensitiveString.getId()) + .build(); MemberShape mapMemberWithSensitiveValueData = MemberShape.builder() - .id("foo.bar#mapSensitiveValue$member") - .target(sensitiveString.getId()) - .build(); + .id("foo.bar#mapSensitiveValue$member") + .target(sensitiveString.getId()) + .build(); StructureShape structureShapeSensitive = StructureShape.builder() - .id("foo.bar#sensitive") - .members( - Collections.singleton(memberWithSensitiveData)) - .build(); + .id("foo.bar#sensitive") + .members( + Collections.singleton(memberWithSensitiveData)) + .build(); StructureShape structureShapeDull = StructureShape.builder() - .id("foo.bar#dull") - .members( - Collections.singleton(memberWithDullData)) - .build(); + .id("foo.bar#dull") + .members( + Collections.singleton(memberWithDullData)) + .build(); CollectionShape collectionSensitive = ListShape.builder() - .id("foo.bar#listSensitive") - .addMember(listMemberWithSensitiveData) - .build(); + .id("foo.bar#listSensitive") + .addMember(listMemberWithSensitiveData) + .build(); CollectionShape collectionDull = ListShape.builder() - .id("foo.bar#listDull") - .addMember(listMemberWithDullData) - .build(); + .id("foo.bar#listDull") + .addMember(listMemberWithDullData) + .build(); MapShape mapSensitiveKey = MapShape.builder() - .id("foo.bar#mapSensitiveKey") - .key(mapMemberWithSensitiveKeyData) - .value(MemberShape.builder() - .id("foo.bar#mapSensitiveKey$key") - .target(dullString.getId()) - .build()) - .build(); + .id("foo.bar#mapSensitiveKey") + .key(mapMemberWithSensitiveKeyData) + .value(MemberShape.builder() + .id("foo.bar#mapSensitiveKey$key") + .target(dullString.getId()) + .build()) + .build(); MapShape mapSensitiveValue = MapShape.builder() - .id("foo.bar#mapSensitiveValue") - .key(MemberShape.builder() - .id("foo.bar#mapSensitiveValue$key") - .target(dullString.getId()) - .build()) - .value(mapMemberWithSensitiveValueData) - .build(); + .id("foo.bar#mapSensitiveValue") + .key(MemberShape.builder() + .id("foo.bar#mapSensitiveValue$key") + .target(dullString.getId()) + .build()) + .value(mapMemberWithSensitiveValueData) + .build(); MapShape mapDull = MapShape.builder() - .id("foo.bar#mapDull") - .key(MemberShape.builder() - .id("foo.bar#mapDull$key") - .target(dullString.getId()) - .build()) - .value(MemberShape.builder() - .id("foo.bar#mapDull$value") - .target(dullString.getId()) - .build()) - .build(); + .id("foo.bar#mapDull") + .key(MemberShape.builder() + .id("foo.bar#mapDull$key") + .target(dullString.getId()) + .build()) + .value(MemberShape.builder() + .id("foo.bar#mapDull$value") + .target(dullString.getId()) + .build()) + .build(); MapShape nested2 = MapShape.builder() - .id("foo.bar#mapNested2") - .key(MemberShape.builder() - .id("foo.bar#mapNested2$key") - .target(dullString.getId()) - .build()) - .value(MemberShape.builder() - .id("foo.bar#mapNested2$value") - .target(mapSensitiveValue) - .build()) - .build(); + .id("foo.bar#mapNested2") + .key(MemberShape.builder() + .id("foo.bar#mapNested2$key") + .target(dullString.getId()) + .build()) + .value(MemberShape.builder() + .id("foo.bar#mapNested2$value") + .target(mapSensitiveValue) + .build()) + .build(); MapShape nested = MapShape.builder() - .id("foo.bar#mapNested") - .key(MemberShape.builder() - .id("foo.bar#mapNested$key") - .target(dullString.getId()) - .build()) - .value(MemberShape.builder() - .id("foo.bar#mapNested$value") - .target(nested2) - .build()) - .build(); + .id("foo.bar#mapNested") + .key(MemberShape.builder() + .id("foo.bar#mapNested$key") + .target(dullString.getId()) + .build()) + .value(MemberShape.builder() + .id("foo.bar#mapNested$value") + .target(nested2) + .build()) + .build(); private Model model = Model.builder() - .addShapes( - sensitiveString, dullString, - memberWithSensitiveData, memberWithDullData, - structureShapeSensitive, structureShapeDull, - collectionSensitive, collectionDull, - mapSensitiveKey, mapSensitiveValue, mapDull, - nested, nested2) - .build(); + .addShapes( + sensitiveString, + dullString, + memberWithSensitiveData, + memberWithDullData, + structureShapeSensitive, + structureShapeDull, + collectionSensitive, + collectionDull, + mapSensitiveKey, + mapSensitiveValue, + mapDull, + nested, + nested2) + .build(); private SensitiveDataFinder sensitiveDataFinder = new SensitiveDataFinder(model); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCallTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCallTest.java index 61d510e9ef4..9857afb5440 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCallTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/validation/UnaryFunctionCallTest.java @@ -1,3 +1,7 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ package software.amazon.smithy.typescript.codegen.validation; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -33,4 +37,4 @@ void toRef() { assertEquals("container.function", UnaryFunctionCall.toRef("container.function(param)")); assertEquals("factory(param)", UnaryFunctionCall.toRef("factory(param)(param2)")); } -} \ No newline at end of file +}