|
17 | 17 |
|
18 | 18 | import static org.openapitools.codegen.utils.StringUtils.underscore; |
19 | 19 |
|
| 20 | +import java.io.File; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.List; |
| 24 | + |
20 | 25 | import org.openapitools.codegen.*; |
| 26 | +import org.openapitools.codegen.config.CodegenConfigurator; |
21 | 27 | import org.openapitools.codegen.languages.CppHttplibServerCodegen; |
22 | 28 | import org.testng.Assert; |
23 | 29 | import org.testng.annotations.Test; |
@@ -144,4 +150,63 @@ public void testApiNamespaceHandling() { |
144 | 150 |
|
145 | 151 | Assert.assertEquals(codegen.additionalProperties().get("apiNamespace"), "myapp::api"); |
146 | 152 | } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testDefaultModelNamespaceIsSetBeforeAnyModelIsProcessed() { |
| 156 | + CppHttplibServerCodegen fresh = new CppHttplibServerCodegen(); |
| 157 | + fresh.processOpts(); |
| 158 | + |
| 159 | + Assert.assertEquals(fresh.additionalProperties().get("modelNamespace"), "models"); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testSpecWithoutModelsGeneratesPrimitiveResponses() throws Exception { |
| 164 | + // #24854: a spec with no schemas never reaches postProcessAllModels |
| 165 | + File output = Files.createTempDirectory("test").toFile(); |
| 166 | + output.deleteOnExit(); |
| 167 | + |
| 168 | + final CodegenConfigurator configurator = new CodegenConfigurator() |
| 169 | + .setGeneratorName("cpp-httplib-server") |
| 170 | + .setInputSpec("src/test/resources/3_1/cpp-httplib-server/primitive-responses-no-models.yaml") |
| 171 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 172 | + |
| 173 | + List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate(); |
| 174 | + files.forEach(File::deleteOnExit); |
| 175 | + |
| 176 | + Path apiHeader = files.stream() |
| 177 | + .map(File::toPath) |
| 178 | + .filter(path -> path.toString().endsWith("Api.h")) |
| 179 | + .findFirst() |
| 180 | + .orElseThrow(() -> new AssertionError("no API header generated: " + files)); |
| 181 | + TestUtils.assertFileContains(apiHeader, |
| 182 | + "using TextGetResponse = std::string;", |
| 183 | + "using FileGetResponse = std::string;"); |
| 184 | + // Without a model header the source must include the JSON library itself |
| 185 | + // and must not name the (undeclared) model namespace. |
| 186 | + Path apiSource = Path.of(apiHeader.toString().replaceAll("\\.h$", ".cpp")); |
| 187 | + TestUtils.assertFileContains(apiSource, "#include <nlohmann/json.hpp>"); |
| 188 | + TestUtils.assertFileNotContains(apiSource, "using namespace models;"); |
| 189 | + for (File file : files) { |
| 190 | + if (file.getName().endsWith(".h") || file.getName().endsWith(".cpp")) { |
| 191 | + TestUtils.assertFileNotContains(file.toPath(), "String.h", "File.h"); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testApiSourceUsesModelNamespaceWhenModelsAreUsed() throws Exception { |
| 198 | + File output = Files.createTempDirectory("test").toFile(); |
| 199 | + output.deleteOnExit(); |
| 200 | + |
| 201 | + final CodegenConfigurator configurator = new CodegenConfigurator() |
| 202 | + .setGeneratorName("cpp-httplib-server") |
| 203 | + .setInputSpec("src/test/resources/3_0/cpp-httplib-server/petstore.json") |
| 204 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 205 | + |
| 206 | + List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate(); |
| 207 | + files.forEach(File::deleteOnExit); |
| 208 | + |
| 209 | + Path petApiSource = output.toPath().resolve("api/PetApi.cpp"); |
| 210 | + TestUtils.assertFileContains(petApiSource, "using namespace models;", "#include <nlohmann/json.hpp>"); |
| 211 | + } |
147 | 212 | } |
0 commit comments