-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codegen flag 'replaceInvalidUtf8' to allow server SDK to replace …
…invalid UTF-8 characters with �
- Loading branch information
Fahad Zubair
committed
Feb 4, 2025
1 parent
b36447e
commit 8a00258
Showing
8 changed files
with
300 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
applies_to: | ||
- server | ||
authors: | ||
- drganjoo | ||
references: [] | ||
breaking: false | ||
new_feature: true | ||
bug_fix: false | ||
--- | ||
Allow invalid UTF-8 characters to be replaced with � |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...c/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ReplaceInvalidUtf8Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package software.amazon.smithy.rust.codegen.server.smithy | ||
|
||
import org.junit.jupiter.api.Test | ||
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.ServerAdditionalSettings | ||
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel | ||
import software.amazon.smithy.rust.codegen.core.testutil.testModule | ||
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest | ||
|
||
internal class ReplaceInvalidUtf8Test { | ||
val model = | ||
""" | ||
namespace test | ||
use aws.protocols#restJson1 | ||
@restJson1 | ||
service SampleService { | ||
operations: [SampleOperation] | ||
} | ||
@http(uri: "/operation", method: "PUT") | ||
operation SampleOperation { | ||
input := { | ||
x : String | ||
} | ||
} | ||
""".asSmithyModel(smithyVersion = "2") | ||
|
||
@Test | ||
fun `invalid utf8 should be replaced if the codegen flag is set`() { | ||
serverIntegrationTest( | ||
model, | ||
IntegrationTestParams( | ||
additionalSettings = | ||
ServerAdditionalSettings | ||
.builder() | ||
.replaceInvalidUtf8(true) | ||
.toObjectNode(), | ||
), | ||
) { _, rustCrate -> | ||
rustCrate.testModule { | ||
rustTemplate( | ||
""" | ||
##[tokio::test] | ||
async fn test_utf8_replaced() { | ||
let body = r##"{ "x" : "\ud800" }"##; | ||
let request = http::Request::builder() | ||
.method("POST") | ||
.uri("/operation") | ||
.header("content-type", "application/json") | ||
.body(hyper::Body::from(body)) | ||
.expect("failed to build request"); | ||
let result = crate::protocol_serde::shape_sample_operation::de_sample_operation_http_request(request).await; | ||
assert!( | ||
result.is_ok(), | ||
"Invalid utf8 should have been replaced. {result:?}" | ||
); | ||
assert_eq!( | ||
result.unwrap().x.unwrap(), | ||
"�", | ||
"payload should have been replaced with �." | ||
); | ||
} | ||
""", | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `invalid utf8 should be rejected if the codegen flag is not set`() { | ||
serverIntegrationTest( | ||
model, | ||
) { _, rustCrate -> | ||
rustCrate.testModule { | ||
rustTemplate( | ||
""" | ||
##[tokio::test] | ||
async fn test_invalid_utf8_raises_an_error() { | ||
let body = r##"{ "x" : "\ud800" }"##; | ||
let request = http::Request::builder() | ||
.method("POST") | ||
.uri("/operation") | ||
.header("content-type", "application/json") | ||
.body(hyper::Body::from(body)) | ||
.expect("failed to build request"); | ||
let result = crate::protocol_serde::shape_sample_operation::de_sample_operation_http_request(request).await; | ||
assert!( | ||
result.is_err(), | ||
"invalid utf8 characters should not be allowed by default {result:?}" | ||
); | ||
} | ||
""", | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.