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

Improve resource property validation and errors #1694

Merged
merged 1 commit into from
Mar 27, 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 @@ -27,7 +27,6 @@
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.IdentifierBindingIndex;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.knowledge.PropertyBindingIndex;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
Expand Down Expand Up @@ -56,22 +55,21 @@ public List<ValidationEvent> validate(Model model) {
private List<ValidationEvent> validateResource(Model model, ResourceShape resource) {
List<ValidationEvent> events = new ArrayList<>();
Set<String> propertiesInOperations = new TreeSet<>();
OperationIndex operationIndex = OperationIndex.of(model);
PropertyBindingIndex propertyBindingIndex = PropertyBindingIndex.of(model);

processLifecycleOperationProperties(model, resource, "put", resource.getPut(), operationIndex,
processLifecycleOperationProperties(model, resource, "put", resource.getPut(),
propertyBindingIndex, propertiesInOperations, events);
processLifecycleOperationProperties(model, resource, "create", resource.getCreate(), operationIndex,
processLifecycleOperationProperties(model, resource, "create", resource.getCreate(),
propertyBindingIndex, propertiesInOperations, events);
processLifecycleOperationProperties(model, resource, "read", resource.getRead(), operationIndex,
processLifecycleOperationProperties(model, resource, "read", resource.getRead(),
propertyBindingIndex, propertiesInOperations, events);
processLifecycleOperationProperties(model, resource, "update", resource.getUpdate(), operationIndex,
processLifecycleOperationProperties(model, resource, "update", resource.getUpdate(),
propertyBindingIndex, propertiesInOperations, events);
processLifecycleOperationProperties(model, resource, "delete", resource.getDelete(), operationIndex,
processLifecycleOperationProperties(model, resource, "delete", resource.getDelete(),
propertyBindingIndex, propertiesInOperations, events);
for (ShapeId operationId : resource.getOperations()) {
processLifecycleOperationProperties(model, resource, operationId.getName(), Optional.of(operationId),
operationIndex, propertyBindingIndex, propertiesInOperations, events);
propertyBindingIndex, propertiesInOperations, events);
}

Set<String> definedProperties = new HashSet<>(resource.getProperties().keySet());
Expand All @@ -89,14 +87,13 @@ private void processLifecycleOperationProperties(
ResourceShape resource,
String name,
Optional<ShapeId> operationShapeId,
OperationIndex operationIndex,
PropertyBindingIndex propertyBindingIndex,
Set<String> propertiesInOperations,
List<ValidationEvent> events
) {
operationShapeId.flatMap(model::getShape).flatMap(Shape::asOperationShape).ifPresent(operation -> {
propertiesInOperations.addAll(getAllOperationProperties(propertyBindingIndex, operation));
validateOperationInputOutput(model, propertyBindingIndex, operationIndex, resource, operation,
validateOperationInputOutput(model, propertyBindingIndex, resource, operation,
name, events);
});
}
Expand All @@ -122,22 +119,18 @@ private List<String> getAllOperationProperties(
private void validateOperationInputOutput(
Model model,
PropertyBindingIndex propertyBindingIndex,
OperationIndex operationIndex,
ResourceShape resource,
OperationShape operation,
String lifecycleOperationName,
List<ValidationEvent> events
) {
validateOperationInput(model, propertyBindingIndex, operationIndex, resource,
operation, lifecycleOperationName, events);
validateOperationOutput(model, propertyBindingIndex, operationIndex, resource,
operation, lifecycleOperationName, events);
validateOperationInput(model, propertyBindingIndex, resource, operation, lifecycleOperationName, events);
validateOperationOutput(model, propertyBindingIndex, resource, operation, lifecycleOperationName, events);
}

private void validateOperationOutput(
Model model,
PropertyBindingIndex propertyBindingIndex,
OperationIndex operationIndex,
ResourceShape resource,
OperationShape operation,
String lifecycleOperationName,
Expand All @@ -162,7 +155,6 @@ private void validateOperationOutput(
private void validateOperationInput(
Model model,
PropertyBindingIndex propertyBindingIndex,
OperationIndex operationIndex,
ResourceShape resource,
OperationShape operation,
String lifecycleOperationName,
Expand Down Expand Up @@ -192,7 +184,7 @@ private void validateConflictingProperties(
for (Map.Entry<String, Set<MemberShape>> entry : propertyToMemberMappings.entrySet()) {
if (entry.getValue().size() > 1) {
events.add(error(shape, String.format(
"This shape contains members with conflicting property names that resolve to '%s': %s",
"This shape contains members with conflicting resource property names that resolve to '%s': %s",
entry.getKey(),
entry.getValue().stream().map(MemberShape::getMemberName)
.collect(Collectors.joining(", ")))));
Expand All @@ -212,13 +204,14 @@ private void validateMember(
) {
String propertyName = propertyBindingIndex.getPropertyName(member.getId()).get();
propertyToMemberMappings.computeIfAbsent(propertyName, m -> new TreeSet<>()).add(member);

if (properties.containsKey(propertyName)) {
if (!properties.get(propertyName).equals(member.getTarget())) {
events.add(error(resource, String.format("The resource property `%s` has a conflicting"
+ " target shape `%s` on the `%s` operation which targets `%s`.",
propertyName, member.getTarget().toString(), lifecycleOperationName,
properties.get(propertyName).toString())));
ShapeId expectedTarget = properties.get(propertyName);
events.add(error(member, String.format(
"This member must target `%s`. This member is used as part of the `%s` operation of the `%s` "
+ "resource and conflicts with its `%s` resource property.",
expectedTarget, lifecycleOperationName,
resource.getId(), propertyName)));
}
} else if (!identifierMembers.contains(member.getMemberName())) {
events.add(error(member, String.format("Member `%s` does not target a property or identifier"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[ERROR] smithy.example#GetForecastOutput: This shape contains members with conflicting property names that resolve to 'chanceOfRain': chanceOfRain, chancesOfRain | ResourceOperationInputOutput
[ERROR] smithy.example#GetForecastInput: This shape contains members with conflicting property names that resolve to 'chanceOfRain': chancesOfRain, howLikelyToRain | ResourceOperationInputOutput
[ERROR] smithy.example#GetForecastOutput: This shape contains members with conflicting resource property names that resolve to 'chanceOfRain': chanceOfRain, chancesOfRain | ResourceOperationInputOutput
[ERROR] smithy.example#GetForecastInput: This shape contains members with conflicting resource property names that resolve to 'chanceOfRain': chancesOfRain, howLikelyToRain | ResourceOperationInputOutput
[ERROR] smithy.example#GetForecastInput$memberIsNotProperty: Member `memberIsNotProperty` does not target a property or identifier for resource `smithy.example#Forecast` | ResourceOperationInputOutput
[ERROR] smithy.example#Forecast: The resource property `booleanProperty` has a conflicting target shape `smithy.api#String` on the `read` operation which targets `smithy.api#Boolean`. | ResourceOperationInputOutput
[ERROR] smithy.example#GetForecastInput$booleanProperty: This member must target `smithy.api#Boolean`. This member is used as part of the `read` operation of the `smithy.example#Forecast` resource and conflicts with its `booleanProperty` resource property. | ResourceOperationInputOutput