-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server codegen flag addValidationExceptionToConstrainedOperations (…
…#3803) The Server SDK requires the model to include `aws.smithy.framework#ValidationException` in each operation that can access a constrained member shape. This becomes problematic when generating the server SDK for a model not owned by the team creating the SDK, as they cannot easily modify the model. This PR introduces a codegen flag, `addValidationExceptionToConstrainedOperations`. When set in `smithy-build-template.json`, this flag will automatically add `ValidationException` to operations that require it but do not already list it among their errors. Closes Issue: [3802](#3802) Sample `smithy-build-template.json` ``` "plugins": { "rust-server-codegen": { "service": "ServiceToGenerateSDKFor", "module": "amzn-sample-server-sdk", "codegen": { "addValidationExceptionToConstrainedOperations": true, } } } ``` --------- Co-authored-by: Fahad Zubair <[email protected]>
- Loading branch information
Showing
6 changed files
with
288 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
applies_to: ["server"] | ||
authors: ["drganjoo"] | ||
references: ["smithy-rs#3803"] | ||
breaking: false | ||
new_feature: true | ||
bug_fix: false | ||
--- | ||
Setting the `addValidationExceptionToConstrainedOperations` codegen flag adds `aws.smithy.framework#ValidationException` to operations with constrained inputs that do not already have this exception added. | ||
|
||
Sample `smithy-build-template.json`: | ||
|
||
``` | ||
{ | ||
"...", | ||
"plugins": { | ||
"rust-server-codegen": { | ||
"service": "ServiceToGenerateSDKFor", | ||
"module": "amzn-sample-server-sdk", | ||
"codegen": { | ||
"addValidationExceptionToConstrainedOperations": true, | ||
} | ||
} | ||
} | ||
} | ||
``` |
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
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
85 changes: 85 additions & 0 deletions
85
...codegen/server/smithy/customizations/AddValidationExceptionToConstrainedOperationsTest.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,85 @@ | ||
/* | ||
* 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 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.IntegrationTestParams | ||
import software.amazon.smithy.rust.codegen.core.testutil.ServerAdditionalSettings | ||
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel | ||
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest | ||
|
||
/** | ||
* Tests whether the server `codegen` flag `addValidationExceptionToConstrainedOperations` works as expected. | ||
*/ | ||
internal class AddValidationExceptionToConstrainedOperationsTest { | ||
private val testModelWithValidationExceptionImported = | ||
""" | ||
namespace test | ||
use smithy.framework#ValidationException | ||
use aws.protocols#restJson1 | ||
use aws.api#data | ||
@restJson1 | ||
service ConstrainedService { | ||
operations: [SampleOperationWithValidation, SampleOperationWithoutValidation] | ||
} | ||
@http(uri: "/anOperationWithValidation", method: "POST") | ||
operation SampleOperationWithValidation { | ||
output: SampleInputOutput | ||
input: SampleInputOutput | ||
errors: [ValidationException, ErrorWithMemberConstraint] | ||
} | ||
@http(uri: "/anOperationWithoutValidation", method: "POST") | ||
operation SampleOperationWithoutValidation { | ||
output: SampleInputOutput | ||
input: SampleInputOutput | ||
errors: [] | ||
} | ||
structure SampleInputOutput { | ||
constrainedInteger : RangedInteger | ||
@range(min: 2, max:100) | ||
constrainedMemberInteger : RangedInteger | ||
patternString : PatternString | ||
} | ||
@pattern("^[a-m]+${'$'}") | ||
string PatternString | ||
@range(min: 0, max:1000) | ||
integer RangedInteger | ||
@error("server") | ||
structure ErrorWithMemberConstraint { | ||
@range(min: 100, max: 999) | ||
statusCode: Integer | ||
} | ||
""".asSmithyModel(smithyVersion = "2") | ||
|
||
@Test | ||
fun `without setting the codegen flag, the model should fail to compile`() { | ||
assertThrows<CodegenException> { | ||
serverIntegrationTest( | ||
testModelWithValidationExceptionImported, | ||
IntegrationTestParams(), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `operations that do not have ValidationException will automatically have one added to them`() { | ||
serverIntegrationTest( | ||
testModelWithValidationExceptionImported, | ||
IntegrationTestParams( | ||
additionalSettings = | ||
ServerAdditionalSettings.builder() | ||
.addValidationExceptionToConstrainedOperations() | ||
.toObjectNode(), | ||
), | ||
) | ||
} | ||
} |