mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 18:48:00 -05:00
refactor(core): word a string against its vendored shape once for both constraint checks (#9350)
QueryConstraintCheck and RequestConstraintCheck each held their own copy of how a string breaks its vendored shape: the length, pattern and enum violations in that order, the values an enum admits, the member name with its first letter lowered, and the loop compiling every shape's pattern. They now share VendoredShapes, a package-private helper in core.common, so the Query and JSON protocols cannot drift apart on how a string is worded. RequestConstraintCheck's boolean reading of a list element or map side (scalarBroken) goes through the same helper rather than a third copy of the three checks. The one reading that differed was a shape declaring an empty enum, which the Query check passed and the JSON check refused; no vendored file declares one, and both now pass it.
This commit is contained in:
1 parent
75f94407d4
commit
8758208313
3 files changed
+85
-103
No files matched your search
@@ -6,7 +6,6 @@ import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -79,17 +78,13 @@ public final class QueryConstraintCheck {
|
||||
private final JsonNode operations;
|
||||
private final JsonNode shapes;
|
||||
private final SmithyValidationErrors.Violations messages;
|
||||
private final Map<String, Pattern> patterns = new HashMap<>();
|
||||
private final Map<String, Pattern> patterns;
|
||||
|
||||
private QueryConstraintCheck(JsonNode model, SmithyValidationErrors.Violations messages) {
|
||||
this.operations = model.path("operations");
|
||||
this.shapes = model.path("shapes");
|
||||
this.messages = messages;
|
||||
for (Map.Entry<String, JsonNode> shape : shapes.properties()) {
|
||||
if (shape.getValue().has("pattern")) {
|
||||
patterns.put(shape.getKey(), VendoredPatterns.compile(shape.getValue().get("pattern").asText()));
|
||||
}
|
||||
}
|
||||
this.patterns = VendoredShapes.patterns(shapes);
|
||||
for (Map.Entry<String, JsonNode> operation : operations.properties()) {
|
||||
requireReadableMembers(operation.getKey(), operation.getValue().asText());
|
||||
}
|
||||
@@ -212,7 +207,7 @@ public final class QueryConstraintCheck {
|
||||
for (Map.Entry<String, JsonNode> member : structure.path("members").properties()) {
|
||||
String name = member.getKey();
|
||||
String shapeName = member.getValue().asText();
|
||||
String memberPath = path + memberPath(name);
|
||||
String memberPath = path + VendoredShapes.memberName(name);
|
||||
String param = prefix + name;
|
||||
boolean mandatory = required.contains(name);
|
||||
JsonNode shape = shapes.path(shapeName);
|
||||
@@ -304,7 +299,7 @@ public final class QueryConstraintCheck {
|
||||
JsonNode shape = shapes.path(shapeName);
|
||||
return List.of(messages.mapKeys(path, key, SmithyValidationErrors.constraintSet(
|
||||
shape.path("min").asInt(0), shape.path("max").asInt(Integer.MAX_VALUE),
|
||||
VendoredPatterns.quoted(shape), allowedValues(shape))));
|
||||
VendoredPatterns.quoted(shape), VendoredShapes.enumValues(shape))));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -370,12 +365,6 @@ public final class QueryConstraintCheck {
|
||||
return structureViolations(shape, param + ".", path + ".", params, Map.of());
|
||||
}
|
||||
|
||||
/** The member's name as a validation message spells it, which is the request's with a lower
|
||||
* first letter. */
|
||||
private static String memberPath(String name) {
|
||||
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
|
||||
}
|
||||
|
||||
/** Every constraint {@code value} breaks, in the order a shape declares them. */
|
||||
private List<String> violations(String shapeName, String value, String path) {
|
||||
JsonNode shape = shapes.path(shapeName);
|
||||
@@ -386,22 +375,7 @@ public final class QueryConstraintCheck {
|
||||
if (!"string".equals(type)) {
|
||||
return List.of();
|
||||
}
|
||||
List<String> violations = new ArrayList<>();
|
||||
String length = messages.lengthOutOfRange(path, value,
|
||||
shape.path("min").asInt(0), shape.path("max").asInt(Integer.MAX_VALUE));
|
||||
if (length != null) {
|
||||
violations.add(length);
|
||||
}
|
||||
Pattern pattern = patterns.get(shapeName);
|
||||
if (pattern != null && !pattern.matcher(value).matches()) {
|
||||
// Quoted as the model declares it, which is what AWS answers and what the service docs
|
||||
// carry, rather than as it was compiled: a wide escape is rewritten to match on. One
|
||||
// member is quoted from a capture instead, where AWS gives a pattern of its own for a
|
||||
// shape botocore shares, and one object decides which text that is.
|
||||
violations.add(messages.pattern(path, value, VendoredPatterns.quoted(shape)));
|
||||
}
|
||||
violations.addAll(single(enumViolation(shape, value, path)));
|
||||
return violations;
|
||||
return VendoredShapes.stringViolations(shape, patterns.get(shapeName), value, path, messages);
|
||||
}
|
||||
|
||||
private static List<String> single(String violation) {
|
||||
@@ -445,19 +419,4 @@ public final class QueryConstraintCheck {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String enumViolation(JsonNode shape, String value, String path) {
|
||||
List<String> allowed = allowedValues(shape);
|
||||
if (allowed.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return allowed.contains(value) ? null : messages.notInEnum(value, path, allowed);
|
||||
}
|
||||
|
||||
/** The values a shape's enum admits, in the model's order, or none for a shape without one. */
|
||||
private static List<String> allowedValues(JsonNode shape) {
|
||||
List<String> allowed = new ArrayList<>();
|
||||
shape.path("enum").forEach(choice -> allowed.add(choice.asText()));
|
||||
return allowed;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -96,18 +95,14 @@ public final class RequestConstraintCheck {
|
||||
}
|
||||
|
||||
private final JsonNode model;
|
||||
private final Map<String, Pattern> patterns = new HashMap<>();
|
||||
private final Map<String, Pattern> patterns;
|
||||
private final Messages messages;
|
||||
private final Set<String> wordable = ConcurrentHashMap.newKeySet();
|
||||
|
||||
private RequestConstraintCheck(JsonNode model, Messages messages) {
|
||||
this.model = model;
|
||||
this.messages = messages;
|
||||
for (Map.Entry<String, JsonNode> shape : model.path("shapes").properties()) {
|
||||
if (shape.getValue().has("pattern")) {
|
||||
patterns.put(shape.getKey(), VendoredPatterns.compile(shape.getValue().get("pattern").asText()));
|
||||
}
|
||||
}
|
||||
this.patterns = VendoredShapes.patterns(model.path("shapes"));
|
||||
}
|
||||
|
||||
/** The vendored resource {@code name}; {@code description} names it in a failure. */
|
||||
@@ -322,7 +317,7 @@ public final class RequestConstraintCheck {
|
||||
shape.path("required").forEach(member -> required.add(member.asText()));
|
||||
for (Map.Entry<String, JsonNode> member : shape.path("members").properties()) {
|
||||
String field = member.getKey();
|
||||
String memberPath = (path.isEmpty() ? "" : path + ".") + lowerFirst(field);
|
||||
String memberPath = (path.isEmpty() ? "" : path + ".") + VendoredShapes.memberName(field);
|
||||
JsonNode child = value.get(field);
|
||||
if (child == null || child.isNull()) {
|
||||
if (required.contains(field)) {
|
||||
@@ -344,7 +339,7 @@ public final class RequestConstraintCheck {
|
||||
if (!value.isTextual()) {
|
||||
throw messages.type(errorPath, type);
|
||||
}
|
||||
collectString(name, shape, value.textValue(), path, form, found);
|
||||
found.addAll(VendoredShapes.stringViolations(shape, patterns.get(name), value.textValue(), path, form));
|
||||
}
|
||||
case "integer", "long" -> collectInteger(shape, type, value, path, errorPath, form, found);
|
||||
case "float", "double" -> {
|
||||
@@ -384,21 +379,6 @@ public final class RequestConstraintCheck {
|
||||
}
|
||||
}
|
||||
|
||||
private void collectString(String name, JsonNode shape, String text, String path,
|
||||
SmithyValidationErrors.Violations form, List<String> found) {
|
||||
String length = form.lengthOutOfRange(path, text, shape.path("min").asInt(0),
|
||||
shape.has("max") ? shape.get("max").asInt() : Integer.MAX_VALUE);
|
||||
if (length != null) {
|
||||
found.add(length);
|
||||
}
|
||||
if (patternBroken(name, text)) {
|
||||
found.add(form.pattern(path, text, VendoredPatterns.quoted(shape)));
|
||||
}
|
||||
if (shape.has("enum") && !inEnum(shape, text)) {
|
||||
found.add(form.notInEnum(text, path, enumValues(shape)));
|
||||
}
|
||||
}
|
||||
|
||||
private void collectList(JsonNode shape, JsonNode value, String path, String errorPath,
|
||||
SmithyValidationErrors.Violations form, List<String> found) {
|
||||
if (!value.isArray()) {
|
||||
@@ -467,34 +447,8 @@ public final class RequestConstraintCheck {
|
||||
if (!"string".equals(type)) {
|
||||
return false;
|
||||
}
|
||||
String text = item.textValue();
|
||||
return lengthBroken(shape, text) || patternBroken(name, text)
|
||||
|| shape.has("enum") && !inEnum(shape, text);
|
||||
}
|
||||
|
||||
private static boolean lengthBroken(JsonNode shape, String text) {
|
||||
int length = text.codePointCount(0, text.length());
|
||||
return shape.has("min") && length < shape.get("min").asInt()
|
||||
|| shape.has("max") && length > shape.get("max").asInt();
|
||||
}
|
||||
|
||||
private boolean patternBroken(String name, String text) {
|
||||
return patterns.containsKey(name) && !patterns.get(name).matcher(text).matches();
|
||||
}
|
||||
|
||||
private static boolean inEnum(JsonNode shape, String text) {
|
||||
for (JsonNode choice : shape.get("enum")) {
|
||||
if (choice.asText().equals(text)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<String> enumValues(JsonNode shape) {
|
||||
List<String> values = new ArrayList<>();
|
||||
shape.get("enum").forEach(choice -> values.add(choice.asText()));
|
||||
return values;
|
||||
return !VendoredShapes.stringViolations(shape, patterns.get(name), item.textValue(), errorPath,
|
||||
SmithyValidationErrors.Violations.WITHOUT_VALUES).isEmpty();
|
||||
}
|
||||
|
||||
/** The rules a string shape declares, a minimum of zero included, in the order AWS lists them. */
|
||||
@@ -511,7 +465,7 @@ public final class RequestConstraintCheck {
|
||||
rules.add("Member must satisfy regular expression pattern: " + VendoredPatterns.quoted(shape));
|
||||
}
|
||||
if (shape.has("enum")) {
|
||||
rules.add("Member must satisfy enum value set: " + enumValues(shape));
|
||||
rules.add("Member must satisfy enum value set: " + VendoredShapes.enumValues(shape));
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
@@ -547,8 +501,4 @@ public final class RequestConstraintCheck {
|
||||
? SmithyValidationErrors.Violations.WITHOUT_VALUES
|
||||
: SmithyValidationErrors.Violations.QUOTED;
|
||||
}
|
||||
|
||||
private static String lowerFirst(String name) {
|
||||
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* What {@link QueryConstraintCheck} and {@link RequestConstraintCheck} both read off a vendored
|
||||
* shape: the patterns the shapes declare, how a string breaks its shape, the values an enum admits
|
||||
* and how a message names a member. The two walk a request differently because the protocols carry
|
||||
* one differently, but a string is held to its shape and worded the same way on either, so that
|
||||
* reading lives here once.
|
||||
*/
|
||||
final class VendoredShapes {
|
||||
|
||||
private VendoredShapes() {
|
||||
}
|
||||
|
||||
/** Each shape's pattern, compiled as the model means it, keyed by the shape's name. */
|
||||
static Map<String, Pattern> patterns(JsonNode shapes) {
|
||||
Map<String, Pattern> patterns = new HashMap<>();
|
||||
for (Map.Entry<String, JsonNode> shape : shapes.properties()) {
|
||||
if (shape.getValue().has("pattern")) {
|
||||
patterns.put(shape.getKey(), VendoredPatterns.compile(shape.getValue().get("pattern").asText()));
|
||||
}
|
||||
}
|
||||
return patterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every constraint {@code text} breaks against its string {@code shape}, in the order AWS lists
|
||||
* them: its length, its {@code pattern} (none for a shape without one) and its enum, each in
|
||||
* the form {@code messages} writes.
|
||||
*
|
||||
* <p>The pattern is quoted as the model declares it, which is what AWS answers and what the
|
||||
* service docs carry, rather than as it was compiled: a wide escape is rewritten to match on.
|
||||
* One member is quoted from a capture instead, where AWS gives a pattern of its own for a shape
|
||||
* botocore shares, and {@link VendoredPatterns#quoted} decides which text that is.
|
||||
*/
|
||||
static List<String> stringViolations(JsonNode shape, Pattern pattern, String text, String path,
|
||||
SmithyValidationErrors.Violations messages) {
|
||||
List<String> violations = new ArrayList<>();
|
||||
String length = messages.lengthOutOfRange(path, text,
|
||||
shape.path("min").asInt(0), shape.path("max").asInt(Integer.MAX_VALUE));
|
||||
if (length != null) {
|
||||
violations.add(length);
|
||||
}
|
||||
if (pattern != null && !pattern.matcher(text).matches()) {
|
||||
violations.add(messages.pattern(path, text, VendoredPatterns.quoted(shape)));
|
||||
}
|
||||
List<String> allowed = enumValues(shape);
|
||||
if (!allowed.isEmpty() && !allowed.contains(text)) {
|
||||
violations.add(messages.notInEnum(text, path, allowed));
|
||||
}
|
||||
return violations;
|
||||
}
|
||||
|
||||
/** The values a shape's enum admits, in the model's order, or none for a shape without one. */
|
||||
static List<String> enumValues(JsonNode shape) {
|
||||
List<String> values = new ArrayList<>();
|
||||
shape.path("enum").forEach(choice -> values.add(choice.asText()));
|
||||
return values;
|
||||
}
|
||||
|
||||
/** A member's name as a validation message spells it, which is the model's with a lower first letter. */
|
||||
static String memberName(String name) {
|
||||
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user