-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Make UnreferencedShape an opt-in linter instead
UnreferencedShape is no longer an on-by-default validator that ensures shapes must be connected to a service shape. There are plenty of cases where teams might create models that define shared shapes and don't necessarily connect those shapes to services. Emitting an UnreferencedShape validation event for these cases is harmless, but also annoying and desensitizing developers for when a shape is actually unreferenced. This commit instead adds a new smithy-linters validator called UnreferencedShape that allows an optional "rootShapeSelector" to be provided that defines the "root" shapes that all other shape must be connected to. It defaults to "service". You might want to customize this to indicate that other shapes are your roots (e.g., maybe there is a specific trait that makes a shape a root). Closes #2093
Showing
23 changed files
with
374 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
smithy-linters/src/main/java/software/amazon/smithy/linters/UnreferencedShapeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.linters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.neighbor.UnreferencedShapes; | ||
import software.amazon.smithy.model.node.ExpectationNotMetException; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.selector.Selector; | ||
import software.amazon.smithy.model.selector.SelectorSyntaxException; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.model.validation.ValidatorService; | ||
|
||
public final class UnreferencedShapeValidator extends AbstractValidator { | ||
|
||
public static final class Config { | ||
|
||
private Selector rootShapeSelector = Selector.parse("service"); | ||
|
||
/** | ||
* The Selector used to find root shapes, and any shape that is not connected to any of the returned root | ||
* shapes is considered unreferenced. | ||
* | ||
* <p>Defaults to "service" if not set. | ||
* | ||
* @return Returns the root selector; | ||
*/ | ||
public Selector getRootShapeSelector() { | ||
return rootShapeSelector; | ||
} | ||
|
||
public void setRootShapeSelector(Selector rootShapeSelector) { | ||
this.rootShapeSelector = Objects.requireNonNull(rootShapeSelector); | ||
} | ||
} | ||
|
||
public static final class Provider extends ValidatorService.Provider { | ||
public Provider() { | ||
super(UnreferencedShapeValidator.class, configuration -> { | ||
Config config = new Config(); | ||
ObjectNode node = configuration.expectObjectNode() | ||
.expectNoAdditionalProperties(Collections.singleton("rootShapeSelector")); | ||
node.getStringMember("rootShapeSelector").ifPresent(rootShapeNode -> { | ||
try { | ||
config.setRootShapeSelector(Selector.parse(rootShapeNode.getValue())); | ||
} catch (SelectorSyntaxException e) { | ||
throw new ExpectationNotMetException("Error parsing `rootShapeSelector`: " + e.getMessage(), | ||
rootShapeNode); | ||
} | ||
}); | ||
return new UnreferencedShapeValidator(config); | ||
}); | ||
} | ||
} | ||
|
||
private final Config config; | ||
|
||
private UnreferencedShapeValidator(Config config) { | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
|
||
for (Shape shape : new UnreferencedShapes(config.rootShapeSelector).compute(model)) { | ||
events.add(note(shape, "This shape is unreferenced. It has no modeled connections to shapes " | ||
+ "that match the following selector: `" + config.rootShapeSelector + "`")); | ||
} | ||
|
||
return events; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...resources/software/amazon/smithy/linters/errorfiles/UnreferencedShapes/custom-root.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[WARNING] smithy.example#Baz: This shape is unreferenced. It has no modeled connections to shapes that match the following selector: `[trait|smithy.example#root]` | UnreferencedShape | ||
[WARNING] smithy.example#Bam: This shape is unreferenced. It has no modeled connections to shapes that match the following selector: `[trait|smithy.example#root]` | UnreferencedShape |
37 changes: 37 additions & 0 deletions
37
...resources/software/amazon/smithy/linters/errorfiles/UnreferencedShapes/custom-root.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
$version: "2.0" | ||
|
||
metadata validators = [ | ||
{ | ||
name: "UnreferencedShape" | ||
// Elevate the severity so that NOTE events don't slip through. | ||
severity: "WARNING" | ||
configuration: { | ||
rootShapeSelector: "[trait|smithy.example#root]" | ||
} | ||
} | ||
] | ||
|
||
namespace smithy.example | ||
|
||
// Considered referenced because it's a trait. | ||
@trait | ||
structure root {} | ||
|
||
// Considered referenced because of the root trait. | ||
@root | ||
structure MyAbc { | ||
MyString: MyString | ||
} | ||
|
||
// Considered referenced because it's referenced by MyAbc$MyString | ||
string MyString | ||
|
||
// Considered referenced because of the root trait. | ||
@root | ||
string MyDef | ||
|
||
// Unreferenced. | ||
string Baz | ||
|
||
// Unreferenced. | ||
string Bam |
1 change: 1 addition & 0 deletions
1
...software/amazon/smithy/linters/errorfiles/UnreferencedShapes/invalid-root-selector.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[ERROR] -: Error creating `UnreferencedShape` validator: Error parsing | Model |
10 changes: 10 additions & 0 deletions
10
...software/amazon/smithy/linters/errorfiles/UnreferencedShapes/invalid-root-selector.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
$version: "2.0" | ||
|
||
metadata validators = [ | ||
{ | ||
name: "UnreferencedShape" | ||
configuration: { | ||
rootShapeSelector: "!!!!" | ||
} | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
.../amazon/smithy/linters/errorfiles/UnreferencedShapes/selector-defaults-to-services.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[NOTE] smithy.example#Baz: This shape is unreferenced. It has no modeled connections to shapes that match the following selector: `service` | UnreferencedShape |
29 changes: 29 additions & 0 deletions
29
.../amazon/smithy/linters/errorfiles/UnreferencedShapes/selector-defaults-to-services.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
$version: "2.0" | ||
|
||
metadata validators = [ | ||
{ | ||
name: "UnreferencedShape" | ||
} | ||
] | ||
|
||
namespace smithy.example | ||
|
||
// A "root" shape, so does not emit. | ||
service MyService { | ||
operations: [ | ||
MyOperation | ||
] | ||
} | ||
|
||
// Connected. | ||
operation MyOperation { | ||
input := { | ||
foo: MyString | ||
} | ||
} | ||
|
||
// Connected. | ||
string MyString | ||
|
||
// Not connected. | ||
string Baz |
2 changes: 2 additions & 0 deletions
2
.../amazon/smithy/linters/errorfiles/UnreferencedShapes/service-root-multiple-matches.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[NOTE] smithy.example#Baz: This shape is unreferenced. It has no modeled connections to shapes that match the following selector: `service` | UnreferencedShape | ||
[NOTE] smithy.example#Bam: This shape is unreferenced. It has no modeled connections to shapes that match the following selector: `service` | UnreferencedShape |
48 changes: 48 additions & 0 deletions
48
.../amazon/smithy/linters/errorfiles/UnreferencedShapes/service-root-multiple-matches.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
$version: "2.0" | ||
|
||
metadata validators = [ | ||
{ | ||
name: "UnreferencedShape" | ||
configuration: { | ||
rootShapeSelector: "service" | ||
} | ||
} | ||
] | ||
|
||
namespace smithy.example | ||
|
||
service MyService1 { | ||
operations: [ | ||
MyOperation1 | ||
] | ||
} | ||
|
||
service MyService2 { | ||
operations: [ | ||
MyOperation2 | ||
] | ||
} | ||
|
||
operation MyOperation1 { | ||
input := { | ||
foo: MyAbc | ||
} | ||
} | ||
|
||
operation MyOperation2 { | ||
output := { | ||
bar: MyDef | ||
} | ||
} | ||
|
||
structure MyAbc { | ||
MyString: MyString | ||
} | ||
|
||
string MyString | ||
|
||
string MyDef | ||
|
||
string Baz | ||
|
||
string Bam |
2 changes: 2 additions & 0 deletions
2
...ware/amazon/smithy/linters/errorfiles/UnreferencedShapes/service-root-single-match.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[NOTE] smithy.example#Baz: This shape is unreferenced. It has no modeled connections to shapes that match the following selector: `service` | UnreferencedShape | ||
[NOTE] smithy.example#Bam: This shape is unreferenced. It has no modeled connections to shapes that match the following selector: `service` | UnreferencedShape |
39 changes: 39 additions & 0 deletions
39
...ware/amazon/smithy/linters/errorfiles/UnreferencedShapes/service-root-single-match.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
$version: "2.0" | ||
|
||
metadata validators = [ | ||
{ | ||
name: "UnreferencedShape" | ||
configuration: { | ||
rootShapeSelector: "service" | ||
} | ||
} | ||
] | ||
|
||
namespace smithy.example | ||
|
||
service MyService { | ||
operations: [ | ||
MyOperation | ||
] | ||
} | ||
|
||
operation MyOperation { | ||
input := { | ||
foo: MyAbc | ||
} | ||
output := { | ||
bar: MyDef | ||
} | ||
} | ||
|
||
structure MyAbc { | ||
MyString: MyString | ||
} | ||
|
||
string MyString | ||
|
||
string MyDef | ||
|
||
string Baz | ||
|
||
string Bam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
...es/software/amazon/smithy/model/errorfiles/validators/unreferenced-shape-validator.errors
This file was deleted.
Oops, something went wrong.
76 changes: 0 additions & 76 deletions
76
...rces/software/amazon/smithy/model/errorfiles/validators/unreferenced-shape-validator.json
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
...test/resources/software/amazon/smithy/model/loader/validation-event-decorator-test.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
@deprecated | ||
string MyString | ||
|
||
string OtherString | ||
|
||
structure Foo { | ||
a: MyString | ||
b: OtherString | ||
} |