Skip to content
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

Server streaming body #1023

Merged
merged 5 commits into from
Feb 3, 2022
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
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 All @@ -39,6 +41,7 @@ class ServerOperationHandlerGenerator(
"PinProjectLite" to ServerCargoDependency.PinProjectLite.asType(),
"Tower" to ServerCargoDependency.Tower.asType(),
"FuturesUtil" to ServerCargoDependency.FuturesUtil.asType(),
"SmithyHttp" to CargoDependency.SmithyHttp(runtimeConfig).asType(),
"SmithyHttpServer" to CargoDependency.SmithyHttpServer(runtimeConfig).asType(),
"SmithyRejection" to ServerHttpProtocolGenerator.smithyRejection(runtimeConfig),
"Phantom" to ServerRuntimeType.Phantom,
Expand Down Expand Up @@ -132,13 +135,18 @@ class ServerOperationHandlerGenerator(
} else {
symbolProvider.toSymbol(operation.outputShape(model)).fullName
}
val streamingBodyTraitBounds = if (operation.inputShape(model).hasStreamingMember(model)) {
"\n B: Into<#{SmithyHttp}::byte_stream::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 @@ -39,6 +39,7 @@ import software.amazon.smithy.rust.codegen.util.getTrait
import software.amazon.smithy.rust.codegen.util.hasStreamingMember
import software.amazon.smithy.rust.codegen.util.hasTrait
import software.amazon.smithy.rust.codegen.util.inputShape
import software.amazon.smithy.rust.codegen.util.isStreaming
import software.amazon.smithy.rust.codegen.util.orNull
import software.amazon.smithy.rust.codegen.util.outputShape
import software.amazon.smithy.rust.codegen.util.toSnakeCase
Expand Down Expand Up @@ -212,16 +213,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 @@ -326,15 +327,37 @@ class ServerProtocolTestGenerator(
"""
use #{AxumCore}::extract::FromRequest;
let mut http_request = #{AxumCore}::extract::RequestParts::new(http_request);
let input_wrapper = super::$operationName::from_request(&mut http_request).await.expect("failed to parse request");
let input = input_wrapper.0;
let parsed = super::$operationName::from_request(&mut http_request).await.expect("failed to parse request").0;
""",
*codegenScope,
)
if (operationShape.outputShape(model).hasStreamingMember(model)) {
rustWriter.rust("""todo!("streaming types aren't supported yet");""")

if (inputShape.hasStreamingMember(model)) {
// A streaming shape does not implement `PartialEq`, so we have to iterate over the input shape's members
// and handle the equality assertion separately.
for (member in inputShape.members()) {
val memberName = codegenContext.symbolProvider.toMemberName(member)
if (member.isStreaming(codegenContext.model)) {
rustWriter.rustTemplate(
"""
#{AssertEq}(
parsed.$memberName.collect().await.unwrap().into_bytes(),
expected.$memberName.collect().await.unwrap().into_bytes()
);
""",
*codegenScope
)
} else {
rustWriter.rustTemplate(
"""
#{AssertEq}(parsed.$memberName, expected.$memberName, "Unexpected value for `$memberName`");
""",
*codegenScope
)
}
}
} else {
rustWriter.rustTemplate("#{AssertEq}(input, expected);", *codegenScope)
rustWriter.rustTemplate("#{AssertEq}(parsed, expected);", *codegenScope)
}
}

Expand All @@ -357,7 +380,7 @@ class ServerProtocolTestGenerator(
assertOk(rustWriter) {
rustWriter.write(
"#T(&body, ${
rustWriter.escape(body).dq()
rustWriter.escape(body).dq()
}, #T::from(${(mediaType ?: "unknown").dq()}))",
RuntimeType.ProtocolTestHelper(codegenContext.runtimeConfig, "validate_body"),
RuntimeType.ProtocolTestHelper(codegenContext.runtimeConfig, "MediaType")
Expand Down Expand Up @@ -386,19 +409,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 @@ -511,16 +534,7 @@ class ServerProtocolTestGenerator(
FailingTest(RestJson, "RestJsonNoInputAndNoOutput", Action.Response),
FailingTest(RestJson, "RestJsonNoInputAndOutputWithJson", Action.Response),
FailingTest(RestJson, "RestJsonSupportsNaNFloatInputs", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsWithBlob", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsWithNoBlobBody", Action.Request),
FailingTest(RestJson, "RestJsonStreamingTraitsWithBlob", Action.Response),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@david-perez @crisidev I was not sure what to do here - should I remove these tests from the expect-fail list as these now seem to pass...?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, if it passes it can be removed from the failing list.

Copy link
Contributor

@david-perez david-perez Feb 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guymguym I manually verified the now-passing tests and they look good.

I also had a look at the remaining failing request streaming protocol tests and pushed a new commit to this branch, Add blob streaming support for server request protocol tests, to get rid of them.

The only remaining failing protocol test for streaming is RestJsonStreamingTraitsRequireLengthWithBlob, but that is due to us not setting Content-Length appropriately, so I've cut #1146 to track that.

Let me know if you agree with the fix or have any questions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great @david-perez. Thank you for making it so easy!

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 @@ -591,56 +605,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