From 3988115cc40286f76bfbd357a912952d5238565e Mon Sep 17 00:00:00 2001 From: George Fu Date: Tue, 12 Nov 2024 18:05:19 +0000 Subject: [PATCH] feat: allow undefined when property optional for exactOptionalPropertyTypes --- .../smithy/typescript/codegen/StructuredMemberWriter.java | 3 +++ .../amazon/smithy/typescript/codegen/TypeScriptSettings.java | 2 +- .../smithy/typescript/codegen/StructureGeneratorTest.java | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) 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 ee76483efb3..9edab34d0a1 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 @@ -106,6 +106,9 @@ void writeMembers(TypeScriptWriter writer, Shape shape) { String optionalSuffix = shape.isUnionShape() || !isRequiredMember(member) ? "?" : ""; String typeSuffix = requiredMemberMode == RequiredMemberMode.NULLABLE && isRequiredMember(member) ? " | undefined" : ""; + if (optionalSuffix.equals("?")) { + typeSuffix = " | undefined"; // support exactOptionalPropertyTypes. + } writer.write("${L}${L}${L}: ${T}${L};", memberPrefix, memberName, optionalSuffix, symbolProvider.toSymbol(member), typeSuffix); 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 da555865ac0..29601f09e15 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 @@ -558,7 +558,7 @@ public enum RequiredMemberMode { NULLABLE("nullable"), /** - * This will dissallow members marked as {@link RequiredTrait} to be {@code undefined}. + * This will disallow members marked as {@link RequiredTrait} to be {@code undefined}. * Use this mode with CAUTION because it comes with certain risks. When a server drops * {@link RequiredTrait} from an output shape (and it is replaced with {@link DefaultTrait} * as defined by the spec), if the server does not always serialize a value, 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 4ae57404ef6..9ce6432a8fb 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 @@ -81,7 +81,7 @@ public void properlyGeneratesOptionalNonMessageMemberOfException() { "export class Err extends __BaseException {\n" + " readonly name: \"Err\" = \"Err\";\n" + " readonly $fault: \"client\" = \"client\";\n" - + " foo?: string;\n" + + " foo?: string | undefined;\n" + " /**\n" + " * @internal\n" + " */\n" @@ -525,7 +525,7 @@ public void generatesNonErrorStructures() { String output = writer.toString(); assertThat(output, containsString("export interface Bar {")); - assertThat(output, containsString("foo?: string;")); + assertThat(output, containsString("foo?: string | undefined;")); } private StructureShape createNonErrorStructure() {