Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assembler addTraits for some model sources #1927

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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