Skip to content

Commit 7f23732

Browse files
committed
test: rename documents with matching names
1 parent 022509d commit 7f23732

File tree

10 files changed

+203
-19
lines changed

10 files changed

+203
-19
lines changed

test/document-group.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const groupToOperationIdsMap = {
2525
'path2-post',
2626
],
2727
}
28+
const groupToOperationIdsMap2 = {
29+
[GROUP_NAME]: [
30+
'some-path1-get',
31+
'another-path1-put',
32+
'some-path2-post',
33+
],
34+
}
2835
const EXPECTED_RESULT_FILE = 'result.yaml'
2936

3037
describe('Document Group test', () => {
@@ -50,4 +57,37 @@ describe('Document Group test', () => {
5057
const expectedResult = load((await loadFileAsString(pkg.projectsDir, pkg.packageId, EXPECTED_RESULT_FILE))!)
5158
expect(result.merged?.data).toEqual(expectedResult)
5259
})
60+
61+
test('should rename documents with matching names', async () => {
62+
const dashboard = LocalRegistry.openPackage('documents-collision', groupToOperationIdsMap2)
63+
const package1 = LocalRegistry.openPackage('documents-collision/package1')
64+
const package2 = LocalRegistry.openPackage('documents-collision/package2')
65+
const package3 = LocalRegistry.openPackage('documents-collision/package3')
66+
67+
await dashboard.publish(dashboard.packageId, { packageId: dashboard.packageId })
68+
await package1.publish(package1.packageId, { packageId: package1.packageId })
69+
await package2.publish(package2.packageId, { packageId: package2.packageId })
70+
await package3.publish(package3.packageId, { packageId: package3.packageId })
71+
72+
const editor = await Editor.openProject(dashboard.packageId, dashboard)
73+
const result = await editor.run({
74+
packageId: dashboard.packageId,
75+
buildType: BUILD_TYPE.REDUCED_SOURCE_SPECIFICATIONS,
76+
groupName: GROUP_NAME,
77+
apiType: REST_API_TYPE,
78+
})
79+
80+
expect(Array.from(result.documents.values())).toEqual(
81+
expect.toIncludeSameMembers([
82+
expect.objectContaining({ fileId: '1.yaml', filename: '1.json' }),
83+
expect.objectContaining({ fileId: '2.yaml', filename: '2.json' }),
84+
expect.objectContaining({ fileId: '1-1.yaml', filename: '1-1.json' }),
85+
expect.objectContaining({ fileId: '1-2.yaml', filename: '1-2.json' }),
86+
]),
87+
)
88+
89+
for (const document of Array.from(result.documents.values())) {
90+
expect(Object.keys(document.data.paths).length).toEqual(document.operationIds.length)
91+
}
92+
})
5393
})

