Browse Source

Merge pull request #19877 from abpframework/issue/19656

Fix generic type import attempt to models file
pull/19908/head
Masum ULU 2 years ago
committed by GitHub
parent
commit
29f57b8ca1
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 28
      npm/ng-packs/packages/schematics/src/commands/api/index.ts
  2. 34
      npm/ng-packs/packages/schematics/src/utils/model.ts
  3. 29
      npm/ng-packs/packages/schematics/src/utils/type.ts

28
npm/ng-packs/packages/schematics/src/commands/api/index.ts

@ -48,21 +48,28 @@ export default function (schema: GenerateProxySchema) {
const createProxyConfigWriter = createProxyConfigWriterCreator(targetPath);
const data = readProxyConfig(tree);
data.types = sanitizeTypeNames(data.types);
const types = data.types;
const modules = data.modules;
const serviceType = schema.serviceType || defaultEServiceType;
if (!types || !modules) throw new SchematicsException(Exception.InvalidApiDefinition);
if (!types || !modules) {
throw new SchematicsException(Exception.InvalidApiDefinition);
}
const definition = data.modules[moduleName];
if (!definition)
if (!definition) {
throw new SchematicsException(interpolate(Exception.InvalidModule, moduleName));
}
const apiName = definition.remoteServiceName;
data.modules[moduleName].controllers = sanitizeControllerTypeNames(definition.controllers);
const controllers = filterControllersByServiceType(
serviceType,
data.modules[moduleName].controllers,
);
const serviceImports: Record<string, string[]> = {};
const generateServices = createServiceGenerator({
targetPath,
@ -90,8 +97,12 @@ export default function (schema: GenerateProxySchema) {
modelImports,
});
if (!data.generated.includes(moduleName)) data.generated.push(moduleName);
if (!data.generated.includes(moduleName)) {
data.generated.push(moduleName);
}
data.generated.sort();
const json = generateProxyConfigJson(data);
const overwriteProxyConfig = createProxyConfigWriter('overwrite', json);
@ -129,11 +140,18 @@ function createModelGenerator(params: ModelGeneratorParams) {
const { targetPath, serviceImports, modelImports } = params;
const reduceImportRefsToModels = createImportRefsToModelReducer(params);
const models = Object.values(serviceImports).reduce(reduceImportRefsToModels, []);
models.forEach(({ imports }) =>
imports.forEach(({ refs, path }) =>
refs.forEach(ref => {
if (path === '@abp/ng.core') return;
if (!modelImports[path]) return (modelImports[path] = [ref]);
if (path === '@abp/ng.core') {
return;
}
if (!modelImports[path]) {
return (modelImports[path] = [ref]);
}
modelImports[path] = [...new Set([...modelImports[path], ref])];
}),
),

34
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -71,23 +71,30 @@ export function createImportRefsToModelReducer(params: ModelGeneratorParams) {
model.interfaces.forEach(_interface => {
const { baseType } = types[_interface.ref];
if (baseType && parseNamespace(solution, baseType) !== model.namespace){
if (baseType && parseNamespace(solution, baseType) !== model.namespace) {
const baseTypeWithGenericParams = parseBaseTypeWithGenericTypes(baseType);
baseTypeWithGenericParams.forEach(t => {
toBeImported.push({
type: t,
isEnum: false,
});
})
});
}
}
[..._interface.properties, ..._interface.generics].forEach(prop => {
prop.refs.forEach(ref => {
const propType = types[ref];
if (!propType) return;
if (propType.isEnum) toBeImported.push({ type: ref, isEnum: true });
else if (parseNamespace(solution, ref) !== model.namespace)
if (!propType) {
return;
}
if (propType.isEnum) {
toBeImported.push({ type: ref, isEnum: true });
}
if (parseNamespace(solution, ref) !== model.namespace) {
toBeImported.push({ type: ref, isEnum: false });
}
});
});
});
@ -176,20 +183,21 @@ export function createRefToImportReducerCreator(params: ModelGeneratorParams) {
}
function isOptionalProperty(prop: PropertyDef) {
return (prop.typeSimple.endsWith('?') || (prop.typeSimple === 'string' && !prop.isRequired));
return prop.typeSimple.endsWith('?') || (prop.typeSimple === 'string' && !prop.isRequired);
}
export function parseBaseTypeWithGenericTypes(type: string): string[] {
export function parseBaseTypeWithGenericTypes(type: string): string[] {
const parsedTypeNode = parseGenerics(type);
const nodeToText = (node: TypeNode,acc:string[] = []): string[] => {
const nodeToText = (node: TypeNode, acc: string[] = []): string[] => {
acc.push(node.data);
if(node.children && node.children.length > 0){
if (node.children && node.children.length > 0) {
node.children.forEach(child => {
nodeToText(child,acc);
})
nodeToText(child, acc);
});
}
return acc;
}
};
return nodeToText(parsedTypeNode);
}

29
npm/ng-packs/packages/schematics/src/utils/type.ts

@ -14,7 +14,7 @@ export function createTypeSimplifier() {
);
type = /any</.test(type) ? 'any' : type;
const { identifier, generics, array} = extractSimpleGenerics(type);
const { identifier, generics, array } = extractSimpleGenerics(type);
return generics.length ? `${identifier}<${generics.join(', ')}>${array}` : identifier;
});
@ -64,12 +64,21 @@ export function createTypesToImportsReducer(solution: string, namespace: string)
return (imports: Import[], types: TypeWithEnum[]) => {
types.forEach(({ type, isEnum }) => {
const newImport = mapTypeToImport(type, isEnum);
if (!newImport) return;
if (!newImport) {
return;
}
if(newImport.specifiers.some(f => f.toLocaleLowerCase() === type.toLocaleLowerCase())){
return;
}
const existingImport = imports.find(
({ keyword, path }) => keyword === newImport.keyword && path === newImport.path,
);
if (!existingImport) return imports.push(newImport);
if (!existingImport){
return imports.push(newImport);
}
existingImport.refs = [...new Set([...existingImport.refs, ...newImport.refs])];
existingImport.specifiers = [
@ -91,11 +100,15 @@ export function createTypeToImportMapper(solution: string, namespace: string) {
const modelNamespace = parseNamespace(solution, type);
const refs = [removeTypeModifiers(type)];
const specifiers = [adaptType(simplifyType(refs[0]).split('<')[0])];
const path = VOLO_REGEX.test(type)
? '@abp/ng.core'
: isEnum
? relativePathToEnum(namespace, modelNamespace, specifiers[0])
: relativePathToModel(namespace, modelNamespace);
let path = relativePathToModel(namespace, modelNamespace);
if (VOLO_REGEX.test(type)) {
path = '@abp/ng.core';
}
if (isEnum) {
path = relativePathToEnum(namespace, modelNamespace, specifiers[0]);
}
return new Import({ keyword: eImportKeyword.Type, path, refs, specifiers });
};

Loading…
Cancel
Save