|
| 1 | +package software.amazon.smithy.rust.codegen.server.python.smithy.generators |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test |
| 4 | +import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter |
| 5 | +import software.amazon.smithy.rust.codegen.core.rustlang.rust |
| 6 | +import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel |
| 7 | +import software.amazon.smithy.rust.codegen.core.testutil.unitTest |
| 8 | +import software.amazon.smithy.rust.codegen.server.python.smithy.testutil.cargoTest |
| 9 | +import software.amazon.smithy.rust.codegen.server.python.smithy.testutil.executePythonServerCodegenVisitor |
| 10 | +import software.amazon.smithy.rust.codegen.server.python.smithy.testutil.generatePythonServerPluginContext |
| 11 | +import kotlin.io.path.appendText |
| 12 | + |
| 13 | +internal class PythonServerRequiredPrecedeOptionalTest { |
| 14 | + @Test |
| 15 | + fun `mandatory fields are reordered to be before optional`() { |
| 16 | + val model = |
| 17 | + """ |
| 18 | + namespace test |
| 19 | +
|
| 20 | + use aws.protocols#restJson1 |
| 21 | + use smithy.framework#ValidationException |
| 22 | +
|
| 23 | + @restJson1 |
| 24 | + service SampleService { |
| 25 | + operations: [ |
| 26 | + OpWithIncorrectOrder, OpWithCorrectOrder, OpWithDefaults |
| 27 | + ], |
| 28 | + } |
| 29 | +
|
| 30 | + @http(method: "POST", uri: "/opIncorrect") |
| 31 | + operation OpWithIncorrectOrder { |
| 32 | + input:= { |
| 33 | + a: String |
| 34 | + @required |
| 35 | + b: String |
| 36 | + c: String |
| 37 | + @required |
| 38 | + d: String |
| 39 | + } |
| 40 | + output:= { |
| 41 | + a: String |
| 42 | + @required |
| 43 | + b: String |
| 44 | + c: String |
| 45 | + @required |
| 46 | + d: String |
| 47 | + } |
| 48 | + errors: [ValidationException] |
| 49 | + } |
| 50 | +
|
| 51 | + @http(method: "POST", uri: "/opCorrect") |
| 52 | + operation OpWithCorrectOrder { |
| 53 | + input:= { |
| 54 | + @required |
| 55 | + b: String |
| 56 | + @required |
| 57 | + d: String |
| 58 | + a: String |
| 59 | + c: String |
| 60 | + } |
| 61 | + output:= { |
| 62 | + @required |
| 63 | + b: String |
| 64 | + @required |
| 65 | + d: String |
| 66 | + a: String |
| 67 | + c: String |
| 68 | + } |
| 69 | + errors: [ValidationException] |
| 70 | + } |
| 71 | +
|
| 72 | + @http(method: "POST", uri: "/opWithDefaults") |
| 73 | + operation OpWithDefaults { |
| 74 | + input:= { |
| 75 | + a: String, |
| 76 | + b: String = "hi" |
| 77 | + } |
| 78 | + } |
| 79 | + """.asSmithyModel(smithyVersion = "2") |
| 80 | + |
| 81 | + val (pluginCtx, testDir) = generatePythonServerPluginContext(model) |
| 82 | + executePythonServerCodegenVisitor(pluginCtx) |
| 83 | + |
| 84 | + val writer = RustWriter.forModule("service") |
| 85 | + writer.unitTest("test_required_fields") { |
| 86 | + rust( |
| 87 | + """ |
| 88 | + use crate::input; |
| 89 | + use pyo3::{types::IntoPyDict, Python}; |
| 90 | +
|
| 91 | + pyo3::prepare_freethreaded_python(); |
| 92 | + Python::with_gil(|py| { |
| 93 | + let globals = [ |
| 94 | + ( |
| 95 | + "OpWithIncorrectOrderInput", |
| 96 | + py.get_type::<input::OpWithIncorrectOrderInput>(), |
| 97 | + ), |
| 98 | + ( |
| 99 | + "OpWithCorrectOrderInput", |
| 100 | + py.get_type::<input::OpWithCorrectOrderInput>(), |
| 101 | + ), |
| 102 | + ( |
| 103 | + "OpWithDefaultsInput", |
| 104 | + py.get_type::<input::OpWithDefaultsInput>(), |
| 105 | + )] |
| 106 | + .into_py_dict(py); |
| 107 | + let locals = [( |
| 108 | + "OpWithIncorrectOrderInput", |
| 109 | + py.get_type::<input::OpWithIncorrectOrderInput>(), |
| 110 | + )] |
| 111 | + .into_py_dict(py); |
| 112 | +
|
| 113 | + py.run( |
| 114 | + "input = OpWithIncorrectOrderInput(\"b\", \"d\")", |
| 115 | + Some(globals), |
| 116 | + Some(locals), |
| 117 | + ) |
| 118 | + .unwrap(); |
| 119 | +
|
| 120 | + // Python should have been able to construct input. |
| 121 | + let input = locals |
| 122 | + .get_item("input") |
| 123 | + .expect("Python exception occurred during dictionary lookup") |
| 124 | + .unwrap() |
| 125 | + .extract::<input::OpWithIncorrectOrderInput>() |
| 126 | + .unwrap(); |
| 127 | + assert_eq!(input.b, "b"); |
| 128 | + assert_eq!(input.d, "d"); |
| 129 | +
|
| 130 | + py.run( |
| 131 | + "input = OpWithCorrectOrderInput(\"b\", \"d\")", |
| 132 | + Some(globals), |
| 133 | + Some(locals), |
| 134 | + ) |
| 135 | + .unwrap(); |
| 136 | +
|
| 137 | + // Python should have been able to construct input. |
| 138 | + let input = locals |
| 139 | + .get_item("input") |
| 140 | + .expect("Python exception occurred during dictionary lookup") |
| 141 | + .unwrap() |
| 142 | + .extract::<input::OpWithCorrectOrderInput>() |
| 143 | + .unwrap(); |
| 144 | + assert_eq!(input.b, "b"); |
| 145 | + assert_eq!(input.d, "d"); |
| 146 | +
|
| 147 | + py.run( |
| 148 | + "input = OpWithDefaultsInput(\"a\")", |
| 149 | + Some(globals), |
| 150 | + Some(locals), |
| 151 | + ) |
| 152 | + .unwrap(); |
| 153 | +
|
| 154 | + // KanchaBilla |
| 155 | + // Python should have been able to construct input. |
| 156 | + let input = locals |
| 157 | + .get_item("input") |
| 158 | + .expect("Python exception occurred during dictionary lookup") |
| 159 | + .unwrap() |
| 160 | + .extract::<input::OpWithDefaultsInput>() |
| 161 | + .unwrap(); |
| 162 | + assert_eq!(input.a, Some("a".to_string())); |
| 163 | + assert_eq!(input.b, "hi"); |
| 164 | + }); |
| 165 | + """, |
| 166 | + ) |
| 167 | + } |
| 168 | + |
| 169 | + testDir.resolve("src/service.rs").appendText(writer.toString()) |
| 170 | + |
| 171 | + cargoTest(testDir) |
| 172 | + } |
| 173 | +} |
0 commit comments