Skip to content

Commit

Permalink
Mitigate possible NSE exception in PrivateAccessValidator.java (#2015)
Browse files Browse the repository at this point in the history
Using `Relationship#expectNeighborShape` in place of the optional .get
defined for `Relationship`.
  • Loading branch information
jyoo980 authored Oct 26, 2023
1 parent 5b4c3b1 commit 6d1a7cd
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import software.amazon.smithy.model.neighbor.Relationship;
import software.amazon.smithy.model.neighbor.RelationshipType;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.PrivateTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;
Expand All @@ -50,17 +51,18 @@ private void validateNeighbors(Shape shape, List<Relationship> relationships, Li
String namespace = shape.getId().getNamespace();
for (Relationship rel : relationships) {
if (!rel.getShape().getId().getNamespace().equals(namespace)) {
ValidationEvent privateAccessValidationEvent = getPrivateAccessValidationEvent(rel);
events.add(privateAccessValidationEvent);
events.add(getPrivateAccessValidationEvent(rel));
}
}
}

private ValidationEvent getPrivateAccessValidationEvent(Relationship relationship) {
ShapeId neighborId = relationship.expectNeighborShape().getId();
String message = String.format(
"This shape has an invalid %s relationship that targets a private shape, `%s`, in another namespace.",
relationship.getRelationshipType().toString().toLowerCase(Locale.US),
relationship.getNeighborShape().get().getId());
neighborId
);

// For now, emit a warning for trait relationships instead of an error. This is because private access on trait
// relationships was not being validated in the past, so emitting a warning maintains backward compatibility.
Expand Down

0 comments on commit 6d1a7cd

Please sign in to comment.