From 878f09f4709fee8bc99287f26e20796f38a0dbd7 Mon Sep 17 00:00:00 2001 From: Jimmy Liu Jia <91264074+JimmyLiuJia@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:13:09 +0800 Subject: [PATCH] fix: group CloudFormation string constraint violations (#8949) --- .../aws/core/common/RequestShapeModel.java | 4 -- .../provisioners/CfnPropertyTypes.java | 42 +++++++------ .../provisioners/CfnPropertyTypesTest.java | 60 +++++++++++++++++-- 3 files changed, 77 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/floci/aws/core/common/RequestShapeModel.java b/src/main/java/io/floci/aws/core/common/RequestShapeModel.java index 23f65aeb42..3ef45dcdef 100644 --- a/src/main/java/io/floci/aws/core/common/RequestShapeModel.java +++ b/src/main/java/io/floci/aws/core/common/RequestShapeModel.java @@ -27,10 +27,6 @@ public record RequestShapeModel(Map operations, Map members, String element, List required, Integer minLength, Integer maxLength, String pattern) { - - public Shape(String type, Map members, String element, List required) { - this(type, members, element, required, null, null, null); - } } /** The vendored resource {@code name}; {@code description} names it in a failure. */ diff --git a/src/main/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypes.java b/src/main/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypes.java index 273821d799..88bd8160f0 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypes.java +++ b/src/main/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypes.java @@ -87,6 +87,10 @@ final class CfnPropertyTypes { + "; add the type to tools/aws/regen_cfn_property_types.py"); } + private static boolean hasStringConstraints(Shape shape) { + return shape.minLength() != null || shape.maxLength() != null || shape.pattern() != null; + } + /** * Refuses a value of another type than {@code expected}, an object or a list, with template * validation's message, where a number or a boolean reads as a String. @@ -400,10 +404,10 @@ final class CfnPropertyTypes { * (#/ContainerDefinitions/0/Memory: expected type: Integer, found: String. This may occur ... * \n#/ContainerDefinitions/0/LinuxParameters/Capabilities/Add: expected type: JSONArray, found: * String)". At each object its missing required members come first, then its members in the - * schema's order. When every value converts, only the required members the converted model lacks - * fail it, grouped where their paths meet as the schema validator groups them: "Model validation - * failed (#/ContainerDefinitions: 2 schema violations found)\n#/ContainerDefinitions/0: required - * key [Name] not found (#/ContainerDefinitions/0)\n...". + * schema's order. When every value converts, missing required members and each failing string + * constraint are grouped where their paths meet as the schema validator groups them: "Model + * validation failed (#/ContainerDefinitions: 2 schema violations found)\n#/ContainerDefinitions/0: + * required key [Name] not found (#/ContainerDefinitions/0)\n...". */ static final class HandlerReader extends ShapeWalk { @@ -432,9 +436,9 @@ final class CfnPropertyTypes { if (unconverted) { throw modelValidation(String.join("\n", violations.stream().map(Violation::text).toList()) + ")"); } - List missing = violations.stream().filter(Violation::required).toList(); - if (!missing.isEmpty()) { - throw modelValidation(grouped(missing)); + List groupedViolations = violations.stream().filter(Violation::grouped).toList(); + if (!groupedViolations.isEmpty()) { + throw modelValidation(grouped(groupedViolations)); } return read; } @@ -478,19 +482,20 @@ final class CfnPropertyTypes { @Override void checkString(String path, Shape shape, String value) { - if (shape.minLength() == null && shape.maxLength() == null && shape.pattern() == null) { + if (!hasStringConstraints(shape)) { return; } int length = value.codePointCount(0, value.length()); if (shape.minLength() != null && length < shape.minLength()) { record(new Violation(path, "expected minLength: " + shape.minLength() + ", actual: " + length, - false), false); - } else if (shape.maxLength() != null && length > shape.maxLength()) { + true), true); + } + if (shape.maxLength() != null && length > shape.maxLength()) { record(new Violation(path, "expected maxLength: " + shape.maxLength() + ", actual: " + length, - false), false); - } else if (shape.pattern() != null && !Pattern.compile(shape.pattern()).matcher(value).matches()) { - record(new Violation(path, "failed validation constraint for keyword [pattern]", false), - false); + true), true); + } + if (shape.pattern() != null && !Pattern.compile(shape.pattern()).matcher(value).matches()) { + record(new Violation(path, "failed validation constraint for keyword [pattern]", true), true); } } @@ -500,13 +505,12 @@ final class CfnPropertyTypes { JsonNode raw = value.get(member); boolean absent = raw == null; if (!absent && !preserveEcsHandlerSemantics) { - absent = raw.isNull() || raw.isMissingNode(); + absent = raw.isNull(); Shape memberShape = model.shapes().get(shape.members().get(member)); if (!absent && "string".equals(memberShape.type())) { JsonNode resolved = engine.resolveNode(raw); absent = resolved == null || resolved.isNull() || resolved.isMissingNode(); - if (!absent && memberShape.minLength() == null && memberShape.maxLength() == null - && memberShape.pattern() == null) { + if (!absent && !hasStringConstraints(memberShape)) { absent = resolved.isTextual() && resolved.textValue().isBlank(); } } @@ -591,8 +595,8 @@ final class CfnPropertyTypes { return path.isEmpty() ? "#" : "#/" + path; } - /** One violation of the model at {@code path}, a missing required member or a type. */ - private record Violation(String path, String message, boolean required) { + /** One violation of the model at {@code path}; schema keyword failures use grouped output. */ + private record Violation(String path, String message, boolean grouped) { String text() { return pointer(path) + ": " + message; diff --git a/src/test/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypesTest.java b/src/test/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypesTest.java index 3fd934eade..7f1f3eeb0f 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypesTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/provisioners/CfnPropertyTypesTest.java @@ -295,8 +295,6 @@ class CfnPropertyTypesTest { "failed validation constraint for keyword [pattern]"), Arguments.of("SNS ARN is below schema minimum", "SnsDestination", "TopicARN", "arn:aws:sns:a:000000000000:x", "expected minLength: 36, actual: 28"), - Arguments.of("SNS ARN is blank", "SnsDestination", "TopicARN", "", - "expected minLength: 36, actual: 0"), Arguments.of("SNS ARN exceeds schema maximum", "SnsDestination", "TopicARN", snsPrefix + "x".repeat(1100 - snsPrefix.length()), "expected maxLength: 1024, actual: 1100"), @@ -305,6 +303,49 @@ class CfnPropertyTypesTest { "failed validation constraint for keyword [pattern]")); } + @Test + void handlerReaderGroupsEveryFailingConstraintForBlankTopicArn() { + String path = "#/EventDestination/SnsDestination/TopicARN"; + String properties = """ + {"ConfigurationSetName":"set","EventDestination":{"MatchingEventTypes":["SEND"], + "SnsDestination":{"TopicARN":""}}} + """; + + assertHandlerReaderFailure("AWS::SES::ConfigurationSetEventDestination", properties, + "Model validation failed (" + path + ": 2 schema violations found)\n" + + path + ": expected minLength: 36, actual: 0 (" + path + ")\n" + + path + ": failed validation constraint for keyword [pattern] (" + path + ")"); + } + + @Test + void handlerReaderGroupsRequiredAndStringConstraintViolations() { + assertHandlerReaderFailure("AWS::SES::ConfigurationSetEventDestination", """ + {"ConfigurationSetName":"set","EventDestination":{"SnsDestination":{"TopicARN":""}}} + """, """ + Model validation failed (#/EventDestination: 3 schema violations found) + #/EventDestination: required key [MatchingEventTypes] not found (#/EventDestination) + #/EventDestination/SnsDestination/TopicARN: 2 schema violations found (#/EventDestination/SnsDestination/TopicARN) + """.stripTrailing()); + } + + @Test + void handlerReaderChecksEveryStringConstraintAlongsideAnUnconvertedType() { + RequestShapeModel model = model(""" + {"operations":{"AWS::Test::Thing":"Root"},"shapes":{ + "Root":{"type":"structure","members":{"Name":"Name","Count":"Count"}}, + "Name":{"type":"string","minLength":2,"pattern":"[A-Z]+"}, + "Count":{"type":"integer"}}} + """); + + AwsException failure = assertThrows(AwsException.class, + () -> new CfnPropertyTypes.HandlerReader(engine(), model) + .read("AWS::Test::Thing", json("{\"Name\":\"\",\"Count\":\"not-a-number\"}"))); + + assertEquals("Model validation failed (#/Name: expected minLength: 2, actual: 0\n" + + "#/Name: failed validation constraint for keyword [pattern]\n" + + "#/Count: expected type: Integer, found: String" + HINT + ")", failure.getMessage()); + } + @ParameterizedTest(name = "{0}") @MethodSource("invalidSesDestinationStrings") void handlerReaderChecksEveryConstrainedSesString(String caseName, String properties, @@ -325,11 +366,18 @@ class CfnPropertyTypesTest { "MatchingEventTypes":["SEND"],"Name":"bad name"}} """, "#/EventDestination/Name: failed validation constraint for keyword [pattern]"), Arguments.of("dimension name minimum", dimensions.formatted("", "value"), - "#/EventDestination/CloudWatchDestination/DimensionConfigurations/0/DimensionName: " - + "expected minLength: 1, actual: 0"), + groupedStringViolations( + "#/EventDestination/CloudWatchDestination/DimensionConfigurations/0/DimensionName", + "expected minLength: 1, actual: 0")), Arguments.of("default dimension value maximum", dimensions.formatted("name", "x".repeat(257)), - "#/EventDestination/CloudWatchDestination/DimensionConfigurations/0/DefaultDimensionValue: " - + "expected maxLength: 256, actual: 257")); + groupedStringViolations( + "#/EventDestination/CloudWatchDestination/DimensionConfigurations/0/DefaultDimensionValue", + "expected maxLength: 256, actual: 257"))); + } + + private static String groupedStringViolations(String path, String lengthViolation) { + return path + ": 2 schema violations found)\n" + path + ": " + lengthViolation + " (" + path + + ")\n" + path + ": failed validation constraint for keyword [pattern] (" + path; } @Test