Skip to content

Commit

Permalink
Fix documentation being duplicated with dynamic doc trait (#2544)
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenbaker authored Feb 26, 2025
1 parent de486fa commit 7479355
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ private void serializeTraits(Map<ShapeId, Trait> traits, TraitFeature... traitFe

traits.values()
.stream()
.filter(trait -> noSpecialDocsSyntax || !(trait instanceof DocumentationTrait))
.filter(trait -> noSpecialDocsSyntax || !trait.toShapeId().equals(DocumentationTrait.ID))
// The default and enumValue traits are serialized using the assignment syntactic sugar.
.filter(trait -> {
if (trait instanceof EnumValueTrait) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
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.io.IOException;
import java.net.URISyntaxException;
Expand All @@ -29,9 +31,11 @@
import org.junit.jupiter.api.TestFactory;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.loader.ModelAssembler;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.traits.DefaultTrait;
import software.amazon.smithy.model.traits.DocumentationTrait;
import software.amazon.smithy.model.traits.DynamicTrait;
import software.amazon.smithy.model.traits.RequiredTrait;
import software.amazon.smithy.model.traits.synthetic.OriginalShapeIdTrait;
import software.amazon.smithy.utils.IoUtils;
Expand Down Expand Up @@ -134,7 +138,7 @@ public void filtersDocumentationTrait() {
.assemble()
.unwrap();
SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder()
.traitFilter(trait -> !(trait instanceof DocumentationTrait))
.traitFilter(trait -> !trait.toShapeId().equals(DocumentationTrait.ID))
.build();
Map<Path, String> serialized = serializer.serialize(model);
for (String output : serialized.values()) {
Expand Down Expand Up @@ -345,4 +349,24 @@ public void coercesInlineIO() {
.replace("\r\n", "\n");
assertEquals(expected, modelResult);
}

@Test
public void docsNotDuplicateDocsForDynamicDocTrait() {
Model model = Model.builder()
.addShape(StructureShape.builder()
.id("test#test")
.addTrait(
new DynamicTrait(
ShapeId.from("smithy.api#documentation"),
Node.from("hello")))
.build())
.build();
Map<Path, String> serialized = SmithyIdlModelSerializer.builder().build().serialize(model);
String modelResult = serialized.values().iterator().next().replace("\r\n", "\n");

// IDL serializer will serialize docs with the special syntax, so we can check that it does not also add
// the doc trait itself
assertFalse(modelResult.contains("@documentation"));
assertTrue(modelResult.contains("/// hello"));
}
}

0 comments on commit 7479355

Please sign in to comment.