Browse Source

Merge pull request #26213 from abpframework/auto-merge-forward/rel-10.7-to-dev-59

Auto-merge forward rel-10.7 → dev
pull/26220/head
Volosoft Agent 2 months ago
committed by GitHub
parent
commit
925e22d4e6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 169
      npm/ng-packs/packages/schematics/src/tests/proxy-package-proxy-imports.spec.ts
  2. 72
      npm/ng-packs/packages/schematics/src/tests/proxy-self-import-generic-base.spec.ts
  3. 23
      npm/ng-packs/packages/schematics/src/utils/model.ts
  4. 7
      npm/ng-packs/packages/schematics/src/utils/type.ts

169
npm/ng-packs/packages/schematics/src/tests/proxy-package-proxy-imports.spec.ts

@ -0,0 +1,169 @@
import { describe, expect, it } from 'vitest';
import { Action, Controller, Type } from '../models/api-definition';
import { createImportRefsToModelReducer, ModelGeneratorParams } from '../utils/model';
import { createControllerToServiceMapper } from '../utils/service';
import { createTypesToImportsReducer } from '../utils/type';
// DTOs listed in VOLO_PACKAGE_PROXY_IMPORTS (e.g. EntityChangeDto) are not generated locally,
// they must be imported from their package proxy (e.g. @volo/abp.ng.audit-logging/proxy).
describe('proxy generation - package proxy imports', () => {
const solution = 'Acme.BookStore';
const auditLoggingProxy = '@volo/abp.ng.audit-logging/proxy';
const dto = (baseType: string | null, properties: [string, string][] = []): Type => ({
baseType,
isEnum: false,
enumNames: null,
enumValues: null,
genericArguments: null,
properties: properties.map(([name, type]) => ({
name,
jsonName: null,
type,
typeSimple: type,
isRequired: false,
isNullable: false,
})),
});
const types: Record<string, Type> = {
'Volo.Abp.AuditLogging.AuditLogDto': dto(
'Volo.Abp.Application.Dtos.ExtensibleEntityDto<System.Guid>',
[['UserName', 'System.String']],
),
'Volo.Abp.AuditLogging.EntityChangeDto': dto(
'Volo.Abp.Application.Dtos.ExtensibleEntityDto<System.Guid>',
[['AuditLogId', 'System.Guid']],
),
'Volo.Abp.AuditLogging.EntityChangeWithUsernameDto': dto(null, [
['EntityChange', 'Volo.Abp.AuditLogging.EntityChangeDto'],
['UserName', 'System.String'],
]),
'Volo.Abp.AuditLogging.EntityChangePagedResultDto': dto(
'Volo.Abp.Application.Dtos.PagedResultDto<Volo.Abp.AuditLogging.EntityChangeDto>',
),
'Volo.Abp.AuditLogging.LocalListBase<T0>': {
...dto(null, [['Items', '[T]']]),
genericArguments: ['T'],
},
'Volo.Abp.AuditLogging.EntityChangeListDto': dto(
'Volo.Abp.AuditLogging.LocalListBase<Volo.Abp.AuditLogging.EntityChangeDto>',
[['Total', 'System.Int32']],
),
};
const params: ModelGeneratorParams = {
targetPath: 'src/app/proxy',
solution,
types,
serviceImports: {},
modelImports: {},
};
const generateModel = (ref: string) => {
const models = createImportRefsToModelReducer(params)([], [ref]);
const model = models.find(m => m.namespace === 'Volo.Abp.AuditLogging');
expect(model).toBeDefined();
return model!;
};
it('imports a same-namespace package DTO used by a property from its package proxy', () => {
const model = generateModel('Volo.Abp.AuditLogging.EntityChangeWithUsernameDto');
expect(model.interfaces.map(i => i.identifier)).toEqual(['EntityChangeWithUsernameDto']);
expect(model.imports.find(i => i.path === auditLoggingProxy)?.specifiers).toEqual([
'EntityChangeDto',
]);
expect(model.imports.some(i => i.path === './models')).toBe(false);
});
it('imports a package DTO used as a generic argument of an external base type', () => {
const model = generateModel('Volo.Abp.AuditLogging.EntityChangePagedResultDto');
expect(model.imports.find(i => i.path === auditLoggingProxy)?.specifiers).toEqual([
'EntityChangeDto',
]);
expect(model.imports.some(i => i.path === './models')).toBe(false);
});
it('imports a package DTO used as a generic argument of a same-namespace base type', () => {
const model = generateModel('Volo.Abp.AuditLogging.EntityChangeListDto');
expect(model.imports.find(i => i.path === auditLoggingProxy)?.specifiers).toEqual([
'EntityChangeDto',
]);
expect(model.imports.some(i => i.path === './models')).toBe(false);
});
it('imports package DTOs from the package proxy and other DTOs from local models', () => {
const reduceTypesToImports = createTypesToImportsReducer(solution, 'EntityHistory');
const imports = reduceTypesToImports(
[],
[
{ type: 'Volo.Abp.AuditLogging.EntityChangeDto', isEnum: false },
{ type: 'Volo.Abp.AuditLogging.EntityChangeWithUsernameDto', isEnum: false },
],
);
expect(imports.map(i => ({ path: i.path, specifiers: i.specifiers }))).toEqual([
{ path: auditLoggingProxy, specifiers: ['EntityChangeDto'] },
{ path: '../volo/abp/audit-logging/models', specifiers: ['EntityChangeWithUsernameDto'] },
]);
});
it('imports package DTOs returned by a service from the package proxy', () => {
const action = (name: string, type: string, typeSimple: string) =>
({
uniqueName: name,
name,
httpMethod: 'GET',
url: `api/app/entity-history/${name}`,
supportedVersions: [],
parametersOnMethod: [],
parameters: [],
returnValue: { type, typeSimple },
}) as unknown as Action;
const controller = {
controllerName: 'EntityHistory',
type: 'Acme.BookStore.EntityHistory.EntityHistoryAppService',
isRemoteService: true,
isIntegrationService: false,
interfaces: [],
actions: {
GetAuditLogsAsync: action(
'GetAuditLogsAsync',
'Volo.Abp.Application.Dtos.PagedResultDto<Volo.Abp.AuditLogging.AuditLogDto>',
'Volo.Abp.Application.Dtos.PagedResultDto<Volo.Abp.AuditLogging.AuditLogDto>',
),
GetEntityChangesAsync: action(
'GetEntityChangesAsync',
'System.Collections.Generic.List<Volo.Abp.AuditLogging.EntityChangeDto>',
'[Volo.Abp.AuditLogging.EntityChangeDto]',
),
GetHistoryAsync: action(
'GetHistoryAsync',
'System.Collections.Generic.List<Volo.Abp.AuditLogging.EntityChangeWithUsernameDto>',
'[Volo.Abp.AuditLogging.EntityChangeWithUsernameDto]',
),
},
} as Controller;
const service = createControllerToServiceMapper({
targetPath: 'src/app/proxy',
solution,
types,
apiName: 'Default',
controllers: [controller],
serviceImports: {},
})(controller);
expect(service.imports.find(i => i.path === auditLoggingProxy)?.specifiers).toEqual([
'AuditLogDto',
'EntityChangeDto',
]);
expect(
service.imports.find(i => i.path === '../volo/abp/audit-logging/models')?.specifiers,
).toEqual(['EntityChangeWithUsernameDto']);
});
});

