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

Emit warnings for deprecated traits #279

Merged
merged 5 commits into from
Feb 20, 2020
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
@@ -0,0 +1,63 @@
/*
* 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 static java.lang.String.format;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.DeprecatedTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;

/**
* Emits a validation event if a model contains shapes that are bound to deprecated traits.
*/
public final class DeprecatedTraitsValidator extends AbstractValidator {
@Override
public List<ValidationEvent> validate(Model model) {
Set<ShapeId> deprecatedTraits = model.getTraitShapes().stream()
.filter(trait -> trait.hasTrait(DeprecatedTrait.class))
.map(Shape::getId)
.collect(Collectors.toSet());

return model.shapes()
.flatMap(shape -> validateShape(model, shape, deprecatedTraits).stream())
.collect(Collectors.toList());
}

private List<ValidationEvent> validateShape(Model model, Shape shape, Set<ShapeId> deprecatedTraits) {
List<ValidationEvent> events = new ArrayList<>();
shape.getAllTraits().forEach((shapeId, trait) -> {
if (!deprecatedTraits.contains(trait.toShapeId())) {
return;
}
DeprecatedTrait deprecatedTrait = model.expectShape(trait.toShapeId()).expectTrait(DeprecatedTrait.class);
String traitMessage = trait.toShapeId().toString();
if (deprecatedTrait.getMessage().isPresent()) {
traitMessage = traitMessage + ", " + deprecatedTrait.getMessage().get();
}
events.add(warning(shape, trait, format("This shape applies a trait that is deprecated: %s",
traitMessage)));
});
return events;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
software.amazon.smithy.model.validation.validators.AuthValidator
software.amazon.smithy.model.validation.validators.AuthProtocolsValidator
software.amazon.smithy.model.validation.validators.DeprecatedTraitsValidator
software.amazon.smithy.model.validation.validators.EnumTraitValidator
software.amazon.smithy.model.validation.validators.EventPayloadTraitValidator
software.amazon.smithy.model.validation.validators.EventStreamValidator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WARNING] ns.foo#MyString: This shape applies a trait that is deprecated: ns.foo#fooTrait | DeprecatedTraits
[WARNING] ns.foo#MyString: This shape applies a trait that is deprecated: ns.foo#barTrait, This trait is no longer used. | DeprecatedTraits
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"smithy": "0.5.0",
"shapes": {
"ns.foo#MyString": {
"type": "string",
"traits": {
"ns.foo#fooTrait": { },
"ns.foo#barTrait": { },
"ns.foo#bazTrait": { }
}
},
"ns.foo#fooTrait": {
"type": "structure",
"members": { },
"traits": {
"smithy.api#deprecated": { },
"smithy.api#trait": {
"selector": "*"
}
}
},
"ns.foo#barTrait": {
"type": "structure",
"members": { },
"traits": {
"smithy.api#deprecated": {
"message": "This trait is no longer used."
},
"smithy.api#trait": {
"selector": "*"
}
}
},
"ns.foo#bazTrait": {
"type": "structure",
"members": { },
"traits": {
"smithy.api#trait": {
"selector": "*"
}
}
}
}
}