[TS-FETCH] Generate models/index.ts in postProcessModels (#18256)

* [TS-FETCH] Generate models.index in postProcessModels

* [TS-FETCH] Add test for spec without path
This commit is contained in:
gianlucaparadise
2024-04-22 08:31:08 +02:00
committed by GitHub
parent 73bca68fea
commit d25c5a8b26
3 changed files with 63 additions and 0 deletions

View File

@@ -340,6 +340,12 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
this.processCodeGenModel(cm);
}
// Add supporting file only if we plan to generate files in /models
if (!objs.isEmpty() && !addedModelIndex) {
addedModelIndex = true;
supportingFiles.add(new SupportingFile("models.index.mustache", modelPackage().replace('.', File.separatorChar), "index.ts"));
}
return objs;
}

View File

@@ -6,8 +6,12 @@ import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.Generator;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.AbstractTypeScriptClientCodegen;
@@ -17,6 +21,14 @@ import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@Test(groups = {TypeScriptGroups.TYPESCRIPT, TypeScriptGroups.TYPESCRIPT_FETCH})
@@ -55,6 +67,32 @@ public class TypeScriptFetchClientCodegenTest {
Assert.assertEquals(operation.isResponseOptional, true);
}
@Test
public void testModelsWithoutPaths() throws IOException {
final String specPath = "src/test/resources/3_1/reusable-components-without-paths.yaml";
Map<String, Object> properties = new HashMap<>();
properties.put("supportsES6", true);
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("typescript-fetch")
.setInputSpec(specPath)
.setAdditionalProperties(properties)
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
Generator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);
TestUtils.assertFileExists(Paths.get(output + "/index.ts"));
TestUtils.assertFileExists(Paths.get(output + "/runtime.ts"));
TestUtils.assertFileExists(Paths.get(output + "/models/Pet.ts"));
TestUtils.assertFileExists(Paths.get(output + "/models/index.ts"));
}
@Test
public void testWithoutSnapshotVersion() {
OpenAPI api = TestUtils.createOpenAPI();

View File

@@ -0,0 +1,19 @@
openapi: 3.1.0
info:
title: Reusable components without paths example
version: 1.0.0
# Since OAS 3.1.0 the paths element isn't necessary. Now a valid OpenAPI Document can describe only paths, webhooks, or even only reusable components
components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string