-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add jsonPayload trait #89
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
38cc993
feat: Add jsonPayload trait
ghostbuster91 7f2fc65
Remove useless comments
ghostbuster91 ed81eb1
Add tests for JsonPayloadValidator
ghostbuster91 603786b
Transform errors as well
ghostbuster91 e0f7a0a
Use traitValidator
ghostbuster91 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
99 changes: 99 additions & 0 deletions
99
modules/smithy-tests/src/test/scala/jsonrpclib/JsonPayloadValidatorSpec.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,99 @@ | ||
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 JsonPayloadValidatorSpec extends FunSuite { | ||
test("no error when jsonPayload is used on the input, output or error structure's member") { | ||
|
||
assembleModel( | ||
"""$version: "2" | ||
|namespace test | ||
| | ||
|use jsonrpclib#jsonRPC | ||
|use jsonrpclib#jsonRequest | ||
|use jsonrpclib#jsonPayload | ||
| | ||
|@jsonRPC | ||
|service MyService { | ||
| operations: [OpA] | ||
|} | ||
| | ||
|@jsonRequest("foo") | ||
|operation OpA { | ||
| input: OpInput | ||
| output: OpOutput | ||
| errors: [OpError] | ||
|} | ||
| | ||
|structure OpInput { | ||
| @jsonPayload | ||
| data: String | ||
|} | ||
| | ||
|structure OpOutput { | ||
| @jsonPayload | ||
| data: String | ||
|} | ||
| | ||
|@error("client") | ||
|structure OpError { | ||
| @jsonPayload | ||
| data: String | ||
|} | ||
| | ||
|""".stripMargin | ||
).unwrap() | ||
|
||
success | ||
} | ||
test("return an error when jsonPayload is used in a nested structure") { | ||
val events = eventsWithoutLocations( | ||
assembleModel( | ||
"""$version: "2" | ||
|namespace test | ||
| | ||
|use jsonrpclib#jsonRPC | ||
|use jsonrpclib#jsonRequest | ||
|use jsonrpclib#jsonPayload | ||
| | ||
|@jsonRPC | ||
|service MyService { | ||
| operations: [OpA] | ||
|} | ||
| | ||
|@jsonRequest("foo") | ||
|operation OpA { | ||
| input: OpInput | ||
|} | ||
| | ||
|structure OpInput { | ||
| data: NestedStructure | ||
|} | ||
| | ||
|structure NestedStructure { | ||
| @jsonPayload | ||
| data: String | ||
|} | ||
|""".stripMargin | ||
) | ||
) | ||
|
||
val expected = ValidationEvent | ||
.builder() | ||
.id("jsonPayload.OnlyTopLevel") | ||
.shapeId(ShapeId.fromParts("test", "NestedStructure", "data")) | ||
.severity(Severity.ERROR) | ||
.message( | ||
"Found an incompatible shape when validating the constraints of the `jsonrpclib#jsonPayload` trait attached to `test#NestedStructure$data`: jsonPayload can only be used on the top level of an operation input/output/error." | ||
) | ||
.build() | ||
|
||
assert(events.contains(expected)) | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...hy/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
jsonrpclib.validation.JsonNotificationOutputValidator | ||
jsonrpclib.validation.UniqueJsonRpcMethodNamesValidator | ||
jsonrpclib.validation.JsonRpcOperationValidator | ||
jsonrpclib.validation.JsonRpcOperationValidator |
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
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
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
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
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
21 changes: 21 additions & 0 deletions
21
modules/smithy4s/src/main/scala/jsonrpclib/smithy4sinterop/JsonPayloadTransformation.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,21 @@ | ||
package jsonrpclib.smithy4sinterop | ||
|
||
import jsonrpclib.JsonPayload | ||
import smithy4s.~> | ||
import smithy4s.Schema | ||
import smithy4s.Schema.StructSchema | ||
|
||
private[jsonrpclib] object JsonPayloadTransformation extends (Schema ~> Schema) { | ||
|
||
def apply[A0](fa: Schema[A0]): Schema[A0] = | ||
fa match { | ||
case struct: StructSchema[b] => | ||
struct.fields | ||
.collectFirst { | ||
case field if field.hints.has[JsonPayload] => | ||
field.schema.biject[b]((f: Any) => struct.make(Vector(f)))(field.get) | ||
} | ||
.getOrElse(fa) | ||
case _ => fa | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
modules/smithy4s/src/main/scala/jsonrpclib/smithy4sinterop/JsonRpcTransformations.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,33 @@ | ||
package jsonrpclib.smithy4sinterop | ||
|
||
import smithy4s.~> | ||
import smithy4s.schema.ErrorSchema | ||
import smithy4s.schema.OperationSchema | ||
import smithy4s.Endpoint | ||
import smithy4s.Schema | ||
import smithy4s.Service | ||
|
||
private[jsonrpclib] object JsonRpcTransformations { | ||
|
||
def apply[Alg[_[_, _, _, _, _]]]: Service[Alg] => Service[Alg] = | ||
_.toBuilder | ||
.mapEndpointEach( | ||
Endpoint.mapSchema( | ||
OperationSchema | ||
.mapInputK(JsonPayloadTransformation) | ||
.andThen(OperationSchema.mapOutputK(JsonPayloadTransformation)) | ||
.andThen(OperationSchema.mapErrorK(errorTransformation)) | ||
) | ||
) | ||
.build | ||
|
||
private val payloadTransformation: Schema ~> Schema = Schema | ||
.transformTransitivelyK(JsonPayloadTransformation) | ||
|
||
private val errorTransformation: ErrorSchema ~> ErrorSchema = | ||
new smithy4s.kinds.PolyFunction[ErrorSchema, ErrorSchema] { | ||
def apply[A](e: ErrorSchema[A]): ErrorSchema[A] = { | ||
payloadTransformation(e.schema).error(e.unliftError)(e.liftError.unlift) | ||
} | ||
} | ||
} |
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
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
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.