From d8ee25e2673bee0fee2ddb5129eb0a98f79ff90e Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Fri, 28 Jul 2023 15:42:59 -0500 Subject: [PATCH] Add support for overriding validation severity severityOverride cannot set an event to SUPPRESSED. Use suppressions for that. It cannot lower the severity of an event, only elevate. This prevents a backdoor for disabling built-in validation or ignoring ERROR events. Closes #856 --- docs/source-2.0/spec/model-validation.rst | 53 +- .../model/loader/LoadOperationProcessor.java | 10 +- .../smithy/model/loader/ModelAssembler.java | 72 +-- .../smithy/model/loader/ModelValidator.java | 549 +++++++++--------- .../validation/ValidationEventDecorator.java | 47 ++ .../MetadataSeverityOverride.java | 60 ++ .../suppressions/MetadataSuppression.java | 11 +- .../suppressions/NamespacePredicate.java | 39 ++ .../suppressions/SeverityOverride.java | 47 ++ .../knowledge/PropertyBindingIndexTest.java | 11 +- .../model/loader/ModelAssemblerTest.java | 25 + .../json-invalid-list-and-map-members.errors | 6 + .../loader/json-invalid-set-members.errors | 2 + .../invalid-output-identifier.smithy | 7 - .../cannot-change-severity-of-error.errors | 1 + .../cannot-change-severity-of-error.smithy | 15 + .../cannot-elevate-to-error.errors | 1 + .../cannot-elevate-to-error.smithy | 10 + .../severityOverrides/elevate-severity.errors | 3 + .../severityOverrides/elevate-severity.smithy | 61 ++ .../severityOverrides/missing-id.errors | 1 + .../severityOverrides/missing-id.smithy | 8 + .../missing-namespace.errors | 1 + .../missing-namespace.smithy | 8 + .../severityOverrides/missing-severity.errors | 1 + .../severityOverrides/missing-severity.smithy | 9 + .../override-hierarchy.errors | 2 + .../override-hierarchy.smithy | 38 ++ .../should-be-array-of-objects.errors | 1 + .../should-be-array-of-objects.smithy | 6 + .../severityOverrides/should-be-array.errors | 1 + .../severityOverrides/should-be-array.smithy | 4 + .../suppressions-take-precedence.errors | 1 + .../suppressions-take-precedence.smithy | 32 + 34 files changed, 804 insertions(+), 339 deletions(-) create mode 100644 smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSeverityOverride.java create mode 100644 smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/NamespacePredicate.java create mode 100644 smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/SeverityOverride.java create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.smithy create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.errors create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.smithy diff --git a/docs/source-2.0/spec/model-validation.rst b/docs/source-2.0/spec/model-validation.rst index bd4a532bf87..6bbc5b48f59 100644 --- a/docs/source-2.0/spec/model-validation.rst +++ b/docs/source-2.0/spec/model-validation.rst @@ -229,7 +229,7 @@ following properties: - Description * - id - ``string`` - - **Required**. The validation event ID to suppress. + - **Required**. The hierarchical validation event ID to suppress. * - namespace - ``string`` - **Required**. The validation event is only suppressed if it matches the @@ -315,6 +315,57 @@ specific. Further, a suppression ID of "ABC" does not match an event ID of - No +------------------ +Severity overrides +------------------ + +The ``severityOverrides`` metadata property is used to elevate the severity +of non-suppressed validation events. This property contains an array of +severity override objects that support the following properties: + +.. list-table:: + :header-rows: 1 + :widths: 20 20 60 + + * - Property + - Type + - Description + * - id + - ``string`` + - **Required**. The hierarchical validation event ID to elevate. + * - namespace + - ``string`` + - **Required**. The validation event is only elevated if it matches the + supplied namespace. A value of ``*`` can be provided to match any namespace. + * - severity + - ``string`` + - Defines the :ref:`severity ` to elevate matching + events to. This value can only be set to ``WARNING`` or ``DANGER``. + +The following example elevates the events of ``SomeValidator`` to ``DANGER`` +in any namespace, and ``OtherValidator`` is elevated to ``WARNING`` but only +for events emitted for shapes in the ``smithy.example`` namespace: + +.. code-block:: smithy + + $version: "2" + + metadata severityOverrides = [ + { + namespace: "*" + id: "SomeValidator" + severity: "DANGER" + } + { + namespace: "smithy.example" + id: "OtherValidator" + severity: "WARNING" + } + ] + + namespace smithy.example + + ------------------- Built-in validators ------------------- diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/loader/LoadOperationProcessor.java b/smithy-model/src/main/java/software/amazon/smithy/model/loader/LoadOperationProcessor.java index 4a01bcacc5f..a54959d73c5 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/loader/LoadOperationProcessor.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/loader/LoadOperationProcessor.java @@ -31,6 +31,7 @@ import software.amazon.smithy.model.shapes.ShapeType; import software.amazon.smithy.model.traits.TraitFactory; import software.amazon.smithy.model.validation.ValidationEvent; +import software.amazon.smithy.model.validation.ValidationEventDecorator; final class LoadOperationProcessor implements Consumer { @@ -47,22 +48,25 @@ final class LoadOperationProcessor implements Consumer { TraitFactory traitFactory, Model prelude, boolean allowUnknownTraits, - Consumer validationEventListener + Consumer validationEventListener, + ValidationEventDecorator decorator ) { // Emit events as the come in. this.events = new ArrayList() { @Override public boolean add(ValidationEvent e) { + e = decorator.decorate(e); validationEventListener.accept(e); return super.add(e); } @Override public boolean addAll(Collection validationEvents) { + ensureCapacity(size() + validationEvents.size()); for (ValidationEvent e : validationEvents) { - validationEventListener.accept(e); + add(e); } - return super.addAll(validationEvents); + return true; } }; diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java b/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java index 365ffe1f94f..03e9d6f417b 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java @@ -36,9 +36,7 @@ import java.util.Set; import java.util.function.Consumer; import java.util.function.Supplier; -import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; import java.util.stream.Stream; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.SourceException; @@ -47,7 +45,6 @@ import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.traits.Trait; import software.amazon.smithy.model.traits.TraitFactory; -import software.amazon.smithy.model.validation.Severity; import software.amazon.smithy.model.validation.ValidatedResult; import software.amazon.smithy.model.validation.ValidationEvent; import software.amazon.smithy.model.validation.ValidationEventDecorator; @@ -487,10 +484,9 @@ public ModelAssembler disableValidation() { * while loading and validating the model. * *

