Unverified Commit 835b2b6d authored by david-perez's avatar david-perez Committed by GitHub
Browse files

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._
parent 42751e5d
Loading
Loading
Loading
Loading
+13 −18
Original line number Diff line number Diff line
@@ -33,19 +33,7 @@ class ClientSerdeDecorator : ClientCodegenDecorator {
    override fun extras(
        codegenContext: ClientCodegenContext,
        rustCrate: RustCrate,
    ) {
        rustCrate.mergeFeature(SerdeFeature)
        val generator = SerializeImplGenerator(codegenContext)
        rustCrate.withModule(SerdeModule) {
            serializationRoots(codegenContext).forEach {
                generator.generateRootSerializerForShape(
                    it,
                )(this)
            }
            addDependency(SupportStructures.serializeRedacted().toSymbol())
            addDependency(SupportStructures.serializeUnredacted().toSymbol())
        }
    }
    ) = extrasCommon(codegenContext, rustCrate)
}

class ServerSerdeDecorator : ServerCodegenDecorator {
@@ -55,14 +43,21 @@ class ServerSerdeDecorator : ServerCodegenDecorator {
    override fun extras(
        codegenContext: ServerCodegenContext,
        rustCrate: RustCrate,
    ) = extrasCommon(codegenContext, rustCrate)
}

// Just a common function to keep things DRY.
private fun extrasCommon(
    codegenContext: CodegenContext,
    rustCrate: RustCrate,
) {
    val roots = serializationRoots(codegenContext)
    if (roots.isNotEmpty()) {
        rustCrate.mergeFeature(SerdeFeature)
        val generator = SerializeImplGenerator(codegenContext)
        rustCrate.withModule(SerdeModule) {
            serializationRoots(codegenContext).forEach {
                generator.generateRootSerializerForShape(
                    it,
                )(this)
            roots.forEach {
                generator.generateRootSerializerForShape(it)(this)
            }
            addDependency(SupportStructures.serializeRedacted().toSymbol())
            addDependency(SupportStructures.serializeUnredacted().toSymbol())
+30 −0
Original line number Diff line number Diff line
@@ -7,9 +7,13 @@ package software.amazon.smithy.rust.codegen.serde

import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.client.testutil.clientIntegrationTest
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.cfg
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.feature
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.CratesIo
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
@@ -231,6 +235,32 @@ class SerdeDecoratorTest {
        }
    }

    @Test
    fun `feature should not be added if trait is not used`() {
        val model =
            """
            namespace com.example
            use aws.protocols#awsJson1_0
            
            @awsJson1_0
            service MyService {
                operations: [MyOperation]
            }
            
            operation MyOperation { }
        """.asSmithyModel(smithyVersion = "2")

        val params =
            IntegrationTestParams(cargoCommand = "cargo test --all-features", service = "com.example#MyService")
        serverIntegrationTest(model, params = params) { _, crate ->
            crate.integrationTest("test_serde") {
                unitTest("fails_if_serde_feature_exists", additionalAttributes = listOf(Attribute(cfg(feature("serde"))))) {
                    rust("assert!(false);")
                }
            }
        }
    }

    @Test
    fun generateSerializersThatWorkServer() {
        serverIntegrationTest(simpleModel, params = params) { ctx, crate ->