Skip to content

Commit a1040c6

Browse files
committed
Error out if ignoreUnsupportedConstraintTraits has no effect
Now that constraint traits are supported in server SDKs (with some corner case caveats, see #1401), this flag will almost always be useless for those early adopters of constraint traits. It is thus convenient to inform the user that they should remove it.
1 parent 421488e commit a1040c6

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

CHANGELOG.next.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ message = "Add `into_segments` method to `AggregatedBytes`, for zero-copy conver
8080
references = ["smithy-rs#2525"]
8181
meta = { "breaking" = false, "tada" = false, "bug" = false }
8282
author = "parker-timmerman"
83+
84+
[[smithy-rs]]
85+
message = "Code generation will abort if the `ignoreUnsupportedConstraints` codegen flag has no effect, that is, if all constraint traits used in your model are well-supported. Please remove the flag in such case."
86+
references = ["smithy-rs#2539"]
87+
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "server" }
88+
author = "david-perez"

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ data class LogMessage(val level: Level, val message: String)
158158
data class ValidationResult(val shouldAbort: Boolean, val messages: List<LogMessage>) :
159159
Throwable(message = messages.joinToString("\n") { it.message })
160160

161-
private val unsupportedConstraintsOnMemberShapes = allConstraintTraits - RequiredTrait::class.java
162-
163161
/**
164162
* Validate that all constrained operations have the shape [validationExceptionShapeId] shape attached to their errors.
165163
*/
@@ -280,7 +278,7 @@ fun validateUnsupportedConstraints(
280278
.toSet()
281279

282280
val messages =
283-
unsupportedLengthTraitOnStreamingBlobShapeSet.map {
281+
(unsupportedLengthTraitOnStreamingBlobShapeSet.map {
284282
it.intoLogMessage(codegenConfig.ignoreUnsupportedConstraints)
285283
} +
286284
unsupportedConstraintShapeReachableViaAnEventStreamSet.map {
@@ -289,7 +287,17 @@ fun validateUnsupportedConstraints(
289287
unsupportedRangeTraitOnShapeSet.map { it.intoLogMessage(codegenConfig.ignoreUnsupportedConstraints) } +
290288
mapShapeReachableFromUniqueItemsListShapeSet.map {
291289
it.intoLogMessage(codegenConfig.ignoreUnsupportedConstraints)
292-
}
290+
}).toMutableList()
291+
292+
if (messages.isEmpty() && codegenConfig.ignoreUnsupportedConstraints) {
293+
messages += LogMessage(
294+
Level.SEVERE,
295+
"""
296+
The `ignoreUnsupportedConstraints` flag in the `codegen` configuration is set to `true`, but it has no
297+
effect. All the constraint traits used in the model are well-supported, please remove this flag.
298+
""".trimIndent().replace("\n", " ")
299+
)
300+
}
293301

294302
return ValidationResult(shouldAbort = messages.any { it.level == Level.SEVERE }, messages)
295303
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.transformers.EventStreamN
1919
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
2020
import software.amazon.smithy.rust.codegen.core.util.lookup
2121
import software.amazon.smithy.rust.codegen.server.smithy.customizations.SmithyValidationExceptionConversionGenerator
22+
import java.io.File
2223
import java.util.logging.Level
2324

2425
internal class ValidateUnsupportedConstraintsAreNotUsedTest {
@@ -37,7 +38,7 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
3738
"""
3839

3940
private fun validateModel(model: Model, serverCodegenConfig: ServerCodegenConfig = ServerCodegenConfig()): ValidationResult {
40-
val service = model.lookup<ServiceShape>("test#TestService")
41+
val service = model.serviceShapes.first()
4142
return validateUnsupportedConstraints(model, service, serverCodegenConfig)
4243
}
4344

@@ -100,7 +101,7 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
100101
""".trimIndent().replace("\n", " ")
101102
}
102103

103-
val constrainedShapesInEventStreamModel =
104+
private val constrainedShapesInEventStreamModel =
104105
"""
105106
$baseModel
106107
@@ -242,4 +243,25 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest {
242243
validationResult.messages shouldHaveAtLeastSize 1
243244
validationResult.messages.shouldForAll { it.level shouldBe Level.WARNING }
244245
}
246+
247+
@Test
248+
fun `it should abort when ignoreUnsupportedConstraints is true and all used constraints are supported`() {
249+
val allConstraintTraitsAreSupported = File("../codegen-core/common-test-models/constraints.smithy")
250+
.readText()
251+
.asSmithyModel()
252+
253+
val validationResult = validateModel(
254+
allConstraintTraitsAreSupported,
255+
ServerCodegenConfig().copy(ignoreUnsupportedConstraints = true),
256+
)
257+
258+
validationResult.messages shouldHaveSize 1
259+
validationResult.shouldAbort shouldBe true
260+
validationResult.messages[0].message shouldContain(
261+
"""
262+
The `ignoreUnsupportedConstraints` flag in the `codegen` configuration is set to `true`, but it has no
263+
effect. All the constraint traits used in the model are well-supported, please remove this flag.
264+
""".trimIndent().replace("\n", " ")
265+
)
266+
}
245267
}

0 commit comments

Comments
 (0)