Skip to content

Commit de18e9b

Browse files
committed
Add compilation tests for reqiured and optional fields
1 parent a79e7d5 commit de18e9b

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

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

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import org.junit.jupiter.api.Test
1111
import software.amazon.smithy.model.Model
1212
import software.amazon.smithy.model.shapes.ShapeId
1313
import software.amazon.smithy.model.shapes.StructureShape
14+
import software.amazon.smithy.rust.codegen.core.rustlang.rust
1415
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
16+
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest
1517
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestCodegenContext
1618
import software.amazon.smithy.rust.codegen.traits.ValidationExceptionTrait
1719
import software.amazon.smithy.rust.codegen.traits.ValidationFieldListTrait
@@ -204,4 +206,154 @@ internal class CustomValidationExceptionDecoratorTest {
204206

205207
generator shouldBe null
206208
}
209+
210+
private val completeTestModel =
211+
"""
212+
namespace com.aws.example
213+
214+
use aws.protocols#restJson1
215+
use smithy.rust.codegen.traits#validationException
216+
use smithy.rust.codegen.traits#validationFieldList
217+
use smithy.rust.codegen.traits#validationFieldMessage
218+
use smithy.rust.codegen.traits#validationFieldName
219+
use smithy.rust.codegen.traits#validationMessage
220+
221+
@restJson1
222+
service CustomValidationExample {
223+
version: "1.0.0"
224+
operations: [
225+
TestOperation
226+
]
227+
errors: [
228+
MyCustomValidationException
229+
]
230+
}
231+
232+
@http(method: "POST", uri: "/test")
233+
operation TestOperation {
234+
input: TestInput
235+
}
236+
237+
structure TestInput {
238+
@required
239+
@length(min: 1, max: 10)
240+
name: String
241+
242+
@range(min: 1, max: 100)
243+
age: Integer
244+
}
245+
246+
@error("client")
247+
@httpError(400)
248+
@validationException
249+
structure MyCustomValidationException {
250+
@required
251+
@validationMessage
252+
customMessage: String
253+
254+
@required
255+
@default("testReason1")
256+
reason: ValidationExceptionReason
257+
258+
@validationFieldList
259+
customFieldList: CustomValidationFieldList
260+
}
261+
262+
enum ValidationExceptionReason {
263+
TEST_REASON_0 = "testReason0"
264+
TEST_REASON_1 = "testReason1"
265+
}
266+
267+
structure CustomValidationField {
268+
@required
269+
@validationFieldName
270+
customFieldName: String
271+
272+
@required
273+
@validationFieldMessage
274+
customFieldMessage: String
275+
}
276+
277+
list CustomValidationFieldList {
278+
member: CustomValidationField
279+
}
280+
""".asSmithyModel(smithyVersion = "2.0")
281+
282+
@Test
283+
fun `code compiles with custom validation exception`() {
284+
serverIntegrationTest(completeTestModel)
285+
}
286+
287+
private val completeTestModelWithOptionals =
288+
"""
289+
namespace com.aws.example
290+
291+
use aws.protocols#restJson1
292+
use smithy.rust.codegen.traits#validationException
293+
use smithy.rust.codegen.traits#validationFieldList
294+
use smithy.rust.codegen.traits#validationFieldMessage
295+
use smithy.rust.codegen.traits#validationFieldName
296+
use smithy.rust.codegen.traits#validationMessage
297+
298+
@restJson1
299+
service CustomValidationExample {
300+
version: "1.0.0"
301+
operations: [
302+
TestOperation
303+
]
304+
errors: [
305+
MyCustomValidationException
306+
]
307+
}
308+
309+
@http(method: "POST", uri: "/test")
310+
operation TestOperation {
311+
input: TestInput
312+
}
313+
314+
structure TestInput {
315+
@required
316+
@length(min: 1, max: 10)
317+
name: String
318+
319+
@range(min: 1, max: 100)
320+
age: Integer
321+
}
322+
323+
@error("client")
324+
@httpError(400)
325+
@validationException
326+
structure MyCustomValidationException {
327+
@validationMessage
328+
customMessage: String
329+
330+
@default("testReason1")
331+
reason: ValidationExceptionReason
332+
333+
@validationFieldList
334+
customFieldList: CustomValidationFieldList
335+
}
336+
337+
enum ValidationExceptionReason {
338+
TEST_REASON_0 = "testReason0"
339+
TEST_REASON_1 = "testReason1"
340+
}
341+
342+
structure CustomValidationField {
343+
@validationFieldName
344+
customFieldName: String
345+
346+
@validationFieldMessage
347+
customFieldMessage: String
348+
}
349+
350+
list CustomValidationFieldList {
351+
member: CustomValidationField
352+
}
353+
""".asSmithyModel(smithyVersion = "2.0")
354+
355+
@Test
356+
fun `code compiles with custom validation exception using optionals`() {
357+
serverIntegrationTest(completeTestModelWithOptionals)
358+
}
207359
}

0 commit comments

Comments
 (0)