Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow non backward compatible required member #566

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion smithy-typescript-codegen-test/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"packageVersion": "0.0.1",
"packageJson": {
"license": "Apache-2.0"
}
},
"memberNullabilityCompatibilityMode": "strict"
eduardomourar marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ public void generateStructure(GenerateStructureDirective<TypeScriptCodegenContex
directive.symbolProvider(),
writer,
directive.shape(),
directive.settings().generateServerSdk()
directive.settings().generateServerSdk(),
directive.settings().getMemberNullabilityCompatibilityMode()
);
generator.run();
});
Expand All @@ -342,7 +343,8 @@ public void generateError(GenerateErrorDirective<TypeScriptCodegenContext, TypeS
directive.symbolProvider(),
writer,
directive.shape(),
directive.settings().generateServerSdk()
directive.settings().generateServerSdk(),
directive.settings().getMemberNullabilityCompatibilityMode()
);
generator.run();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.MemberNullabilityCompatibilityMode;
import software.amazon.smithy.typescript.codegen.integration.HttpProtocolGeneratorUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

Expand Down Expand Up @@ -67,24 +68,29 @@ final class StructureGenerator implements Runnable {
private final TypeScriptWriter writer;
private final StructureShape shape;
private final boolean includeValidation;
private final MemberNullabilityCompatibilityMode memberNullabilityCompatibilityMode;

/**
* sets 'includeValidation' to 'false' for backwards compatibility.
* sets 'includeValidation' to 'false' and backwards compatibility
eduardomourar marked this conversation as resolved.
Show resolved Hide resolved
* to {@link MemberNullabilityCompatibilityMode#RELAXED}.
*/
StructureGenerator(Model model, SymbolProvider symbolProvider, TypeScriptWriter writer, StructureShape shape) {
this(model, symbolProvider, writer, shape, false);
this(model, symbolProvider, writer, shape, false,
MemberNullabilityCompatibilityMode.RELAXED);
}

StructureGenerator(Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer,
StructureShape shape,
boolean includeValidation) {
boolean includeValidation,
MemberNullabilityCompatibilityMode memberNullabilityCompatibilityMode) {
this.model = model;
this.symbolProvider = symbolProvider;
this.writer = writer;
this.shape = shape;
this.includeValidation = includeValidation;
this.memberNullabilityCompatibilityMode = memberNullabilityCompatibilityMode;
}

@Override
Expand Down Expand Up @@ -158,7 +164,7 @@ private void renderNonErrorStructure() {
}

StructuredMemberWriter config = new StructuredMemberWriter(
model, symbolProvider, shape.getAllMembers().values());
model, symbolProvider, shape.getAllMembers().values(), this.memberNullabilityCompatibilityMode);
config.writeMembers(writer, shape);
writer.closeBlock("}");
writer.write("");
Expand Down Expand Up @@ -255,7 +261,7 @@ private void renderErrorStructure() {
HttpProtocolGeneratorUtils.writeRetryableTrait(writer, shape, ";");
}
StructuredMemberWriter structuredMemberWriter = new StructuredMemberWriter(model, symbolProvider,
shape.getAllMembers().values());
shape.getAllMembers().values(), this.memberNullabilityCompatibilityMode);
// since any error interface must extend from JavaScript Error interface, message member is already
// required in the JavaScript Error interface
structuredMemberWriter.skipMembers.add("message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.traits.UniqueItemsTrait;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.MemberNullabilityCompatibilityMode;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
Expand All @@ -60,12 +61,19 @@ final class StructuredMemberWriter {
Collection<MemberShape> members;
String memberPrefix = "";
boolean noDocs;
MemberNullabilityCompatibilityMode memberNullabilityCompatibilityMode;
final Set<String> skipMembers = new HashSet<>();

StructuredMemberWriter(Model model, SymbolProvider symbolProvider, Collection<MemberShape> members) {
this(model, symbolProvider, members, MemberNullabilityCompatibilityMode.RELAXED);
}

StructuredMemberWriter(Model model, SymbolProvider symbolProvider, Collection<MemberShape> members,
MemberNullabilityCompatibilityMode memberNullabilityCompatibilityMode) {
this.model = model;
this.symbolProvider = symbolProvider;
this.members = new LinkedHashSet<>(members);
this.memberNullabilityCompatibilityMode = memberNullabilityCompatibilityMode;
}

void writeMembers(TypeScriptWriter writer, Shape shape) {
Expand All @@ -79,7 +87,8 @@ void writeMembers(TypeScriptWriter writer, Shape shape) {
boolean wroteDocs = !noDocs && writer.writeMemberDocs(model, member);
String memberName = getSanitizedMemberName(member);
String optionalSuffix = shape.isUnionShape() || !isRequiredMember(member) ? "?" : "";
String typeSuffix = isRequiredMember(member) ? " | undefined" : "";
String typeSuffix = memberNullabilityCompatibilityMode == MemberNullabilityCompatibilityMode.RELAXED
&& isRequiredMember(member) ? " | undefined" : "";
writer.write("${L}${L}${L}: ${T}${L};", memberPrefix, memberName, optionalSuffix,
symbolProvider.toSymbol(member), typeSuffix);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.RequiredTrait;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
Expand All @@ -43,6 +44,7 @@
public final class TypeScriptSettings {

static final String DISABLE_DEFAULT_VALIDATION = "disableDefaultValidation";
static final String MEMBER_NULLABILITY_COMPATIBILITY_MODE = "memberNullabilityCompatibilityMode";
static final String TARGET_NAMESPACE = "targetNamespace";
private static final Logger LOGGER = Logger.getLogger(TypeScriptSettings.class.getName());

Expand All @@ -65,6 +67,8 @@ public final class TypeScriptSettings {
private boolean isPrivate;
private ArtifactType artifactType = ArtifactType.CLIENT;
private boolean disableDefaultValidation = false;
private MemberNullabilityCompatibilityMode memberNullabilityCompatibilityMode =
MemberNullabilityCompatibilityMode.RELAXED;
private PackageManager packageManager = PackageManager.YARN;

@Deprecated
Expand Down Expand Up @@ -105,6 +109,10 @@ public static TypeScriptSettings from(Model model, ObjectNode config, ArtifactTy
if (artifactType == ArtifactType.SSDK) {
settings.setDisableDefaultValidation(config.getBooleanMemberOrDefault(DISABLE_DEFAULT_VALIDATION));
}
settings.setMemberNullabilityCompatibilityMode(
config.getStringMember(MEMBER_NULLABILITY_COMPATIBILITY_MODE)
.map(s -> MemberNullabilityCompatibilityMode.fromString(s.getValue()))
.orElse(MemberNullabilityCompatibilityMode.RELAXED));

settings.setPluginSettings(config);
return settings;
Expand Down Expand Up @@ -295,6 +303,21 @@ public void setDisableDefaultValidation(boolean disableDefaultValidation) {
this.disableDefaultValidation = disableDefaultValidation;
}

/**
* Returns the compatibility mode in regards to member nullability.
*
* @return the configured compatibility mode in regards to member nullability.
* Defaults to {@link MemberNullabilityCompatibilityMode#RELAXED}
*/
public MemberNullabilityCompatibilityMode getMemberNullabilityCompatibilityMode() {
return memberNullabilityCompatibilityMode;
}

public void setMemberNullabilityCompatibilityMode(
MemberNullabilityCompatibilityMode memberNullabilityCompatibilityMode) {
this.memberNullabilityCompatibilityMode = memberNullabilityCompatibilityMode;
}

/**
* Returns the package manager used by the generated package.
*
Expand Down Expand Up @@ -407,6 +430,44 @@ public SymbolProvider createSymbolProvider(Model model, TypeScriptSettings setti
}
}

/**
* An enum indicating the compatibility mode in regards to member nullability.
*/
public enum MemberNullabilityCompatibilityMode {
/**
* This is the current behavior and it will be the default. When set,
* it allows a member that has the {@link RequiredTrait} applied to be "undefined".
* By doing so it can still be considered a backwards compatible change fo
* Smithy services even when the required constraint is dropped from a member .
*/
RELAXED("relaxed"),

/**
* This will dissallow members marked as {@link RequiredTrait} to be "undefined".
*/
eduardomourar marked this conversation as resolved.
Show resolved Hide resolved
STRICT("strict");

private final String mode;

MemberNullabilityCompatibilityMode(String mode) {
this.mode = mode;
}

public String getMode() {
return mode;
}

public static MemberNullabilityCompatibilityMode fromString(String s) {
if ("relaxed".equals(s)) {
return RELAXED;
}
if ("strict".equals(s)) {
return STRICT;
}
throw new CodegenException(String.format("Unsupported member nullability compatibility mode: %s", s));
}
}

public enum PackageManager {
YARN("yarn"),
NPM("npm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.MemberNullabilityCompatibilityMode;

public class StructureGeneratorTest {
@Test
Expand Down Expand Up @@ -508,7 +509,19 @@ public void skipsFilterOnEncounteringRecursiveShapes() {
+ "})");
}

@Test
public void properlyGeneratesRequiredMessageMemberNotBackwardCompatible() {
testStructureCodegenBase("test-required-member.smithy",
"export interface GetFooOutput {\n"
+ " someRequiredMember: string;\n"
+ "}\n", MemberNullabilityCompatibilityMode.STRICT);
}

private String testStructureCodegen(String file, String expectedType) {
return testStructureCodegenBase(file, expectedType, MemberNullabilityCompatibilityMode.RELAXED);
}

private String testStructureCodegenBase(String file, String expectedType, MemberNullabilityCompatibilityMode memberNullabilityCompatibilityMode) {
Model model = Model.assembler()
.addImport(getClass().getResource(file))
.assemble()
Expand All @@ -521,6 +534,7 @@ private String testStructureCodegen(String file, String expectedType) {
.withMember("service", Node.from("smithy.example#Example"))
.withMember("package", Node.from("example"))
.withMember("packageVersion", Node.from("1.0.0"))
.withMember("memberNullabilityCompatibilityMode", Node.from(memberNullabilityCompatibilityMode.getMode()))
.build())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace smithy.example

service Example {
version: "1.0.0",
operations: [GetFoo]
}

operation GetFoo {
input: GetFooInput,
output: GetFooOutput
}

structure GetFooInput {}
structure GetFooOutput {
@required
someRequiredMember: String
}