From c82e28ba0e853cfd5702ebff8bdb984e6b28ce68 Mon Sep 17 00:00:00 2001 From: Chase Coalwell <782571+srchase@users.noreply.github.com> Date: Tue, 5 Sep 2023 13:58:12 -0600 Subject: [PATCH] Only serialize allowConstraintErrors when true --- .../smithy/model/traits/ExamplesTrait.java | 5 ++-- .../model/traits/ExamplesTraitTest.java | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) 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 fe46e028fd7..3cbba6eadbf 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 @@ -178,8 +178,9 @@ public Node toNode() { .withMember("title", Node.from(title)) .withOptionalMember("documentation", getDocumentation().map(Node::from)) .withOptionalMember("error", getError().map(ErrorExample::toNode)) - .withOptionalMember("allowConstraintErrors", BooleanNode.from(allowConstraintErrors) - .asBooleanNode()); + .withOptionalMember("allowConstraintErrors", Optional.of(allowConstraintErrors) + .filter(val -> val.equals(true)) + .map(BooleanNode::from)); if (!input.isEmpty()) { builder.withMember("input", input); 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 19dc1ab68d4..37c3dab47d1 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 @@ 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 void loadsTrait() { 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 = 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()); + } }