Skip to content

Commit 25cc226

Browse files
committed
Rename custom validation exception traits to PascalCase and validationException to CustomValidationException to avoid naming collision
1 parent 54f4e95 commit 25cc226

File tree

22 files changed

+166
-166
lines changed

22 files changed

+166
-166
lines changed

.changelog/1759254918.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ breaking: false
66
new_feature: true
77
bug_fix: false
88
---
9-
Adds validators and codegen support for the custom traits custom traits `@validationException`, `@validationMessage`,
10-
`@validationFieldList`, `@validationFieldName`, and `@validationFieldMessage` for defining a custom validation exception
9+
Adds validators and codegen support for the custom traits custom traits `@CustomValidationException`, `@ValidationMessage`,
10+
`@ValidationFieldList`, `@ValidationFieldName`, and `@ValidationFieldMessage` for defining a custom validation exception
1111
to use instead of `smithy.framework#ValidationException`.

codegen-server-test/custom-test-models/custom-validation-exception.smithy

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ $version: "2.0"
33
namespace com.aws.example
44

55
use aws.protocols#restJson1
6-
use smithy.framework.rust#validationException
7-
use smithy.framework.rust#validationFieldList
8-
use smithy.framework.rust#validationFieldMessage
9-
use smithy.framework.rust#validationFieldName
10-
use smithy.framework.rust#validationMessage
6+
use smithy.framework.rust#CustomValidationException
7+
use smithy.framework.rust#ValidationFieldList
8+
use smithy.framework.rust#ValidationFieldMessage
9+
use smithy.framework.rust#ValidationFieldName
10+
use smithy.framework.rust#ValidationMessage
1111

