software.amazon.smithy.model.validation.validators.HttpPayloadValidator Maven / Gradle / Ivy
/*
* Copyright 2019 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.
*/
package software.amazon.smithy.model.validation.validators;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.HttpBinding;
import software.amazon.smithy.model.knowledge.HttpBindingIndex;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.HttpPayloadTrait;
import software.amazon.smithy.model.traits.HttpTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.model.validation.ValidationUtils;
/**
* Validates that if a payload trait is present then all members of the
* input of an operation are bound to part of the message.
*/
public final class HttpPayloadValidator extends AbstractValidator {
@Override
public List validate(Model model) {
// Skip the expensive checks if the httpPayload trait isn't event used.
if (!model.isTraitApplied(HttpPayloadTrait.class)) {
return Collections.emptyList();
}
OperationIndex opIndex = OperationIndex.of(model);
HttpBindingIndex bindings = HttpBindingIndex.of(model);
List events = new ArrayList<>();
for (OperationShape operation : model.getOperationShapesWithTrait(HttpTrait.class)) {
events.addAll(validateOperation(bindings, opIndex, operation));
}
for (StructureShape structure : model.getStructureShapesWithTrait(ErrorTrait.class)) {
events.addAll(validateError(structure, bindings));
}
return events;
}
private List validateOperation(
HttpBindingIndex bindings,
OperationIndex opIndex,
OperationShape shape
) {
List events = new ArrayList<>();
validatePayload(shape.getId(), opIndex.expectInputShape(shape), bindings, true).ifPresent(events::add);
validatePayload(shape.getId(), opIndex.expectOutputShape(shape), bindings, false).ifPresent(events::add);
return events;
}
private List validateError(StructureShape shape, HttpBindingIndex bindings) {
return validatePayload(shape.getId(), shape, bindings, false)
.map(Collections::singletonList)
.orElseGet(Collections::emptyList);
}
private Optional validatePayload(
ShapeId subject,
StructureShape inputOrError,
HttpBindingIndex bindings,
boolean request
) {
Map resolved = request
? bindings.getRequestBindings(subject)
: bindings.getResponseBindings(subject);
Set unbound = resolved.entrySet().stream()
.filter(binding -> binding.getValue().getLocation() == HttpBinding.Location.UNBOUND)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
if (!unbound.isEmpty()) {
return Optional.of(error(inputOrError, String.format(
"A member of this structure is marked with the `httpPayload` trait, but the following "
+ "structure members are not explicitly bound to the HTTP message: %s",
ValidationUtils.tickedList(unbound))));
}
return Optional.empty();
}
}