Skip to content

Commit

Permalink
Skip indexing endpoint modifier traits without shape definition
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed Jan 11, 2024
1 parent 5b4bbae commit 3609c8e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.KnowledgeIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
Expand All @@ -22,15 +25,22 @@
* Endpoint modifier traits are traits that are marked by {@link EndpointModifierTrait}
*/
public final class EndpointModifierIndex implements KnowledgeIndex {
private static final Logger LOGGER = Logger.getLogger(EndpointModifierIndex.class.getName());

private final Map<ShapeId, Map<ShapeId, Trait>> endpointModifierTraits = new HashMap<>();

public EndpointModifierIndex(Model model) {
for (ServiceShape serviceShape : model.getServiceShapes()) {
Map<ShapeId, Trait> result = new TreeMap<>();
for (Trait trait : serviceShape.getAllTraits().values()) {
Shape traitShape = model.expectShape(trait.toShapeId());
if (traitShape.hasTrait(EndpointModifierTrait.ID)) {
Optional<Shape> traitShape = model.getShape(trait.toShapeId());
if (!traitShape.isPresent()) {
LOGGER.log(Level.WARNING, String.format(
"%s trait found in service %s but it's missing definition",
trait.toShapeId(), serviceShape.toShapeId()));
continue;
}
if (traitShape.get().hasTrait(EndpointModifierTrait.ID)) {
result.put(trait.toShapeId(), trait);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ public void loadsFromModel() {
index.getEndpointModifierTraits(service4).get(RuleBasedEndpointsTrait.ID));
}


@Test
public void indexSkipsLoadingTraitsWhenDefinitionIsMissing() {
Model model = Model.assembler()
.discoverModels(getClass().getClassLoader())
.addImport(getClass().getResource("endpointModifierIndex.smithy"))
.assemble()
.unwrap();

// Remove trait shape definition from model to verify index can skip it
model = model.toBuilder().removeShape(StandardRegionalEndpointsTrait.ID).build();

EndpointModifierIndex index = new EndpointModifierIndex(model);

ShapeId service1 = getServiceShapeId(model, "ns.foo#Service1");
ShapeId service3 = getServiceShapeId(model, "ns.foo#Service3");

assertEquals(index.getEndpointModifierTraits(service1).size(), 0);
assertEquals(index.getEndpointModifierTraits(service3).size(), 1);
}

private ShapeId getServiceShapeId(Model model, String service) {
return model
.expectShape(ShapeId.from(service), ServiceShape.class).toShapeId();
Expand Down

0 comments on commit 3609c8e

Please sign in to comment.