Skip to content

Commit

Permalink
Fix assembler addTraits for some model sources
Browse files Browse the repository at this point in the history
  • Loading branch information
kstich committed Aug 15, 2023
1 parent 9ec4f9d commit a13d754
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,6 @@ public ValidatedResult<Model> assemble() {
// Register manually added shapes. Skip members because they are part of aggregate shapes.
shapes.forEach(processor::putCreatedShape);

// Register manually added traits.
for (Pair<ShapeId, Trait> entry : pendingTraits) {
processor.accept(LoadOperation.ApplyTrait.from(entry.getKey(), entry.getValue()));
}

// Register manually added Models.
for (Model model : mergeModels) {
// Add manually added metadata from the Model.
Expand Down Expand Up @@ -561,6 +556,12 @@ public ValidatedResult<Model> assemble() {
}
}

// Register manually added traits. Do this after loading any other sources of shapes
// so that traits can be applied to them.
for (Pair<ShapeId, Trait> entry : pendingTraits) {
processor.accept(LoadOperation.ApplyTrait.from(entry.getKey(), entry.getValue()));
}

Model processedModel = processor.buildModel();

// Do the 1.0 -> 2.0 transform before full-model validation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,51 @@ public void addsExplicitTraits() {
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
}

@Test
public void addsExplicitTraitsToBuiltModel() {
StringShape shape = StringShape.builder().id("ns.foo#Bar").build();
SuppressTrait trait = SuppressTrait.builder().build();
ValidatedResult<Model> result = new ModelAssembler()
.addModel(Model.assembler().addShape(shape).assemble().unwrap())
.addTrait(shape.toShapeId(), trait)
.assemble();

assertThat(result.getValidationEvents(), empty());
Shape resultShape = result.unwrap().getShape(ShapeId.from("ns.foo#Bar")).get();
assertTrue(resultShape.findTrait("smithy.api#suppress").isPresent());
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
}

@Test
public void addsExplicitTraitsToUnparsedModel() {
String unparsed = "{\"smithy\": \"" + Model.MODEL_VERSION + "\", \"shapes\": { \"ns.foo#Bar\": { \"type\": \"string\"}}}";
SuppressTrait trait = SuppressTrait.builder().build();
ValidatedResult<Model> result = new ModelAssembler()
.addUnparsedModel(SourceLocation.NONE.getFilename(), unparsed)
.addTrait(ShapeId.from("ns.foo#Bar"), trait)
.assemble();

assertThat(result.getValidationEvents(), empty());
Shape resultShape = result.unwrap().getShape(ShapeId.from("ns.foo#Bar")).get();
assertTrue(resultShape.findTrait("smithy.api#suppress").isPresent());
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
}

@Test
public void addsExplicitTraitsToParsedDocumentNode() {
String unparsed = "{\"smithy\": \"" + Model.MODEL_VERSION + "\", \"shapes\": { \"ns.foo#Bar\": { \"type\": \"string\"}}}";
SuppressTrait trait = SuppressTrait.builder().build();
ValidatedResult<Model> result = new ModelAssembler()
.addDocumentNode(Node.parse(unparsed, SourceLocation.NONE.getFilename()))
.addTrait(ShapeId.from("ns.foo#Bar"), trait)
.assemble();

assertThat(result.getValidationEvents(), empty());
Shape resultShape = result.unwrap().getShape(ShapeId.from("ns.foo#Bar")).get();
assertTrue(resultShape.findTrait("smithy.api#suppress").isPresent());
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
}

@Test
public void addsExplicitDocumentNode_1_0_0() {
ObjectNode node = Node.objectNode()
Expand Down

0 comments on commit a13d754

Please sign in to comment.