test/helpers/registry/local.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,27 +193,45 @@ export class LocalRegistry implements IRegistry {
193193
packageId: string,
194194
filterByOperationGroup: string,
195195
): Promise<ResolvedDocuments | null> {
196-
const { documents } = await this.getVersion(packageId || this.packageId, version) ?? {}
196+
const { config: { refs = [] } = {}, documents } = await this.getVersion(packageId || this.packageId, version) ?? {}
197197

198-
const filterOperationIdsByGroup = (id: string): boolean => this.groupToOperationIdsMap[filterByOperationGroup]?.includes(id)
199-
const versionDocuments: ResolvedDocument[] = [...documents?.values() ?? []]
200-
.filter(versionDocument => versionDocument.operationIds.some(filterOperationIdsByGroup))
201-
.map(versionDocument => {
202-
return {
203-
version: versionDocument.version,
204-
fileId: versionDocument.fileId,
205-
slug: versionDocument.slug,
206-
type: versionDocument.type,
207-
format: versionDocument.format,
208-
filename: versionDocument.filename,
209-
labels: [],
210-
title: versionDocument.title,
211-
includedOperationIds: versionDocument.operationIds.filter(filterOperationIdsByGroup),
212-
data: toBase64(JSON.stringify(versionDocument.data)),
213-
}
214-
})
198+
const documentsFromVersion = Array.from(documents?.values() ?? [])
199+
200+
if (isNotEmpty(documentsFromVersion)) {
201+
return { documents: this.resolveDocuments(documentsFromVersion, this.filterOperationIdsByGroup(filterByOperationGroup)) }
202+
}
203+
204+
const documentsFromRefs = (
205+
await Promise.all(refs.map(async ({ refId, version }) => {
206+
const versionCache = await this.getVersion(refId, version)
207+
if (!versionCache) return []
208+
const { documents } = versionCache
209+
return Array.from(documents.values())
210+
}))
211+
).flat()
215212

216-
return { documents: versionDocuments }
213+
return { documents: this.resolveDocuments(documentsFromRefs, this.filterOperationIdsByGroup(filterByOperationGroup)) }
214+
}
215+
216+
private filterOperationIdsByGroup(filterByOperationGroup: string): (id: string) => boolean {
217+
return (id: string): boolean => this.groupToOperationIdsMap[filterByOperationGroup]?.includes(id)
218+
}
219+
220+
private resolveDocuments(documents: VersionDocument[], filterOperationIdsByGroup: (id: string) => boolean): ResolvedDocument[] {
221+
return documents
222+
.filter(versionDocument => versionDocument.operationIds.some(filterOperationIdsByGroup))
223+
.map(document => ({
224+
version: document.version,
225+
fileId: document.fileId,
226+
slug: document.slug,
227+
type: document.type,
228+
format: document.format,
229+
filename: document.filename,
230+
labels: [],
231+
title: document.title,
232+
includedOperationIds: document.operationIds.filter(filterOperationIdsByGroup),
233+
data: toBase64(JSON.stringify(document.data)),
234+
}))
217235
}
218236

219237
async versionDeprecatedResolver(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"packageId": "documents-collision",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"refs": [
6+
{
7+
"refId": "documents-collision/package1",
8+
"version": "v1"
9+
},
10+
{
11+
"refId": "documents-collision/package2",
12+
"version": "v1"
13+
},
14+
{
15+
"refId": "documents-collision/package3",
16+
"version": "v1"
17+
}
18+
]
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: some test
4+
version: 0.1.0
5+
paths:
6+
/some/path1:
7+
summary: some path item summary
8+
description: some path item description
9+
get:
10+
summary: some operation summary
11+
description: some operation description
12+
responses:
13+
'200':
14+
description: some response description
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: some test
4+
version: 0.1.0
5+
paths:
6+
/another/path1:
7+
summary: another path item summary
8+
description: another path item description
9+
put:
10+
summary: another operation summary
11+
description: another operation description
12+
responses:
13+
'200':
14+
description: another response description
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"packageId": "package1",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "1.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cd"
11+
},
12+
{
13+
"fileId": "2.yaml",
14+
"publish": true,
15+
"labels": [],
16+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1xd"
17+
}
18+
]
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: some test
4+
version: 0.1.0
5+
paths:
6+
/some/path1:
7+
summary: some path item summary
8+
description: some path item description
9+
get:
10+
summary: some operation summary
11+
description: some operation description
12+
responses:
13+
'200':
14+
description: some response description
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageId": "package2",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "1.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cc"
11+
}
12+
]
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: some test
4+
version: 0.1.0
5+
paths:
6+
/some/path2:
7+
summary: some path item summary
8+
description: some path item description
9+
get:
10+
summary: some operation summary
11+
description: some operation description
12+
responses:
13+
'200':
14+
description: some response description
15+
post:
16+
summary: some operation summary
17+
description: some operation description
18+
responses:
19+
'200':
20+
description: some response description
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageId": "package3",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "1.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cw"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)