The consumer could be invoked simultaneously by multiple threads. It's - * up to the consumer to perform any necessary synchronization. Providing - * an event listener is useful for things like CLIs so that events can - * be streamed to stdout as soon as they are encountered, rather than - * waiting until the entire model is parsed and validated. + * up to the consumer to perform any necessary synchronization. If a validator + * or decorator throws, then there is no guarantee that all validation events + * are emitted to the listener. * * @param eventListener Listener invoked for each ValidationEvent. * @return Returns the assembler. @@ -510,13 +506,19 @@ public ValidatedResult assemble() { if (traitFactory == null) { traitFactory = LazyTraitFactoryHolder.INSTANCE; } + if (validatorFactory == null) { validatorFactory = ModelValidator.defaultValidationFactory(); } + // Create a singular, composed event decorator used to modify events. + ValidationEventDecorator decorator = ValidationEventDecorator.compose(validatorFactory.loadDecorators()); + Model prelude = disablePrelude ? null : Prelude.getPreludeModel(); + + // As issues are encountered, they are decorated and then emitted. LoadOperationProcessor processor = new LoadOperationProcessor( - traitFactory, prelude, areUnknownTraitsAllowed(), validationEventListener); + traitFactory, prelude, areUnknownTraitsAllowed(), validationEventListener, decorator); List events = processor.events(); // Register manually added metadata. @@ -560,32 +562,24 @@ public ValidatedResult assemble() { } Model processedModel = processor.buildModel(); - Model transformed; // Do the 1.0 -> 2.0 transform before full-model validation. - try { - transformed = new ModelInteropTransformer(processedModel, events, processor::getShapeVersion) - .transform(); - } catch (SourceException e) { - // The transformation shouldn't throw, but if it does, return here with the original model. - LOGGER.log(Level.SEVERE, "Error in ModelInteropTransformer: ", e); - events.add(ValidationEvent.fromSourceException(e)); - return new ValidatedResult<>(processedModel, events); - } + Model transformed = new ModelInteropTransformer(processedModel, events, processor::getShapeVersion).transform(); - // If ERROR validation events occur while loading, then performing more - // granular semantic validation will only obscure the root cause of errors. - if (LoaderUtils.containsErrorEvents(events)) { - return returnOnlyErrors(transformed, events); - } - - if (disableValidation) { - List decorators = validatorFactory.loadDecorators(); - return new ValidatedResult<>(transformed, ModelValidator.decorateEvents(decorators, events)); + if (disableValidation || LoaderUtils.containsErrorEvents(events)) { + // All events have been emitted and decorated at this point. + return new ValidatedResult<>(transformed, events); } try { - return validate(transformed, events); + List mergedEvents = ModelValidator.builder() + .validators(validators) + .validatorFactory(validatorFactory, decorator) + .eventListener(validationEventListener) + .includeEvents(events) + .build() + .validate(transformed); + return new ValidatedResult<>(transformed, mergedEvents); } catch (SourceException e) { events.add(ValidationEvent.fromSourceException(e)); return new ValidatedResult<>(transformed, events); @@ -598,28 +592,6 @@ private void addMetadataToProcessor(Map metadataMap, LoadOperation } } - private ValidatedResult returnOnlyErrors(Model model, List events) { - List decorators = validatorFactory.loadDecorators(); - return new ValidatedResult<>(model, events.stream() - .filter(event -> event.getSeverity() == Severity.ERROR) - .map(event -> ModelValidator.decorateEvent(decorators, event)) - .collect(Collectors.toList())); - } - - private ValidatedResult validate(Model model, List events) { - // Validate the model based on the explicit validators and model metadata. - // Note the ModelValidator handles emitting events to the validationEventListener. - List mergedEvents = new ModelValidator() - .validators(validators) - .validatorFactory(validatorFactory) - .eventListener(validationEventListener) - .includeEvents(events) - .createValidator() - .validate(model); - - return new ValidatedResult<>(model, mergedEvents); - } - private boolean areUnknownTraitsAllowed() { Object allowUnknown = properties.get(ModelAssembler.ALLOW_UNKNOWN_TRAITS); return allowUnknown != null && (boolean) allowUnknown; diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java b/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java index 27f540cb77e..af9284f5251 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 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. @@ -20,7 +20,6 @@ import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -28,6 +27,8 @@ import software.amazon.smithy.model.SourceException; import software.amazon.smithy.model.SourceLocation; import software.amazon.smithy.model.node.ObjectNode; +import software.amazon.smithy.model.shapes.Shape; +import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.traits.SuppressTrait; import software.amazon.smithy.model.validation.Severity; import software.amazon.smithy.model.validation.ValidatedResult; @@ -35,22 +36,37 @@ import software.amazon.smithy.model.validation.ValidationEventDecorator; import software.amazon.smithy.model.validation.Validator; import software.amazon.smithy.model.validation.ValidatorFactory; +import software.amazon.smithy.model.validation.suppressions.SeverityOverride; import software.amazon.smithy.model.validation.suppressions.Suppression; import software.amazon.smithy.model.validation.validators.ResourceCycleValidator; import software.amazon.smithy.model.validation.validators.TargetValidator; import software.amazon.smithy.utils.SetUtils; +import software.amazon.smithy.utils.SmithyBuilder; /** - * Validates a model, including validators and suppressions loaded from - * traits and metadata. + * Validates a model, including validators and suppressions loaded from traits and metadata. * - *

Validators found in metadata and suppressions found in traits are - * automatically created and applied to the model. Explicitly provided - * validators are merged together with the validators and suppressions - * loaded from metadata. + *

Validators found in metadata and suppressions found in traits are automatically created and applied to the + * model. Explicitly provided validators are merged with the validators and suppressions loaded from metadata. + * + *

The internal implementation of this class is broken into three parts: a builder, a validator, and a loaded + * validator. + * + *

+ * ModelValidator.builder().build().validate(model);
+ * //            ^ creates Builder
+ * //                      ^ creates ModelValidator
+ * //                              ^ creates a LoadedModelValidator
+ * 
+ * + *

