From 93f4c4f0548aa6d5f6e70dab3631be5f153df86c Mon Sep 17 00:00:00 2001 From: 82marbag <69267416+82marbag@users.noreply.github.com> Date: Tue, 17 Jan 2023 09:26:50 -0500 Subject: [PATCH] Enable streaming support for AwsJson (#2207) * Enable streaming support for AwsJson Signed-off-by: Daniele Ahmed --- codegen-client-test/build.gradle.kts | 1 + .../common-test-models/pokemon-awsjson.smithy | 104 ++++++++++++++++++ .../codegen/core/smithy/protocols/AwsJson.kt | 9 +- codegen-server-test/build.gradle.kts | 5 + 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 codegen-core/common-test-models/pokemon-awsjson.smithy diff --git a/codegen-client-test/build.gradle.kts b/codegen-client-test/build.gradle.kts index f961f08b47..434bcef65f 100644 --- a/codegen-client-test/build.gradle.kts +++ b/codegen-client-test/build.gradle.kts @@ -89,6 +89,7 @@ val allCodegenTests = "../codegen-core/common-test-models".let { commonModels -> ), CodegenTest("aws.protocoltests.json#TestService", "endpoint-rules"), CodegenTest("com.aws.example.rust#PokemonService", "pokemon-service-client", imports = listOf("$commonModels/pokemon.smithy", "$commonModels/pokemon-common.smithy")), + CodegenTest("com.aws.example.rust#PokemonService", "pokemon-service-awsjson-client", imports = listOf("$commonModels/pokemon-awsjson.smithy", "$commonModels/pokemon-common.smithy")), ) } diff --git a/codegen-core/common-test-models/pokemon-awsjson.smithy b/codegen-core/common-test-models/pokemon-awsjson.smithy new file mode 100644 index 0000000000..4cae17cedc --- /dev/null +++ b/codegen-core/common-test-models/pokemon-awsjson.smithy @@ -0,0 +1,104 @@ +$version: "1.0" + +// TODO(https://github.com/awslabs/smithy-rs/issues/2215) +// This is a temporary model to test AwsJson 1.0 with @streaming. +// This model will be removed when protocol tests support @streaming. + +namespace com.aws.example.rust + +use aws.protocols#awsJson1_0 +use smithy.framework#ValidationException +use com.aws.example#PokemonSpecies +use com.aws.example#GetServerStatistics +use com.aws.example#DoNothing +use com.aws.example#CheckHealth + +/// The Pokémon Service allows you to retrieve information about Pokémon species. +@title("Pokémon Service") +@awsJson1_0 +service PokemonService { + version: "2021-12-01", + operations: [ + GetServerStatistics, + DoNothing, + CapturePokemon, + CheckHealth + ], +} + +/// Capture Pokémons via event streams. +@http(uri: "/capture-pokemon-event/{region}", method: "POST") +operation CapturePokemon { + input: CapturePokemonEventsInput, + output: CapturePokemonEventsOutput, + errors: [UnsupportedRegionError, ThrottlingError, ValidationException] +} + +@input +structure CapturePokemonEventsInput { + @httpPayload + events: AttemptCapturingPokemonEvent, + + @httpLabel + @required + region: String, +} + +@output +structure CapturePokemonEventsOutput { + @httpPayload + events: CapturePokemonEvents, +} + +@streaming +union AttemptCapturingPokemonEvent { + event: CapturingEvent, + masterball_unsuccessful: MasterBallUnsuccessful, +} + +structure CapturingEvent { + @eventPayload + payload: CapturingPayload, +} + +structure CapturingPayload { + name: String, + pokeball: String, +} + +@streaming +union CapturePokemonEvents { + event: CaptureEvent, + invalid_pokeball: InvalidPokeballError, + throttlingError: ThrottlingError, +} + +structure CaptureEvent { + @eventHeader + name: String, + @eventHeader + captured: Boolean, + @eventHeader + shiny: Boolean, + @eventPayload + pokedex_update: Blob, +} + +@error("server") +structure UnsupportedRegionError { + @required + region: String, +} + +@error("client") +structure InvalidPokeballError { + @required + pokeball: String, +} +@error("server") +structure MasterBallUnsuccessful { + message: String, +} + +@error("client") +structure ThrottlingError {} diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/AwsJson.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/AwsJson.kt index 8c0ad26d88..b49314bebe 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/AwsJson.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/AwsJson.kt @@ -25,6 +25,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.protocols.parse.Structure import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.JsonSerializerGenerator import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.StructuredDataSerializerGenerator import software.amazon.smithy.rust.codegen.core.util.inputShape +import software.amazon.smithy.rust.codegen.core.util.isStreaming sealed class AwsJsonVersion { abstract val value: String @@ -50,7 +51,13 @@ class AwsJsonHttpBindingResolver( private fun bindings(shape: ToShapeId) = shape.let { model.expectShape(it.toShapeId()) }.members() - .map { HttpBindingDescriptor(it, HttpLocation.DOCUMENT, "document") } + .map { + if (it.isStreaming(model)) { + HttpBindingDescriptor(it, HttpLocation.PAYLOAD, "document") + } else { + HttpBindingDescriptor(it, HttpLocation.DOCUMENT, "document") + } + } .toList() override fun httpTrait(operationShape: OperationShape): HttpTrait = httpTrait diff --git a/codegen-server-test/build.gradle.kts b/codegen-server-test/build.gradle.kts index 82187c2db6..8e79429725 100644 --- a/codegen-server-test/build.gradle.kts +++ b/codegen-server-test/build.gradle.kts @@ -98,6 +98,11 @@ val allCodegenTests = "../codegen-core/common-test-models".let { commonModels -> "pokemon-service-server-sdk", imports = listOf("$commonModels/pokemon.smithy", "$commonModels/pokemon-common.smithy"), ), + CodegenTest( + "com.aws.example.rust#PokemonService", + "pokemon-service-awsjson-server-sdk", + imports = listOf("$commonModels/pokemon-awsjson.smithy", "$commonModels/pokemon-common.smithy"), + ), ) }