Skip to content

Commit 45225cf

Browse files
committed
feat: Added tests and review
1 parent 0c2cb21 commit 45225cf

File tree

10 files changed

+128
-111
lines changed

10 files changed

+128
-111
lines changed

src/strategies/document-group.strategy.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
EXPORT_FORMAT_TO_FILE_FORMAT,
3030
fromBase64,
3131
removeFirstSlash,
32-
setValueByPath,
3332
slugify,
3433
takeIfDefined,
3534
toVersionDocument,
@@ -149,7 +148,7 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
149148
continue
150149
}
151150

152-
const { updatedPathItem, extraComponents } = buildPathAndComponents(
151+
const updatedPathItem = buildPath(
153152
sourceDocument,
154153
path,
155154
inferredMethod,
@@ -163,7 +162,6 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
163162
}
164163
resultDocument.components = {
165164
...takeIfDefined({ securitySchemes: sourceComponents?.securitySchemes }),
166-
...extraComponents,
167165
}
168166
}
169167
}
@@ -190,42 +188,28 @@ function isNonNullObject(value: unknown): value is Record<string, unknown> {
190188
return typeof value === 'object' && value !== null
191189
}
192190

193-
function buildPathAndComponents(
191+
function buildPath(
194192
sourceDocument: OpenAPIV3.Document,
195193
path: string,
196194
method: OpenAPIV3.HttpMethods,
197195
commonPathProps: Partial<OpenAPIV3.PathItemObject>,
198196
pathItemRef?: string,
199-
): { updatedPathItem: OpenAPIV3.PathItemObject; extraComponents?: OpenAPIV3.ComponentsObject } {
197+
): OpenAPIV3.PathItemObject {
200198
if (!pathItemRef) {
201199
const originalPathItem = sourceDocument.paths[path]!
202200
return {
203-
updatedPathItem: {
204-
...commonPathProps,
205-
[method]: { ...originalPathItem[method] },
206-
} as OpenAPIV3.PathItemObject,
207-
}
201+
...commonPathProps,
202+
[method]: { ...originalPathItem[method] },
203+
} as OpenAPIV3.PathItemObject
208204
}
209205

210206
const { jsonPath } = parseRef(pathItemRef)
211207
const targetPathItem = getValueByPath(sourceDocument, jsonPath) as OpenAPIV3.PathItemObject
212-
if (!targetPathItem) return { updatedPathItem: {} }
213-
214-
const resolvedPathItem = {
215-
...extractCommonPathItemProperties(targetPathItem),
216-
[method]: { ...targetPathItem[method] },
217-
}
218-
const componentsContainer: any = {}
219-
setValueByPath(componentsContainer, jsonPath, resolvedPathItem)
208+
if (!targetPathItem) return {}
220209

221210
const originalPathItem = sourceDocument.paths[path]!
222-
const mergedPathItem: OpenAPIV3.PathItemObject = {
211+
return {
223212
...(originalPathItem),
224213
...commonPathProps,
225214
}
226-
227-
return {
228-
updatedPathItem: mergedPathItem,
229-
extraComponents: componentsContainer.components,
230-
}
231215
}

test/document-group.test.ts

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,6 @@ describe('Document Group test', () => {
4747
await runMergeOperationsCase('case1')
4848
})
4949

50-
describe('PathItems tests', () => {
51-
test('should have properly merged documents', async () => {
52-
await runMergeOperationsCase('case2')
53-
})
54-
55-
test('should have properly merged documents and delete unused operations', async () => {
56-
await runMergeOperationsCase('case3')
57-
})
58-
59-
test('should have properly merged documents mixed formats (operation + pathItems operation)', async () => {
60-
await runMergeOperationsCase('case4')
61-
})
62-
})
63-
64-
async function runMergeOperationsCase(caseName: string): Promise<void> {
65-
const pkg = LocalRegistry.openPackage(`merge-operations/${caseName}`, groupToOperationIdsMap)
66-
const editor = await Editor.openProject(pkg.packageId, pkg)
67-
68-
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
69-
70-
const result = await editor.run({
71-
packageId: pkg.packageId,
72-
buildType: BUILD_TYPE.MERGED_SPECIFICATION,
73-
groupName: GROUP_NAME,
74-
apiType: REST_API_TYPE,
75-
})
76-
77-
const expectedResult = load(
78-
(await loadFileAsString(pkg.projectsDir, pkg.packageId, EXPECTED_RESULT_FILE))!,
79-
)
80-
81-
expect(result.merged?.data).toEqual(expectedResult)
82-
}
8350

8451
test('should rename documents with matching names', async () => {
8552
const dashboard = LocalRegistry.openPackage('documents-collision', groupToOperationIdsMap2)
@@ -113,4 +80,67 @@ describe('Document Group test', () => {
11380
expect(Object.keys(document.data.paths).length).toEqual(document.operationIds.length)
11481
}
11582
})
83+
84+
describe('PathItems tests', () => {
85+
test('should have documents with keep pathItems in components', async () => {
86+
const pkg = LocalRegistry.openPackage('document-group/case1', groupToOperationIdsMap)
87+
const editor = await Editor.openProject(pkg.packageId, pkg)
88+
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
89+
90+
const result = await editor.run({
91+
packageId: pkg.packageId,
92+
groupName: GROUP_NAME,
93+
buildType: BUILD_TYPE.REDUCED_SOURCE_SPECIFICATIONS,
94+
})
95+
96+
for (const document of Array.from(result.documents.values())) {
97+
expect(Object.keys(document.data.components.pathItems).length).toEqual(document.operationIds.length)
98+
}
99+
})
100+
101+
test('should have documents stripped of operations other than from provided group', async () => {
102+
const pkg = LocalRegistry.openPackage('document-group/case2', groupToOperationIdsMap)
103+
const editor = await Editor.openProject(pkg.packageId, pkg)
104+
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
105+
106+
const result = await editor.run({
107+
packageId: pkg.packageId,
108+
groupName: GROUP_NAME,
109+
buildType: BUILD_TYPE.REDUCED_SOURCE_SPECIFICATIONS,
110+
})
111+
for (const document of Array.from(result.documents.values())) {
112+
expect(Object.keys(document.data.paths).length).toEqual(document.operationIds.length)
113+
}
114+
})
115+
116+
describe('Merge Operations', () => {
117+
test('should have properly merged documents', async () => {
118+
await runMergeOperationsCase('case2')
119+
})
120+
121+
test('should have properly merged documents mixed formats (operation + pathItems operation)', async () => {
122+
await runMergeOperationsCase('case3')
123+
})
124+
})
125+
})
126+
127+
async function runMergeOperationsCase(caseName: string): Promise<void> {
128+
const pkg = LocalRegistry.openPackage(`merge-operations/${caseName}`, groupToOperationIdsMap)
129+
const editor = await Editor.openProject(pkg.packageId, pkg)
130+
131+
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
132+
133+
const result = await editor.run({
134+
packageId: pkg.packageId,
135+
buildType: BUILD_TYPE.MERGED_SPECIFICATION,
136+
groupName: GROUP_NAME,
137+
apiType: REST_API_TYPE,
138+
})
139+
140+
const expectedResult = load(
141+
(await loadFileAsString(pkg.projectsDir, pkg.packageId, EXPECTED_RESULT_FILE))!,
142+
)
143+
144+
expect(result.merged?.data).toEqual(expectedResult)
145+
}
116146
})

