Skip to content
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
15 changes: 14 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ val smithy = projectMatrix
smithyTraitCodegenNamespace := "jsonrpclib"
)

val smithyTests = projectMatrix
.in(file("modules/smithy-tests"))
.jvmPlatform(Seq(scala213))
.dependsOn(smithy)
.settings(
publish / skip := true,
libraryDependencies ++= Seq(
"com.disneystreaming" %%% "weaver-cats" % "0.8.4" % Test
)
)
.disablePlugins(MimaPlugin)

lazy val buildTimeProtocolDependency =
/** By default, smithy4sInternalDependenciesAsJars doesn't contain the jars in the "smithy4s" configuration. We have
* to add them manually - this is the equivalent of a "% Smithy4s"-scoped dependency.
Expand Down Expand Up @@ -137,7 +149,7 @@ val smithy4s = projectMatrix
)

val smithy4sTests = projectMatrix
.in(file("modules") / "smithy4sTests")
.in(file("modules") / "smithy4s-tests")
.jvmPlatform(jvmScalaVersions, commonJvmSettings)
.jsPlatform(jsScalaVersions)
.nativePlatform(Seq(scala3))
Expand Down Expand Up @@ -251,6 +263,7 @@ val root = project
exampleServer,
exampleClient,
smithy,
smithyTests,
smithy4s,
smithy4sTests,
exampleSmithyShared,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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:={
| message: String
| }
|}
|
|""".stripMargin
)
)

val expected = ValidationEvent
.builder()
.id("JsonNotificationOutput")
.shapeId(ShapeId.fromParts("test", "NotifySomething"))
.severity(Severity.ERROR)
.message(
"Operation marked as @jsonNotification must not return anything, but found `test#NotifySomethingOutput`."
)
.build()

assert(events.contains(expected))
}

}
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 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 modules/smithy-tests/src/test/scala/jsonrpclib/ModelUtils.scala
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())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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))
}

test("no error if two services use the same operation") {
assembleModel(
"""$version: "2"
|namespace test
|
|use jsonrpclib#jsonRPC
|use jsonrpclib#jsonRequest
|use jsonrpclib#jsonNotification
|
|@jsonRPC
|service MyService {
| operations: [OpA]
|}
|
|@jsonRPC
|service MyOtherService {
| operations: [OpA]
|}
|
|@jsonRequest("foo")
|operation OpA {}
|
|""".stripMargin
).unwrap()
success
}

test("no error if two services use the same operation") {
assembleModel(
"""$version: "2"
|namespace test
|
|use jsonrpclib#jsonRequest
|use jsonrpclib#jsonNotification
|
|
|service NonJsonRpcService {
| operations: [OpA]
|}
|
|@jsonRequest("foo")
|operation OpA {}
|
|@jsonNotification("foo")
|operation OpB {} // duplicate method name "foo"
|""".stripMargin
).unwrap()
success
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package jsonrpclib.validation;

import jsonrpclib.JsonNotificationTrait;
import software.amazon.smithy.model.Model;
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.getShapesWithTrait(JsonNotificationTrait.ID).stream().flatMap(op -> {
ShapeId outputShapeId = op.asOperationShape().orElseThrow().getOutputShape();
var outputShape = model.expectShape(outputShapeId);
if (outputShape.asStructureShape().map(s -> !s.members().isEmpty()).orElse(true)) {
return Stream.of(error(op, String.format(
"Operation marked as @jsonNotification must not return anything, but found `%s`.", outputShapeId)));
} else {
return Stream.empty();
}
}).collect(Collectors.toUnmodifiableList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package jsonrpclib.validation;

import jsonrpclib.JsonNotificationTrait;
import jsonrpclib.JsonRPCTrait;
import jsonrpclib.JsonRequestTrait;
import software.amazon.smithy.model.Model;
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(op -> !hasJsonRpcMethod(op))
.map(op -> error(op, String.format(
"Operation is part of service `%s` marked with @jsonRPC but is missing @jsonRequest or @jsonNotification.", service.getId())));
}

private boolean hasJsonRpcMethod(Shape op) {
return op.hasTrait(JsonRequestTrait.ID) || op.hasTrait(JsonNotificationTrait.ID);
}
}

Loading