Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions test/document-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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)
}
})
})
56 changes: 37 additions & 19 deletions test/helpers/registry/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,45 @@ export class LocalRegistry implements IRegistry {
packageId: string,
filterByOperationGroup: string,
): Promise<ResolvedDocuments | null> {
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(
Expand Down
19 changes: 19 additions & 0 deletions test/projects/documents-collision/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
14 changes: 14 additions & 0 deletions test/projects/documents-collision/package1/1.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions test/projects/documents-collision/package1/2.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions test/projects/documents-collision/package1/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
14 changes: 14 additions & 0 deletions test/projects/documents-collision/package2/1.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/projects/documents-collision/package2/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"packageId": "package2",
"apiType": "rest",
"version": "v1",
"files": [
{
"fileId": "1.yaml",
"publish": true,
"labels": [],
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cc"
}
]
}
20 changes: 20 additions & 0 deletions test/projects/documents-collision/package3/1.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/projects/documents-collision/package3/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"packageId": "package3",
"apiType": "rest",
"version": "v1",
"files": [
{
"fileId": "1.yaml",
"publish": true,
"labels": [],
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cw"
}
]
}