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

Fix IDL serialization of enums with mixins #1865

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,17 @@ private void writeEnumMembers(Collection<MemberShape> members) {

private void applyIntroducedTraits(Collection<MemberShape> members) {
for (MemberShape member : members) {
Map<ShapeId, Trait> introducedTraits = new LinkedHashMap<>(member.getIntroducedTraits());

// The @enumValue trait is serialized using the `=` IDL syntax, so remove it here.
introducedTraits.remove(EnumValueTrait.ID);

// Use short form for a single trait, and block form for multiple traits.
if (member.getIntroducedTraits().size() == 1) {
if (introducedTraits.size() == 1) {
codeWriter.writeInline("apply $I ", member.getId());
serializeTraits(member.getIntroducedTraits(), TraitFeature.NO_SPECIAL_DOCS_SYNTAX);
codeWriter.write("");
} else if (!member.getIntroducedTraits().isEmpty()) {
} else if (!introducedTraits.isEmpty()) {
codeWriter.openBlock("apply $I {", "}", member.getId(), () -> {
// Only serialize local traits, and don't use special documentation syntax here.
serializeTraits(member.getIntroducedTraits(), TraitFeature.NO_SPECIAL_DOCS_SYNTAX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,20 @@ public void sortsAlphabetically() {

assertThat(modelResult, equalTo(IoUtils.readUtf8Url(resource).replace("\r\n", "\n")));
}

@Test
public void handlesEnumMixins() {
URL resource = getClass().getResource("idl-serialization/enum-mixin-input.smithy");
Model model = Model.assembler().addImport(resource).assemble().unwrap();
Map<Path, String> serialized = SmithyIdlModelSerializer.builder().build().serialize(model);

if (serialized.size() != 1) {
throw new RuntimeException("Exactly one smithy file should be output for generated tests.");
}

String expectedOutput = IoUtils.readUtf8Resource(getClass(), "idl-serialization/enum-mixin-output.smithy")
.replaceAll("\\R", "\n");
String serializedString = serialized.entrySet().iterator().next().getValue();
Assertions.assertEquals(expectedOutput, serializedString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$version: "2.0"

namespace ns.foo

@mixin
intEnum IntMixin {
FOO = 1
BAR = 2
}

intEnum IntEnum with [IntMixin] {
FOO = 1
BAR = 2
}

@mixin
enum StringMixin {
BAZ = "baz"
}

enum StringEnum with [StringMixin] {
BAZ = "baz"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$version: "2.0"

namespace ns.foo

intEnum IntEnum with [IntMixin] {}

@mixin
intEnum IntMixin {
FOO = 1
BAR = 2
}

enum StringEnum with [StringMixin] {}

@mixin
enum StringMixin {
BAZ = "baz"
}