1212
@restJson1
1313
service CustomValidationExample {
@@ -36,17 +36,17 @@ structure TestInput {
3636

3737
@error("client")
3838
@httpError(400)
39-
@validationException
39+
@CustomValidationException
4040
structure MyCustomValidationException {
4141
@required
42-
@validationMessage
42+
@ValidationMessage
4343
customMessage: String
4444

4545
@required
4646
@default("testReason1")
4747
reason: ValidationExceptionReason
4848

49-
@validationFieldList
49+
@ValidationFieldList
5050
customFieldList: CustomValidationFieldList
5151
}
5252

@@ -57,11 +57,11 @@ enum ValidationExceptionReason {
5757

5858
structure CustomValidationField {
5959
@required
60-
@validationFieldName
60+
@ValidationFieldName
6161
customFieldName: String
6262

6363
@required
64-
@validationFieldMessage
64+
@ValidationFieldMessage
6565
customFieldMessage: String
6666
}
6767

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package software.amazon.smithy.rust.codegen.server.smithy
77

8-
import software.amazon.smithy.framework.rust.ValidationExceptionTrait
8+
import software.amazon.smithy.framework.rust.CustomValidationExceptionTrait
99
import software.amazon.smithy.model.Model
1010
import software.amazon.smithy.model.shapes.BlobShape
1111
import software.amazon.smithy.model.shapes.ByteShape
@@ -211,7 +211,7 @@ fun validateOperationsWithConstrainedInputHaveValidationExceptionAttached(
211211
validationExceptionShapeId: ShapeId,
212212
): ValidationResult {
213213
// Traverse the model and error out if an operation uses constrained input, but it does not have
214-
// `ValidationException` or a structure with the @validationException trait attached in `errors`.
214+
// `ValidationException` or a structure with the @CustomValidationException trait attached in `errors`.
215215
// https://github.com/smithy-lang/smithy-rs/pull/1199#discussion_r809424783
216216
// TODO(https://github.com/smithy-lang/smithy-rs/issues/1401): This check will go away once we add support for
217217
// `disableDefaultValidation` set to `true`, allowing service owners to map from constraint violations to operation errors.
@@ -222,7 +222,7 @@ fun validateOperationsWithConstrainedInputHaveValidationExceptionAttached(
222222
it.errors.none { error ->
223223
model
224224
.expectShape(error)
225-
.hasTrait(ValidationExceptionTrait.ID)
225+
.hasTrait(CustomValidationExceptionTrait.ID)
226226
}
227227
}
228228
.map { OperationWithConstrainedInputWithoutValidationException(it) }
@@ -258,7 +258,7 @@ fun validateOperationsWithConstrainedInputHaveValidationExceptionAttached(
258258

259259
/**
260260
* Validate that all constrained operations have exactly one of: the default smithy.framework#ValidationException or a
261-
* custom validation exception (shape with @validationException) attached to their errors.
261+
* custom validation exception (shape with @CustomValidationException) attached to their errors.
262262
*/
263263
fun validateOperationsWithConstrainedInputHaveOneValidationExceptionAttached(
264264
model: Model,
@@ -270,7 +270,7 @@ fun validateOperationsWithConstrainedInputHaveOneValidationExceptionAttached(
270270
.filter {
271271
it.errors.count { error ->
272272
val errorShape = model.expectShape(error)
273-
errorShape.hasTrait(ValidationExceptionTrait.ID) || errorShape.id == validationExceptionShapeId
273+
errorShape.hasTrait(CustomValidationExceptionTrait.ID) || errorShape.id == validationExceptionShapeId
274274
} > 1
275275
}
276276
.map { OperationWithConstrainedInputWithMultipleValidationExceptions(it) }
@@ -302,7 +302,7 @@ fun validateModelHasAtMostOneValidationException(
302302
): ValidationResult {
303303
val customValidationExceptionShapes =
304304
model.shapes()
305-
.filter { it.hasTrait(ValidationExceptionTrait.ID) }
305+
.filter { it.hasTrait(CustomValidationExceptionTrait.ID) }
306306
.toList()
307307

308308
val messages = mutableListOf<LogMessage>()

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/UserProvidedValidationExceptionDecorator.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package software.amazon.smithy.rust.codegen.server.smithy.customizations
77

88
import software.amazon.smithy.codegen.core.CodegenException
9-
import software.amazon.smithy.framework.rust.ValidationExceptionTrait
9+
import software.amazon.smithy.framework.rust.CustomValidationExceptionTrait
1010
import software.amazon.smithy.framework.rust.ValidationFieldListTrait
1111
import software.amazon.smithy.framework.rust.ValidationFieldMessageTrait
1212
import software.amazon.smithy.framework.rust.ValidationFieldNameTrait
@@ -84,13 +84,13 @@ class UserProvidedValidationExceptionDecorator : ServerCodegenDecorator {
8484
.shapes(StructureShape::class.java)
8585
.toList()
8686
// Defining multiple validation exceptions is unsupported. See `ValidateUnsupportedConstraints`
87-
.firstOrNull({ it.hasTrait(ValidationExceptionTrait.ID) })
87+
.firstOrNull({ it.hasTrait(CustomValidationExceptionTrait.ID) })
8888

8989
internal fun validationMessageMember(validationExceptionStructure: StructureShape): MemberShape =
9090
validationExceptionStructure
9191
.members()
9292
.firstOrNull { it.isValidationMessage() }
93-
?: throw CodegenException("Expected `$validationExceptionStructure` to contain a member named `message` or annotated with the `@validationMessageTrait`")
93+
?: throw CodegenException("Expected `$validationExceptionStructure` to contain a member named `message` or annotated with the `@ValidationMessageTrait`")
9494

9595
internal fun additionalFieldMembers(validationExceptionStructure: StructureShape): List<MemberShape> =
9696
validationExceptionStructure.members().filter { member ->
@@ -104,9 +104,9 @@ class UserProvidedValidationExceptionDecorator : ServerCodegenDecorator {
104104
* Returns a [ValidationFieldList] if the following exist:
105105
* - A structure type representing the field
106106
* - A list type representing the field list with a single member targeting the field type
107-
* - A member in the validation exception structure annotated with `@validationFieldList` targeting the list type
107+
* - A member in the validation exception structure annotated with `@ValidationFieldList` targeting the list type
108108
*
109-
* Returns null if there is no member annotated with the `@validationFieldList` trait in the given validation exception structure
109+
* Returns null if there is no member annotated with the `@ValidationFieldList` trait in the given validation exception structure
110110
* Otherwise, throws a [CodegenException] if it exists, but is misconfigured
111111
*/
112112
internal fun maybeValidationFieldList(
@@ -139,12 +139,12 @@ class UserProvidedValidationExceptionDecorator : ServerCodegenDecorator {
139139
CodegenException("Expected $validationFieldListShapeMember to target a structure type")
140140
}
141141

142-
// It is required that a member of the user provided validation field structure has @validationFieldName
142+
// It is required that a member of the user provided validation field structure has @ValidationFieldName
143143
val validationFieldNameMember =
144144
validationFieldStructure
145145
.members()
146146
.firstOrNull { it.isValidationFieldName() }
147-
?: throw CodegenException("Expected `$validationFieldStructure` to contain a member with the `@validationFieldName` trait")
147+
?: throw CodegenException("Expected `$validationFieldStructure` to contain a member with the `@ValidationFieldName` trait")
148148

149149
val maybeValidationFieldMessageMember =
150150
validationFieldStructure
@@ -174,25 +174,25 @@ class UserProvidedValidationExceptionDecorator : ServerCodegenDecorator {
174174
}
175175

176176
/**
177-
* Annotates the "message" member of the validation exception structure with @validationMessage when there is no
177+
* Annotates the "message" member of the validation exception structure with @ValidationMessage when there is no
178178
* explicitly annotated member
179179
*/
180180
internal fun annotateValidationMessageMember(validationExceptionStructure: StructureShape) {
181181
val member = validationMessageMember(validationExceptionStructure)
182182
if (!member.hasTrait(ValidationMessageTrait.ID)) {
183-
// When there is no field annotated with the @validationMessage trait, we will annotate the field named "message"
183+
// When there is no field annotated with the @ValidationMessage trait, we will annotate the field named "message"
184184
member.toBuilder().addTrait(ValidationMessageTrait(SourceLocation.none()))
185185
}
186186
}
187187

188188
/**
189-
* Annotates the "name" member of the validation field structure with @validationFieldName when there is no
189+
* Annotates the "name" member of the validation field structure with @ValidationFieldName when there is no
190190
* explicitly annotated member
191191
*/
192192
internal fun annotateValidationFieldName(validationFieldList: ValidationFieldList) {
193193
val member = validationFieldList.validationFieldNameMember
194194
if (!member.hasTrait(ValidationFieldNameTrait.ID)) {
195-
// When there is no field annotated with the @validationMessage trait, we will annotate the field named "name"
195+
// When there is no field annotated with the @ValidationMessage trait, we will annotate the field named "name"
196196
member.toBuilder().addTrait(ValidationFieldNameTrait(SourceLocation.none()))
197197
}
198198
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ interface ServerCodegenDecorator : CoreCodegenDecorator<ServerCodegenContext, Se
4444
/**
4545
* Injection point to allow a decorator to postprocess the error message that arises when an operation is
4646
* constrained and there are multiple of: `ValidationException` and/or custom structures with the
47-
* @validationException trait attached to the operation's errors.
47+
* @CustomValidationException trait attached to the operation's errors.
4848
*/
4949
fun postprocessMultipleValidationExceptionsErrorMessage(validationResult: ValidationResult) = validationResult
5050

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/util/CustomValidationExceptionUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import software.amazon.smithy.model.shapes.MemberShape
1111

1212
/**
1313
* Helper function to determine if this [MemberShape] is a validation message either explicitly with the
14-
* @validationMessage trait or implicitly because it is named "message"
14+
* @ValidationMessage trait or implicitly because it is named "message"
1515
*/
1616
fun MemberShape.isValidationMessage(): Boolean {
1717
return this.hasTrait(ValidationMessageTrait.ID) || this.memberName == "message"

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/validators/CustomValidationExceptionValidator.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package software.amazon.smithy.rust.codegen.server.smithy.validators
77

8-
import software.amazon.smithy.framework.rust.ValidationExceptionTrait
8+
import software.amazon.smithy.framework.rust.CustomValidationExceptionTrait
99
import software.amazon.smithy.model.Model
1010
import software.amazon.smithy.model.shapes.Shape
1111
import software.amazon.smithy.model.shapes.ShapeType
@@ -25,19 +25,19 @@ class CustomValidationExceptionValidator : AbstractValidator() {
2525
override fun validate(model: Model): List<ValidationEvent> {
2626
val events = mutableListOf<ValidationEvent>()
2727

28-
model.shapes(StructureShape::class.java).filter { it.hasTrait(ValidationExceptionTrait.ID) }
28+
model.shapes(StructureShape::class.java).filter { it.hasTrait(CustomValidationExceptionTrait.ID) }
2929
.forEach { shape ->
3030
// Validate that the shape also has @error trait
3131
if (!shape.hasTrait(ErrorTrait::class.java)) {
3232
events.add(
3333
ValidationEvent.builder().id("CustomValidationException.MissingErrorTrait")
3434
.severity(Severity.ERROR).shape(shape)
35-
.message("@validationException requires @error trait")
35+
.message("@CustomValidationException requires @error trait")
3636
.build(),
3737
)
3838
}
3939

40-
// Validate exactly one member with @validationMessage trait (explicit) or named "message" (implicit)
40+
// Validate exactly one member with @ValidationMessage trait (explicit) or named "message" (implicit)
4141
val messageFields =
4242
shape.members().filter { it.isValidationMessage() }
4343

@@ -47,8 +47,8 @@ class CustomValidationExceptionValidator : AbstractValidator() {
4747
ValidationEvent.builder().id("CustomValidationException.MissingMessageField")
4848
.severity(Severity.ERROR).shape(shape)
4949
.message(
50-
"@validationException requires exactly one String member named " +
51-
"\"message\" or with the @validationMessage trait",
50+
"@CustomValidationException requires exactly one String member named " +
51+
"\"message\" or with the @ValidationMessage trait",
5252
).build(),
5353
)
5454

@@ -58,7 +58,7 @@ class CustomValidationExceptionValidator : AbstractValidator() {
5858
events.add(
5959
ValidationEvent.builder().id("CustomValidationException.NonStringMessageField")
6060
.severity(Severity.ERROR).shape(shape)
61-
.message("@validationMessage field must be a String").build(),
61+
.message("@ValidationMessage field must be a String").build(),
6262
)
6363
}
6464
}
@@ -68,8 +68,8 @@ class CustomValidationExceptionValidator : AbstractValidator() {
6868
ValidationEvent.builder().id("CustomValidationException.MultipleMessageFields")
6969
.severity(Severity.ERROR).shape(shape)
7070
.message(
71-
"@validationException can have only one member explicitly marked with the" +
72-
"@validationMessage trait or implicitly selected via the \"message\" field name convention.",
71+
"@CustomValidationException can have only one member explicitly marked with the" +
72+
"@ValidationMessage trait or implicitly selected via the \"message\" field name convention.",
7373
).build(),
7474
)
7575
}

codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraintsAreNotUsedTest.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
8989
$version
9090
namespace test
9191
92-
use smithy.framework.rust#validationException
93-
use smithy.framework.rust#validationMessage
92+
use smithy.framework.rust#CustomValidationException
93+
use smithy.framework.rust#ValidationMessage
9494
9595
service TestService {
9696
operations: [TestOperation]
@@ -109,10 +109,10 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
109109
requiredString: String
110110
}
111111
112-
@validationException
112+
@CustomValidationException
113113
@error("client")
114114
structure CustomValidationException {
115-
@validationMessage
115+
@ValidationMessage
116116
@default("Validation Failed")
117117
@required
118118
message: String,
@@ -378,8 +378,8 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
378378
"""
379379
namespace test
380380
381-
use smithy.framework.rust#validationException
382-
use smithy.framework.rust#validationMessage
381+
use smithy.framework.rust#CustomValidationException
382+
use smithy.framework.rust#ValidationMessage
383383
384384
service TestService {
385385
operations: [TestOperation]
@@ -390,17 +390,17 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
390390
output: TestInputOutput,
391391
}
392392
393-
@validationException
393+
@CustomValidationException
394394
@error("client")
395395
structure CustomValidationException {
396-
@validationMessage
396+
@ValidationMessage
397397
message: String,
398398
}
399399
400-
@validationException
400+
@CustomValidationException
401401
@error("client")
402402
structure AnotherValidationException {
403-
@validationMessage
403+
@ValidationMessage
404404
message: String,
405405
}
406406
@@ -423,8 +423,8 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
423423
"""
424424
namespace test
425425
426-
use smithy.framework.rust#validationException
427-
use smithy.framework.rust#validationMessage
426+
use smithy.framework.rust#CustomValidationException
427+
use smithy.framework.rust#ValidationMessage
428428
429429
service TestService {
430430
operations: [TestOperation]
@@ -435,10 +435,10 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
435435
output: TestInputOutput,
436436
}
437437
438-
@validationException
438+
@CustomValidationException
439439
@error("client")
440440
structure CustomValidationException {
441-
@validationMessage
441+
@ValidationMessage
442442
message: String,
443443
}
444444
@@ -461,8 +461,8 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
461461
namespace test
462462
463463
use smithy.framework#ValidationException
464-
use smithy.framework.rust#validationException
465-
use smithy.framework.rust#validationMessage
464+
use smithy.framework.rust#CustomValidationException
465+
use smithy.framework.rust#ValidationMessage
466466
467467
service TestService {
468468
operations: [TestOperation]
@@ -476,10 +476,10 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
476476
]
477477
}
478478
479-
@validationException
479+
@CustomValidationException
480480
@error("client")
481481
structure CustomValidationException {
482-
@validationMessage
482+
@ValidationMessage
483483
message: String,
484484
}
485485

0 commit comments

Comments
 (0)