test/projects/merge-operations/case4/1.yaml renamed to test/projects/document-group/case1/1.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ info:
55
paths:
66
/path1:
77
$ref: '#/components/pathItems/pathItem1'
8+
/path2:
9+
$ref: '#/components/pathItems/pathItem2'
810
components:
911
pathItems:
1012
pathItem1:
1113
get:
1214
responses:
1315
'200':
1416
description: response description main
17+
pathItem2:
18+
post:
19+
responses:
20+
'200':
21+
description: response description main

test/projects/merge-operations/case4/config.json renamed to test/projects/document-group/case1/config.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
"fileId": "1.yaml",
88
"publish": true,
99
"labels": [],
10-
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a1cd"
11-
},
12-
{
13-
"fileId": "2.yaml",
14-
"publish": true,
15-
"labels": [],
1610
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a3cd"
1711
}
1812
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: "3.1.0"
2+
info:
3+
title: test
4+
version: 0.1.0
5+
paths:
6+
/path1:
7+
$ref: '#/components/pathItems/pathItem1'
8+
/path2:
9+
$ref: '#/components/pathItems/pathItem2'
10+
# operation other than from provided group
11+
/path3:
12+
$ref: '#/components/pathItems/pathItem3'
13+
components:
14+
pathItems:
15+
pathItem1:
16+
get:
17+
responses:
18+
'200':
19+
description: response description main
20+
pathItem2:
21+
post:
22+
responses:
23+
'200':
24+
description: response description main
25+
pathItem3:
26+
post:
27+
responses:
28+
'200':
29+
description: response description main
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageId": "55",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "1.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a3cd"
11+
}
12+
]
13+
}

test/projects/merge-operations/case3/2.yaml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@ info:
44
version: 0.1.0
55
paths:
66
/path2:
7-
$ref: '#/components/pathItems/pathItem2'
8-
components:
9-
pathItems:
10-
pathItem2:
11-
post:
12-
responses:
13-
'200':
14-
description: response description main
15-
pathItem3:
16-
post:
17-
responses:
18-
'200':
19-
description: response description unused
7+
post:
8+
responses:
9+
'200':
10+
description: response description main

test/projects/merge-operations/case3/result.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ paths:
66
/path1:
77
$ref: '#/components/pathItems/pathItem1'
88
/path2:
9-
$ref: '#/components/pathItems/pathItem2'
9+
post:
10+
responses:
11+
'200':
12+
description: response description main
1013
components:
1114
pathItems:
1215
pathItem1:
1316
get:
1417
responses:
1518
'200':
1619
description: response description main
17-
pathItem2:
18-
post:
19-
responses:
20-
'200':
21-
description: response description main

test/projects/merge-operations/case4/2.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/projects/merge-operations/case4/result.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)