Skip to content

Commit

Permalink
Define a function to return operation shapes that need a `ValidationE…
Browse files Browse the repository at this point in the history
…xception` (#3720)

Refactor and define a separate function that returns a set of operation
shapes that must have a supported validation exception shape in their
associated errors list.

This helps identify which type of `ValidationException` has been added
to the operation shape's errors list.

Closes: [3722](#3722)

---------

Co-authored-by: Fahad Zubair <[email protected]>
Co-authored-by: david-perez <[email protected]>
  • Loading branch information
3 people authored Jun 28, 2024
1 parent e56f0dd commit 5498a18
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ data class LogMessage(val level: Level, val message: String)
data class ValidationResult(val shouldAbort: Boolean, val messages: List<LogMessage>) :
Throwable(message = messages.joinToString("\n") { it.message })

/*
* Returns the set of operation shapes that must have a supported validation exception shape
* in their associated errors list.
*/
fun operationShapesThatMustHaveValidationException(
model: Model,
service: ServiceShape,
): Set<OperationShape> {
val walker = DirectedWalker(model)
return walker.walkShapes(service)
.filterIsInstance<OperationShape>()
.asSequence()
.filter { operationShape ->
// Walk the shapes reachable via this operation input.
walker.walkShapes(operationShape.inputShape(model))
.any { it is SetShape || it is EnumShape || it.hasConstraintTrait() }
}
.toSet()
}

/**
* Validate that all constrained operations have the shape [validationExceptionShapeId] shape attached to their errors.
*/
Expand All @@ -189,14 +209,7 @@ fun validateOperationsWithConstrainedInputHaveValidationExceptionAttached(
// `disableDefaultValidation` set to `true`, allowing service owners to map from constraint violations to operation errors.
val walker = DirectedWalker(model)
val operationsWithConstrainedInputWithoutValidationExceptionSet =
walker.walkShapes(service)
.filterIsInstance<OperationShape>()
.asSequence()
.filter { operationShape ->
// Walk the shapes reachable via this operation input.
walker.walkShapes(operationShape.inputShape(model))
.any { it is SetShape || it is EnumShape || it.hasConstraintTrait() }
}
operationShapesThatMustHaveValidationException(model, service)
.filter { !it.errors.contains(validationExceptionShapeId) }
.map { OperationWithConstrainedInputWithoutValidationException(it) }
.toSet()
Expand Down

0 comments on commit 5498a18

Please sign in to comment.