Unverified Commit 5498a180 authored by Fahad Zubair's avatar Fahad Zubair Committed by GitHub
Browse files

Define a function to return operation shapes that need a `ValidationException` (#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](https://github.com/smithy-lang/smithy-rs/issues/3722

)

---------

Co-authored-by: default avatarFahad Zubair <fahadzub@amazon.com>
Co-authored-by: default avatardavid-perez <d@vidp.dev>
parent e56f0dd6
Loading
Loading
Loading
Loading
+21 −8
Original line number Diff line number Diff line
@@ -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.
 */
@@ -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()