diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt index acee524ba0..96f4bdd020 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypes.kt @@ -75,21 +75,18 @@ data class RuntimeConfig( /** * Load a `RuntimeConfig` from an [ObjectNode] (JSON) */ - fun fromNode(node: Optional): RuntimeConfig { - return if (node.isPresent) { - val crateVersionMap = node.get().getObjectMember("versions").orElse(Node.objectNode()).members.entries.let { members -> - val map = members.associate { it.key.toString() to it.value.expectStringNode().value } - CrateVersionMap(map) - } - val path = node.get().getStringMember("relativePath").orNull()?.value - val runtimeCrateLocation = RuntimeCrateLocation(path = path, versions = crateVersionMap) - RuntimeConfig( - node.get().getStringMemberOrDefault("cratePrefix", "aws-smithy"), - runtimeCrateLocation = runtimeCrateLocation, - ) - } else { - RuntimeConfig() + fun fromNode(maybeNode: Optional): RuntimeConfig { + val node = maybeNode.orElse(Node.objectNode()) + val crateVersionMap = node.getObjectMember("versions").orElse(Node.objectNode()).members.entries.let { members -> + val map = members.associate { it.key.toString() to it.value.expectStringNode().value } + CrateVersionMap(map) } + val path = node.getStringMember("relativePath").orNull()?.value + val runtimeCrateLocation = RuntimeCrateLocation(path = path, versions = crateVersionMap) + return RuntimeConfig( + node.getStringMemberOrDefault("cratePrefix", "aws-smithy"), + runtimeCrateLocation = runtimeCrateLocation, + ) } } diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypesTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypesTest.kt index 61e850993e..b6695a84a8 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypesTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/codegen/smithy/RuntimeTypesTest.kt @@ -6,6 +6,7 @@ package software.amazon.smithy.rust.codegen.smithy import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.MethodSource @@ -13,6 +14,7 @@ import software.amazon.smithy.model.node.Node import software.amazon.smithy.rust.codegen.rustlang.CratesIo import software.amazon.smithy.rust.codegen.rustlang.DependencyLocation import software.amazon.smithy.rust.codegen.rustlang.Local +import java.util.Optional class RuntimeTypesTest { @ParameterizedTest @@ -26,6 +28,13 @@ class RuntimeTypesTest { cfg.runtimeCrateLocation shouldBe expectedCrateLocation } + @Test + fun `succeeded to provide a default runtime config if missing`() { + // This default config should share the same behaviour with `{}` empty object. + val cfg = RuntimeConfig.fromNode(Optional.empty()) + cfg.runtimeCrateLocation shouldBe RuntimeCrateLocation(null, CrateVersionMap(mapOf())) + } + @ParameterizedTest @MethodSource("runtimeCrateLocationProvider") fun `runtimeCrateLocation provides dependency location`(