refactor: normalize array type notation from Array<T> to T[] in generators and type mapper
This commit is contained in:
@@ -44,7 +44,7 @@ export function generateCleanArchitecture(
|
||||
if (rawProperties[k].$ref) {
|
||||
tsType = rawProperties[k].$ref.split('/').pop() || 'unknown';
|
||||
} else if (rawProperties[k].type === 'array' && rawProperties[k].items?.$ref) {
|
||||
tsType = `Array<${rawProperties[k].items.$ref.split('/').pop()}>`;
|
||||
tsType = `${rawProperties[k].items.$ref.split('/').pop()}[]`;
|
||||
}
|
||||
return {
|
||||
name: k,
|
||||
@@ -163,7 +163,7 @@ export function generateCleanArchitecture(
|
||||
returnBaseType = returnType;
|
||||
} else if (responseSchema.type === 'array' && responseSchema.items?.$ref) {
|
||||
returnBaseType = responseSchema.items.$ref.split('/').pop() || 'unknown';
|
||||
returnType = `Array<${returnBaseType}>`;
|
||||
returnType = `${returnBaseType}[]`;
|
||||
isListContainer = true;
|
||||
}
|
||||
}
|
||||
@@ -203,10 +203,10 @@ export function generateCleanArchitecture(
|
||||
const imports: { classname: string; classFilename: string; classVarName: string }[] = [];
|
||||
Object.keys(schemas).forEach((s) => {
|
||||
const usedAsReturn = tagsMap[tag].some(
|
||||
(op) => op.returnType === s || op.returnType === `Array<${s}>`
|
||||
(op) => op.returnType === s || op.returnType === `${s}[]`
|
||||
);
|
||||
const usedAsParam = tagsMap[tag].some((op) =>
|
||||
op.allParams.some((p) => p.dataType === s || p.dataType === `Array<${s}>`)
|
||||
op.allParams.some((p) => p.dataType === s || p.dataType === `${s}[]`)
|
||||
);
|
||||
if (usedAsReturn || usedAsParam) {
|
||||
imports.push({
|
||||
|
||||
@@ -61,7 +61,7 @@ export function organizeFiles(tempDir: string, outputDir: string): void {
|
||||
logSuccess(`${filesMoved} DTOs movidos correctamente`);
|
||||
}
|
||||
|
||||
/** Post-procesa los DTOs generados añadiendo los imports de tipos referenciados. */
|
||||
/** Post-procesa los DTOs generados añadiendo imports y normalizando Array<T> → T[]. */
|
||||
export function addDtoImports(outputDir: string): void {
|
||||
logStep('Añadiendo imports a los DTOs generados...');
|
||||
|
||||
@@ -85,11 +85,15 @@ export function addDtoImports(outputDir: string): void {
|
||||
|
||||
files.forEach((file) => {
|
||||
const filePath = path.join(dtosDir, file);
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
const originalContent = fs.readFileSync(filePath, 'utf8');
|
||||
let content = originalContent;
|
||||
|
||||
const selfMatch = content.match(/export interface (\w+)/);
|
||||
const selfName = selfMatch ? selfMatch[1] : '';
|
||||
|
||||
// Normalize Array<T> → T[] (openapi-generator-cli always outputs Array<T>)
|
||||
content = content.replace(/Array<(\w+)>/g, '$1[]');
|
||||
|
||||
// Find all Dto type references in the file body (excluding the interface name itself)
|
||||
const references = new Set<string>();
|
||||
const typeRegex = /\b(\w+Dto)\b/g;
|
||||
@@ -100,8 +104,6 @@ export function addDtoImports(outputDir: string): void {
|
||||
}
|
||||
}
|
||||
|
||||
if (references.size === 0) return;
|
||||
|
||||
// Build import lines for each referenced type that exists in the dtoMap
|
||||
const imports: string[] = [];
|
||||
references.forEach((ref) => {
|
||||
@@ -112,9 +114,12 @@ export function addDtoImports(outputDir: string): void {
|
||||
|
||||
if (imports.length > 0) {
|
||||
content = imports.join('\n') + '\n' + content;
|
||||
}
|
||||
|
||||
if (content !== originalContent) {
|
||||
fs.writeFileSync(filePath, content);
|
||||
filesProcessed++;
|
||||
logInfo(` Imports añadidos a ${file}`);
|
||||
logInfo(` Procesado ${file}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ export function mapSwaggerTypeToTs(type?: string): string {
|
||||
string: 'string',
|
||||
boolean: 'boolean',
|
||||
number: 'number',
|
||||
array: 'Array<unknown>',
|
||||
array: 'unknown[]',
|
||||
object: 'unknown'
|
||||
};
|
||||
return typeMap[type] || 'unknown';
|
||||
|
||||
Reference in New Issue
Block a user