diff --git a/test/document-group.test.ts b/test/document-group.test.ts index bccd67f..161a196 100644 --- a/test/document-group.test.ts +++ b/test/document-group.test.ts @@ -25,6 +25,13 @@ const groupToOperationIdsMap = { 'path2-post', ], } +const groupToOperationIdsMap2 = { + [GROUP_NAME]: [ + 'some-path1-get', + 'another-path1-put', + 'some-path2-post', + ], +} const EXPECTED_RESULT_FILE = 'result.yaml' describe('Document Group test', () => { @@ -50,4 +57,37 @@ describe('Document Group test', () => { const expectedResult = load((await loadFileAsString(pkg.projectsDir, pkg.packageId, EXPECTED_RESULT_FILE))!) expect(result.merged?.data).toEqual(expectedResult) }) + + test('should rename documents with matching names', async () => { + const dashboard = LocalRegistry.openPackage('documents-collision', groupToOperationIdsMap2) + const package1 = LocalRegistry.openPackage('documents-collision/package1') + const package2 = LocalRegistry.openPackage('documents-collision/package2') + const package3 = LocalRegistry.openPackage('documents-collision/package3') + + await dashboard.publish(dashboard.packageId, { packageId: dashboard.packageId }) + await package1.publish(package1.packageId, { packageId: package1.packageId }) + await package2.publish(package2.packageId, { packageId: package2.packageId }) + await package3.publish(package3.packageId, { packageId: package3.packageId }) + + const editor = await Editor.openProject(dashboard.packageId, dashboard) + const result = await editor.run({ + packageId: dashboard.packageId, + buildType: BUILD_TYPE.REDUCED_SOURCE_SPECIFICATIONS, + groupName: GROUP_NAME, + apiType: REST_API_TYPE, + }) + + expect(Array.from(result.documents.values())).toEqual( + expect.toIncludeSameMembers([ + expect.objectContaining({ fileId: '1.yaml', filename: '1.json' }), + expect.objectContaining({ fileId: '2.yaml', filename: '2.json' }), + expect.objectContaining({ fileId: '1-1.yaml', filename: '1-1.json' }), + expect.objectContaining({ fileId: '1-2.yaml', filename: '1-2.json' }), + ]), + ) + + for (const document of Array.from(result.documents.values())) { + expect(Object.keys(document.data.paths).length).toEqual(document.operationIds.length) + } + }) }) diff --git a/test/helpers/registry/local.ts b/test/helpers/registry/local.ts index 1bf5a35..bdc8f6a 100644 --- a/test/helpers/registry/local.ts +++ b/test/helpers/registry/local.ts @@ -193,27 +193,45 @@ export class LocalRegistry implements IRegistry { packageId: string, filterByOperationGroup: string, ): Promise { - const { documents } = await this.getVersion(packageId || this.packageId, version) ?? {} + const { config: { refs = [] } = {}, documents } = await this.getVersion(packageId || this.packageId, version) ?? {} - const filterOperationIdsByGroup = (id: string): boolean => this.groupToOperationIdsMap[filterByOperationGroup]?.includes(id) - const versionDocuments: ResolvedDocument[] = [...documents?.values() ?? []] - .filter(versionDocument => versionDocument.operationIds.some(filterOperationIdsByGroup)) - .map(versionDocument => { - return { - version: versionDocument.version, - fileId: versionDocument.fileId, - slug: versionDocument.slug, - type: versionDocument.type, - format: versionDocument.format, - filename: versionDocument.filename, - labels: [], - title: versionDocument.title, - includedOperationIds: versionDocument.operationIds.filter(filterOperationIdsByGroup), - data: toBase64(JSON.stringify(versionDocument.data)), - } - }) + const documentsFromVersion = Array.from(documents?.values() ?? []) + + if (isNotEmpty(documentsFromVersion)) { + return { documents: this.resolveDocuments(documentsFromVersion, this.filterOperationIdsByGroup(filterByOperationGroup)) } + } + + const documentsFromRefs = ( + await Promise.all(refs.map(async ({ refId, version }) => { + const versionCache = await this.getVersion(refId, version) + if (!versionCache) return [] + const { documents } = versionCache + return Array.from(documents.values()) + })) + ).flat() - return { documents: versionDocuments } + return { documents: this.resolveDocuments(documentsFromRefs, this.filterOperationIdsByGroup(filterByOperationGroup)) } + } + + private filterOperationIdsByGroup(filterByOperationGroup: string): (id: string) => boolean { + return (id: string): boolean => this.groupToOperationIdsMap[filterByOperationGroup]?.includes(id) + } + + private resolveDocuments(documents: VersionDocument[], filterOperationIdsByGroup: (id: string) => boolean): ResolvedDocument[] { + return documents + .filter(versionDocument => versionDocument.operationIds.some(filterOperationIdsByGroup)) + .map(document => ({ + version: document.version, + fileId: document.fileId, + slug: document.slug, + type: document.type, + format: document.format, + filename: document.filename, + labels: [], + title: document.title, + includedOperationIds: document.operationIds.filter(filterOperationIdsByGroup), + data: toBase64(JSON.stringify(document.data)), + })) } async versionDeprecatedResolver( diff --git a/test/projects/documents-collision/config.json b/test/projects/documents-collision/config.json new file mode 100644 index 0000000..dbfa14f --- /dev/null +++ b/test/projects/documents-collision/config.json @@ -0,0 +1,19 @@ +{ + "packageId": "documents-collision", + "apiType": "rest", + "version": "v1", + "refs": [ + { + "refId": "documents-collision/package1", + "version": "v1" + }, + { + "refId": "documents-collision/package2", + "version": "v1" + }, + { + "refId": "documents-collision/package3", + "version": "v1" + } + ] +} diff --git a/test/projects/documents-collision/package1/1.yaml b/test/projects/documents-collision/package1/1.yaml new file mode 100644 index 0000000..400bf78 --- /dev/null +++ b/test/projects/documents-collision/package1/1.yaml @@ -0,0 +1,14 @@ +openapi: "3.0.0" +info: + title: some test + version: 0.1.0 +paths: + /some/path1: + summary: some path item summary + description: some path item description + get: + summary: some operation summary + description: some operation description + responses: + '200': + description: some response description diff --git a/test/projects/documents-collision/package1/2.yaml b/test/projects/documents-collision/package1/2.yaml new file mode 100644 index 0000000..c1bb6b1 --- /dev/null +++ b/test/projects/documents-collision/package1/2.yaml @@ -0,0 +1,14 @@ +openapi: "3.0.0" +info: + title: some test + version: 0.1.0 +paths: + /another/path1: + summary: another path item summary + description: another path item description + put: + summary: another operation summary + description: another operation description + responses: + '200': + description: another response description diff --git a/test/projects/documents-collision/package1/config.json b/test/projects/documents-collision/package1/config.json new file mode 100644 index 0000000..108d88b --- /dev/null +++ b/test/projects/documents-collision/package1/config.json @@ -0,0 +1,19 @@ +{ + "packageId": "package1", + "apiType": "rest", + "version": "v1", + "files": [ + { + "fileId": "1.yaml", + "publish": true, + "labels": [], + "commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cd" + }, + { + "fileId": "2.yaml", + "publish": true, + "labels": [], + "commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1xd" + } + ] +} diff --git a/test/projects/documents-collision/package2/1.yaml b/test/projects/documents-collision/package2/1.yaml new file mode 100644 index 0000000..400bf78 --- /dev/null +++ b/test/projects/documents-collision/package2/1.yaml @@ -0,0 +1,14 @@ +openapi: "3.0.0" +info: + title: some test + version: 0.1.0 +paths: + /some/path1: + summary: some path item summary + description: some path item description + get: + summary: some operation summary + description: some operation description + responses: + '200': + description: some response description diff --git a/test/projects/documents-collision/package2/config.json b/test/projects/documents-collision/package2/config.json new file mode 100644 index 0000000..3c49f39 --- /dev/null +++ b/test/projects/documents-collision/package2/config.json @@ -0,0 +1,13 @@ +{ + "packageId": "package2", + "apiType": "rest", + "version": "v1", + "files": [ + { + "fileId": "1.yaml", + "publish": true, + "labels": [], + "commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cc" + } + ] +} diff --git a/test/projects/documents-collision/package3/1.yaml b/test/projects/documents-collision/package3/1.yaml new file mode 100644 index 0000000..46cf6be --- /dev/null +++ b/test/projects/documents-collision/package3/1.yaml @@ -0,0 +1,20 @@ +openapi: "3.0.0" +info: + title: some test + version: 0.1.0 +paths: + /some/path2: + summary: some path item summary + description: some path item description + get: + summary: some operation summary + description: some operation description + responses: + '200': + description: some response description + post: + summary: some operation summary + description: some operation description + responses: + '200': + description: some response description diff --git a/test/projects/documents-collision/package3/config.json b/test/projects/documents-collision/package3/config.json new file mode 100644 index 0000000..730f015 --- /dev/null +++ b/test/projects/documents-collision/package3/config.json @@ -0,0 +1,13 @@ +{ + "packageId": "package3", + "apiType": "rest", + "version": "v1", + "files": [ + { + "fileId": "1.yaml", + "publish": true, + "labels": [], + "commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cw" + } + ] +}