Skip to content

Commit

Permalink
Server streaming body
Browse files Browse the repository at this point in the history
Signed-off-by: Guy Margalit <[email protected]>
  • Loading branch information
guymguym committed Feb 1, 2022
1 parent 384c492 commit 3722ea5
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerHttpPro
import software.amazon.smithy.rust.codegen.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.util.hasStreamingMember
import software.amazon.smithy.rust.codegen.util.inputShape
import software.amazon.smithy.rust.codegen.util.outputShape

/**
Expand Down Expand Up @@ -132,13 +134,18 @@ class ServerOperationHandlerGenerator(
} else {
symbolProvider.toSymbol(operation.outputShape(model)).fullName
}
val streamingBodyTraitBounds = if (operation.inputShape(model).hasStreamingMember(model)) {
"\n B: Into<#{SmithyHttpServer}::ByteStream>,"
} else {
""
}
return """
$inputFn
Fut: std::future::Future<Output = $outputType> + Send,
B: $serverCrate::HttpBody + Send + 'static,
B: $serverCrate::HttpBody + Send + 'static, $streamingBodyTraitBounds
B::Data: Send,
B::Error: Into<$serverCrate::BoxError>,
$serverCrate::rejection::SmithyRejection: From<<B as $serverCrate::HttpBody>::Error>
"""
""".trimIndent()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ class ServerProtocolTestGenerator(

rustTemplate(
"""
##[allow(unused_mut)] let mut http_request = http::Request::builder()
.uri("${httpRequestTestCase.uri}")
""",
*codegenScope
##[allow(unused_mut)] let mut http_request = http::Request::builder()
.uri("${httpRequestTestCase.uri}")
""",
*codegenScope
)
for (header in httpRequestTestCase.headers) {
rust(".header(${header.key.dq()}, ${header.value.dq()})")
}
rustTemplate(
"""
"""
.body(#{SmithyHttpServer}::Body::from(#{Bytes}::from_static(b${httpRequestTestCase.body.orNull()?.dq()})))
.unwrap();
""",
Expand Down Expand Up @@ -363,19 +363,19 @@ class ServerProtocolTestGenerator(
basicCheck(
requireHeaders,
rustWriter,
"required_headers",
actualExpression,
"require_headers"
"required_headers",
actualExpression,
"require_headers"
)
}

private fun checkForbidHeaders(rustWriter: RustWriter, actualExpression: String, forbidHeaders: List<String>) {
basicCheck(
forbidHeaders,
rustWriter,
"forbidden_headers",
"forbidden_headers",
actualExpression,
"forbid_headers"
"forbid_headers"
)
}

Expand Down Expand Up @@ -525,14 +525,10 @@ class ServerProtocolTestGenerator(
FailingTest(RestJson, "RestJsonSupportsNegativeInfinityFloatInputs", Action.Response),
FailingTest(RestJson, "RestJsonStreamingTraitsWithBlob", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsWithNoBlobBody", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsWithBlob", Action.Response),
FailingTest(RestJson, "RestJsonStreamingTraitsWithNoBlobBody", Action.Response),
FailingTest(RestJson, "RestJsonStreamingTraitsRequireLengthWithBlob", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsRequireLengthWithNoBlobBody", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsRequireLengthWithBlob", Action.Response),
FailingTest(RestJson, "RestJsonStreamingTraitsRequireLengthWithNoBlobBody", Action.Response),
FailingTest(RestJson, "RestJsonStreamingTraitsWithMediaTypeWithBlob", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsWithMediaTypeWithBlob", Action.Response),
FailingTest(RestJson, "RestJsonHttpWithEmptyBlobPayload", Action.Request),
FailingTest(RestJson, "RestJsonHttpWithEmptyStructurePayload", Action.Request),

Expand Down Expand Up @@ -603,56 +599,64 @@ class ServerProtocolTestGenerator(
).asObjectNode().get()
).build()
private fun fixRestJsonAllQueryStringTypes(testCase: HttpRequestTestCase): HttpRequestTestCase =
testCase.toBuilder().params(
Node.parse("""{
"queryString": "Hello there",
"queryStringList": ["a", "b", "c"],
"queryStringSet": ["a", "b", "c"],
"queryByte": 1,
"queryShort": 2,
"queryInteger": 3,
"queryIntegerList": [1, 2, 3],
"queryIntegerSet": [1, 2, 3],
"queryLong": 4,
"queryFloat": 1.1,
"queryDouble": 1.1,
"queryDoubleList": [1.1, 2.1, 3.1],
"queryBoolean": true,
"queryBooleanList": [true, false, true],
"queryTimestamp": 1,
"queryTimestampList": [1, 2, 3],
"queryEnum": "Foo",
"queryEnumList": ["Foo", "Baz", "Bar"],
"queryParamsMapOfStringList": {
"String": ["Hello there"],
"StringList": ["a", "b", "c"],
"StringSet": ["a", "b", "c"],
"Byte": ["1"],
"Short": ["2"],
"Integer": ["3"],
"IntegerList": ["1", "2", "3"],
"IntegerSet": ["1", "2", "3"],
"Long": ["4"],
"Float": ["1.1"],
"Double": ["1.1"],
"DoubleList": ["1.1", "2.1", "3.1"],
"Boolean": ["true"],
"BooleanList": ["true", "false", "true"],
"Timestamp": ["1970-01-01T00:00:01Z"],
"TimestampList": ["1970-01-01T00:00:01Z", "1970-01-01T00:00:02Z", "1970-01-01T00:00:03Z"],
"Enum": ["Foo"],
"EnumList": ["Foo", "Baz", "Bar"]
testCase.toBuilder().params(
Node.parse(
"""
{
"queryString": "Hello there",
"queryStringList": ["a", "b", "c"],
"queryStringSet": ["a", "b", "c"],
"queryByte": 1,
"queryShort": 2,
"queryInteger": 3,
"queryIntegerList": [1, 2, 3],
"queryIntegerSet": [1, 2, 3],
"queryLong": 4,
"queryFloat": 1.1,
"queryDouble": 1.1,
"queryDoubleList": [1.1, 2.1, 3.1],
"queryBoolean": true,
"queryBooleanList": [true, false, true],
"queryTimestamp": 1,
"queryTimestampList": [1, 2, 3],
"queryEnum": "Foo",
"queryEnumList": ["Foo", "Baz", "Bar"],
"queryParamsMapOfStringList": {
"String": ["Hello there"],
"StringList": ["a", "b", "c"],
"StringSet": ["a", "b", "c"],
"Byte": ["1"],
"Short": ["2"],
"Integer": ["3"],
"IntegerList": ["1", "2", "3"],
"IntegerSet": ["1", "2", "3"],
"Long": ["4"],
"Float": ["1.1"],
"Double": ["1.1"],
"DoubleList": ["1.1", "2.1", "3.1"],
"Boolean": ["true"],
"BooleanList": ["true", "false", "true"],
"Timestamp": ["1970-01-01T00:00:01Z"],
"TimestampList": ["1970-01-01T00:00:01Z", "1970-01-01T00:00:02Z", "1970-01-01T00:00:03Z"],
"Enum": ["Foo"],
"EnumList": ["Foo", "Baz", "Bar"]
}
}
}""".trimMargin()).asObjectNode().get()
).build()
""".trimMargin()
).asObjectNode().get()
).build()
private fun fixRestJsonQueryStringEscaping(testCase: HttpRequestTestCase): HttpRequestTestCase =
testCase.toBuilder().params(
Node.parse("""{
"queryString": "%:/?#[]@!${'$'}&'()*+,;=😹",
"queryParamsMapOfStringList": {
"String": ["%:/?#[]@!${'$'}&'()*+,;=😹"]
}
}""".trimMargin()).asObjectNode().get()
Node.parse(
"""
{
"queryString": "%:/?#[]@!${'$'}&'()*+,;=😹",
"queryParamsMapOfStringList": {
"String": ["%:/?#[]@!${'$'}&'()*+,;=😹"]
}
}
""".trimMargin()
).asObjectNode().get()
).build()
// This test assumes that errors in responses are identified by an `X-Amzn-Errortype` header with the error shape name.
// However, Smithy specifications for AWS protocols that serialize to JSON recommend that new server implementations
Expand Down
Loading

0 comments on commit 3722ea5

Please sign in to comment.