From bba14e17ca59aa63bf97184dfa2c923c7d65fa1a Mon Sep 17 00:00:00 2001 From: Hayden Baker Date: Fri, 20 Dec 2024 08:20:14 -0800 Subject: [PATCH] update --- ...ResourceOperationInputOutputValidator.java | 161 ++++++++++++------ 1 file changed, 105 insertions(+), 56 deletions(-) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/ResourceOperationInputOutputValidator.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/ResourceOperationInputOutputValidator.java index cd194f39d6f..5ead4a372e2 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/ResourceOperationInputOutputValidator.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/validators/ResourceOperationInputOutputValidator.java @@ -1,18 +1,7 @@ /* - * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ - package software.amazon.smithy.model.validation.validators; import java.util.ArrayList; @@ -60,44 +49,79 @@ private List validateResource(Model model, ResourceShape resour Set propertiesInOperations = new TreeSet<>(); PropertyBindingIndex propertyBindingIndex = PropertyBindingIndex.of(model); - processLifecycleOperationProperties(model, resource, "put", resource.getPut(), - propertyBindingIndex, propertiesInOperations, events); - processLifecycleOperationProperties(model, resource, "create", resource.getCreate(), - propertyBindingIndex, propertiesInOperations, events); - processLifecycleOperationProperties(model, resource, "read", resource.getRead(), - propertyBindingIndex, propertiesInOperations, events); - processLifecycleOperationProperties(model, resource, "update", resource.getUpdate(), - propertyBindingIndex, propertiesInOperations, events); - processLifecycleOperationProperties(model, resource, "delete", resource.getDelete(), - propertyBindingIndex, propertiesInOperations, events); + processLifecycleOperationProperties(model, + resource, + "put", + resource.getPut(), + propertyBindingIndex, + propertiesInOperations, + events); + processLifecycleOperationProperties(model, + resource, + "create", + resource.getCreate(), + propertyBindingIndex, + propertiesInOperations, + events); + processLifecycleOperationProperties(model, + resource, + "read", + resource.getRead(), + propertyBindingIndex, + propertiesInOperations, + events); + processLifecycleOperationProperties(model, + resource, + "update", + resource.getUpdate(), + propertyBindingIndex, + propertiesInOperations, + events); + processLifecycleOperationProperties(model, + resource, + "delete", + resource.getDelete(), + propertyBindingIndex, + propertiesInOperations, + events); for (ShapeId operationId : resource.getOperations()) { - processLifecycleOperationProperties(model, resource, operationId.getName(), Optional.of(operationId), - propertyBindingIndex, propertiesInOperations, events); + processLifecycleOperationProperties(model, + resource, + operationId.getName(), + Optional.of(operationId), + propertyBindingIndex, + propertiesInOperations, + events); } Set definedProperties = new HashSet<>(resource.getProperties().keySet()); definedProperties.removeAll(propertiesInOperations); for (String propertyNotInLifecycleOp : definedProperties) { - events.add(error(resource, String.format("Resource property `%s` is not used in the input or output" - + " of create or an instance operation.", propertyNotInLifecycleOp))); + events.add(error(resource, + String.format("Resource property `%s` is not used in the input or output" + + " of create or an instance operation.", propertyNotInLifecycleOp))); } return events; } private void processLifecycleOperationProperties( - Model model, - ResourceShape resource, - String name, - Optional operationShapeId, - PropertyBindingIndex propertyBindingIndex, - Set propertiesInOperations, - List events + Model model, + ResourceShape resource, + String name, + Optional operationShapeId, + PropertyBindingIndex propertyBindingIndex, + Set propertiesInOperations, + List events ) { operationShapeId.flatMap(model::getShape).flatMap(Shape::asOperationShape).ifPresent(operation -> { propertiesInOperations.addAll(getAllOperationProperties(propertyBindingIndex, operation)); - validateOperationInputOutput(model, propertyBindingIndex, resource, operation, - name, events); + validateOperationInputOutput(model, + propertyBindingIndex, + resource, + operation, + name, + events); }); } @@ -143,13 +167,20 @@ private void validateOperationOutput( Map> propertyToMemberMappings = new TreeMap<>(); IdentifierBindingIndex identifierBindingIndex = IdentifierBindingIndex.of(model); Set identifierMembers = new HashSet<>(identifierBindingIndex - .getOperationOutputBindings(resource, operation).values()); + .getOperationOutputBindings(resource, operation) + .values()); Shape shape = propertyBindingIndex.getOutputPropertiesShape(operation); for (MemberShape member : shape.members()) { if (propertyBindingIndex.isMemberShapeProperty(member)) { - validateMember(events, lifecycleOperationName, propertyBindingIndex, resource, member, - identifierMembers, properties, propertyToMemberMappings); + validateMember(events, + lifecycleOperationName, + propertyBindingIndex, + resource, + member, + identifierMembers, + properties, + propertyToMemberMappings); } } validateConflictingProperties(events, shape, propertyToMemberMappings); @@ -167,13 +198,20 @@ private void validateOperationInput( Map> propertyToMemberMappings = new TreeMap<>(); IdentifierBindingIndex identifierBindingIndex = IdentifierBindingIndex.of(model); Set identifierMembers = new HashSet<>(identifierBindingIndex - .getOperationOutputBindings(resource, operation).values()); + .getOperationOutputBindings(resource, operation) + .values()); Shape shape = propertyBindingIndex.getInputPropertiesShape(operation); for (MemberShape member : shape.members()) { if (propertyBindingIndex.isMemberShapeProperty(member)) { - validateMember(events, lifecycleOperationName, propertyBindingIndex, resource, member, - identifierMembers, properties, propertyToMemberMappings); + validateMember(events, + lifecycleOperationName, + propertyBindingIndex, + resource, + member, + identifierMembers, + properties, + propertyToMemberMappings); } } validateConflictingProperties(events, shape, propertyToMemberMappings); @@ -186,11 +224,14 @@ private void validateConflictingProperties( ) { for (Map.Entry> entry : propertyToMemberMappings.entrySet()) { if (entry.getValue().size() > 1) { - events.add(error(shape, String.format( - "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(", "))))); + events.add(error(shape, + String.format( + "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(", "))))); } } } @@ -210,17 +251,25 @@ private void validateMember( if (properties.containsKey(propertyName)) { if (!properties.get(propertyName).equals(member.getTarget())) { 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))); + 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 for resource " - + "`%s`. If it is an identifier, apply the `%s` trait. If it is a property, apply the `%s` trait. " - + "If it is neither, apply the `%s` trait.", member.getMemberName(), resource.getId().toString(), - ResourceIdentifierTrait.ID, PropertyTrait.ID, NotPropertyTrait.ID))); + events.add(error(member, + String.format("Member `%s` does not target a property or identifier for resource " + + "`%s`. If it is an identifier, apply the `%s` trait. If it is a property, apply the `%s` trait. " + + "If it is neither, apply the `%s` trait.", + member.getMemberName(), + resource.getId().toString(), + ResourceIdentifierTrait.ID, + PropertyTrait.ID, + NotPropertyTrait.ID))); } } }