[typescript-fetch] [BUG] Fix duplication of ModelNamePrefix in import statements (#20109)

This commit is contained in:
ASterdyniak 2024-12-06 11:02:18 +01:00 committed by GitHub
parent 474307675b
commit d29196a1f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 1 deletions

View File

@ -477,7 +477,7 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
HashMap<String, String> tsImport = new HashMap<>();
// TVG: This is used as class name in the import statements of the model file
tsImport.put("classname", im);
tsImport.put("filename", toModelFilename(im));
tsImport.put("filename", convertUsingFileNamingConvention(im));
tsImports.add(tsImport);
}
}

View File

@ -266,6 +266,22 @@ public class TypeScriptFetchClientCodegenTest {
codegen.toApiFilename("FirstSimpleController"));
}
@Test(description = "Verify names of files generated in kebab-case and imports with additional model prefix")
public void testGeneratedFilenamesInPascalCaseWithAdditionalModelPrefix() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put("fileNaming", TypeScriptFetchClientCodegen.PASCAL_CASE);
properties.put(CodegenConstants.MODEL_NAME_PREFIX, "SomePrefix");
File output = generate(properties);
Path pet = Paths.get(output + "/models/SomePrefixPet.ts");
TestUtils.assertFileExists(pet);
TestUtils.assertFileContains(pet, "} from './SomePrefixPetCategory';");
TestUtils.assertFileExists(Paths.get(output + "/models/SomePrefixPetCategory.ts"));
TestUtils.assertFileExists(Paths.get(output + "/apis/PetControllerApi.ts"));
}
@Test(description = "Verify names of files generated in kebab-case and imports")
public void testGeneratedFilenamesInKebabCase() throws IOException {
@ -281,6 +297,22 @@ public class TypeScriptFetchClientCodegenTest {
TestUtils.assertFileExists(Paths.get(output + "/apis/pet-controller-api.ts"));
}
@Test(description = "Verify names of files generated in kebab-case and imports with additional model prefix")
public void testGeneratedFilenamesInKebabCaseWithAdditionalModelPrefix() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put("fileNaming", TypeScriptFetchClientCodegen.KEBAB_CASE);
properties.put(CodegenConstants.MODEL_NAME_PREFIX, "SomePrefix");
File output = generate(properties);
Path pet = Paths.get(output + "/models/some-prefix-pet.ts");
TestUtils.assertFileExists(pet);
TestUtils.assertFileContains(pet, "} from './some-prefix-pet-category';");
TestUtils.assertFileExists(Paths.get(output + "/models/some-prefix-pet-category.ts"));
TestUtils.assertFileExists(Paths.get(output + "/apis/pet-controller-api.ts"));
}
@Test(description = "Verify names of files generated in camelCase and imports")
public void testGeneratedFilenamesInCamelCase() throws IOException {
@ -296,6 +328,22 @@ public class TypeScriptFetchClientCodegenTest {
TestUtils.assertFileExists(Paths.get(output + "/apis/petControllerApi.ts"));
}
@Test(description = "Verify names of files generated in camelCase and imports with additional model prefix")
public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put("fileNaming", TypeScriptFetchClientCodegen.CAMEL_CASE);
properties.put(CodegenConstants.MODEL_NAME_PREFIX, "SomePrefix");
File output = generate(properties);
Path pet = Paths.get(output + "/models/somePrefixPet.ts");
TestUtils.assertFileExists(pet);
TestUtils.assertFileContains(pet, "} from './somePrefixPetCategory';");
TestUtils.assertFileExists(Paths.get(output + "/models/somePrefixPetCategory.ts"));
TestUtils.assertFileExists(Paths.get(output + "/apis/petControllerApi.ts"));
}
private static File generate(Map<String, Object> properties) throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();