fix(bedrock-agentcore): use modeled tag constraint errors (#9193)

This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-11-22 14:44:51 -04:00
1 parent dda3b96dee
commit ee722e8da3
5 files changed
+114 -18

No files matched your search

@@ -6,6 +6,8 @@ import java.util.Map;
final class BedrockAgentCoreTagFixtures {
static final String EXPECTED_TAG_PATTERN = "[a-zA-Z0-9\\s._:/=+@-]*";
private BedrockAgentCoreTagFixtures() {
}
@@ -5,9 +5,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
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;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -26,23 +32,72 @@ class BedrockAgentCoreTagValidationTest {
BedrockAgentCoreTagValidation.validateTags(tags);
tags.put("extra", "value");
assertInvalid(tags, "tags must be an object with at most 50 entries");
assertInvalid(tags, "1 validation error detected: Value at 'tags' failed to satisfy constraint: "
+ "Member must have length less than or equal to 50");
}
@Test
void keepsAgentCoreKeyAndValueErrors() {
for (String key : List.of("", "k".repeat(129), "bad?key")) {
assertInvalid(MAPPER.createObjectNode().put(key, "value"),
"tag key does not satisfy AgentCore constraints");
}
for (String value : List.of("v".repeat(257), "bad?value")) {
assertInvalid(MAPPER.createObjectNode().put("key", value),
"tag value does not satisfy AgentCore constraints");
}
void keepsMalformedTagTypeErrors() {
assertInvalid(MAPPER.createObjectNode().put("key", 123), "tag value must be a string");
assertInvalid(MAPPER.createArrayNode(), "tags must be an object with at most 50 entries");
}
@ParameterizedTest
@MethodSource("modeledTagFailures")
void createTagRefusalsUseSmithyMessages(Map<String, String> tags, String message) {
assertInvalid(MAPPER.valueToTree(tags), message);
}
@ParameterizedTest
@MethodSource("modeledTagFailures")
void tagResourceRefusalsUseSmithyMessages(Map<String, String> tags, String message) {
AwsException error = assertThrows(AwsException.class,
() -> BedrockAgentCoreTagValidation.validateTagMap(tags));
assertEquals("ValidationException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
assertEquals(message, error.getMessage());
}
@Test
void preservesEmptyTagsAndModeledBoundariesOnBothPaths() {
Map<String, String> tags = new LinkedHashMap<>();
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTags(MAPPER.valueToTree(tags)));
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTagMap(tags));
tags.put(" ", "");
tags.put("k".repeat(128), "v".repeat(256));
for (int i = 0; i < 48; i++) {
tags.put("key-" + i, "value-" + i);
}
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTags(MAPPER.valueToTree(tags)));
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTagMap(tags));
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTags(null));
}
private static Stream<Arguments> modeledTagFailures() {
String prefix = "1 validation error detected: ";
String keyRules = "Value at 'tags' failed to satisfy constraint: Map keys must satisfy constraint: ["
+ "Member must have length less than or equal to 128, "
+ "Member must have length greater than or equal to 1, "
+ "Member must satisfy regular expression pattern: "
+ BedrockAgentCoreTagFixtures.EXPECTED_TAG_PATTERN + "]";
String valueRules = "Value at 'tags' failed to satisfy constraint: Map value must satisfy constraint: ["
+ "Member must have length less than or equal to 256, "
+ "Member must satisfy regular expression pattern: "
+ BedrockAgentCoreTagFixtures.EXPECTED_TAG_PATTERN + "]";
Map<String, String> tooMany = new LinkedHashMap<>();
for (int i = 0; i < 51; i++) {
tooMany.put("key-" + i, "value");
}
return Stream.of(
Arguments.of(Map.of("", "value"), prefix + keyRules),
Arguments.of(Map.of("k".repeat(129), "value"), prefix + keyRules),
Arguments.of(Map.of("bad?key", "value"), prefix + keyRules),
Arguments.of(Map.of("key", "v".repeat(257)), prefix + valueRules),
Arguments.of(Map.of("key", "bad?value"), prefix + valueRules),
Arguments.of(tooMany, prefix + "Value at 'tags' failed to satisfy constraint: "
+ "Member must have length less than or equal to 50"));
}
private static void assertInvalid(JsonNode tags, String message) {
AwsException error = assertThrows(AwsException.class,
() -> BedrockAgentCoreTagValidation.validateTags(tags));
@@ -906,7 +906,11 @@ class BedrockAgentCoreToolsIntegrationTest {
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("tag value does not satisfy AgentCore constraints"));
.body("message", equalTo("1 validation error detected: Value at 'tags' "
+ "failed to satisfy constraint: Map value must satisfy constraint: ["
+ "Member must have length less than or equal to 256, "
+ "Member must satisfy regular expression pattern: "
+ BedrockAgentCoreTagFixtures.EXPECTED_TAG_PATTERN + "]"));
given()
.contentType("application/json")