Skip to content

Commit d29196a

Browse files
authored
[typescript-fetch] [BUG] Fix duplication of ModelNamePrefix in import statements (#20109)
1 parent 4743076 commit d29196a

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ private List<Map<String, String>> toTsImports(CodegenModel cm, Set<String> impor
477477
HashMap<String, String> tsImport = new HashMap<>();
478478
// TVG: This is used as class name in the import statements of the model file
479479
tsImport.put("classname", im);
480-
tsImport.put("filename", toModelFilename(im));
480+
tsImport.put("filename", convertUsingFileNamingConvention(im));
481481
tsImports.add(tsImport);
482482
}
483483
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,22 @@ public void testApiFileNameInVariousFormat() {
266266
codegen.toApiFilename("FirstSimpleController"));
267267
}
268268

269+
@Test(description = "Verify names of files generated in kebab-case and imports with additional model prefix")
270+
public void testGeneratedFilenamesInPascalCaseWithAdditionalModelPrefix() throws IOException {
271+
272+
Map<String, Object> properties = new HashMap<>();
273+
properties.put("fileNaming", TypeScriptFetchClientCodegen.PASCAL_CASE);
274+
properties.put(CodegenConstants.MODEL_NAME_PREFIX, "SomePrefix");
275+
276+
File output = generate(properties);
277+
278+
Path pet = Paths.get(output + "/models/SomePrefixPet.ts");
279+
TestUtils.assertFileExists(pet);
280+
TestUtils.assertFileContains(pet, "} from './SomePrefixPetCategory';");
281+
TestUtils.assertFileExists(Paths.get(output + "/models/SomePrefixPetCategory.ts"));
282+
TestUtils.assertFileExists(Paths.get(output + "/apis/PetControllerApi.ts"));
283+
}
284+
269285
@Test(description = "Verify names of files generated in kebab-case and imports")
270286
public void testGeneratedFilenamesInKebabCase() throws IOException {
271287

@@ -281,6 +297,22 @@ public void testGeneratedFilenamesInKebabCase() throws IOException {
281297
TestUtils.assertFileExists(Paths.get(output + "/apis/pet-controller-api.ts"));
282298
}
283299

300+
@Test(description = "Verify names of files generated in kebab-case and imports with additional model prefix")
301+
public void testGeneratedFilenamesInKebabCaseWithAdditionalModelPrefix() throws IOException {
302+
303+
Map<String, Object> properties = new HashMap<>();
304+
properties.put("fileNaming", TypeScriptFetchClientCodegen.KEBAB_CASE);
305+
properties.put(CodegenConstants.MODEL_NAME_PREFIX, "SomePrefix");
306+
307+
File output = generate(properties);
308+
309+
Path pet = Paths.get(output + "/models/some-prefix-pet.ts");
310+
TestUtils.assertFileExists(pet);
311+
TestUtils.assertFileContains(pet, "} from './some-prefix-pet-category';");
312+
TestUtils.assertFileExists(Paths.get(output + "/models/some-prefix-pet-category.ts"));
313+
TestUtils.assertFileExists(Paths.get(output + "/apis/pet-controller-api.ts"));
314+
}
315+
284316
@Test(description = "Verify names of files generated in camelCase and imports")
285317
public void testGeneratedFilenamesInCamelCase() throws IOException {
286318

@@ -296,6 +328,22 @@ public void testGeneratedFilenamesInCamelCase() throws IOException {
296328
TestUtils.assertFileExists(Paths.get(output + "/apis/petControllerApi.ts"));
297329
}
298330

331+
@Test(description = "Verify names of files generated in camelCase and imports with additional model prefix")
332+
public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix() throws IOException {
333+
334+
Map<String, Object> properties = new HashMap<>();
335+
properties.put("fileNaming", TypeScriptFetchClientCodegen.CAMEL_CASE);
336+
properties.put(CodegenConstants.MODEL_NAME_PREFIX, "SomePrefix");
337+
338+
File output = generate(properties);
339+
340+
Path pet = Paths.get(output + "/models/somePrefixPet.ts");
341+
TestUtils.assertFileExists(pet);
342+
TestUtils.assertFileContains(pet, "} from './somePrefixPetCategory';");
343+
TestUtils.assertFileExists(Paths.get(output + "/models/somePrefixPetCategory.ts"));
344+
TestUtils.assertFileExists(Paths.get(output + "/apis/petControllerApi.ts"));
345+
}
346+
299347
private static File generate(Map<String, Object> properties) throws IOException {
300348
File output = Files.createTempDirectory("test").toFile();
301349
output.deleteOnExit();

0 commit comments

Comments
 (0)