Unverified Commit 191c5771 authored by AWS SDK Rust Bot's avatar AWS SDK Rust Bot Committed by GitHub
Browse files

Fix constraint-related errors in Rpcv2CBOR server implementation (#3794)

parents 684c15f3 27ca7f16
Loading
Loading
Loading
Loading

.changelog/2155171.md

0 → 100644
+9 −0
Original line number Diff line number Diff line
---
applies_to: ["server","client"]
authors: ["drganjoo"]
references: [smithy-rs#3573]
breaking: false
new_feature: true
bug_fix: false
---
Support for the [rpcv2Cbor](https://smithy.io/2.0/additional-specs/protocols/smithy-rpc-v2.html) protocol has been added, allowing services to serialize RPC payloads as CBOR (Concise Binary Object Representation), improving performance and efficiency in data transmission.
+2 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import org.junit.jupiter.params.provider.ArgumentsSource
import software.amazon.smithy.rust.codegen.client.testutil.clientIntegrationTest
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamTestModels
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamUnmarshallTestCases.generateRustPayloadInitializer
import software.amazon.smithy.rust.codegen.core.testutil.EventStreamUnmarshallTestCases.writeUnmarshallTestCases
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
import software.amazon.smithy.rust.codegen.core.testutil.testModule
@@ -46,7 +47,7 @@ class ClientEventStreamUnmarshallerGeneratorTest {
                        "exception",
                        "UnmodeledError",
                        "${testCase.responseContentType}",
                        br#"${testCase.validUnmodeledError}"#
                        ${testCase.generateRustPayloadInitializer(testCase.validUnmodeledError)}
                    );
                    let result = $generator::new().unmarshall(&message);
                    assert!(result.is_ok(), "expected ok, got: {:?}", result);
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ dependencies {
    implementation("org.jsoup:jsoup:1.16.2")
    api("software.amazon.smithy:smithy-codegen-core:$smithyVersion")
    api("com.moandjiezana.toml:toml4j:0.7.2")
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.13.0")
    implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
    implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
    implementation("software.amazon.smithy:smithy-waiters:$smithyVersion")
+17 −2
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ToShapeId
import software.amazon.smithy.model.traits.HttpTrait
import software.amazon.smithy.model.traits.TimestampFormatTrait
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
@@ -140,9 +141,23 @@ open class RpcV2Cbor(val codegenContext: CodegenContext) : Protocol {
    override fun parseHttpErrorMetadata(operationShape: OperationShape): RuntimeType =
        RuntimeType.cborErrors(runtimeConfig).resolve("parse_error_metadata")

    // TODO(https://github.com/smithy-lang/smithy-rs/issues/3573)
    override fun parseEventStreamErrorMetadata(operationShape: OperationShape): RuntimeType =
        TODO("rpcv2Cbor event streams have not yet been implemented")
        ProtocolFunctions.crossOperationFn("parse_event_stream_error_metadata") { fnName ->
            rustTemplate(
                """
                pub fn $fnName(payload: &#{Bytes}) -> Result<#{ErrorMetadataBuilder}, #{DeserializeError}> {
                    #{cbor_errors}::parse_error_metadata(0, &#{Headers}::new(), payload)
                }
                """,
                "cbor_errors" to RuntimeType.cborErrors(runtimeConfig),
                "Bytes" to RuntimeType.Bytes,
                "ErrorMetadataBuilder" to RuntimeType.errorMetadataBuilder(runtimeConfig),
                "DeserializeError" to
                    CargoDependency.smithyCbor(runtimeConfig).toType()
                        .resolve("decode::DeserializeError"),
                "Headers" to RuntimeType.headers(runtimeConfig),
            )
        }

    // Unlike other protocols, the `rpcv2Cbor` protocol requires that `Content-Length` is always set
    // unless there is no input or if the operation is an event stream, see
+18 −2
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpBindingReso
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpLocation
import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolFunctions
import software.amazon.smithy.rust.codegen.core.util.PANIC
import software.amazon.smithy.rust.codegen.core.util.UNREACHABLE
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.core.util.inputShape
@@ -447,7 +446,24 @@ class CborParserGenerator(
    }

    override fun payloadParser(member: MemberShape): RuntimeType {
        UNREACHABLE("No protocol using CBOR serialization supports payload binding")
        val shape = model.expectShape(member.target)
        val returnSymbol = returnSymbolToParse(shape)
        check(shape is UnionShape || shape is StructureShape) {
            "Payload parser should only be used on structure and union shapes."
        }
        return protocolFunctions.deserializeFn(shape, fnNameSuffix = "payload") { fnName ->
            rustTemplate(
                """
                pub(crate) fn $fnName(value: &[u8]) -> #{Result}<#{ReturnType}, #{Error}> {
                    let decoder = &mut #{Decoder}::new(value);
                    #{DeserializeMember}
                }
                """,
                "ReturnType" to returnSymbol.symbol,
                "DeserializeMember" to deserializeMember(member),
                *codegenScope,
            )
        }
    }

    override fun operationParser(operationShape: OperationShape): RuntimeType? {
Loading