Skip to content

Commit 80e9ab4

Browse files
authored
Fix field creation when validation field list structure has no @validationFieldMessage (#4395)
2 parents f76afc5 + d2cab90 commit 80e9ab4

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class UserProvidedValidationExceptionConversionGenerator(
252252
"ValidationException" to codegenContext.symbolProvider.toSymbol(validationExceptionStructure),
253253
"FieldCreation" to
254254
writable {
255-
if (maybeValidationFieldList?.maybeValidationFieldMessageMember != null) {
255+
if (maybeValidationFieldList != null) {
256256
rust("""let first_validation_exception_field = constraint_violation.as_validation_exception_field("".to_owned());""")
257257
}
258258
},
@@ -319,6 +319,7 @@ class UserProvidedValidationExceptionConversionGenerator(
319319
.get(stringTraitInfo) as LengthTrait
320320
rustTemplate(
321321
"""
322+
##[allow(unused_variables)]
322323
Self::Length(length) => #{ValidationExceptionField} {
323324
#{FieldAssignments}
324325
},
@@ -384,6 +385,7 @@ class UserProvidedValidationExceptionConversionGenerator(
384385
blobConstraintsInfo.forEach { blobLength ->
385386
rustTemplate(
386387
"""
388+
##[allow(unused_variables)]
387389
Self::Length(length) => #{ValidationExceptionField} {
388390
#{FieldAssignments}
389391
},
@@ -424,6 +426,7 @@ class UserProvidedValidationExceptionConversionGenerator(
424426
shape.getTrait<LengthTrait>()?.also {
425427
rustTemplate(
426428
"""
429+
##[allow(unused_variables)]
427430
Self::Length(length) => #{ValidationExceptionField} {
428431
#{FieldAssignments}
429432
},""",
@@ -557,6 +560,7 @@ class UserProvidedValidationExceptionConversionGenerator(
557560
is CollectionTraitInfo.Length -> {
558561
rustTemplate(
559562
"""
563+
##[allow(unused_variables)]
560564
Self::Length(length) => #{ValidationExceptionField} {
561565
#{FieldAssignments}
562566
},
@@ -640,7 +644,7 @@ class UserProvidedValidationExceptionConversionGenerator(
640644
val pathExpression = member.wrapValueIfOptional(rawPathExpression)
641645
val messageExpression = member.wrapValueIfOptional(rawMessageExpression)
642646
when {
643-
member.hasTrait(ValidationFieldNameTrait.ID) ->
647+
member.isValidationFieldName() ->
644648
"$memberName: $pathExpression"
645649

646650
member.hasTrait(ValidationFieldMessageTrait.ID) ->

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,61 @@ internal class UserProvidedValidationExceptionDecoratorTest {
384384
fun `code compiles with custom validation exception using optionals`() {
385385
serverIntegrationTest(completeTestModelWithOptionals)
386386
}
387+
388+
private val completeTestModelWithImplicitNamesWithoutFieldMessage =
389+
"""
390+
namespace com.aws.example
391+
392+
use aws.protocols#restJson1
393+
use smithy.framework.rust#validationException
394+
use smithy.framework.rust#validationFieldList
395+
396+
@restJson1
397+
service CustomValidationExample {
398+
version: "1.0.0"
399+
operations: [
400+
TestOperation
401+
]
402+
errors: [
403+
MyCustomValidationException
404+
]
405+
}
406+
407+
@http(method: "POST", uri: "/test")
408+
operation TestOperation {
409+
input: TestInput
410+
}
411+
412+
structure TestInput {
413+
@required
414+
@length(min: 1, max: 10)
415+
name: String
416+
417+
@range(min: 1, max: 100)
418+
age: Integer
419+
}
420+
421+
@error("client")
422+
@httpError(400)
423+
@validationException
424+
structure MyCustomValidationException {
425+
message: String
426+
427+
@validationFieldList
428+
customFieldList: CustomValidationFieldList
429+
}
430+
431+
structure CustomValidationField {
432+
name: String,
433+
}
434+
435+
list CustomValidationFieldList {
436+
member: CustomValidationField
437+
}
438+
""".asSmithyModel(smithyVersion = "2.0")
439+
440+
@Test
441+
fun `code compiles with implicit message and field name and without field message`() {
442+
serverIntegrationTest(completeTestModelWithImplicitNamesWithoutFieldMessage)
443+
}
387444
}

0 commit comments

Comments
 (0)