72
npm/ng-packs/packages/schematics/src/tests/proxy-self-import-generic-base.spec.ts

@ -80,3 +80,75 @@ describe('proxy generation - self-import for generic base type argument (#25080)
expect(importsPagedResultDto).toBe(true);
});
});
describe('proxy generation - generic argument of a same-namespace base type', () => {
const solution = 'MyApp';
// OrderResultDto : ResultBase<CustomerDto>, where ResultBase<T> lives in MyApp.Orders and
// CustomerDto lives in MyApp.Customers.
const types: Record<string, Type> = {
'MyApp.Customers.CustomerDto': {
baseType: null,
isEnum: false,
enumNames: null,
enumValues: null,
genericArguments: null,
properties: [
{
name: 'Name',
jsonName: null,
type: 'System.String',
typeSimple: 'string',
isRequired: false,
isNullable: false,
},
],
},
'MyApp.Orders.ResultBase<T0>': {
baseType: null,
isEnum: false,
enumNames: null,
enumValues: null,
genericArguments: ['T'],
properties: [
{
name: 'Items',
jsonName: null,
type: '[T]',
typeSimple: '[T]',
isRequired: false,
isNullable: false,
},
],
},
'MyApp.Orders.OrderResultDto': {
baseType: 'MyApp.Orders.ResultBase<MyApp.Customers.CustomerDto>',
isEnum: false,
enumNames: null,
enumValues: null,
genericArguments: null,
properties: [],
},
};
const params: ModelGeneratorParams = {
targetPath: 'src/app/proxy',
solution,
types,
serviceImports: {},
modelImports: {},
};
it('imports a generic argument that lives in another namespace', () => {
const reduce = createImportRefsToModelReducer(params);
const models = reduce([], ['MyApp.Orders.OrderResultDto']);
const model = models.find(m => m.namespace === 'Orders');
expect(model).toBeDefined();
expect(model!.imports.find(i => i.path === '../customers/models')?.specifiers).toEqual([
'CustomerDto',
]);
expect(model!.imports.some(i => i.path === './models')).toBe(false);
});
});

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

