fix(translate): serve Translate over smithy-rpc-v2-cbor (#8563)

This commit is contained in:
James Netherton authored and GitHub committed 2026-11-16 07:22:38 -04:00
1 parent e8f5a696a8
commit 8ec2c184f1
4 files changed
+121 -2

No files matched your search

@@ -2,11 +2,13 @@ package io.floci.aws.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import org.junit.jupiter.api.Test;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -284,4 +286,19 @@ class AwsJsonCborSerializerTest {
private static boolean contains(byte[] bytes, byte target) {
return indexOf(bytes, target) >= 0;
}
@Test
void binaryNodeIsWrittenAsCborByteStringInBothDialects() throws Exception {
byte[] blob = {0x00, 0x01, (byte) 0xFF, 'a', 'b'};
ObjectNode node = jsonMapper.createObjectNode();
node.putObject("TranslatedDocument").put("Content", blob);
for (byte[] encoded : new byte[][] {
AwsJsonCborController.nodeToSmithyCbor(node),
AwsJsonCborController.nodeToLegacyCbor(node)}) {
JsonNode content = cborMapper.readTree(encoded).path("TranslatedDocument").path("Content");
assertTrue(content.isBinary(), "blob must be a CBOR byte string (major type 2), not text");
assertArrayEquals(blob, content.binaryValue());
}
}
}
@@ -2,6 +2,7 @@ package io.floci.aws.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
@@ -186,4 +187,57 @@ class SmithyRpcV2RoutingIntegrationTest {
.statusCode(200)
.contentType("application/x-amz-cbor-1.1");
}
@Test
void translateRoutesViaTargetPrefixServiceName() throws Exception {
byte[] response = given()
.contentType("application/cbor")
.accept("application/cbor")
.header("smithy-protocol", "rpc-v2-cbor")
.body(CBOR_MAPPER.writeValueAsBytes(JSON_MAPPER.readTree("""
{"Text":"Ciao","SourceLanguageCode":"it","TargetLanguageCode":"de"}
""")))
.when()
.post("/service/AWSShineFrontendService_20170701/operation/TranslateText")
.then()
.statusCode(200)
.header("smithy-protocol", equalTo("rpc-v2-cbor"))
.contentType("application/cbor")
.extract().asByteArray();
assertThat(CBOR_MAPPER.readTree(response)
.path("TranslatedText").asText(), equalTo("Ciao"));
}
@Test
void translateDocumentRoundTripsContentAsCborByteString() throws Exception {
// Non-UTF-8 bytes included on purpose: Document.Content is a blob, not text.
byte[] document = {'H', 'e', 'l', 'l', 'o', ' ', (byte) 0xC3, (byte) 0xA9, 0x00, (byte) 0xFF, '\n'};
ObjectNode request = JSON_MAPPER.createObjectNode();
request.putObject("Document")
.put("Content", document)
.put("ContentType", "text/plain");
request.put("SourceLanguageCode", "en");
request.put("TargetLanguageCode", "es");
byte[] response = given()
.contentType("application/cbor")
.accept("application/cbor")
.header("smithy-protocol", "rpc-v2-cbor")
.body(CBOR_MAPPER.writeValueAsBytes(request))
.when()
.post("/service/AWSShineFrontendService_20170701/operation/TranslateDocument")
.then()
.statusCode(200)
.header("smithy-protocol", equalTo("rpc-v2-cbor"))
.contentType("application/cbor")
.extract().asByteArray();
JsonNode decoded = CBOR_MAPPER.readTree(response);
JsonNode content = decoded.path("TranslatedDocument").path("Content");
assertThat(content.isBinary(), equalTo(true));
assertThat(content.binaryValue(), equalTo(document));
assertThat(decoded.path("SourceLanguageCode").asText(), equalTo("en"));
assertThat(decoded.path("TargetLanguageCode").asText(), equalTo("es"));
}
}