From 2e0e037e6bd339d9ba715c191013689f815fffd1 Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Fri, 15 Mar 2024 17:52:40 -0400 Subject: [PATCH] Fix issue where an empty union was treated as 'None' --- build.gradle.kts | 5 ++++- buildSrc/build.gradle.kts | 1 - .../smithy/protocols/parse/JsonParserGenerator.kt | 12 +++++++++++- .../protocols/parse/JsonParserGeneratorTest.kt | 9 +++++++++ gradle.properties | 1 + 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 6853712902..20f2d9e400 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,8 +15,11 @@ buildscript { } allprojects { + val allowLocalDeps: String by project repositories { - mavenLocal() + if (allowLocalDeps.toBoolean()) { + mavenLocal() + } mavenCentral() google() } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 104ba2680d..c032bfc8f8 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -12,7 +12,6 @@ plugins { repositories { mavenCentral() google() - mavenLocal() } // Load properties manually to avoid hard coding smithy version diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt index 36b0286e13..4a8b8a1054 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGenerator.kt @@ -565,7 +565,7 @@ class JsonParserGenerator( rustTemplate( """ if let #{Some}(#{Ok}(#{Token}::ValueNull { .. })) = tokens.peek() { - #{skip_value}(tokens)?; + let _ = tokens.next().expect("peek returned a token")?; continue; } """, @@ -633,6 +633,16 @@ class JsonParserGenerator( *codegenScope, ) } + // If we've gotten to the point where the union had a `{ ... }` section, we can't return None + // anymore. If we didn't parse a union at this point, this is an error. + rustTemplate( + """ + if variant.is_none() { + return Err(#{Error}::custom("Union did not contain a valid variant.")) + } + """, + *codegenScope, + ) rust("Ok(variant)") } } diff --git a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt index dabc155b75..8e7985a27a 100644 --- a/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt +++ b/codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/JsonParserGeneratorTest.kt @@ -194,6 +194,15 @@ class JsonParserGeneratorTest { """, ) + unitTest( + "all_variants_null", + """ + // __type field should be ignored during deserialization + let input = br#"{ "top": { "choice": { "blob": null, "boolean": null, "int": null, "long": null, "__type": "value-should-be-ignored-anyway" } } }"#; + let _err = ${format(operationGenerator)}(input, test_output::OpOutput::builder()).expect_err("invalid union"); + """, + ) + unitTest( "empty_error", """ diff --git a/gradle.properties b/gradle.properties index d5e5184bdb..e198984afd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,6 +22,7 @@ kotlin.code.style=official # codegen smithyGradlePluginVersion=0.9.0 smithyVersion=1.45.0 +allowLocalDeps=false # kotlin kotlinVersion=1.9.20