Skip to content

Commit

Permalink
Enable streaming support for AwsJson (#2207)
Browse files Browse the repository at this point in the history
* Enable streaming support for AwsJson

Signed-off-by: Daniele Ahmed <[email protected]>
  • Loading branch information
82marbag authored Jan 17, 2023
1 parent acffa7d commit 93f4c4f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions codegen-client-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
)
}

Expand Down
104 changes: 104 additions & 0 deletions codegen-core/common-test-models/pokemon-awsjson.smithy
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions codegen-server-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
)
}

Expand Down

0 comments on commit 93f4c4f

Please sign in to comment.