Skip to content

Commit 835b2b6

Browse files
authored
Don't add serde feature unnecessarily (#3891)
If the model is not using the `@smithy.rust#serde` trait, there's no need for the code-generated crate to have a `serde` feature. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 42751e5 commit 835b2b6

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

codegen-serde/src/main/kotlin/software/amazon/smithy/rust/codegen/serde/SerdeDecorator.kt

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,7 @@ class ClientSerdeDecorator : ClientCodegenDecorator {
3333
override fun extras(
3434
codegenContext: ClientCodegenContext,
3535
rustCrate: RustCrate,
36-
) {
37-
rustCrate.mergeFeature(SerdeFeature)
38-
val generator = SerializeImplGenerator(codegenContext)
39-
rustCrate.withModule(SerdeModule) {
40-
serializationRoots(codegenContext).forEach {
41-
generator.generateRootSerializerForShape(
42-
it,
43-
)(this)
44-
}
45-
addDependency(SupportStructures.serializeRedacted().toSymbol())
46-
addDependency(SupportStructures.serializeUnredacted().toSymbol())
47-
}
48-
}
36+
) = extrasCommon(codegenContext, rustCrate)
4937
}
5038

5139
class ServerSerdeDecorator : ServerCodegenDecorator {
@@ -55,14 +43,21 @@ class ServerSerdeDecorator : ServerCodegenDecorator {
5543
override fun extras(
5644
codegenContext: ServerCodegenContext,
5745
rustCrate: RustCrate,
58-
) {
46+
) = extrasCommon(codegenContext, rustCrate)
47+
}
48+
49+
// Just a common function to keep things DRY.
50+
private fun extrasCommon(
51+
codegenContext: CodegenContext,
52+
rustCrate: RustCrate,
53+
) {
54+
val roots = serializationRoots(codegenContext)
55+
if (roots.isNotEmpty()) {
5956
rustCrate.mergeFeature(SerdeFeature)
6057
val generator = SerializeImplGenerator(codegenContext)
6158
rustCrate.withModule(SerdeModule) {
62-
serializationRoots(codegenContext).forEach {
63-
generator.generateRootSerializerForShape(
64-
it,
65-
)(this)
59+
roots.forEach {
60+
generator.generateRootSerializerForShape(it)(this)
6661
}
6762
addDependency(SupportStructures.serializeRedacted().toSymbol())
6863
addDependency(SupportStructures.serializeUnredacted().toSymbol())

codegen-serde/src/test/kotlin/software/amazon/smithy/rust/codegen/serde/SerdeDecoratorTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ package software.amazon.smithy.rust.codegen.serde
77

88
import org.junit.jupiter.api.Test
99
import software.amazon.smithy.rust.codegen.client.testutil.clientIntegrationTest
10+
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
11+
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.cfg
12+
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.feature
1013
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
1114
import software.amazon.smithy.rust.codegen.core.rustlang.CratesIo
1215
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
16+
import software.amazon.smithy.rust.codegen.core.rustlang.rust
1317
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
1418
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
1519
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
@@ -231,6 +235,32 @@ class SerdeDecoratorTest {
231235
}
232236
}
233237

238+
@Test
239+
fun `feature should not be added if trait is not used`() {
240+
val model =
241+
"""
242+
namespace com.example
243+
use aws.protocols#awsJson1_0
244+
245+
@awsJson1_0
246+
service MyService {
247+
operations: [MyOperation]
248+
}
249+
250+
operation MyOperation { }
251+
""".asSmithyModel(smithyVersion = "2")
252+
253+
val params =
254+
IntegrationTestParams(cargoCommand = "cargo test --all-features", service = "com.example#MyService")
255+
serverIntegrationTest(model, params = params) { _, crate ->
256+
crate.integrationTest("test_serde") {
257+
unitTest("fails_if_serde_feature_exists", additionalAttributes = listOf(Attribute(cfg(feature("serde"))))) {
258+
rust("assert!(false);")
259+
}
260+
}
261+
}
262+
}
263+
234264
@Test
235265
fun generateSerializersThatWorkServer() {
236266
serverIntegrationTest(simpleModel, params = params) { ctx, crate ->

0 commit comments

Comments
 (0)