Skip to content

Commit

Permalink
Only serialize allowConstraintErrors when true
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase committed Sep 5, 2023
1 parent 86ff69b commit c82e28b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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> 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());
}
}

0 comments on commit c82e28b

Please sign in to comment.