Skip to content

Commit 35a9099

Browse files
authored
Merge branch 'main' into fahadzub/cbor-constraint
2 parents fcf670d + d8fbf47 commit 35a9099

File tree

22 files changed

+187
-99
lines changed

22 files changed

+187
-99
lines changed

.changelog/4106176.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
applies_to: ["server"]
3+
authors: ["drganjoo"]
4+
references: []
5+
breaking: true
6+
new_feature: false
7+
bug_fix: false
8+
---
9+
The generated crates no longer have the `aws-lambda` feature flag enabled by default. This prevents the [aws-lambda](https://docs.rs/crate/aws-smithy-http-server/0.63.3/features#aws-lambda) feature from being automatically enabled in [aws-smithy-http-server](https://docs.rs/aws-smithy-http-server/0.63.3/aws_smithy_http_server/) when the SDK is not intended for AWS Lambda.

.changelog/9278363.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
applies_to: ["server"]
3+
authors: ["drganjoo"]
4+
references: []
5+
breaking: false
6+
new_feature: true
7+
bug_fix: false
8+
---
9+
All relevant types from [aws-smithy-http-server](https://docs.rs/aws-smithy-http-server/0.63.3/aws_smithy_http_server/) are now re-exported within the generated crates. This removes the need to explicitly depend on [aws-smithy-http-server](https://docs.rs/aws-smithy-http-server/0.63.3/aws_smithy_http_server/) in service handler code and prevents compilation errors caused by version mismatches.
10+

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustModule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ sealed class RustModule {
8282
parent: RustModule = LibRs,
8383
documentationOverride: String? = null,
8484
additionalAttributes: List<Attribute> = emptyList(),
85+
inline: Boolean = false,
8586
): LeafModule =
8687
new(
8788
name,
8889
visibility = Visibility.PUBLIC,
89-
inline = false,
90+
inline = inline,
9091
parent = parent,
9192
documentationOverride = documentationOverride,
9293
additionalAttributes = additionalAttributes,

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRustModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ object ServerRustModule {
4141
val Input = RustModule.public("input")
4242
val Output = RustModule.public("output")
4343
val Types = RustModule.public("types")
44-
val Server = RustModule.public("server")
4544
val Service = RustModule.private("service")
45+
val Server = RustModule.public("server", inline = true)
4646

4747
val UnconstrainedModule =
4848
software.amazon.smithy.rust.codegen.core.smithy.UnconstrainedModule

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/ServerRequiredCustomizations.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,19 @@ class ServerRequiredCustomizations : ServerCodegenDecorator {
5151
rustCrate.mergeFeature(
5252
Feature(
5353
"aws-lambda",
54-
true,
54+
false,
5555
listOf("aws-smithy-http-server/aws-lambda"),
5656
),
5757
)
5858

59+
rustCrate.mergeFeature(
60+
Feature(
61+
"request-id",
62+
true,
63+
listOf("aws-smithy-http-server/request-id"),
64+
),
65+
)
66+
5967
rustCrate.withModule(ServerRustModule.Types) {
6068
pubUseSmithyPrimitives(codegenContext, codegenContext.model, rustCrate)(this)
6169
rustTemplate(

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ScopeMacroGenerator.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@ import software.amazon.smithy.rust.codegen.core.rustlang.Writable
1111
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
1212
import software.amazon.smithy.rust.codegen.core.rustlang.writable
1313
import software.amazon.smithy.rust.codegen.core.util.toPascalCase
14-
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency
1514
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
1615

1716
class ScopeMacroGenerator(
1817
private val codegenContext: ServerCodegenContext,
1918
) {
20-
private val runtimeConfig = codegenContext.runtimeConfig
21-
private val codegenScope =
22-
arrayOf(
23-
"SmithyHttpServer" to ServerCargoDependency.smithyHttpServer(runtimeConfig).toType(),
24-
)
25-
2619
/** Calculate all `operationShape`s contained within the `ServiceShape`. */
2720
private val index = TopDownIndex.of(codegenContext.model)
2821
private val operations = index.getContainedOperations(codegenContext.serviceShape).toSortedSet(compareBy { it.id })
@@ -37,7 +30,7 @@ class ScopeMacroGenerator(
3730

3831
// When writing `macro_rules!` we add whitespace between `$` and the arguments to avoid Kotlin templating.
3932

40-
// To acheive the desired API we need to calculate the set theoretic complement `B \ A`.
33+
// To achieve the desired API we need to calculate the set theoretic complement `B \ A`.
4134
// The macro below, for rules prefixed with `@`, encodes a state machine which performs this.
4235
// The initial state is `(A) () (B)`, where `A` and `B` are lists of elements of `A` and `B`.
4336
// The rules, in order:
@@ -87,9 +80,9 @@ class ScopeMacroGenerator(
8780

8881
rustTemplate(
8982
"""
90-
/// A macro to help with scoping [plugins](#{SmithyHttpServer}::plugin) to a subset of all operations.
83+
/// A macro to help with scoping [plugins](crate::server::plugin) to a subset of all operations.
9184
///
92-
/// In contrast to [`aws_smithy_http_server::scope`](#{SmithyHttpServer}::scope), this macro has knowledge
85+
/// In contrast to [`crate::server::scope`](crate::server::scope), this macro has knowledge
9386
/// of the service and any operations _not_ specified will be placed in the opposing group.
9487
///
9588
/// ## Example
@@ -109,7 +102,7 @@ class ScopeMacroGenerator(
109102
/// }
110103
/// }
111104
///
112-
/// ## use #{SmithyHttpServer}::plugin::{Plugin, Scoped};
105+
/// ## use $crateName::server::plugin::{Plugin, Scoped};
113106
/// ## use $crateName::scope;
114107
/// ## struct MockPlugin;
115108
/// ## impl<S, Op, T> Plugin<S, Op, T> for MockPlugin { type Output = u32; fn apply(&self, input: T) -> u32 { 3 } }
@@ -125,13 +118,13 @@ class ScopeMacroGenerator(
125118
// Completed, render impls
126119
(@ $ name: ident, $ contains: ident () ($($ temp: ident)*) ($($ not_member: ident)*)) => {
127120
$(
128-
impl #{SmithyHttpServer}::plugin::scoped::Membership<$ temp> for $ name {
129-
type Contains = #{SmithyHttpServer}::plugin::scoped::$ contains;
121+
impl $ crate::server::plugin::scoped::Membership<$ temp> for $ name {
122+
type Contains = $ crate::server::plugin::scoped::$ contains;
130123
}
131124
)*
132125
$(
133-
impl #{SmithyHttpServer}::plugin::scoped::Membership<$ not_member> for $ name {
134-
type Contains = #{SmithyHttpServer}::plugin::scoped::$ contains;
126+
impl $ crate::server::plugin::scoped::Membership<$ not_member> for $ name {
127+
type Contains = $ crate::server::plugin::scoped::$ contains;
135128
}
136129
)*
137130
};
@@ -147,7 +140,7 @@ class ScopeMacroGenerator(
147140
}
148141
) => {
149142
use $ crate::operation_shape::*;
150-
#{SmithyHttpServer}::scope! {
143+
$ crate::server::scope! {
151144
$(##[$ attrs])*
152145
$ vis struct $ name {
153146
includes: [$($ include),*],
@@ -164,7 +157,7 @@ class ScopeMacroGenerator(
164157
) => {
165158
use $ crate::operation_shape::*;
166159
167-
#{SmithyHttpServer}::scope! {
160+
$ crate::server::scope! {
168161
$(##[$ attrs])*
169162
$ vis struct $ name {
170163
includes: [],
@@ -175,7 +168,6 @@ class ScopeMacroGenerator(
175168
};
176169
}
177170
""",
178-
*codegenScope,
179171
"FurtherTests" to furtherTests,
180172
)
181173
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerRootGenerator.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ open class ServerRootGenerator(
6969
//! ## Using $serviceName
7070
//!
7171
//! The primary entrypoint is [`$serviceName`]: it satisfies the [`Service<http::Request, Response = http::Response>`](#{Tower}::Service)
72-
//! trait and therefore can be handed to a [`hyper` server](https://github.com/hyperium/hyper) via [`$serviceName::into_make_service`] or used in Lambda via [`LambdaHandler`](#{SmithyHttpServer}::routing::LambdaHandler).
72+
//! trait and therefore can be handed to a [`hyper` server](https://github.com/hyperium/hyper) via [`$serviceName::into_make_service`]
73+
//! or used in AWS Lambda
74+
##![cfg_attr(
75+
feature = "aws-lambda",
76+
doc = " via [`LambdaHandler`](crate::server::routing::LambdaHandler).")]
77+
##![cfg_attr(
78+
not(feature = "aws-lambda"),
79+
doc = " by enabling the `aws-lambda` feature flag and utilizing the `LambdaHandler`.")]
7380
//! The [`crate::${InputModule.name}`], ${if (!hasErrors) "and " else ""}[`crate::${OutputModule.name}`], ${if (hasErrors) "and [`crate::${ErrorModule.name}`]" else "" }
7481
//! modules provide the types used in each operation.
7582
//!
@@ -93,10 +100,8 @@ open class ServerRootGenerator(
93100
//!
94101
//! ###### Running on Lambda
95102
//!
96-
//! This requires the `aws-lambda` feature flag to be passed to the [`#{SmithyHttpServer}`] crate.
97-
//!
98103
//! ```rust,ignore
99-
//! use #{SmithyHttpServer}::routing::LambdaHandler;
104+
//! use $crateName::server::routing::LambdaHandler;
100105
//! use $crateName::$serviceName;
101106
//!
102107
//! ## async fn dummy() {
@@ -120,10 +125,10 @@ open class ServerRootGenerator(
120125
//! Plugins allow you to build middleware which is aware of the operation it is being applied to.
121126
//!
122127
//! ```rust,no_run
123-
//! ## use #{SmithyHttpServer}::plugin::IdentityPlugin as LoggingPlugin;
124-
//! ## use #{SmithyHttpServer}::plugin::IdentityPlugin as MetricsPlugin;
128+
//! ## use $crateName::server::plugin::IdentityPlugin as LoggingPlugin;
129+
//! ## use $crateName::server::plugin::IdentityPlugin as MetricsPlugin;
125130
//! ## use #{Hyper}::Body;
126-
//! use #{SmithyHttpServer}::plugin::HttpPlugins;
131+
//! use $crateName::server::plugin::HttpPlugins;
127132
//! use $crateName::{$serviceName, ${serviceName}Config, $builderName};
128133
//!
129134
//! let http_plugins = HttpPlugins::new()
@@ -133,14 +138,14 @@ open class ServerRootGenerator(
133138
//! let builder: $builderName<Body, _, _, _> = $serviceName::builder(config);
134139
//! ```
135140
//!
136-
//! Check out [`#{SmithyHttpServer}::plugin`] to learn more about plugins.
141+
//! Check out [`crate::server::plugin`] to learn more about plugins.
137142
//!
138143
//! #### Handlers
139144
//!
140145
//! [`$builderName`] provides a setter method for each operation in your Smithy model. The setter methods expect an async function as input, matching the signature for the corresponding operation in your Smithy model.
141146
//! We call these async functions **handlers**. This is where your application business logic lives.
142147
//!
143-
//! Every handler must take an `Input`, and optional [`extractor arguments`](#{SmithyHttpServer}::request), while returning:
148+
//! Every handler must take an `Input`, and optional [`extractor arguments`](crate::server::request), while returning:
144149
//!
145150
//! * A `Result<Output, Error>` if your operation has modeled errors, or
146151
//! * An `Output` otherwise.
@@ -162,7 +167,7 @@ open class ServerRootGenerator(
162167
//! ## struct Error;
163168
//! ## struct State;
164169
//! ## use std::net::SocketAddr;
165-
//! use #{SmithyHttpServer}::request::{extension::Extension, connect_info::ConnectInfo};
170+
//! use $crateName::server::request::{extension::Extension, connect_info::ConnectInfo};
166171
//!
167172
//! async fn handler_with_no_extensions(input: Input) -> Output {
168173
//! todo!()
@@ -181,7 +186,7 @@ open class ServerRootGenerator(
181186
//! }
182187
//! ```
183188
//!
184-
//! See the [`operation module`](#{SmithyHttpServer}::operation) for information on precisely what constitutes a handler.
189+
//! See the [`operation module`](crate::operation) for information on precisely what constitutes a handler.
185190
//!
186191
//! #### Build
187192
//!
@@ -233,7 +238,6 @@ open class ServerRootGenerator(
233238
"HandlerImports" to handlerImports(crateName, operations, commentToken = "//!"),
234239
"Handlers" to handlers,
235240
"ExampleHandler" to operations.take(1).map { operation -> DocHandlerGenerator(codegenContext, operation, builderFieldNames[operation]!!, "//!").docSignature() },
236-
"SmithyHttpServer" to ServerCargoDependency.smithyHttpServer(codegenContext.runtimeConfig).toType(),
237241
"Hyper" to ServerCargoDependency.HyperDev.toType(),
238242
"Tokio" to ServerCargoDependency.TokioDev.toType(),
239243
"Tower" to ServerCargoDependency.Tower.toType(),

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerRuntimeTypesReExportsGenerator.kt

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,8 @@ class ServerRuntimeTypesReExportsGenerator(
2222
fun render(writer: RustWriter) {
2323
writer.rustTemplate(
2424
"""
25-
pub mod body {
26-
pub use #{SmithyHttpServer}::body::BoxBody;
27-
}
28-
pub mod operation {
29-
pub use #{SmithyHttpServer}::operation::OperationShape;
30-
}
31-
pub mod plugin {
32-
pub use #{SmithyHttpServer}::plugin::HttpPlugins;
33-
pub use #{SmithyHttpServer}::plugin::ModelPlugins;
34-
pub use #{SmithyHttpServer}::plugin::HttpMarker;
35-
pub use #{SmithyHttpServer}::plugin::ModelMarker;
36-
pub use #{SmithyHttpServer}::plugin::Plugin;
37-
pub use #{SmithyHttpServer}::plugin::PluginStack;
38-
}
39-
pub mod request {
40-
pub use #{SmithyHttpServer}::request::FromParts;
41-
42-
##[cfg(feature = "aws-lambda")]
43-
pub mod lambda {
44-
pub use #{SmithyHttpServer}::request::lambda::Context;
45-
}
46-
}
47-
pub mod response {
48-
pub use #{SmithyHttpServer}::response::IntoResponse;
49-
}
50-
pub mod routing {
51-
pub use #{SmithyHttpServer}::routing::IntoMakeService;
52-
pub use #{SmithyHttpServer}::routing::IntoMakeServiceWithConnectInfo;
53-
pub use #{SmithyHttpServer}::routing::Router;
54-
55-
##[cfg(feature = "aws-lambda")]
56-
pub use #{SmithyHttpServer}::routing::LambdaHandler;
57-
}
58-
59-
pub use #{SmithyHttpServer}::instrumentation;
60-
pub use #{SmithyHttpServer}::protocol;
61-
62-
pub use #{SmithyHttpServer}::Extension;
25+
// Re-export all types from the `aws-smithy-http-server` crate.
26+
pub use #{SmithyHttpServer}::*;
6327
""",
6428
*codegenScope,
6529
)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.rust.codegen.server.smithy
6+
7+
import org.junit.jupiter.api.Test
8+
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
9+
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
10+
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
11+
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
12+
import software.amazon.smithy.rust.codegen.core.testutil.testModule
13+
import software.amazon.smithy.rust.codegen.core.testutil.unitTest
14+
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest
15+
16+
class ServerTypesReExportTest {
17+
private val sampleModel =
18+
"""
19+
namespace amazon
20+
use aws.protocols#restJson1
21+
22+
@restJson1
23+
service SampleService {
24+
operations: [SampleOperation]
25+
}
26+
@http(uri: "/sample", method: "GET")
27+
operation SampleOperation {
28+
output := {}
29+
}
30+
""".asSmithyModel(smithyVersion = "2")
31+
32+
@Test
33+
fun `ensure types are exported from aws-smithy-http-server`() {
34+
serverIntegrationTest(sampleModel, IntegrationTestParams(service = "amazon#SampleService")) { _, rustCrate ->
35+
rustCrate.testModule {
36+
fun Set<String>.generateUseStatements(prefix: String) =
37+
this.joinToString(separator = "\n") {
38+
"#[allow(unused_imports)] use $prefix::$it;"
39+
}
40+
41+
// Ensure all types that were exported before version 0.64 and used
42+
// under the `{generated_sdk_crate_name}::server` namespace remain available.
43+
// Additionally, include all types requested by customers.
44+
unitTest(
45+
"types_exists_in_server_module",
46+
setOf(
47+
"extension::{OperationExtensionExt, OperationExtension}",
48+
"plugin::Scoped",
49+
"routing::{Route, RoutingService}",
50+
"body::boxed",
51+
"shape_id::ShapeId",
52+
"body::BoxBody",
53+
"operation::OperationShape",
54+
"plugin::HttpPlugins",
55+
"plugin::ModelPlugins",
56+
"plugin::HttpMarker",
57+
"plugin::ModelMarker",
58+
"plugin::Plugin",
59+
"plugin::PluginStack",
60+
"request::{self, FromParts}",
61+
"response::IntoResponse",
62+
"routing::IntoMakeService",
63+
"routing::IntoMakeServiceWithConnectInfo",
64+
"routing::Router",
65+
"instrumentation",
66+
"protocol",
67+
"Extension",
68+
"scope",
69+
).generateUseStatements("crate::server"),
70+
)
71+
72+
unitTest(
73+
"request_id_reexports",
74+
additionalAttributes = listOf(Attribute.featureGate("request-id")),
75+
) {
76+
rustTemplate(
77+
"""
78+
##[allow(unused_imports)] use crate::server::request::request_id::ServerRequestId;
79+
""",
80+
)
81+
}
82+
83+
unitTest(
84+
"aws_lambda_reexports",
85+
additionalAttributes = listOf(Attribute.featureGate("aws-lambda")),
86+
) {
87+
rustTemplate(
88+
"""
89+
##[allow(unused_imports)] use crate::server::{request::lambda::Context, routing::LambdaHandler};
90+
""",
91+
)
92+
}
93+
}
94+
}
95+
}
96+
}

examples/pokemon-service-common/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ tower = "0.4"
1818
# Local paths
1919
aws-smithy-runtime = { path = "../../rust-runtime/aws-smithy-runtime", features = ["client", "connector-hyper-0-14-x"] }
2020
aws-smithy-runtime-api = { path = "../../rust-runtime/aws-smithy-runtime-api", features = ["client"] }
21-
aws-smithy-http-server = { path = "../../rust-runtime/aws-smithy-http-server" }
22-
pokemon-service-client = { path = "../pokemon-service-client" }
21+
pokemon-service-client = { path = "../pokemon-service-client/", features = [
22+
"behavior-version-latest",
23+
] }
2324
pokemon-service-server-sdk = { path = "../pokemon-service-server-sdk" }
2425

2526
[dev-dependencies]

0 commit comments

Comments
 (0)