|
25 | 25 | import java.io.*; |
26 | 26 | import java.nio.charset.StandardCharsets; |
27 | 27 | import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
28 | 29 | import java.util.ArrayList; |
29 | 30 | import java.util.List; |
30 | 31 | import java.util.Locale; |
@@ -93,6 +94,54 @@ public void testKeywords() { |
93 | 94 | } |
94 | 95 | } |
95 | 96 |
|
| 97 | + @Test |
| 98 | + public void testImportMappingsInSerializersAndBarrelFile() throws IOException { |
| 99 | + File output = Files.createTempDirectory("test").toFile(); |
| 100 | + output.deleteOnExit(); |
| 101 | + |
| 102 | + final String pubName = "my_api"; |
| 103 | + final CodegenConfigurator configurator = new CodegenConfigurator() |
| 104 | + .setGeneratorName("dart-dio") |
| 105 | + .setGitUserId("my-user") |
| 106 | + .setGitRepoId("my-repo") |
| 107 | + .setInputSpec("src/test/resources/3_0/dart-dio/import_mapping.yaml") |
| 108 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 109 | + |
| 110 | + configurator.addAdditionalProperty("pubName", pubName); |
| 111 | + configurator.addTypeMapping("Address", "CustomAddress"); |
| 112 | + configurator.addImportMapping("CustomAddress", "package:my_api/src/custom_models/custom_address.dart"); |
| 113 | + |
| 114 | + ClientOptInput opts = configurator.toClientOptInput(); |
| 115 | + |
| 116 | + Generator generator = new DefaultGenerator().opts(opts); |
| 117 | + List<File> files = generator.generate(); |
| 118 | + files.forEach(File::deleteOnExit); |
| 119 | + |
| 120 | + // The model file for the mapped type should NOT be generated |
| 121 | + TestUtils.assertFileNotExists(output.toPath().resolve("lib/src/model/custom_address.dart")); |
| 122 | + |
| 123 | + // order_out.dart should use the custom import path (this already works per the issue report) |
| 124 | + Path orderOutPath = output.toPath().resolve("lib/src/model/order_out.dart"); |
| 125 | + TestUtils.assertFileContains(orderOutPath, |
| 126 | + "package:my_api/src/custom_models/custom_address.dart"); |
| 127 | + TestUtils.assertFileNotContains(orderOutPath, |
| 128 | + "package:my_api/src/model/custom_address.dart"); |
| 129 | + |
| 130 | + // serializers.dart should use the custom import path, not the hardcoded model/ path |
| 131 | + Path serializersPath = output.toPath().resolve("lib/src/serializers.dart"); |
| 132 | + TestUtils.assertFileContains(serializersPath, |
| 133 | + "package:my_api/src/custom_models/custom_address.dart"); |
| 134 | + TestUtils.assertFileNotContains(serializersPath, |
| 135 | + "package:my_api/src/model/custom_address.dart"); |
| 136 | + |
| 137 | + // The barrel file should use the custom export path, not the hardcoded model/ path |
| 138 | + Path barrelPath = output.toPath().resolve("lib/my_api.dart"); |
| 139 | + TestUtils.assertFileContains(barrelPath, |
| 140 | + "package:my_api/src/custom_models/custom_address.dart"); |
| 141 | + TestUtils.assertFileNotContains(barrelPath, |
| 142 | + "package:my_api/src/model/custom_address.dart"); |
| 143 | + } |
| 144 | + |
96 | 145 | @Test |
97 | 146 | public void verifyDartDioGeneratorRuns() throws IOException { |
98 | 147 | File output = Files.createTempDirectory("test").toFile(); |
|
0 commit comments