Skip to content

Commit

Permalink
Fix issue where an empty union was treated as 'None'
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Mar 15, 2024
1 parent ca0c43d commit 2e0e037
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ buildscript {
}

allprojects {
val allowLocalDeps: String by project
repositories {
mavenLocal()
if (allowLocalDeps.toBoolean()) {
mavenLocal()
}
mavenCentral()
google()
}
Expand Down
1 change: 0 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ plugins {
repositories {
mavenCentral()
google()
mavenLocal()
}

// Load properties manually to avoid hard coding smithy version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
""",
Expand Down Expand Up @@ -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)")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"""
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ kotlin.code.style=official
# codegen
smithyGradlePluginVersion=0.9.0
smithyVersion=1.45.0
allowLocalDeps=false

# kotlin
kotlinVersion=1.9.20
Expand Down

0 comments on commit 2e0e037

Please sign in to comment.