forked from smithy-lang/smithy-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow server decorators to postprocess
ValidationException
not atta…
…ched error messages (smithy-lang#2338) Should they want to, a server decorator can now postprocess the error message that arises when a constrained operation does not have the `ValidationException` shape attached to its errors. This commit adds a test to ensure that when such a decorator is registered, the `ValidationResult` can indeed be altered, but no such decorator is added to the `rust-server-codegen` plugin.
- Loading branch information
1 parent
d7f8130
commit 1c1a3ef
Showing
4 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ithy/customizations/PostprocessValidationExceptionNotAttachedErrorMessageDecoratorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.server.smithy.customizations | ||
|
||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldContain | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import software.amazon.smithy.codegen.core.CodegenException | ||
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel | ||
import software.amazon.smithy.rust.codegen.server.smithy.LogMessage | ||
import software.amazon.smithy.rust.codegen.server.smithy.ValidationResult | ||
import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest | ||
|
||
internal class PostprocessValidationExceptionNotAttachedErrorMessageDecoratorTest { | ||
@Test | ||
fun `validation exception not attached error message is postprocessed if decorator is registered`() { | ||
val model = | ||
""" | ||
namespace test | ||
use aws.protocols#restJson1 | ||
@restJson1 | ||
service TestService { | ||
operations: ["ConstrainedOperation"], | ||
} | ||
operation ConstrainedOperation { | ||
input: ConstrainedOperationInput | ||
} | ||
structure ConstrainedOperationInput { | ||
@required | ||
requiredString: String | ||
} | ||
""".asSmithyModel() | ||
|
||
val validationExceptionNotAttachedErrorMessageDummyPostprocessorDecorator = object : ServerCodegenDecorator { | ||
override val name: String | ||
get() = "ValidationExceptionNotAttachedErrorMessageDummyPostprocessorDecorator" | ||
override val order: Byte | ||
get() = 69 | ||
|
||
override fun postprocessValidationExceptionNotAttachedErrorMessage(validationResult: ValidationResult): ValidationResult { | ||
check(validationResult.messages.size == 1) | ||
|
||
val level = validationResult.messages.first().level | ||
val message = | ||
""" | ||
${validationResult.messages.first().message} | ||
There are three things all wise men fear: the sea in storm, a night with no moon, and the anger of a gentle man. | ||
""" | ||
|
||
return validationResult.copy(messages = listOf(LogMessage(level, message))) | ||
} | ||
} | ||
|
||
val exception = assertThrows<CodegenException> { | ||
serverIntegrationTest( | ||
model, | ||
additionalDecorators = listOf(validationExceptionNotAttachedErrorMessageDummyPostprocessorDecorator), | ||
) | ||
} | ||
val exceptionCause = (exception.cause!! as ValidationResult) | ||
exceptionCause.messages.size shouldBe 1 | ||
exceptionCause.messages.first().message shouldContain "There are three things all wise men fear: the sea in storm, a night with no moon, and the anger of a gentle man." | ||
} | ||
} |