Skip to content

Commit a13d754

Browse files
committed
Fix assembler addTraits for some model sources
1 parent 9ec4f9d commit a13d754

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,6 @@ public ValidatedResult<Model> assemble() {
527527
// Register manually added shapes. Skip members because they are part of aggregate shapes.
528528
shapes.forEach(processor::putCreatedShape);
529529

530-
// Register manually added traits.
531-
for (Pair<ShapeId, Trait> entry : pendingTraits) {
532-
processor.accept(LoadOperation.ApplyTrait.from(entry.getKey(), entry.getValue()));
533-
}
534-
535530
// Register manually added Models.
536531
for (Model model : mergeModels) {
537532
// Add manually added metadata from the Model.
@@ -561,6 +556,12 @@ public ValidatedResult<Model> assemble() {
561556
}
562557
}
563558

559+
// Register manually added traits. Do this after loading any other sources of shapes
560+
// so that traits can be applied to them.
561+
for (Pair<ShapeId, Trait> entry : pendingTraits) {
562+
processor.accept(LoadOperation.ApplyTrait.from(entry.getKey(), entry.getValue()));
563+
}
564+
564565
Model processedModel = processor.buildModel();
565566

566567
// Do the 1.0 -> 2.0 transform before full-model validation.

smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,51 @@ public void addsExplicitTraits() {
130130
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
131131
}
132132

133+
@Test
134+
public void addsExplicitTraitsToBuiltModel() {
135+
StringShape shape = StringShape.builder().id("ns.foo#Bar").build();
136+
SuppressTrait trait = SuppressTrait.builder().build();
137+
ValidatedResult<Model> result = new ModelAssembler()
138+
.addModel(Model.assembler().addShape(shape).assemble().unwrap())
139+
.addTrait(shape.toShapeId(), trait)
140+
.assemble();
141+
142+
assertThat(result.getValidationEvents(), empty());
143+
Shape resultShape = result.unwrap().getShape(ShapeId.from("ns.foo#Bar")).get();
144+
assertTrue(resultShape.findTrait("smithy.api#suppress").isPresent());
145+
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
146+
}
147+
148+
@Test
149+
public void addsExplicitTraitsToUnparsedModel() {
150+
String unparsed = "{\"smithy\": \"" + Model.MODEL_VERSION + "\", \"shapes\": { \"ns.foo#Bar\": { \"type\": \"string\"}}}";
151+
SuppressTrait trait = SuppressTrait.builder().build();
152+
ValidatedResult<Model> result = new ModelAssembler()
153+
.addUnparsedModel(SourceLocation.NONE.getFilename(), unparsed)
154+
.addTrait(ShapeId.from("ns.foo#Bar"), trait)
155+
.assemble();
156+
157+
assertThat(result.getValidationEvents(), empty());
158+
Shape resultShape = result.unwrap().getShape(ShapeId.from("ns.foo#Bar")).get();
159+
assertTrue(resultShape.findTrait("smithy.api#suppress").isPresent());
160+
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
161+
}
162+
163+
@Test
164+
public void addsExplicitTraitsToParsedDocumentNode() {
165+
String unparsed = "{\"smithy\": \"" + Model.MODEL_VERSION + "\", \"shapes\": { \"ns.foo#Bar\": { \"type\": \"string\"}}}";
166+
SuppressTrait trait = SuppressTrait.builder().build();
167+
ValidatedResult<Model> result = new ModelAssembler()
168+
.addDocumentNode(Node.parse(unparsed, SourceLocation.NONE.getFilename()))
169+
.addTrait(ShapeId.from("ns.foo#Bar"), trait)
170+
.assemble();
171+
172+
assertThat(result.getValidationEvents(), empty());
173+
Shape resultShape = result.unwrap().getShape(ShapeId.from("ns.foo#Bar")).get();
174+
assertTrue(resultShape.findTrait("smithy.api#suppress").isPresent());
175+
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
176+
}
177+
133178
@Test
134179
public void addsExplicitDocumentNode_1_0_0() {
135180
ObjectNode node = Node.objectNode()

0 commit comments

Comments
 (0)