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

Skip indexing endpoint modifier traits without shape definition #2096

Merged
merged 1 commit into from
Jan 12, 2024
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 @@ -7,7 +7,9 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
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 +24,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()) {
AndrewFossAWS marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.warning(String.format(
"`%s` trait found in service `%s`, but the trait definition is missing",
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