Skip to content

Commit 2d11b44

Browse files
jjantaws-sdk-rust-ci
authored andcommitted
Fix protocol test: RestJsonMalformedPatternSensitiveString (#2321)
* Make `RestJsonMalformedPatternSensitiveString` pass * Use `AwsJson11` variable instead of hardcoded string * Refactor `errorMessage` into private function * Add comment about remaining `@range` on floats tests * Use `AwsJson11` variable instead of hardcoded string (missed one) * Use `hasTrait` over `getTrait() != null`
1 parent de5485f commit 2d11b44

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import software.amazon.smithy.model.shapes.Shape
1010
import software.amazon.smithy.model.shapes.StringShape
1111
import software.amazon.smithy.model.traits.LengthTrait
1212
import software.amazon.smithy.model.traits.PatternTrait
13+
import software.amazon.smithy.model.traits.SensitiveTrait
1314
import software.amazon.smithy.model.traits.Trait
1415
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
1516
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
@@ -22,13 +23,15 @@ import software.amazon.smithy.rust.codegen.core.rustlang.join
2223
import software.amazon.smithy.rust.codegen.core.rustlang.render
2324
import software.amazon.smithy.rust.codegen.core.rustlang.rust
2425
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
26+
import software.amazon.smithy.rust.codegen.core.rustlang.writable
2527
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
2628
import software.amazon.smithy.rust.codegen.core.smithy.expectRustMetadata
2729
import software.amazon.smithy.rust.codegen.core.smithy.makeMaybeConstrained
2830
import software.amazon.smithy.rust.codegen.core.smithy.module
2931
import software.amazon.smithy.rust.codegen.core.smithy.testModuleForShape
3032
import software.amazon.smithy.rust.codegen.core.testutil.unitTest
3133
import software.amazon.smithy.rust.codegen.core.util.PANIC
34+
import software.amazon.smithy.rust.codegen.core.util.hasTrait
3235
import software.amazon.smithy.rust.codegen.core.util.orNull
3336
import software.amazon.smithy.rust.codegen.core.util.redactIfNecessary
3437
import software.amazon.smithy.rust.codegen.server.smithy.PubCrateConstraintViolationSymbolProvider
@@ -63,7 +66,7 @@ class ConstrainedStringGenerator(
6366
private val constraintsInfo: List<TraitInfo> =
6467
supportedStringConstraintTraits
6568
.mapNotNull { shape.getTrait(it).orNull() }
66-
.map { StringTraitInfo.fromTrait(symbol, it) }
69+
.map { StringTraitInfo.fromTrait(symbol, it, isSensitive = shape.hasTrait<SensitiveTrait>()) }
6770
.map(StringTraitInfo::toTraitInfo)
6871

6972
fun render() {
@@ -184,6 +187,7 @@ class ConstrainedStringGenerator(
184187
}
185188
}
186189
}
190+
187191
private data class Length(val lengthTrait: LengthTrait) : StringTraitInfo() {
188192
override fun toTraitInfo(): TraitInfo = TraitInfo(
189193
tryFromCheck = { rust("Self::check_length(&value)?;") },
@@ -229,10 +233,9 @@ private data class Length(val lengthTrait: LengthTrait) : StringTraitInfo() {
229233
}
230234
}
231235

232-
private data class Pattern(val symbol: Symbol, val patternTrait: PatternTrait) : StringTraitInfo() {
236+
private data class Pattern(val symbol: Symbol, val patternTrait: PatternTrait, val isSensitive: Boolean) :
237+
StringTraitInfo() {
233238
override fun toTraitInfo(): TraitInfo {
234-
val pattern = patternTrait.pattern
235-
236239
return TraitInfo(
237240
tryFromCheck = { rust("let value = Self::check_pattern(value)?;") },
238241
constraintViolationVariant = {
@@ -241,13 +244,14 @@ private data class Pattern(val symbol: Symbol, val patternTrait: PatternTrait) :
241244
rust("Pattern(String)")
242245
},
243246
asValidationExceptionField = {
244-
rust(
247+
rustTemplate(
245248
"""
246-
Self::Pattern(string) => crate::model::ValidationExceptionField {
247-
message: format!("${patternTrait.validationErrorMessage()}", &string, &path, r##"$pattern"##),
249+
Self::Pattern(_string) => crate::model::ValidationExceptionField {
250+
message: #{ErrorMessage:W},
248251
path
249252
},
250253
""",
254+
"ErrorMessage" to errorMessage(),
251255
)
252256
},
253257
this::renderValidationFunction,
@@ -264,6 +268,28 @@ private data class Pattern(val symbol: Symbol, val patternTrait: PatternTrait) :
264268
)
265269
}
266270

271+
private fun errorMessage(): Writable {
272+
val pattern = patternTrait.pattern
273+
274+
return if (isSensitive) {
275+
writable {
276+
rust(
277+
"""
278+
format!("Value at '{}' failed to satisfy constraint: Member must satisfy regular expression pattern: {}", &path, r##"$pattern"##)
279+
""",
280+
)
281+
}
282+
} else {
283+
writable {
284+
rust(
285+
"""
286+
format!("Value {} at '{}' failed to satisfy constraint: Member must satisfy regular expression pattern: {}", &_string, &path, r##"$pattern"##)
287+
""",
288+
)
289+
}
290+
}
291+
}
292+
267293
/**
268294
* Renders a `check_pattern` function to validate the string matches the
269295
* supplied regex in the `@pattern` trait.
@@ -303,14 +329,16 @@ private data class Pattern(val symbol: Symbol, val patternTrait: PatternTrait) :
303329

304330
private sealed class StringTraitInfo {
305331
companion object {
306-
fun fromTrait(symbol: Symbol, trait: Trait) =
332+
fun fromTrait(symbol: Symbol, trait: Trait, isSensitive: Boolean) =
307333
when (trait) {
308334
is PatternTrait -> {
309-
Pattern(symbol, trait)
335+
Pattern(symbol, trait, isSensitive)
310336
}
337+
311338
is LengthTrait -> {
312339
Length(trait)
313340
}
341+
314342
else -> PANIC("StringTraitInfo.fromTrait called with unsupported trait $trait")
315343
}
316344
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,12 @@ class ServerProtocolTestGenerator(
774774

775775
FailingTest(RestJson, "RestJsonWithBodyExpectsApplicationJsonContentType", TestType.MalformedRequest),
776776

777-
// Tests involving constraint traits, which are not yet fully implemented.
778-
// See https://github.com/awslabs/smithy-rs/issues/1401.
777+
// Tests involving `@range` on floats.
778+
// Pending resolution from the Smithy team, see https://github.com/awslabs/smithy-rs/issues/2007.
779779
FailingTest(RestJsonValidation, "RestJsonMalformedRangeFloat_case0", TestType.MalformedRequest),
780780
FailingTest(RestJsonValidation, "RestJsonMalformedRangeFloat_case1", TestType.MalformedRequest),
781781
FailingTest(RestJsonValidation, "RestJsonMalformedRangeMaxFloat", TestType.MalformedRequest),
782782
FailingTest(RestJsonValidation, "RestJsonMalformedRangeMinFloat", TestType.MalformedRequest),
783-
FailingTest(RestJsonValidation, "RestJsonMalformedPatternSensitiveString", TestType.MalformedRequest),
784783

785784
// See https://github.com/awslabs/smithy-rs/issues/1969
786785
FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeShortOverride_case0", TestType.MalformedRequest),
@@ -872,16 +871,11 @@ class ServerProtocolTestGenerator(
872871
FailingTest("aws.protocoltests.json10#JsonRpc10", "AwsJson10EndpointTrait", TestType.Request),
873872

874873
// AwsJson1.1 failing tests.
875-
FailingTest("aws.protocoltests.json#JsonProtocol", "AwsJson11EndpointTraitWithHostLabel", TestType.Request),
876-
FailingTest("aws.protocoltests.json#JsonProtocol", "AwsJson11EndpointTrait", TestType.Request),
877-
FailingTest("aws.protocoltests.json#JsonProtocol", "parses_httpdate_timestamps", TestType.Response),
878-
FailingTest("aws.protocoltests.json#JsonProtocol", "parses_iso8601_timestamps", TestType.Response),
879-
FailingTest(
880-
"aws.protocoltests.json#JsonProtocol",
881-
"parses_the_request_id_from_the_response",
882-
TestType.Response,
883-
),
884-
874+
FailingTest(AwsJson11, "AwsJson11EndpointTraitWithHostLabel", TestType.Request),
875+
FailingTest(AwsJson11, "AwsJson11EndpointTrait", TestType.Request),
876+
FailingTest(AwsJson11, "parses_httpdate_timestamps", TestType.Response),
877+
FailingTest(AwsJson11, "parses_iso8601_timestamps", TestType.Response),
878+
FailingTest(AwsJson11, "parses_the_request_id_from_the_response", TestType.Response),
885879
)
886880
private val RunOnly: Set<String>? = null
887881

0 commit comments

Comments
 (0)