Skip to content

Commit

Permalink
feat: remove "| string" and "| number" from enum targeted members (#1028
Browse files Browse the repository at this point in the history
)

* feat: remove "| string" from enum targeted members

* feat: closed enums for numbers
  • Loading branch information
kuhe authored Oct 13, 2023
1 parent 0170356 commit d7f5cda
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .changeset/selfish-planes-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* import { TypedYesNo } from "./TypedYesNo";
*
* interface MyStructure {
* "yesNo": TypedYesNo | string;
* "yesNo": TypedYesNo;
* }
* }</pre>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* import { FaceCard } from "./FaceCard";
*
* interface MyStructure {
* "facecard": FaceCard | number;
* "facecard": FaceCard;
* }
* }</pre>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,7 @@ public Symbol memberShape(MemberShape shape) {
.orElseThrow(() -> new CodegenException("Shape not found: " + shape.getTarget()));
Symbol targetSymbol = toSymbol(targetShape);

if (targetShape.isIntEnumShape()) {
return createMemberSymbolWithIntEnumTarget(targetSymbol);
}

if (targetSymbol.getProperties().containsKey(EnumTrait.class.getName())) {
if (targetSymbol.getProperties().containsKey(EnumTrait.class.getName()) || targetShape.isIntEnumShape()) {
return createMemberSymbolWithEnumTarget(targetSymbol);
}

Expand All @@ -371,24 +367,15 @@ public Symbol memberShape(MemberShape shape) {
return targetSymbol;
}

// Enums are considered "open", meaning it is a backward compatible change to add new
// members. Adding the `string` variant allows for previously generated clients to be
// able to handle unknown enum values that may be added in the future.
// Enums were considered "open" with the union `| string` or `| number`, meaning it was a backwards
// compatible change to add new members. This behavior was later removed to improve the helpfulness
// of closed enumerated union types.
// For overrides, users should use available type system overrides such as "as any" or
// pragma comments.
private Symbol createMemberSymbolWithEnumTarget(Symbol targetSymbol) {
return targetSymbol.toBuilder()
.namespace(null, "/")
.name(targetSymbol.getName() + " | string")
.addReference(targetSymbol)
.build();
}

// IntEnums are considered "open", meaning it is a backward compatible change to add new
// members. Adding the `number` variant allows for previously generated clients to be
// able to handle unknown int enum values that may be added in the future.
private Symbol createMemberSymbolWithIntEnumTarget(Symbol targetSymbol) {
return targetSymbol.toBuilder()
.namespace(null, "/")
.name(targetSymbol.getName() + " | number")
.name(targetSymbol.getName())
.addReference(targetSymbol)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static boolean isValidPropertyName(String value) {
/**
* Creates a list of sorted, pipe separated enum variants as a union.
*
* <p>For example, `"foo" | "baz" | string`. Note: special characters
* <p>For example, `"foo" | "baz"`. Note: special characters
* and quotes are escaped as needed.
*
* @param values Values to create into a union.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void usesLazyJsonStringForJsonMediaType() {
}

@Test
public void addsUnknownStringEnumVariant() {
public void omitsUnknownStringEnumVariant() {
EnumTrait trait = EnumTrait.builder()
.addEnum(EnumDefinition.builder().value("FOO").name("FOO").build())
.addEnum(EnumDefinition.builder().value("BAR").name("BAR").build())
Expand All @@ -218,11 +218,11 @@ public void addsUnknownStringEnumVariant() {
SymbolProvider provider = new SymbolVisitor(model, settings);
Symbol memberSymbol = provider.toSymbol(member);

assertThat(memberSymbol.getName(), equalTo("EnumString | string"));
assertThat(memberSymbol.getName(), equalTo("EnumString"));
}

@Test
public void addsUnknownNumberIntEnumVariant() {
public void omitsUnknownNumberIntEnumVariant() {
IntEnumShape shape = IntEnumShape.builder()
.id("com.foo#Foo")
.addMember("BAR", 2)
Expand All @@ -246,7 +246,7 @@ public void addsUnknownNumberIntEnumVariant() {
SymbolProvider provider = new SymbolVisitor(model, settings);
Symbol memberSymbol = provider.toSymbol(member);

assertThat(memberSymbol.getName(), equalTo("Foo | number"));
assertThat(memberSymbol.getName(), equalTo("Foo"));
}

}

0 comments on commit d7f5cda

Please sign in to comment.