Repository: smithy-lang/smithy
Pull request #1968 (2023-09-05 19:59:22)

Only serialize allowConstraintErrors when true
Updates the Examples trait to only serialize the `allowConstraintErrors` when true. If not set, or false, it will not be serialized.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.



Patch:
diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/traits/ExamplesTrait.java b/smithy-model/src/main/java/software/amazon/smithy/model/traits/ExamplesTrait.java
index fe46e028f..f3ac1a863 100644
--- a/smithy-model/src/main/java/software/amazon/smithy/model/traits/ExamplesTrait.java
+++ b/smithy-model/src/main/java/software/amazon/smithy/model/traits/ExamplesTrait.java
@@ -177,9 +177,7 @@ public final class ExamplesTrait extends AbstractTrait implements ToSmithyBuilde
             ObjectNode.Builder builder = Node.objectNodeBuilder()
                     .withMember("title", Node.from(title))
                     .withOptionalMember("documentation", getDocumentation().map(Node::from))
-                    .withOptionalMember("error", getError().map(ErrorExample::toNode))
-                    .withOptionalMember("allowConstraintErrors", BooleanNode.from(allowConstraintErrors)
-                            .asBooleanNode());
+                    .withOptionalMember("error", getError().map(ErrorExample::toNode));
 
             if (!input.isEmpty()) {
                 builder.withMember("input", input);
@@ -188,6 +186,10 @@ public final class ExamplesTrait extends AbstractTrait implements ToSmithyBuilde
                 builder.withMember("output", output);
             }
 
+            if (this.allowConstraintErrors) {
+                builder.withMember("allowConstraintErrors", BooleanNode.from(allowConstraintErrors));
+            }
+
             return builder.build();
         }
 


Test patch:
diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/traits/ExamplesTraitTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/traits/ExamplesTraitTest.java
index 19dc1ab68..37c3dab47 100644
--- a/smithy-model/src/test/java/software/amazon/smithy/model/traits/ExamplesTraitTest.java
+++ b/smithy-model/src/test/java/software/amazon/smithy/model/traits/ExamplesTraitTest.java
@@ -18,12 +18,14 @@ package software.amazon.smithy.model.traits;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.instanceOf;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.Optional;
 import org.junit.jupiter.api.Test;
 import software.amazon.smithy.model.node.ArrayNode;
 import software.amazon.smithy.model.node.Node;
+import software.amazon.smithy.model.node.ObjectNode;
 import software.amazon.smithy.model.shapes.ShapeId;
 
 public class ExamplesTraitTest {
@@ -52,4 +54,31 @@ public class ExamplesTraitTest {
         assertThat(examples.toNode(), equalTo(node));
         assertThat(examples.toBuilder().build(), equalTo(examples));
     }
+
+    @Test
+    public void omitsAllowConstraintErrorsFromSerializedNodeWhenNotTrue() {
+        TraitFactory provider = TraitFactory.createServiceFactory();
+        ArrayNode node = Node.arrayNode(
+                Node.objectNode()
+                        .withMember("title", Node.from("foo")),
+                Node.objectNode()
+                        .withMember("title", Node.from("qux"))
+                        .withMember("documentation", Node.from("docs"))
+                        .withMember("input", Node.objectNode().withMember("a", Node.from("b")))
+                        .withMember("output", Node.objectNode().withMember("c", Node.from("d")))
+                        .withMember("allowConstraintErrors", Node.from(false)),
+                Node.objectNode()
+                        .withMember("title", Node.from("qux"))
+                        .withMember("documentation", Node.from("docs"))
+                        .withMember("input", Node.objectNode().withMember("a", Node.from("b")))
+                        .withMember("output", Node.objectNode().withMember("c", Node.from("d")))
+                        .withMember("allowConstraintErrors", Node.from(true)));
+        Optional<Trait> trait = provider.createTrait(
+                ShapeId.from("smithy.api#examples"), ShapeId.from("ns.qux#foo"), node);
+        ArrayNode serialized = ((ExamplesTrait) trait.get()).createNode().expectArrayNode();
+
+        assertFalse(serialized.get(0).get().asObjectNode().get().getMember("allowConstraintErrors").isPresent());
+        assertFalse(serialized.get(1).get().asObjectNode().get().getMember("allowConstraintErrors").isPresent());
+        assertTrue(serialized.get(2).get().asObjectNode().get().getMember("allowConstraintErrors").isPresent());
+    }
 }