The builder is used to build up the customized context of the validator. ModelValidator is the created + * {@link Validator} implementation isolated from the builder. LoadedModelValidator loads metadata from a Model and + * performs the actual validation in an isolated context from the ModelValidator. */ -final class ModelValidator { +final class ModelValidator implements Validator { + private static final String SUPPRESSIONS = "suppressions"; + private static final String SEVERITY_OVERRIDES = "severityOverrides"; // Lazy initialization holder class idiom to hold a default validator factory. private static final class LazyValidatorFactoryHolder { @@ -64,307 +80,302 @@ private static final class LazyValidatorFactoryHolder { ResourceCycleValidator.class ); + private final ValidatorFactory validatorFactory; + private final List events = new ArrayList<>(); private final List validators = new ArrayList<>(); - private final List suppressions = new ArrayList<>(); - private final List includeEvents = new ArrayList<>(); - private ValidatorFactory validatorFactory; - private Consumer eventListener; - - /** - * Sets the custom {@link Validator}s to use when running the ModelValidator. - * - * @param validators Validators to set. - * @return Returns the ModelValidator. - */ - public ModelValidator validators(Collection validators) { - this.validators.clear(); - validators.forEach(this::addValidator); - return this; + private final List severityOverrides = new ArrayList<>(); + private final ValidationEventDecorator validationEventDecorator; + private final Consumer eventListener; + + ModelValidator(Builder builder) { + this.validatorFactory = builder.validatorFactory; + this.eventListener = builder.eventListener; + this.validationEventDecorator = builder.validationEventDecorator; + this.events.addAll(builder.includeEvents); + this.validators.addAll(builder.validators); } - /** - * Adds a custom {@link Validator} to the ModelValidator. - * - * @param validator Validator to add. - * @return Returns the ModelValidator. - */ - public ModelValidator addValidator(Validator validator) { - validators.add(Objects.requireNonNull(validator)); - return this; + @Override + public List validate(Model model) { + return new LoadedModelValidator(model, this).validate(); } - /** - * Sets the {@link Suppression}s to use with the validator. - * - * @param suppressions Suppressions to set. - * @return Returns the ModelValidator. - */ - public ModelValidator suppressions(Collection suppressions) { - this.suppressions.clear(); - suppressions.forEach(this::addSuppression); - return this; + static Builder builder() { + return new Builder(); } - /** - * Adds a custom {@link Suppression} to the validator. - * - * @param suppression Suppression to add. - * @return Returns the ModelValidator. - */ - public ModelValidator addSuppression(Suppression suppression) { - suppressions.add(Objects.requireNonNull(suppression)); - return this; + static ValidatorFactory defaultValidationFactory() { + return LazyValidatorFactoryHolder.INSTANCE; } - /** - * Sets the factory used to find built-in {@link Validator}s and to load - * validators found in model metadata. - * - * @param validatorFactory Factory to use to load {@code Validator}s. - * - * @return Returns the ModelValidator. - */ - public ModelValidator validatorFactory(ValidatorFactory validatorFactory) { - this.validatorFactory = validatorFactory; - return this; - } + static final class Builder implements SmithyBuilder { + + private final List validators = new ArrayList<>(); + private final List includeEvents = new ArrayList<>(); + private ValidatorFactory validatorFactory = LazyValidatorFactoryHolder.INSTANCE; + private Consumer eventListener = event -> { }; + private ValidationEventDecorator validationEventDecorator; + + private Builder() {} + + /** + * Sets the custom {@link Validator}s to use when running the ModelValidator. + * + * @param validators Validators to set. + * @return Returns the ModelValidator. + */ + public Builder validators(Collection validators) { + this.validators.clear(); + validators.forEach(this::addValidator); + return this; + } - /** - * Sets a custom event listener that receives each {@link ValidationEvent} - * as it is emitted. - * - * @param eventListener Event listener that consumes each event. - * @return Returns the ModelValidator. - */ - public ModelValidator eventListener(Consumer eventListener) { - this.eventListener = eventListener; - return this; - } + /** + * Adds a custom {@link Validator}. + * + * @param validator Validator to add. + * @return Returns the builder. + */ + public Builder addValidator(Validator validator) { + validators.add(Objects.requireNonNull(validator)); + return this; + } - /** - * Includes a set of events that were already encountered in the result. - * - *

The included events may be suppressed if they match any registered - * suppressions or suppressions loaded from the model. - * - * @param events Events to include. - * @return Returns the ModelValidator. - */ - public ModelValidator includeEvents(List events) { - this.includeEvents.clear(); - this.includeEvents.addAll(events); - return this; - } + /** + * Sets the factory used to find built-in {@link Validator}s and to load validators found in model metadata. + * + * @param validatorFactory Factory to use to load {@code Validator}s. + * @param validationEventDecorator Provide a previously loaded and composed decorator. + * @return Returns the builder. + */ + public Builder validatorFactory( + ValidatorFactory validatorFactory, + ValidationEventDecorator validationEventDecorator + ) { + this.validatorFactory = Objects.requireNonNull(validatorFactory); + this.validationEventDecorator = validationEventDecorator; + return this; + } - /** - * Creates a reusable Model Validator that uses every registered validator, - * suppression, and extracts validators and suppressions from each - * provided model. - * - * @return Returns the created {@link Validator}. - */ - public Validator createValidator() { - if (validatorFactory == null) { - validatorFactory = LazyValidatorFactoryHolder.INSTANCE; + /** + * Sets a custom event listener that receives each {@link ValidationEvent} as it is emitted. + * + * @param eventListener Event listener that consumes each event. + * @return Returns the builder. + */ + public Builder eventListener(Consumer eventListener) { + this.eventListener = Objects.requireNonNull(eventListener); + return this; } - List staticValidators = resolveStaticValidators(); - List staticDecorators = validatorFactory.loadDecorators(); + /** + * Includes a set of events that were already encountered in the result. + * + *

Suppressions and severity overrides will be applied to the given {@code events}. However, the validator + * assumes that the event has already been decroated and the event listener has already seen the event. + * + * @param events Events to include. + * @return Returns the builder. + */ + public Builder includeEvents(List events) { + this.includeEvents.clear(); + this.includeEvents.addAll(events); + return this; + } - return model -> { - List coreEvents = new ArrayList<>(); + @Override + public ModelValidator build() { + validators.addAll(validatorFactory.loadBuiltinValidators()); + validators.removeIf(v -> CORE_VALIDATORS.contains(v.getClass())); + return new ModelValidator(this); + } + } - // Add suppressions found in the model via metadata. - List modelSuppressions = new ArrayList<>(suppressions); - loadModelSuppressions(modelSuppressions, model, coreEvents); + private static final class LoadedModelValidator { + + private final Model model; + private final List suppressions = new ArrayList<>(); + private final List severityOverrides; + private final List validators; + private final List events = new ArrayList<>(); + private final ValidationEventDecorator validationEventDecorator; + private final Consumer eventListener; + + private LoadedModelValidator(Model model, ModelValidator validator) { + this.model = model; + this.validationEventDecorator = validator.validationEventDecorator; + this.eventListener = validator.eventListener; + this.severityOverrides = new ArrayList<>(validator.severityOverrides); + this.validators = new ArrayList<>(validator.validators); + + loadMetadataSuppressions(); + loadMetadataSeverityOverrides(); + + // Given events have already been emitted and decorated, but have not been suppressed/elevated. + for (ValidationEvent event : validator.events) { + events.add(modifyEventSeverity(event)); + } - // Add validators defined in the model through metadata. - List modelValidators = new ArrayList<>(staticValidators); - loadModelValidators(validatorFactory, modelValidators, model, coreEvents, modelSuppressions); + loadModelValidators(validator.validatorFactory); + } - // Perform critical validation before other more granular semantic validators. - // If these validators fail, then many other validators will fail as well, - // which will only obscure the root cause. - coreEvents.addAll(suppressEvents(model, new TargetValidator().validate(model), modelSuppressions)); - coreEvents.addAll(suppressEvents(model, new ResourceCycleValidator().validate(model), modelSuppressions)); - // Decorate all the events - coreEvents = decorateEvents(staticDecorators, coreEvents); - // Emit any events that have already occurred. - coreEvents.forEach(eventListener); - - if (LoaderUtils.containsErrorEvents(coreEvents)) { - return coreEvents; - } + private void loadMetadataSeverityOverrides() { + model.getMetadataProperty(SEVERITY_OVERRIDES).ifPresent(value -> { + try { + List values = value.expectArrayNode().getElementsAs(ObjectNode.class); + for (ObjectNode rule : values) { + try { + severityOverrides.add(SeverityOverride.fromMetadata(rule)); + } catch (SourceException e) { + pushEvent(ValidationEvent.fromSourceException(e)); + } + } + } catch (SourceException e) { + pushEvent(ValidationEvent.fromSourceException(e)); + } + }); + } - List result = modelValidators.parallelStream() - .map(validator -> validator.validate(model)) - .flatMap(Collection::stream) - .filter(ModelValidator::filterPrelude) - .map(event -> suppressEvent(model, event, modelSuppressions)) - .map(event -> decorateEvent(staticDecorators, event)) - // Emit events as they occur during validation. - .peek(eventListener) - .collect(Collectors.toList()); + private void loadMetadataSuppressions() { + model.getMetadataProperty(SUPPRESSIONS).ifPresent(value -> { + try { + List values = value.expectArrayNode().getElementsAs(ObjectNode.class); + for (ObjectNode rule : values) { + try { + suppressions.add(Suppression.fromMetadata(rule)); + } catch (SourceException e) { + pushEvent(ValidationEvent.fromSourceException(e)); + } + } + } catch (SourceException e) { + pushEvent(ValidationEvent.fromSourceException(e)); + } + }); + } - for (ValidationEvent event : includeEvents) { - if (ModelValidator.filterPrelude(event)) { - result.add(decorateEvent(staticDecorators, suppressEvent(model, event, modelSuppressions))); + private void loadModelValidators(ValidatorFactory validatorFactory) { + // Load validators defined in metadata. + ValidatedResult> loaded = ValidationLoader + .loadValidators(model.getMetadata()); + pushEvents(loaded.getValidationEvents()); + List definitions = loaded.getResult().orElseGet(Collections::emptyList); + ValidatorFromDefinitionFactory factory = new ValidatorFromDefinitionFactory(validatorFactory); + + // Attempt to create the Validator instances and collect errors along the way. + for (ValidatorDefinition val : definitions) { + ValidatedResult result = factory.loadValidator(val); + result.getResult().ifPresent(validators::add); + pushEvents(result.getValidationEvents()); + if (result.getValidationEvents().isEmpty() && !result.getResult().isPresent()) { + ValidationEvent event = unknownValidatorError(val.name, val.sourceLocation); + pushEvent(event); } } + } - // Add in events encountered while building up validators and suppressions. - result.addAll(coreEvents); - - return result; - }; - } + // Unknown validators don't fail the build! + private static ValidationEvent unknownValidatorError(String name, SourceLocation location) { + return ValidationEvent.builder() + // Per the spec, the eventID is "UnknownValidator_". + .id("UnknownValidator_" + name) + .severity(Severity.WARNING) + .sourceLocation(location) + .message("Unable to locate a validator named `" + name + "`") + .build(); + } - static List decorateEvents( - List decorators, - List events - ) { - if (!decorators.isEmpty()) { - for (int idx = 0; idx < events.size(); idx++) { - events.set(idx, decorateEvent(decorators, events.get(idx))); + private void pushEvents(List source) { + for (ValidationEvent event : source) { + pushEvent(event); } } - return events; - } - static ValidationEvent decorateEvent(List decorators, ValidationEvent event) { - ValidationEvent decoratedEvent = event; - for (ValidationEventDecorator decorator : decorators) { - if (decorator.canDecorate(event)) { - decoratedEvent = decorator.decorate(decoratedEvent); - } + private void pushEvent(ValidationEvent event) { + event = modifyEventSeverity(event); + event = validationEventDecorator.decorate(event); + events.add(event); + eventListener.accept(event); } - return decoratedEvent; - } - static ValidatorFactory defaultValidationFactory() { - return LazyValidatorFactoryHolder.INSTANCE; - } + private List validate() { + // Perform critical validation before other more granular semantic validators. + pushEvents(new TargetValidator().validate(model)); + pushEvents(new ResourceCycleValidator().validate(model)); - private List resolveStaticValidators() { - List resolvedValidators = new ArrayList<>(validatorFactory.loadBuiltinValidators()); - resolvedValidators.addAll(validators); - // These core validators are applied first, so don't run them again. - resolvedValidators.removeIf(v -> CORE_VALIDATORS.contains(v.getClass())); - return resolvedValidators; - } + // Fail early if errors were detected since further validation will just obscure the root cause. + if (LoaderUtils.containsErrorEvents(events)) { + return events; + } - private static boolean filterPrelude(ValidationEvent event) { - // Don't emit any non-error events for prelude shapes and traits. - // This prevents custom validators from unnecessarily needing to - // worry about prelude shapes and trait definitions, but still - // allows for validation events when the prelude is broken. - return event.getSeverity() == Severity.ERROR || !event.getShapeId() - .filter(Prelude::isPreludeShape) - .isPresent(); - } + List result = validators.parallelStream() + .flatMap(validator -> validator.validate(model).stream()) + .filter(this::filterPrelude) + .map(this::modifyEventSeverity) + .map(validationEventDecorator::decorate) + // Emit events as they occur during validation. + .peek(eventListener) + .collect(Collectors.toList()); - private static void loadModelValidators( - ValidatorFactory validatorFactory, - List validators, - Model model, - List events, - List suppressions - ) { - // Load validators defined in metadata. - ValidatedResult> loaded = ValidationLoader - .loadValidators(model.getMetadata()); - events.addAll(loaded.getValidationEvents()); - List definitions = loaded.getResult().orElseGet(Collections::emptyList); - ValidatorFromDefinitionFactory factory = new ValidatorFromDefinitionFactory(validatorFactory); - - // Attempt to create the Validator instances and collect errors along the way. - for (ValidatorDefinition val : definitions) { - ValidatedResult result = factory.loadValidator(val); - result.getResult().ifPresent(validators::add); - events.addAll(result.getValidationEvents()); - if (result.getValidationEvents().isEmpty() && !result.getResult().isPresent()) { - ValidationEvent event = unknownValidatorError(val.name, val.sourceLocation); - events.add(suppressEvent(model, event, suppressions)); - } + // Add in events encountered while building up validators and suppressions. + result.addAll(events); + + return result; } - } - // Unknown validators don't fail the build! - private static ValidationEvent unknownValidatorError(String name, SourceLocation location) { - return ValidationEvent.builder() - // Per the spec, the eventID is "UnknownValidator_". - .id("UnknownValidator_" + name) - .severity(Severity.WARNING) - .sourceLocation(location) - .message("Unable to locate a validator named `" + name + "`") - .build(); - } + private boolean filterPrelude(ValidationEvent event) { + // Don't emit any non-error events for prelude shapes and traits. + // This prevents custom validators from unnecessarily needing to worry about prelude shapes and trait + // definitions, but still allows for validation events when the prelude is broken. + return event.getSeverity() == Severity.ERROR || !event.getShapeId() + .filter(Prelude::isPreludeShape) + .isPresent(); + } - private static void loadModelSuppressions( - List suppressions, - Model model, - List events - ) { - model.getMetadataProperty(SUPPRESSIONS).ifPresent(value -> { - List values = value.expectArrayNode().getElementsAs(ObjectNode.class); - for (ObjectNode rule : values) { - try { - suppressions.add(Suppression.fromMetadata(rule)); - } catch (SourceException e) { - events.add(ValidationEvent.fromSourceException(e)); + private ValidationEvent modifyEventSeverity(ValidationEvent event) { + // Use a suppress trait if present. + if (event.getShapeId().isPresent()) { + ShapeId target = event.getShapeId().get(); + Shape shape = model.getShape(target).orElse(null); + if (shape != null) { + if (shape.hasTrait(SuppressTrait.class)) { + Suppression suppression = Suppression.fromSuppressTrait(shape); + if (suppression.test(event)) { + return changeSeverity(event, Severity.SUPPRESSED, suppression.getReason().orElse(null)); + } + } } } - }); - } - - private static List suppressEvents( - Model model, - List events, - List suppressions) { - return events.stream().map(event -> suppressEvent(model, event, suppressions)).collect(Collectors.toList()); - } - private static ValidationEvent suppressEvent(Model model, ValidationEvent event, List suppressions) { - // ERROR and SUPPRESSED events cannot be suppressed. - if (!event.getSeverity().canSuppress()) { - return event; - } + // Check metadata and manual suppressions. + for (Suppression suppression : suppressions) { + if (suppression.test(event)) { + return changeSeverity(event, Severity.SUPPRESSED, suppression.getReason().orElse(null)); + } + } - Suppression matchedSuppression = findMatchingSuppression(model, event, suppressions); + Severity appliedSeverity = event.getSeverity(); + for (SeverityOverride override : severityOverrides) { + Severity overrideResult = override.apply(event); + if (overrideResult.ordinal() > appliedSeverity.ordinal()) { + appliedSeverity = overrideResult; + } + } - if (matchedSuppression == null) { - return event; + return changeSeverity(event, appliedSeverity, null); } - // The event was suppressed so change the severity and reason. - ValidationEvent.Builder builder = event.toBuilder(); - builder.severity(Severity.SUPPRESSED); - matchedSuppression.getReason().ifPresent(builder::suppressionReason); - - return builder.build(); - } - - private static Suppression findMatchingSuppression( - Model model, - ValidationEvent event, - List suppressions - ) { - return event.getShapeId() - .flatMap(model::getShape) - // First check for trait based suppressions. - .flatMap(shape -> shape.hasTrait(SuppressTrait.class) - ? Optional.of(Suppression.fromSuppressTrait(shape)) - : Optional.empty()) - // Try to suppress it. - .flatMap(suppression -> suppression.test(event) ? Optional.of(suppression) : Optional.empty()) - // If it wasn't suppressed, then try the rules loaded from metadata. - .orElseGet(() -> { - for (Suppression suppression : suppressions) { - if (suppression.test(event)) { - return suppression; - } - } - return null; - }); + private static ValidationEvent changeSeverity(ValidationEvent event, Severity severity, String reason) { + if (event.getSeverity() == severity) { + return event; + } else { + // The event was suppressed so change the severity and reason. + ValidationEvent.Builder builder = event.toBuilder(); + builder.severity(severity); + if (reason != null) { + builder.suppressionReason(reason); + } + return builder.build(); + } + } } } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecorator.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecorator.java index a18560cbd7f..8cfba31d16a 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecorator.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecorator.java @@ -15,6 +15,8 @@ package software.amazon.smithy.model.validation; +import java.util.List; + /** * Validation event decorators take validation events and transform them by adding more contextual information, * usually adding a hint to let the user know what can it be done to solve the issue. This might add context specific @@ -37,4 +39,49 @@ public interface ValidationEventDecorator { * @return The decorated event or the original one if no decoration took place. */ ValidationEvent decorate(ValidationEvent ev); + + /** + * Creates a decorator composed of one or more decorators. + * + * @param decorators Decorators to compose. + * @return Returns the composed decorator. + */ + static ValidationEventDecorator compose(List decorators) { + if (decorators.isEmpty()) { + return new ValidationEventDecorator() { + @Override + public boolean canDecorate(ValidationEvent ev) { + return false; + } + + @Override + public ValidationEvent decorate(ValidationEvent ev) { + return ev; + } + }; + } else { + return new ValidationEventDecorator() { + @Override + public boolean canDecorate(ValidationEvent ev) { + for (ValidationEventDecorator decorator : decorators) { + if (decorator.canDecorate(ev)) { + return true; + } + } + return false; + } + + @Override + public ValidationEvent decorate(ValidationEvent ev) { + ValidationEvent decoratedEvent = ev; + for (ValidationEventDecorator decorator : decorators) { + if (decorator.canDecorate(ev)) { + decoratedEvent = decorator.decorate(decoratedEvent); + } + } + return decoratedEvent; + } + }; + } + } } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSeverityOverride.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSeverityOverride.java new file mode 100644 index 00000000000..bb781251bf1 --- /dev/null +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSeverityOverride.java @@ -0,0 +1,60 @@ +/* + * Copyright 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.suppressions; + +import java.util.Collection; +import java.util.function.Predicate; +import software.amazon.smithy.model.node.Node; +import software.amazon.smithy.model.node.ObjectNode; +import software.amazon.smithy.model.validation.Severity; +import software.amazon.smithy.model.validation.ValidationEvent; +import software.amazon.smithy.utils.ListUtils; + +final class MetadataSeverityOverride implements SeverityOverride { + + private static final String ID = "id"; + private static final String NAMESPACE = "namespace"; + private static final String SEVERITY = "severity"; + private static final Collection KEYS = ListUtils.of(ID, NAMESPACE, SEVERITY); + private static final Predicate STAR_MATCHER = event -> true; + private static final String[] SEVERITIES = {"WARNING", "DANGER"}; + + private final String id; + private final String namespace; + private final Severity severity; + private final Predicate namespaceMatcher; + + MetadataSeverityOverride(String id, String namespace, Severity severity) { + this.id = id; + this.namespace = namespace; + this.severity = severity; + this.namespaceMatcher = namespace.equals("*") ? STAR_MATCHER : new NamespacePredicate(this.namespace); + } + + static MetadataSeverityOverride fromNode(Node node) { + ObjectNode rule = node.expectObjectNode(); + rule.warnIfAdditionalProperties(KEYS); + String id = rule.expectStringMember(ID).getValue(); + String namespace = rule.expectStringMember(NAMESPACE).getValue(); + String severity = rule.expectStringMember(SEVERITY).expectOneOf(SEVERITIES); + return new MetadataSeverityOverride(id, namespace, Severity.valueOf(severity)); + } + + @Override + public Severity apply(ValidationEvent event) { + return event.containsId(id) && namespaceMatcher.test(event) ? severity : event.getSeverity(); + } +} diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSuppression.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSuppression.java index db3af4835b4..a48da21cca2 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSuppression.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/MetadataSuppression.java @@ -17,6 +17,7 @@ import java.util.Collection; import java.util.Optional; +import java.util.function.Predicate; import software.amazon.smithy.model.node.Node; import software.amazon.smithy.model.node.ObjectNode; import software.amazon.smithy.model.validation.ValidationEvent; @@ -33,15 +34,18 @@ final class MetadataSuppression implements Suppression { private static final String NAMESPACE = "namespace"; private static final String REASON = "reason"; private static final Collection SUPPRESSION_KEYS = ListUtils.of(ID, NAMESPACE, REASON); + private static final Predicate STAR_MATCHER = event -> true; private final String id; private final String namespace; private final String reason; + private final Predicate namespaceMatcher; MetadataSuppression(String id, String namespace, String reason) { this.id = id; this.namespace = namespace; this.reason = reason; + this.namespaceMatcher = namespace.equals("*") ? STAR_MATCHER : new NamespacePredicate(namespace); } static MetadataSuppression fromNode(Node node) { @@ -55,16 +59,11 @@ static MetadataSuppression fromNode(Node node) { @Override public boolean test(ValidationEvent event) { - return event.containsId(id) && matchesNamespace(event); + return event.containsId(id) && namespaceMatcher.test(event); } @Override public Optional getReason() { return Optional.ofNullable(reason); } - - private boolean matchesNamespace(ValidationEvent event) { - return namespace.equals("*") - || event.getShapeId().filter(id -> id.getNamespace().equals(namespace)).isPresent(); - } } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/NamespacePredicate.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/NamespacePredicate.java new file mode 100644 index 00000000000..02eab6e7775 --- /dev/null +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/NamespacePredicate.java @@ -0,0 +1,39 @@ +/* + * Copyright 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.suppressions; + +import java.util.function.Predicate; +import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.model.validation.ValidationEvent; + +final class NamespacePredicate implements Predicate { + + private final String namespace; + + NamespacePredicate(String namespace) { + this.namespace = namespace; + } + + @Override + public boolean test(ValidationEvent event) { + ShapeId id = event.getShapeId().orElse(null); + if (id == null) { + return false; + } + + return id.getNamespace().equals(namespace); + } +} diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/SeverityOverride.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/SeverityOverride.java new file mode 100644 index 00000000000..caca968c5b9 --- /dev/null +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/suppressions/SeverityOverride.java @@ -0,0 +1,47 @@ +/* + * Copyright 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.suppressions; + +import software.amazon.smithy.model.node.ExpectationNotMetException; +import software.amazon.smithy.model.node.Node; +import software.amazon.smithy.model.validation.Severity; +import software.amazon.smithy.model.validation.ValidationEvent; + +/** + * Changes the severity of a validation event. + */ +@FunctionalInterface +public interface SeverityOverride { + + /** + * Returns the severity to apply to the validation event. + * + * @param event Event to modify. + * @return Returns the severity to use with the event. + */ + Severity apply(ValidationEvent event); + + /** + * Creates a severity override from a {@link Node} found in the "severityOverrides" metadata of a Smithy model. + * + * @param node Node to parse. + * @return Returns the loaded override. + * @throws ExpectationNotMetException if the override node is malformed. + */ + static SeverityOverride fromMetadata(Node node) { + return MetadataSeverityOverride.fromNode(node); + } +} diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/knowledge/PropertyBindingIndexTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/knowledge/PropertyBindingIndexTest.java index d11da1713f4..b51c20ac8a6 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/knowledge/PropertyBindingIndexTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/knowledge/PropertyBindingIndexTest.java @@ -15,18 +15,20 @@ package software.amazon.smithy.model.knowledge; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.MemberShape; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.model.validation.Severity; import software.amazon.smithy.model.validation.ValidatedResult; -import software.amazon.smithy.model.validation.ValidatedResultException; public class PropertyBindingIndexTest { @@ -68,6 +70,9 @@ public void testIndex() { assertTrue(index.doesMemberShapeRequireProperty(model.expectShape( ShapeId.from("com.example#ChangeResourceOutput$id"), MemberShape.class))); - Assertions.assertThrows(ValidatedResultException.class, () -> vrmodel.unwrap()); + + assertThat(vrmodel.getValidationEvents(Severity.SUPPRESSED), hasSize(1)); + assertThat(vrmodel.getValidationEvents(Severity.SUPPRESSED).get(0).getId(), + equalTo("ResourceOperationInputOutput")); } } diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java index 1b5ff58d82f..9578cc80454 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java @@ -1247,4 +1247,29 @@ public void failsOnInvalidJarJsonFile() throws URISyntaxException, IOException { assertThat(e.getMessage(), containsString("Invalid file referenced by Smithy JAR manifest")); } + + @Test + public void doesNotThrowOnInvalidSuppression() { + ObjectNode node = Node.objectNode() + .withMember("smithy", "1.0") + .withMember("metadata", Node.objectNode().withMember("suppressions", "hi!")); + + ValidatedResult result = new ModelAssembler().addDocumentNode(node).assemble(); + + assertThat(result.getValidationEvents(Severity.ERROR), not(empty())); + } + + @Test + public void modelLoadingErrorsAreEmittedToListener() { + ObjectNode node = Node.objectNode().withMember("smithy", Node.fromStrings("Hi", "there")); + List events = new ArrayList<>(); + + ValidatedResult result = new ModelAssembler() + .addDocumentNode(node) + .validationEventListener(events::add) + .assemble(); + + assertThat(result.getValidationEvents(Severity.ERROR), hasSize(1)); + assertThat(events, equalTo(result.getValidationEvents())); + } } diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-list-and-map-members.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-list-and-map-members.errors index 9130e4c9e8e..3742a6e7a84 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-list-and-map-members.errors +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-list-and-map-members.errors @@ -6,3 +6,9 @@ [ERROR] com.test#WrongMemberMap: Missing required member of shape `com.test#WrongMemberMap`: key | Model [ERROR] com.test#OtherWrongMemberMap: Missing required member of shape `com.test#OtherWrongMemberMap`: value | Model [ERROR] com.test#BothWrongMembersMap: Missing required members of shape `com.test#BothWrongMembersMap`: key, value | Model +[WARNING] com.test#WrongMemberList: Expected an object with possible properties of `member`, `mixins`, `traits`, `type`, but found additional properties: `foo` | Model +[WARNING] com.test#WrongMemberMap: Expected an object with possible properties of `key`, `mixins`, `traits`, `type`, `value`, but found additional properties: `foo` | Model +[WARNING] com.test#OtherWrongMemberMap: Expected an object with possible properties of `key`, `mixins`, `traits`, `type`, `value`, but found additional properties: `foo` | Model +[WARNING] com.test#BothWrongMembersMap: Expected an object with possible properties of `key`, `mixins`, `traits`, `type`, `value`, but found additional properties: `bar`, `foo` | Model +[WARNING] com.test#ExtraMemberList: Expected an object with possible properties of `member`, `mixins`, `traits`, `type`, but found additional properties: `foo` | Model +[WARNING] com.test#ExtraMemberMap: Expected an object with possible properties of `key`, `mixins`, `traits`, `type`, `value`, but found additional properties: `foo` | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-set-members.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-set-members.errors index 4806e262593..190b9e04e46 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-set-members.errors +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/json-invalid-set-members.errors @@ -1,2 +1,4 @@ [ERROR] com.test#NoMemberSet: Missing required member of shape `com.test#NoMemberSet`: member | Model [ERROR] com.test#WrongMemberSet: Missing required member of shape `com.test#WrongMemberSet`: member | Model +[WARNING] com.test#WrongMemberSet: Expected an object with possible properties of `member`, `mixins`, `traits`, `type`, but found additional properties: `foo` | Model +[WARNING] com.test#ExtraMemberSet: Expected an object with possible properties of `member`, `mixins`, `traits`, `type`, but found additional properties: `foo` | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/resource-properties/invalid-output-identifier.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/resource-properties/invalid-output-identifier.smithy index 649e961bf9e..aaf228cb343 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/resource-properties/invalid-output-identifier.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/resource-properties/invalid-output-identifier.smithy @@ -1,12 +1,5 @@ $version: "2.0" -metadata suppressions = [ - { - id: "ResourceOperationInputOutput", - namespace: "com.example" - } -] - namespace com.example resource Resource1 { diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.errors new file mode 100644 index 00000000000..418bdc06da9 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.errors @@ -0,0 +1 @@ +[ERROR] smithy.example#Invalid: A length trait is applied with a negative `min` value. | LengthTrait diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.smithy new file mode 100644 index 00000000000..d6521386266 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-change-severity-of-error.smithy @@ -0,0 +1,15 @@ +$version: "2.0" + +metadata severityOverrides = [ + { + namespace: "*" + id: "LengthTrait" + severity: "WARNING" + } +] + +namespace smithy.example + +// This emits an error which the above severityOverride attempts, but fails, to lower the severity of. +@length(min: -1) +string Invalid diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.errors new file mode 100644 index 00000000000..e27c6bf9628 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.errors @@ -0,0 +1 @@ +[ERROR] -: Expected one of `DANGER`, `WARNING`; got `ERROR`. | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.smithy new file mode 100644 index 00000000000..89694ebee8a --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/cannot-elevate-to-error.smithy @@ -0,0 +1,10 @@ +$version: "2.0" + +// Cannot override a severity to ERROR. +metadata severityOverrides = [ + { + namespace: "*" + id: "Foo" + severity: "ERROR" + } +] diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.errors new file mode 100644 index 00000000000..c9e16fdfeab --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.errors @@ -0,0 +1,3 @@ +[DANGER] smithy.example#IntegerTarget: Note the integer please | NoteTheInteger +[DANGER] smithy.example#ListTarget: Note the list please | NoteTheList +[WARNING] smithy.example#MyString: Note the string please | NoteTheString diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.smithy new file mode 100644 index 00000000000..0be03d03631 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/elevate-severity.smithy @@ -0,0 +1,61 @@ +$version: "2.0" + +metadata validators = [ + { + name: "EmitEachSelector" + id: "NoteTheList" + message: "Note the list please" + severity: "NOTE" + configuration: { + selector: "[id|namespace = smithy.example] list" + } + } + { + name: "EmitEachSelector" + id: "NoteTheInteger" + message: "Note the integer please" + severity: "WARNING" + configuration: { + selector: "[id|namespace = smithy.example] integer" + } + } + // This event is left alone. + { + name: "EmitEachSelector" + id: "NoteTheString" + message: "Note the string please" + severity: "WARNING" + configuration: { + selector: "[id|namespace = smithy.example] string" + } + } +] + +metadata severityOverrides = [ + { + namespace: "*" + id: "NoteTheList" + severity: "DANGER" + } + { + namespace: "smithy.example" + id: "NoteTheInteger" + severity: "DANGER" + } + // This one is ignored because it has a severity < DANGER. + { + namespace: "smithy.example" + id: "NoteTheList" + severity: "WARNING" + } +] + +namespace smithy.example + +string MyString + +integer IntegerTarget + +list ListTarget { + member: String +} diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.errors new file mode 100644 index 00000000000..7aff59c0183 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.errors @@ -0,0 +1 @@ +[ERROR] -: Missing expected member `id`. | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.smithy new file mode 100644 index 00000000000..4f96adba4a5 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-id.smithy @@ -0,0 +1,8 @@ +$version: "2.0" + +// Each element requires an ID. +metadata severityOverrides = [ + { + namespace: "*" + } +] diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.errors new file mode 100644 index 00000000000..03ba62a88a8 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.errors @@ -0,0 +1 @@ +[ERROR] -: Missing expected member `namespace`. | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.smithy new file mode 100644 index 00000000000..5aaa063af23 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-namespace.smithy @@ -0,0 +1,8 @@ +$version: "2.0" + +// Each element requires a namespace. +metadata severityOverrides = [ + { + id: "Foo" + } +] diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.errors new file mode 100644 index 00000000000..5669c6b7972 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.errors @@ -0,0 +1 @@ +[ERROR] -: Missing expected member `severity`. | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.smithy new file mode 100644 index 00000000000..e0d73293cf9 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/missing-severity.smithy @@ -0,0 +1,9 @@ +$version: "2.0" + +// Each element requires a severity. +metadata severityOverrides = [ + { + namespace: "*" + id: "Foo" + } +] diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.errors new file mode 100644 index 00000000000..4badff1a9e3 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.errors @@ -0,0 +1,2 @@ +[WARNING] smithy.example#IntegerTarget: Note the shape | NoteTheShape.Integer +[WARNING] smithy.example#ListTarget: Note the list please | NoteTheShape.List diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.smithy new file mode 100644 index 00000000000..b836a142f09 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/override-hierarchy.smithy @@ -0,0 +1,38 @@ +$version: "2.0" + +metadata validators = [ + { + name: "EmitEachSelector" + id: "NoteTheShape.Integer" + message: "Note the shape" + severity: "NOTE" + configuration: { + selector: "[id|namespace = smithy.example] integer" + } + } + { + name: "EmitEachSelector" + id: "NoteTheShape.List" + message: "Note the list please" + severity: "NOTE" + configuration: { + selector: "[id|namespace = smithy.example] list" + } + } +] + +metadata severityOverrides = [ + { + namespace: "*" + id: "NoteTheShape" + severity: "WARNING" + } +] + +namespace smithy.example + +integer IntegerTarget + +list ListTarget { + member: String +} diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.errors new file mode 100644 index 00000000000..dbe12a4ac1e --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.errors @@ -0,0 +1 @@ +[ERROR] -: Expected array element 0 to be a object but found boolean. | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.smithy new file mode 100644 index 00000000000..0ca3e83d856 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array-of-objects.smithy @@ -0,0 +1,6 @@ +$version: "2.0" + +// Should be array of objects +metadata severityOverrides = [ + true +] diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.errors new file mode 100644 index 00000000000..bb163d0b05e --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.errors @@ -0,0 +1 @@ +[ERROR] -: Expected array, but found object. | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.smithy new file mode 100644 index 00000000000..48c1a73a5ef --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/should-be-array.smithy @@ -0,0 +1,4 @@ +$version: "2.0" + +// Should be array +metadata severityOverrides = {} diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.errors new file mode 100644 index 00000000000..171c6862279 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.errors @@ -0,0 +1 @@ +[SUPPRESSED] smithy.example#IntegerTarget: Note the integer please | NoteTheInteger diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.smithy new file mode 100644 index 00000000000..e1c994ac97c --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/severityOverrides/suppressions-take-precedence.smithy @@ -0,0 +1,32 @@ +$version: "2.0" + +metadata validators = [ + { + name: "EmitEachSelector" + id: "NoteTheInteger" + message: "Note the integer please" + severity: "WARNING" + configuration: { + selector: "[id|namespace = smithy.example] integer" + } + } +] + +metadata suppressions = [ + { + namespace: "*" + id: "NoteTheInteger" + } +] + +metadata severityOverrides = [ + { + namespace: "smithy.example" + id: "NoteTheInteger" + severity: "DANGER" + } +] + +namespace smithy.example + +integer IntegerTarget