-
Notifications
You must be signed in to change notification settings - Fork 6
Add smithy validations #88
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
58 changes: 58 additions & 0 deletions
58
modules/smithy-tests/src/test/scala/jsonrpclib/JsonNotificationOutputValidatorSpec.scala
This file contains hidden or 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,58 @@ | ||
| package jsonrpclib | ||
|
|
||
| import jsonrpclib.ModelUtils.assembleModel | ||
| import jsonrpclib.ModelUtils.eventsWithoutLocations | ||
| import software.amazon.smithy.model.shapes.ShapeId | ||
| import software.amazon.smithy.model.validation.Severity | ||
| import software.amazon.smithy.model.validation.ValidationEvent | ||
| import weaver._ | ||
|
|
||
| object JsonNotificationOutputValidatorSpec extends FunSuite { | ||
| test("no error when a @jsonNotification operation has unit output") { | ||
| assembleModel( | ||
| """$version: "2" | ||
| |namespace test | ||
| | | ||
| |use jsonrpclib#jsonNotification | ||
| | | ||
| |@jsonNotification("notify") | ||
| |operation NotifySomething { | ||
| |} | ||
| |""".stripMargin | ||
| ) | ||
| success | ||
| } | ||
| test("return an error when a @jsonNotification operation does not have unit output") { | ||
| val events = eventsWithoutLocations( | ||
| assembleModel( | ||
| """$version: "2" | ||
| |namespace test | ||
| | | ||
| |use jsonrpclib#jsonNotification | ||
| | | ||
| |@jsonNotification("notify") | ||
| |operation NotifySomething { | ||
| | output: NotifyOutput | ||
| |} | ||
| | | ||
| |structure NotifyOutput { | ||
| | message: String | ||
| |} | ||
| |""".stripMargin | ||
| ) | ||
| ) | ||
|
|
||
| val expected = ValidationEvent | ||
| .builder() | ||
| .id("JsonNotificationOutput") | ||
| .shapeId(ShapeId.fromParts("test", "NotifySomething")) | ||
| .severity(Severity.ERROR) | ||
| .message( | ||
| "Operation `test#NotifySomething` marked as @jsonNotification must not have output defined, but found `test#NotifyOutput`." | ||
| ) | ||
| .build() | ||
|
|
||
| assert(events.contains(expected)) | ||
| } | ||
|
|
||
| } | ||
72 changes: 72 additions & 0 deletions
72
modules/smithy-tests/src/test/scala/jsonrpclib/JsonRpcOperationValidatorSpec.scala
This file contains hidden or 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,72 @@ | ||
| package jsonrpclib | ||
|
|
||
| import jsonrpclib.ModelUtils.assembleModel | ||
| import jsonrpclib.ModelUtils.eventsWithoutLocations | ||
| import software.amazon.smithy.model.shapes.ShapeId | ||
| import software.amazon.smithy.model.validation.Severity | ||
| import software.amazon.smithy.model.validation.ValidationEvent | ||
| import weaver._ | ||
|
|
||
| object JsonRpcOperationValidatorSpec extends FunSuite { | ||
| test("no error when all operations in @jsonRPC service are properly annotated") { | ||
| assembleModel( | ||
| """$version: "2" | ||
| |namespace test | ||
| | | ||
| |use jsonrpclib#jsonRPC | ||
| |use jsonrpclib#jsonRequest | ||
| |use jsonrpclib#jsonNotification | ||
| | | ||
| |@jsonRPC | ||
| |service MyService { | ||
| | operations: [OpA, OpB] | ||
| |} | ||
| | | ||
| |@jsonRequest("methodA") | ||
| |operation OpA {} | ||
| | | ||
| |@jsonNotification("methodB") | ||
| |operation OpB { | ||
| | output: unit | ||
| |} | ||
| |""".stripMargin | ||
| ) | ||
| success | ||
| } | ||
|
|
||
| test("return an error when a @jsonRPC service has an operation without @jsonRequest or @jsonNotification") { | ||
| val events = eventsWithoutLocations( | ||
| assembleModel( | ||
| """$version: "2" | ||
| |namespace test | ||
| | | ||
| |use jsonrpclib#jsonRPC | ||
| |use jsonrpclib#jsonRequest | ||
| | | ||
| |@jsonRPC | ||
| |service MyService { | ||
| | operations: [GoodOp, BadOp] | ||
| |} | ||
| | | ||
| |@jsonRequest("good") | ||
| |operation GoodOp {} | ||
| | | ||
| |operation BadOp {} // ❌ missing jsonRequest or jsonNotification | ||
| |""".stripMargin | ||
| ) | ||
| ) | ||
|
|
||
| val expected = | ||
| ValidationEvent | ||
| .builder() | ||
| .id("JsonRpcOperation") | ||
| .shapeId(ShapeId.fromParts("test", "BadOp")) | ||
| .severity(Severity.ERROR) | ||
| .message( | ||
| "Operation `test#BadOp` is part of service `test#MyService` marked with @jsonRPC but is missing @jsonRequest or @jsonNotification." | ||
| ) | ||
| .build() | ||
|
|
||
| assert(events.contains(expected)) | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
modules/smithy-tests/src/test/scala/jsonrpclib/ModelUtils.scala
This file contains hidden or 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,27 @@ | ||
| package jsonrpclib | ||
|
|
||
| import software.amazon.smithy.model.validation.ValidatedResult | ||
| import software.amazon.smithy.model.validation.ValidationEvent | ||
| import software.amazon.smithy.model.Model | ||
| import software.amazon.smithy.model.SourceLocation | ||
|
|
||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| private object ModelUtils { | ||
|
|
||
| def assembleModel(text: String): ValidatedResult[Model] = { | ||
| Model | ||
| .assembler() | ||
| .discoverModels() | ||
| .addUnparsedModel( | ||
| "test.smithy", | ||
| text | ||
| ) | ||
| .assemble() | ||
| } | ||
|
|
||
| def eventsWithoutLocations(result: ValidatedResult[?]): List[ValidationEvent] = { | ||
| if (!result.isBroken) sys.error("Expected a broken result") | ||
| result.getValidationEvents.asScala.toList.map(e => e.toBuilder.sourceLocation(SourceLocation.NONE).build()) | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
modules/smithy-tests/src/test/scala/jsonrpclib/UniqueJsonRpcMethodNamesValidatorSpec.scala
This file contains hidden or 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,73 @@ | ||
| package jsonrpclib | ||
|
|
||
| import jsonrpclib.ModelUtils.assembleModel | ||
| import jsonrpclib.ModelUtils.eventsWithoutLocations | ||
| import software.amazon.smithy.model.shapes.ShapeId | ||
| import software.amazon.smithy.model.validation.Severity | ||
| import software.amazon.smithy.model.validation.ValidationEvent | ||
| import weaver._ | ||
|
|
||
| object UniqueJsonRpcMethodNamesValidatorSpec extends FunSuite { | ||
| test("no error when all jsonRpc method names are unique within a service") { | ||
|
|
||
| assembleModel( | ||
| """$version: "2" | ||
| |namespace test | ||
| | | ||
| |use jsonrpclib#jsonRPC | ||
| |use jsonrpclib#jsonRequest | ||
| |use jsonrpclib#jsonNotification | ||
| | | ||
| |@jsonRPC | ||
| |service MyService { | ||
| | operations: [OpA, OpB] | ||
| |} | ||
| | | ||
| |@jsonRequest("foo") | ||
| |operation OpA {} | ||
| | | ||
| |@jsonNotification("bar") | ||
| |operation OpB {} | ||
| |""".stripMargin | ||
| ).unwrap() | ||
|
|
||
| success | ||
| } | ||
| test("return an error when two operations use the same jsonRpc method name in a service") { | ||
| val events = eventsWithoutLocations( | ||
| assembleModel( | ||
| """$version: "2" | ||
| |namespace test | ||
| | | ||
| |use jsonrpclib#jsonRPC | ||
| |use jsonrpclib#jsonRequest | ||
| |use jsonrpclib#jsonNotification | ||
| | | ||
| |@jsonRPC | ||
| |service MyService { | ||
| | operations: [OpA, OpB] | ||
| |} | ||
| | | ||
| |@jsonRequest("foo") | ||
| |operation OpA {} | ||
| | | ||
| |@jsonNotification("foo") | ||
| |operation OpB {} // ❌ duplicate method name "foo" | ||
| |""".stripMargin | ||
| ) | ||
| ) | ||
|
|
||
| val expected = ValidationEvent | ||
| .builder() | ||
| .id("UniqueJsonRpcMethodNames") | ||
| .shapeId(ShapeId.fromParts("test", "MyService")) | ||
| .severity(Severity.ERROR) | ||
| .message( | ||
| "Duplicate JSON-RPC method name `foo` in service `test#MyService`. It is used by: test#OpA, test#OpB" | ||
| ) | ||
| .build() | ||
|
|
||
| assert(events.contains(expected)) | ||
| } | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
modules/smithy/src/main/java/jsonrpclib/validation/JsonNotificationOutputValidator.java
This file contains hidden or 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,35 @@ | ||
| package jsonrpclib.validation; | ||
|
|
||
| import jsonrpclib.JsonNotificationTrait; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.OperationShape; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.model.validation.AbstractValidator; | ||
| import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * Validates that operations marked with @jsonNotification don't have any output. | ||
| */ | ||
| public class JsonNotificationOutputValidator extends AbstractValidator { | ||
|
|
||
| @Override | ||
| public List<ValidationEvent> validate(Model model) { | ||
| return model.shapes(OperationShape.class) | ||
| .filter(op -> op.hasTrait(JsonNotificationTrait.ID)) | ||
ghostbuster91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .flatMap(op -> { | ||
| ShapeId outputShapeId = op.getOutputShape(); | ||
| if (outputShapeId != ShapeId.from("smithy.api#Unit")) { | ||
ghostbuster91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Stream.of(error(op, String.format( | ||
| "Operation `%s` marked as @jsonNotification must not have output defined, but found `%s`.", | ||
ghostbuster91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| op.getId(), outputShapeId))); | ||
| } else { | ||
| return Stream.empty(); | ||
| } | ||
| }) | ||
| .collect(Collectors.toUnmodifiableList()); | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
modules/smithy/src/main/java/jsonrpclib/validation/JsonRpcOperationValidator.java
This file contains hidden or 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,42 @@ | ||
| package jsonrpclib.validation; | ||
|
|
||
| import jsonrpclib.JsonNotificationTrait; | ||
| import jsonrpclib.JsonRPCTrait; | ||
| import jsonrpclib.JsonRequestTrait; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.OperationShape; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.model.shapes.Shape; | ||
| import software.amazon.smithy.model.validation.AbstractValidator; | ||
| import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class JsonRpcOperationValidator extends AbstractValidator { | ||
|
|
||
| @Override | ||
| public List<ValidationEvent> validate(Model model) { | ||
| return model.getServiceShapes().stream() | ||
| .filter(service -> service.hasTrait(JsonRPCTrait.class)) | ||
| .flatMap(service -> validateService(model, service)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private Stream<ValidationEvent> validateService(Model model, ServiceShape service) { | ||
| return service.getAllOperations().stream() | ||
| .map(model::expectShape) | ||
| .filter(Shape::isOperationShape) | ||
| .map(shape -> shape.asOperationShape().orElseThrow()) | ||
ghostbuster91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .filter(op -> !hasJsonRpcMethod(op)) | ||
| .map(op -> error(op, String.format( | ||
| "Operation `%s` is part of service `%s` marked with @jsonRPC but is missing @jsonRequest or @jsonNotification.", | ||
| op.getId(), service.getId()))); | ||
| } | ||
|
|
||
| private boolean hasJsonRpcMethod(OperationShape op) { | ||
| return op.hasTrait(JsonRequestTrait.ID) || op.hasTrait(JsonNotificationTrait.ID); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.