@ -1,4 +1,4 @@
import { Import, Interface, Model, Property, PropertyDef, Type, TypeWithEnum } from '../models';
import { Interface, Model, Property, PropertyDef, Type, TypeWithEnum } from '../models';
import {
extractGenerics,
generateRefWithPlaceholders,
@ -67,18 +67,22 @@ export function createImportRefsToModelReducer(params: ModelGeneratorParams) {
models.forEach(model => {
const toBeImported: TypeWithEnum[] = [];
// Types mapped to a package proxy are not generated locally (see above), so they still
// need an import even when they share this model's namespace.
const isDeclaredInModel = (type: string) =>
parseNamespace(solution, type) === model.namespace && !VOLO_PACKAGE_PROXY_IMPORTS.has(type);
model.interfaces.forEach(_interface => {
const { baseType } = types[_interface.ref];
if (baseType && parseNamespace(solution, baseType) !== model.namespace) {
if (baseType) {
const baseTypeWithGenericParams = parseBaseTypeWithGenericTypes(baseType);
baseTypeWithGenericParams.forEach(t => {
// A generic argument of the base type (e.g. T in PagedResultDto<T>) may live in the
// The base type or its generic argument (e.g. T in PagedResultDto<T>) may live in the
// same namespace as this model, which means it is generated into the same models.ts
// file. Importing it would produce an invalid self-import (`from './models'`), so we
// skip same-namespace types here, mirroring the property handling below. See #25080.
if (parseNamespace(solution, t) === model.namespace) {
if (isDeclaredInModel(t)) {
return;
}
@ -98,7 +102,7 @@ export function createImportRefsToModelReducer(params: ModelGeneratorParams) {
if (propType.isEnum) {
toBeImported.push({ type: ref, isEnum: true });
} else if (parseNamespace(solution, ref) !== model.namespace) {
} else if (!isDeclaredInModel(ref)) {
toBeImported.push({ type: ref, isEnum: false });
}
});
@ -220,15 +224,6 @@ export function parseBaseTypeWithGenericTypes(type: string): string[] {
export function resolveAbpPackages(models: Model[]) {
for (const model of models) {
renamePropForTenant(model.interfaces);
model.imports.forEach((imp, i) => {
for (const ref of imp.refs) {
const path = VOLO_PACKAGE_PROXY_IMPORTS.get(ref);
if (path) {
model.imports[i] = new Import({ ...imp, path });
}
}
});
}
}

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

@ -1,4 +1,4 @@
import { SYSTEM_TYPES, VOLO_REGEX } from '../constants';
import { SYSTEM_TYPES, VOLO_PACKAGE_PROXY_IMPORTS, VOLO_REGEX } from '../constants';
import { eImportKeyword } from '../enums';
import { Import, TypeWithEnum } from '../models';
import { extractSimpleGenerics } from './generics';
@ -114,6 +114,11 @@ export function createTypeToImportMapper(solution: string, namespace: string) {
path = relativePathToEnum(namespace, modelNamespace, specifiers[0]);
}
const packagePath = VOLO_PACKAGE_PROXY_IMPORTS.get(refs[0]);
if (packagePath) {
path = packagePath;
}
return new Import({ keyword: eImportKeyword.Type, path, refs, specifiers });
};
}

Loading…
Cancel
Save